Skip to content

Commit

Permalink
fix(vm): clear annotations and labels from child resources after remo…
Browse files Browse the repository at this point in the history
…ving them from the vm (#200)

Clear annotations and labels from child resources after removing them from the vm.
---------
Signed-off-by: yaroslavborbat <[email protected]>
  • Loading branch information
yaroslavborbat authored Jul 11, 2024
1 parent 8ddd8a1 commit df12d38
Showing 1 changed file with 30 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,24 +59,18 @@ func (h *SyncMetadataHandler) Handle(ctx context.Context, s state.VirtualMachine
current := s.VirtualMachine().Current()

// Propagate user specified labels and annotations from the d8 VM to kubevirt VM.
metaUpdated, err := PropagateVMMetadata(current, kvvm, kvvm)
kvvmMetaUpdated, err := PropagateVMMetadata(current, kvvm, kvvm)
if err != nil {
return reconcile.Result{}, err
}

if metaUpdated {
if err = h.client.Update(ctx, kvvm); err != nil {
return reconcile.Result{}, fmt.Errorf("failed to update metadata KubeVirt VM %q: %w", kvvm.GetName(), err)
}
}

kvvmi, err := s.KVVMI(ctx)
if err != nil {
return reconcile.Result{}, err
}
// Propagate user specified labels and annotations from the d8 VM to the kubevirt VirtualMachineInstance.
if kvvmi != nil {
metaUpdated, err = PropagateVMMetadata(current, kvvm, kvvmi)
metaUpdated, err := PropagateVMMetadata(current, kvvm, kvvmi)
if err != nil {
return reconcile.Result{}, err
}
Expand All @@ -100,7 +94,7 @@ func (h *SyncMetadataHandler) Handle(ctx context.Context, s state.VirtualMachine
if pod.Status.Phase != corev1.PodRunning {
continue
}
metaUpdated, err = PropagateVMMetadata(current, kvvm, &pod)
metaUpdated, err := PropagateVMMetadata(current, kvvm, &pod)
if err != nil {
return reconcile.Result{}, err
}
Expand All @@ -112,16 +106,23 @@ func (h *SyncMetadataHandler) Handle(ctx context.Context, s state.VirtualMachine
}
}
}
err = SetLastPropagatedLabels(kvvm, current)

labelsChanged, err := SetLastPropagatedLabels(kvvm, current)
if err != nil {
return reconcile.Result{}, fmt.Errorf("failed to set last propagated labels: %w", err)
}

err = SetLastPropagatedAnnotations(kvvm, current)
annosChanged, err := SetLastPropagatedAnnotations(kvvm, current)
if err != nil {
return reconcile.Result{}, fmt.Errorf("failed to set last propagated annotations: %w", err)
}

if labelsChanged || annosChanged || kvvmMetaUpdated {
if err = h.client.Update(ctx, kvvm); err != nil {
return reconcile.Result{}, fmt.Errorf("failed to update metadata KubeVirt VM %q: %w", kvvm.GetName(), err)
}
}

return reconcile.Result{}, nil
}

Expand Down Expand Up @@ -179,15 +180,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)

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

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

func GetLastPropagatedAnnotations(kvvm *virtv1.VirtualMachine) (map[string]string, error) {
Expand All @@ -203,15 +209,20 @@ 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)

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

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

// RemoveNonPropagatableAnnotations removes well known annotations that are dangerous to propagate.
Expand Down

0 comments on commit df12d38

Please sign in to comment.