Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(vm): fixed annotations and labels propagation #5

Merged
merged 1 commit into from
Feb 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}
Loading