Skip to content

Commit 29182fc

Browse files
Use uninstall annotation on policies
With the move to being event-driven by default, finalizers were not being removed during the uninstall process because those policies were not triggered to be reconciled. Now the `trigger-uninstall` process will add an annotation to policies with finalizers, which the main controller now watches for. The value of the uninstall annotation is now a timestamp, as opposed to just being set to "true" - this helps provide visibility, and lets the `trigger-uninstall` process to update the annotation multiple times to ensure a reconcile is triggered. Refs: - https://issues.redhat.com/browse/ACM-14707 Signed-off-by: Justin Kulikauskas <[email protected]>
1 parent ae2abbc commit 29182fc

File tree

3 files changed

+37
-14
lines changed

3 files changed

+37
-14
lines changed

controllers/configurationpolicy_controller.go

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -121,12 +121,15 @@ func (r *ConfigurationPolicyReconciler) SetupWithManager(
121121
return true
122122
}
123123

124-
oldAnnotations := e.ObjectOld.GetAnnotations()
125-
newAnnotations := e.ObjectNew.GetAnnotations()
124+
oldAnnos := e.ObjectOld.GetAnnotations()
125+
newAnnos := e.ObjectNew.GetAnnotations()
126126

127127
// These are the options that change evaluation behavior that aren't in the spec.
128-
if oldAnnotations[disableTemplatesAnnotation] != newAnnotations[disableTemplatesAnnotation] ||
129-
oldAnnotations[IVAnnotation] != newAnnotations[IVAnnotation] {
128+
specialAnnoChanged := oldAnnos[IVAnnotation] != newAnnos[IVAnnotation] ||
129+
oldAnnos[disableTemplatesAnnotation] != newAnnos[disableTemplatesAnnotation] ||
130+
oldAnnos[common.UninstallingAnnotation] != newAnnos[common.UninstallingAnnotation]
131+
132+
if specialAnnoChanged {
130133
return true
131134
}
132135

@@ -3449,5 +3452,12 @@ func IsBeingUninstalled(client client.Client) (bool, error) {
34493452
return false, err
34503453
}
34513454

3452-
return deployment.Annotations[common.UninstallingAnnotation] == "true", nil
3455+
value, found := deployment.Annotations[common.UninstallingAnnotation]
3456+
if !found {
3457+
return false, nil
3458+
}
3459+
3460+
notUninstalling := value == "false" || value == ""
3461+
3462+
return !notUninstalling, nil
34533463
}

pkg/triggeruninstall/triggeruninstall.go

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func TriggerUninstall(
4545
}
4646

4747
annotations := deployment.GetAnnotations()
48-
annotations[common.UninstallingAnnotation] = "true"
48+
annotations[common.UninstallingAnnotation] = time.Now().Format(time.RFC3339)
4949
deployment.SetAnnotations(annotations)
5050

5151
_, err = deploymentRsrc.Update(ctx, deployment, metav1.UpdateOptions{})
@@ -77,9 +77,9 @@ func TriggerUninstall(
7777
default:
7878
}
7979

80-
configPolicies, err := dynamicClient.Resource(configPolicyGVR).Namespace(policyNamespace).List(
81-
ctx, metav1.ListOptions{},
82-
)
80+
policyClient := dynamicClient.Resource(configPolicyGVR).Namespace(policyNamespace)
81+
82+
configPolicies, err := policyClient.List(ctx, metav1.ListOptions{})
8383
if err != nil {
8484
if k8serrors.IsServerTimeout(err) || k8serrors.IsTimeout(err) {
8585
klog.Infof("Retrying listing the ConfigurationPolicy objects due to error: %s", err)
@@ -96,16 +96,26 @@ func TriggerUninstall(
9696
if len(configPolicy.GetFinalizers()) != 0 {
9797
cleanedUp = false
9898

99-
break
99+
annos := configPolicy.GetAnnotations()
100+
annos[common.UninstallingAnnotation] = time.Now().Format(time.RFC3339)
101+
102+
updatedPolicy := configPolicy.DeepCopy()
103+
104+
updatedPolicy.SetAnnotations(annos)
105+
106+
if _, err := policyClient.Update(ctx, updatedPolicy, metav1.UpdateOptions{}); err != nil {
107+
klog.Errorf("Error updating policy %v/%v with an uninstalling annotation: %s",
108+
configPolicy.GetNamespace(), configPolicy.GetName(), err)
109+
}
100110
}
101111
}
102112

103113
if cleanedUp {
104114
break
105115
}
106116

107-
klog.Info("The uninstall preparation is not complete. Sleeping two seconds before checking again.")
108-
time.Sleep(2 * time.Second)
117+
klog.Info("The uninstall preparation is not complete. Sleeping five seconds before checking again.")
118+
time.Sleep(5 * time.Second)
109119
}
110120

111121
return nil

test/e2e/case29_trigger_uninstall_test.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ var _ = Describe("Clean up during uninstalls", Label("running-in-cluster"), Orde
5353
g.Expect(policy2.GetFinalizers()).To(ContainElement(pruneObjectFinalizer))
5454
}, defaultTimeoutSeconds, 1).Should(Succeed())
5555

56+
By("Waiting 25 seconds to ensure there are no leftover reconciles")
57+
time.Sleep(25 * time.Second)
58+
5659
By("Triggering an uninstall")
5760
config, err := LoadConfig("", kubeconfigManaged, "")
5861
Expect(err).ToNot(HaveOccurred())
@@ -73,9 +76,9 @@ var _ = Describe("Clean up during uninstalls", Label("running-in-cluster"), Orde
7376
context.TODO(), deploymentName, metav1.GetOptions{},
7477
)
7578
Expect(err).ToNot(HaveOccurred())
76-
Expect(deployment.GetAnnotations()).To(HaveKeyWithValue(common.UninstallingAnnotation, "true"))
79+
Expect(deployment.GetAnnotations()).To(HaveKey(common.UninstallingAnnotation))
7780

78-
By("Verifying that the ConfiguratioPolicy finalizers have been removed")
81+
By("Verifying that the ConfigurationPolicy finalizers have been removed")
7982
policy := utils.GetWithTimeout(
8083
clientManagedDynamic, gvrConfigPolicy, policyName, testNamespace, true, defaultTimeoutSeconds,
8184
)

0 commit comments

Comments
 (0)