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): clear annotations and labels from child resources after removing them from the vm #200

Merged
merged 2 commits into from
Jul 11, 2024
Merged
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
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