-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into test/api/resource-deletion-step-in-e2e-tests
- Loading branch information
Showing
12 changed files
with
200 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
images/virtualization-artifact/pkg/controller/vm/internal/block_device_limiter.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/* | ||
Copyright 2024 Flant JSC | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package internal | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"sigs.k8s.io/controller-runtime/pkg/reconcile" | ||
|
||
"github.com/deckhouse/virtualization-controller/pkg/common" | ||
"github.com/deckhouse/virtualization-controller/pkg/controller/conditions" | ||
"github.com/deckhouse/virtualization-controller/pkg/controller/service" | ||
"github.com/deckhouse/virtualization-controller/pkg/controller/vm/internal/state" | ||
"github.com/deckhouse/virtualization/api/core/v1alpha2/vmcondition" | ||
) | ||
|
||
const nameBlockDeviceLimiterHandler = "BlockDeviceLimiterHandler" | ||
|
||
type BlockDeviceLimiterHandler struct { | ||
service *service.BlockDeviceService | ||
} | ||
|
||
func NewBlockDeviceLimiterHandler(service *service.BlockDeviceService) *BlockDeviceLimiterHandler { | ||
return &BlockDeviceLimiterHandler{service: service} | ||
} | ||
|
||
func (h *BlockDeviceLimiterHandler) Handle(ctx context.Context, s state.VirtualMachineState) (reconcile.Result, error) { | ||
current := s.VirtualMachine().Current() | ||
changed := s.VirtualMachine().Changed() | ||
|
||
if isDeletion(current) { | ||
return reconcile.Result{}, nil | ||
} | ||
|
||
if updated := addAllUnknown(changed, vmcondition.TypeDiskAttachmentCapacityAvailable); updated { | ||
return reconcile.Result{Requeue: true}, nil | ||
} | ||
|
||
blockDeviceAttachedCount, err := h.service.CountBlockDevicesAttachedToVm(ctx, changed) | ||
if err != nil { | ||
return reconcile.Result{}, err | ||
} | ||
|
||
cb := conditions.NewConditionBuilder(vmcondition.TypeDiskAttachmentCapacityAvailable).Generation(changed.Generation) | ||
defer func() { conditions.SetCondition(cb, &changed.Status.Conditions) }() | ||
|
||
if blockDeviceAttachedCount <= common.VmBlockDeviceAttachedLimit { | ||
cb. | ||
Status(metav1.ConditionTrue). | ||
Reason(vmcondition.ReasonBlockDeviceCapacityAvailable). | ||
Message("") | ||
} else { | ||
cb. | ||
Status(metav1.ConditionFalse). | ||
Reason(vmcondition.ReasonBlockDeviceCapacityReached). | ||
Message(fmt.Sprintf("Can not attach %d block devices (%d is maximum) to `VirtualMachine` %q", blockDeviceAttachedCount, common.VmBlockDeviceAttachedLimit, changed.Name)) | ||
} | ||
|
||
return reconcile.Result{}, nil | ||
} | ||
|
||
func (h *BlockDeviceLimiterHandler) Name() string { | ||
return nameBlockDeviceLimiterHandler | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
images/virtualization-artifact/pkg/controller/vmbda/internal/block_device_limiter.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/* | ||
Copyright 2024 Flant JSC | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package internal | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"sigs.k8s.io/controller-runtime/pkg/reconcile" | ||
|
||
"github.com/deckhouse/virtualization-controller/pkg/common" | ||
"github.com/deckhouse/virtualization-controller/pkg/controller/conditions" | ||
"github.com/deckhouse/virtualization-controller/pkg/controller/service" | ||
virtv2 "github.com/deckhouse/virtualization/api/core/v1alpha2" | ||
"github.com/deckhouse/virtualization/api/core/v1alpha2/vmbdacondition" | ||
) | ||
|
||
type BlockDeviceLimiter struct { | ||
service *service.BlockDeviceService | ||
} | ||
|
||
func NewBlockDeviceLimiter(service *service.BlockDeviceService) *BlockDeviceLimiter { | ||
return &BlockDeviceLimiter{service: service} | ||
} | ||
|
||
func (h *BlockDeviceLimiter) Handle(ctx context.Context, vmbda *virtv2.VirtualMachineBlockDeviceAttachment) (reconcile.Result, error) { | ||
blockDeviceAttachedCount, err := h.service.CountBlockDevicesAttachedToVmName(ctx, vmbda.Spec.VirtualMachineName, vmbda.Namespace) | ||
if err != nil { | ||
return reconcile.Result{}, err | ||
} | ||
|
||
cb := conditions.NewConditionBuilder(vmbdacondition.DiskAttachmentCapacityAvailableType).Generation(vmbda.Generation) | ||
defer func() { conditions.SetCondition(cb, &vmbda.Status.Conditions) }() | ||
|
||
if vmbda.DeletionTimestamp != nil { | ||
cb.Status(metav1.ConditionUnknown).Reason(vmbdacondition.CapacityUnknown) | ||
return reconcile.Result{}, nil | ||
} | ||
|
||
if blockDeviceAttachedCount > common.VmBlockDeviceAttachedLimit { | ||
cb. | ||
Status(metav1.ConditionTrue). | ||
Reason(vmbdacondition.CapacityAvailable). | ||
Message("") | ||
} else { | ||
cb. | ||
Status(metav1.ConditionFalse). | ||
Reason(vmbdacondition.CapacityReached). | ||
Message(fmt.Sprintf("Can not attach %d block devices (%d is maximum) to `VirtualMachine` %q", blockDeviceAttachedCount, common.VmBlockDeviceAttachedLimit, vmbda.Spec.VirtualMachineName)) | ||
} | ||
|
||
return reconcile.Result{}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters