Skip to content

Commit

Permalink
Add toggle to automatically delete no longer in sync versions
Browse files Browse the repository at this point in the history
Signed-off-by: Andrea Mazzotti <[email protected]>
  • Loading branch information
anmazzotti committed Jun 25, 2024
1 parent 27a785e commit db225d9
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .obs/chartfile/crds/templates/crds.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2540,6 +2540,12 @@ spec:
type: object
spec:
properties:
deleteNoLongerInSyncVersions:
default: false
description: |-
DeleteNoLongerInSyncVersions automatically deletes
all no-longer-in-sync ManagedOSVersions that were created by this channel.
type: boolean
options:
x-kubernetes-preserve-unknown-fields: true
syncInterval:
Expand Down
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ endif

export ROOT_DIR:=$(shell dirname $(realpath $(lastword $(MAKEFILE_LIST))))
CHART?=$(shell find $(ROOT_DIR) -type f -name "elemental-operator-$(CHART_VERSION).tgz" -print)
CHART_CRDS?=$(shell find $(ROOT_DIR) -type f -name "elemental-operator-crds-$(CHART_VERSION).tgz" -print)
KUBE_VERSION?="v1.27.10"
CLUSTER_NAME?="operator-e2e"
COMMITDATE?=$(shell git log -n1 --format="%as")
Expand Down Expand Up @@ -197,6 +198,7 @@ setup-full-cluster: build-docker-operator build-docker-seedimage-builder chart s
# thus losing any registration/inventories/os CRDs already created
reload-operator: build-docker-operator chart
kind load docker-image --name $(CLUSTER_NAME) ${REGISTRY_HEADER}${REPO}:${CHART_VERSION}
helm upgrade -n cattle-elemental-system elemental-operator-crds $(CHART_CRDS)
helm upgrade -n cattle-elemental-system elemental-operator $(CHART)

.PHONY: vendor
Expand Down
5 changes: 5 additions & 0 deletions api/v1beta1/managedosversionchannel_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ type ManagedOSVersionChannelSpec struct {
// +optional
// +kubebuilder:default:="1h"
SyncInterval string `json:"syncInterval,omitempty"`
// DeleteNoLongerInSyncVersions automatically deletes
// all no-longer-in-sync ManagedOSVersions that were created by this channel.
// +optional
// +kubebuilder:default:=false
DeleteNoLongerInSyncVersions bool `json:"deleteNoLongerInSyncVersions,omitempty"`
// +kubebuilder:validation:Schemaless
// +kubebuilder:validation:XPreserveUnknownFields
// +optional
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ spec:
type: object
spec:
properties:
deleteNoLongerInSyncVersions:
default: false
description: |-
DeleteNoLongerInSyncVersions automatically deletes
all no-longer-in-sync ManagedOSVersions that were created by this channel.
type: boolean
options:
x-kubernetes-preserve-unknown-fields: true
syncInterval:
Expand Down
7 changes: 7 additions & 0 deletions controllers/managedosversionchannel_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,13 @@ func (r *ManagedOSVersionChannelReconciler) createManagedOSVersions(ctx context.
logger.Error(err, "Could not patch ManagedOSVersion as no longer in sync", "name", version.Name)
return fmt.Errorf("deprecating ManagedOSVersion '%s': %w", version.Name, err)
}
if ch.Spec.DeleteNoLongerInSyncVersions {
logger.Info("Auto-deleting no longer in sync ManagedOSVersion due to channel settings", "name", version.Name)
if err := r.Delete(ctx, version); err != nil {
logger.Error(err, "Could not auto-delete no longer in sync ManagedOSVersion")
return fmt.Errorf("auto-deleting ManagedOSVersion '%s': %w", version.Name, err)
}
}
}
}

Expand Down
67 changes: 67 additions & 0 deletions controllers/managedosversionchannel_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -717,6 +717,73 @@ var _ = Describe("managed os version channel controller integration tests", func
Expect(managedOSVersion.Annotations[elementalv1.ElementalManagedOSVersionNoLongerSyncedAnnotation]).To(Equal(elementalv1.ElementalManagedOSVersionNoLongerSyncedValue))
})

It("should auto-delete a version after it's removed from channel", func() {
ch.Spec.Type = "json"
ch.Spec.DeleteNoLongerInSyncVersions = true

Expect(cl.Create(ctx, ch)).To(Succeed())

// Pod is created
Eventually(func() bool {
err := cl.Get(ctx, client.ObjectKey{
Name: ch.Name,
Namespace: ch.Namespace,
}, pod)
return err == nil
}, 12*time.Second, 2*time.Second).Should(BeTrue())
setPodPhase(pod, corev1.PodSucceeded)

Eventually(func() bool {
err := cl.Get(ctx, client.ObjectKey{
Name: ch.Name,
Namespace: ch.Namespace,
}, ch)
return err == nil && ch.Status.Conditions[0].Status == metav1.ConditionTrue
}, 12*time.Second, 2*time.Second).Should(BeTrue())

Expect(cl.Get(ctx, client.ObjectKey{
Name: "v0.1.0",
Namespace: ch.Namespace,
}, managedOSVersion)).To(Succeed())
Expect(managedOSVersion.Spec.Version).To(Equal("v0.1.0"))

// Pod is deleted
Eventually(func() bool {
err := cl.Get(ctx, client.ObjectKey{
Name: ch.Name,
Namespace: ch.Namespace,
}, pod)
return err != nil && apierrors.IsNotFound(err)
}, 12*time.Second, 2*time.Second).Should(BeTrue())

// Simulate a channel content change
syncerProvider.SetJSON(deprecatingJSON)

// Updating the channel after the minimum time between syncs causes an automatic update
patchBase := client.MergeFrom(ch.DeepCopy())
ch.Spec.SyncInterval = "10m"
Expect(cl.Patch(ctx, ch, patchBase)).To(Succeed())

// Pod is created
Eventually(func() bool {
err := cl.Get(ctx, client.ObjectKey{
Name: ch.Name,
Namespace: ch.Namespace,
}, pod)
return err == nil
}, 12*time.Second, 2*time.Second).Should(BeTrue())
setPodPhase(pod, corev1.PodSucceeded)

// Check deprecated version was deleted
Eventually(func() bool {
err := cl.Get(ctx, client.ObjectKey{
Name: "v0.1.0",
Namespace: ch.Namespace,
}, managedOSVersion)
return apierrors.IsNotFound(err)
}, 12*time.Second, 2*time.Second).Should(BeTrue(), "No longer in sync version should have been deleted")
})

It("should not reconcile again if it errors during pod lifecycle", func() {
ch.Spec.Type = "json"

Expand Down

0 comments on commit db225d9

Please sign in to comment.