Skip to content

Commit

Permalink
fix(vm): fixed kvvm updating
Browse files Browse the repository at this point in the history
Signed-off-by: Isteb4k <[email protected]>
  • Loading branch information
Isteb4k committed Feb 9, 2024
1 parent 7bf5e68 commit bbb3304
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 27 deletions.
38 changes: 19 additions & 19 deletions images/virtualization-controller/pkg/controller/vm_reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -673,24 +673,9 @@ func (r *VMReconciler) syncMetadata(ctx context.Context, state *VMReconcilerStat
return nil
}

// Propagate user specified labels and annotations from the d8 VM to kubevirt VM.
metaUpdated, err := PropagateVMMetadata(state.VM.Current(), state.KVVM, state.KVVM)
if err != nil {
return err
}

// Ensure kubevirt VM has finalizer in case d8 VM was created manually (use case: take ownership of already existing object).
finalizerUpdated := controllerutil.AddFinalizer(state.KVVM, virtv2.FinalizerKVVMProtection)

if metaUpdated || finalizerUpdated {
if err = opts.Client.Update(ctx, state.KVVM); err != nil {
return fmt.Errorf("error setting finalizer on a KubeVirt VM %q: %w", state.KVVM.Name, err)
}
}

// Propagate user specified labels and annotations from the d8 VM to the kubevirt VirtualMachineInstance.
if state.KVVMI != nil {
metaUpdated, err = PropagateVMMetadata(state.VM.Current(), state.KVVM, state.KVVMI)
metaUpdated, err := PropagateVMMetadata(state.VM.Current(), state.KVVM, state.KVVMI)
if err != nil {
return err
}
Expand All @@ -709,7 +694,7 @@ func (r *VMReconciler) syncMetadata(ctx context.Context, state *VMReconcilerStat
if pod.Status.Phase != corev1.PodRunning {
continue
}
metaUpdated, err = PropagateVMMetadata(state.VM.Current(), state.KVVM, &pod)
metaUpdated, err := PropagateVMMetadata(state.VM.Current(), state.KVVM, &pod)
if err != nil {
return err
}
Expand All @@ -722,16 +707,31 @@ func (r *VMReconciler) syncMetadata(ctx context.Context, state *VMReconcilerStat
}
}

err = SetLastPropagatedLabels(state.KVVM, state.VM.Current())
// Propagate user specified labels and annotations from the d8 VM to kubevirt VM.
metaUpdated, err := PropagateVMMetadata(state.VM.Current(), state.KVVM, state.KVVM)
if err != nil {
return err
}

// Ensure kubevirt VM has finalizer in case d8 VM was created manually (use case: take ownership of already existing object).
finalizerUpdated := controllerutil.AddFinalizer(state.KVVM, virtv2.FinalizerKVVMProtection)

labelsChanged, err := SetLastPropagatedLabels(state.KVVM, state.VM.Current())
if err != nil {
return fmt.Errorf("failed to set last propagated labels: %w", err)
}

err = SetLastPropagatedAnnotations(state.KVVM, state.VM.Current())
annosChanged, err := SetLastPropagatedAnnotations(state.KVVM, state.VM.Current())
if err != nil {
return fmt.Errorf("failed to set last propagated annotations: %w", err)
}

if labelsChanged || annosChanged || metaUpdated || finalizerUpdated {
if err = opts.Client.Update(ctx, state.KVVM); err != nil {
return fmt.Errorf("error setting finalizer on a KubeVirt VM %q: %w", state.KVVM.Name, err)
}
}

return nil
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -536,15 +536,20 @@ func GetLastPropagatedLabels(kvvm *virtv1.VirtualMachine) (map[string]string, er
return lastPropagatedLabels, nil
}

func SetLastPropagatedLabels(kvvm *virtv1.VirtualMachine, vm *virtv2.VirtualMachine) error {
func SetLastPropagatedLabels(kvvm *virtv1.VirtualMachine, vm *virtv2.VirtualMachine) (bool, error) {
data, err := json.Marshal(vm.GetLabels())
if err != nil {
return err
return false, err
}

common.AddLabel(kvvm, common.LastPropagatedVMLabelsAnnotation, string(data))
newAnnoValue := string(data)

return nil
if kvvm.Annotations[common.LastPropagatedVMLabelsAnnotation] == newAnnoValue {
return false, nil
}

common.AddAnnotation(kvvm, common.LastPropagatedVMLabelsAnnotation, newAnnoValue)
return true, nil
}

func GetLastPropagatedAnnotations(kvvm *virtv1.VirtualMachine) (map[string]string, error) {
Expand All @@ -560,13 +565,18 @@ func GetLastPropagatedAnnotations(kvvm *virtv1.VirtualMachine) (map[string]strin
return lastPropagatedAnno, nil
}

func SetLastPropagatedAnnotations(kvvm *virtv1.VirtualMachine, vm *virtv2.VirtualMachine) error {
func SetLastPropagatedAnnotations(kvvm *virtv1.VirtualMachine, vm *virtv2.VirtualMachine) (bool, error) {
data, err := json.Marshal(RemoveNonPropagatableAnnotations(vm.GetAnnotations()))
if err != nil {
return err
return false, err
}

common.AddLabel(kvvm, common.LastPropagatedVMAnnotationsAnnotation, string(data))
newAnnoValue := string(data)

return nil
if kvvm.Annotations[common.LastPropagatedVMAnnotationsAnnotation] == newAnnoValue {
return false, nil
}

common.AddAnnotation(kvvm, common.LastPropagatedVMAnnotationsAnnotation, newAnnoValue)
return true, nil
}

0 comments on commit bbb3304

Please sign in to comment.