Skip to content

Commit

Permalink
fix(vm): lifecycle vm (#168)
Browse files Browse the repository at this point in the history
fix secret keys for provisioning. [userData, userdata]
add observedGeneration to VirtualMachine.Status
remove OwnerReference from pvc when DataVolume is removed

Signed-off-by: yaroslavborbat <yaroslav.752@gmail.com>
---------

Signed-off-by: yaroslavborbat <yaroslav.752@gmail.com>
Signed-off-by: Yaroslav Borbat <86148689+yaroslavborbat@users.noreply.github.com>
Co-authored-by: Ivan Mikheykin <ivan.mikheykin@flant.com>
  • Loading branch information
yaroslavborbat and diafour authored Jun 27, 2024
1 parent 2054631 commit 2100e66
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 6 deletions.
2 changes: 1 addition & 1 deletion api/core/v1alpha2/virtual_machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ type VirtualMachineStatus struct {
Conditions []metav1.Condition `json:"conditions,omitempty"`
Stats *VirtualMachineStats `json:"stats,omitempty"`
MigrationState *VirtualMachineMigrationState `json:"migrationState,omitempty"`

ObservedGeneration int64 `json:"observedGeneration,omitempty"`
// RestartAwaitingChanges holds operations to be manually approved
// before applying to the virtual machine spec.
//
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions crds/doc-ru-virtualmachine.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -639,3 +639,6 @@ spec:
description: "Время ожидания запуска виртуальной машины. starting -> running."
guestOSAgentStarting:
description: "Время ожидания запуска guestOsAgent. running -> running с guestOSAgent."
observedGeneration:
description: |
Поколение ресурса, которое в последний раз обрабатывалось контроллером
4 changes: 4 additions & 0 deletions crds/virtualmachine.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1121,6 +1121,10 @@ spec:
items:
type: object
x-kubernetes-preserve-unknown-fields: true
observedGeneration:
type: integer
description: "Generating a resource that was last processed by the controller"
format: int64
additionalPrinterColumns:
- name: Phase
type: string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@ import (
"context"
"errors"
"fmt"
"slices"

corev1 "k8s.io/api/core/v1"
storev1 "k8s.io/api/storage/v1"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
cdiv1 "kubevirt.io/containerized-data-importer-api/pkg/apis/core/v1beta1"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand Down Expand Up @@ -138,7 +140,6 @@ func (s DiskService) CleanUpSupplements(ctx context.Context, sup *supplements.Ge

if dv != nil {
hasDeleted = true

err = s.protection.RemoveProtection(ctx, dv)
if err != nil {
return false, err
Expand All @@ -148,6 +149,19 @@ func (s DiskService) CleanUpSupplements(ctx context.Context, sup *supplements.Ge
if err != nil && !k8serrors.IsNotFound(err) {
return false, err
}
pvc, err := s.GetPersistentVolumeClaim(ctx, sup)
if err != nil {
return false, err
}
if pvc != nil {
pvc.ObjectMeta.OwnerReferences = slices.DeleteFunc(pvc.ObjectMeta.OwnerReferences, func(ref metav1.OwnerReference) bool {
return ref.Kind == "DataVolume"
})
err = s.client.Update(ctx, pvc)
if err != nil && !k8serrors.IsNotFound(err) {
return false, err
}
}
}

return hasDeleted, supplements.CleanupForDataVolume(ctx, s.client, sup, s.dvcrSettings)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,22 @@ func (h *LifeCycleHandler) Handle(ctx context.Context, s state.VirtualMachineSta
current := s.VirtualMachine().Current()
changed := s.VirtualMachine().Changed()

defer func() {
if changed == nil {
return
}
if len(changed.Status.Conditions) == 0 {
changed.Status.ObservedGeneration = changed.GetGeneration()
return
}
gen := changed.Status.Conditions[0].ObservedGeneration
for _, c := range changed.Status.Conditions {
if gen != c.ObservedGeneration {
return
}
}
changed.Status.ObservedGeneration = gen
}()
if isDeletion(current) {
changed.Status.Phase = virtv2.MachineTerminating
return reconcile.Result{}, nil
Expand Down Expand Up @@ -144,7 +160,7 @@ func (h *LifeCycleHandler) syncPodStarted(vm *virtv2.VirtualMachine, pod *corev1
mgr.Update(cb.
Status(metav1.ConditionTrue).
Reason2(vmcondition.ReasonPodStarted).
Message(fmt.Sprintf("Pod started at %q", pod.Status.StartTime)).
Message(fmt.Sprintf("Pod started at %q", pod.Status.StartTime.String())).
Condition())
vm.Status.Conditions = mgr.Generate()
return
Expand All @@ -161,7 +177,7 @@ func (h *LifeCycleHandler) syncPodStarted(vm *virtv2.VirtualMachine, pod *corev1
}
mgr.Update(cb.
Reason2(vmcondition.ReasonPodNodFound).
Message("Not found any pods").
Message("Pod of the virtual machine was not found").
Condition())
vm.Status.Conditions = mgr.Generate()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,18 @@ func (h *ProvisioningHandler) genConditionFromSecret(ctx context.Context, builde
Message(fmt.Sprintf("Secret %q not found.", secretKey.String()))
return nil
}
if _, ok := secret.Data["userdata"]; !ok {
found := false
keys := []string{"userdata", "userData"}
for _, key := range keys {
if _, ok := secret.Data[key]; ok {
found = true
break
}
}
if !found {
builder.Status(metav1.ConditionFalse).
Reason2(vmcondition.ReasonProvisioningNotReady).
Message(fmt.Sprintf("Secret %q doesn't have key \"userdata\".", secretKey.String()))
Message(fmt.Sprintf("Secret %q should has one of data fields %v.", keys, secretKey.String()))
return nil
}
builder.Reason2(vmcondition.ReasonProvisioningReady).Status(metav1.ConditionTrue)
Expand Down

0 comments on commit 2100e66

Please sign in to comment.