Skip to content

Commit 3508c8f

Browse files
Handle additional namespace in trigger-uninstall
This is intended to help remove finalizers on policies in the "open-cluster-management-policies" namespace. A new argument must be added to the `trigger-uninstall` process to utilize this. Signed-off-by: Justin Kulikauskas <[email protected]>
1 parent 29182fc commit 3508c8f

File tree

3 files changed

+38
-24
lines changed

3 files changed

+38
-24
lines changed

main.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -663,7 +663,7 @@ func main() {
663663
func handleTriggerUninstall() {
664664
triggerUninstallFlagSet := pflag.NewFlagSet("trigger-uninstall", pflag.ExitOnError)
665665

666-
var deploymentName, deploymentNamespace, policyNamespace string
666+
var deploymentName, deploymentNamespace, policyNamespace, additionalNamespace string
667667
var timeoutSeconds uint32
668668

669669
triggerUninstallFlagSet.StringVar(
@@ -678,6 +678,9 @@ func handleTriggerUninstall() {
678678
triggerUninstallFlagSet.StringVar(
679679
&policyNamespace, "policy-namespace", "", "The namespace of where ConfigurationPolicy objects are stored",
680680
)
681+
triggerUninstallFlagSet.StringVar(
682+
&additionalNamespace, "additional-namespace", "", "An additional namespace for ConfigurationPolicy objects",
683+
)
681684
triggerUninstallFlagSet.Uint32Var(
682685
&timeoutSeconds, "timeout-seconds", 300, "The number of seconds before the operation is canceled",
683686
)
@@ -695,6 +698,12 @@ func handleTriggerUninstall() {
695698
os.Exit(1)
696699
}
697700

701+
namespacesToClean := []string{policyNamespace}
702+
703+
if additionalNamespace != "" {
704+
namespacesToClean = append(namespacesToClean, additionalNamespace)
705+
}
706+
698707
terminatingCtx := ctrl.SetupSignalHandler()
699708
ctx, cancelCtx := context.WithDeadline(terminatingCtx, time.Now().Add(time.Duration(timeoutSeconds)*time.Second))
700709

@@ -707,7 +716,7 @@ func handleTriggerUninstall() {
707716
os.Exit(1)
708717
}
709718

710-
err = triggeruninstall.TriggerUninstall(ctx, cfg, deploymentName, deploymentNamespace, policyNamespace)
719+
err = triggeruninstall.TriggerUninstall(ctx, cfg, deploymentName, deploymentNamespace, namespacesToClean)
711720
if err != nil {
712721
klog.Errorf("Failed to trigger the uninstall due to the error: %s", err)
713722
os.Exit(1)

pkg/triggeruninstall/triggeruninstall.go

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,11 @@ import (
2222
// TriggerUninstall will add an annotation to the controller's Deployment indicating that the controller needs to
2323
// prepare to be uninstalled. This function will run until all ConfigurationPolicy objects have no finalizers.
2424
func TriggerUninstall(
25-
ctx context.Context, config *rest.Config, deploymentName, deploymentNamespace, policyNamespace string,
25+
ctx context.Context,
26+
config *rest.Config,
27+
deploymentName string,
28+
deploymentNamespace string,
29+
policyNamespaces []string,
2630
) error {
2731
client := kubernetes.NewForConfigOrDie(config)
2832
dynamicClient := dynamic.NewForConfigOrDie(config)
@@ -77,35 +81,35 @@ func TriggerUninstall(
7781
default:
7882
}
7983

80-
policyClient := dynamicClient.Resource(configPolicyGVR).Namespace(policyNamespace)
84+
cleanedUp := true
8185

82-
configPolicies, err := policyClient.List(ctx, metav1.ListOptions{})
83-
if err != nil {
84-
if k8serrors.IsServerTimeout(err) || k8serrors.IsTimeout(err) {
85-
klog.Infof("Retrying listing the ConfigurationPolicy objects due to error: %s", err)
86+
for _, ns := range policyNamespaces {
87+
policyClient := dynamicClient.Resource(configPolicyGVR).Namespace(ns)
8688

87-
continue
88-
}
89+
configPolicies, err := policyClient.List(ctx, metav1.ListOptions{})
90+
if err != nil {
91+
klog.Errorf("Error listing policies: %s", err)
8992

90-
return err
91-
}
93+
return err
94+
}
9295

93-
cleanedUp := true
96+
for _, configPolicy := range configPolicies.Items {
97+
if len(configPolicy.GetFinalizers()) != 0 {
98+
cleanedUp = false
9499

95-
for _, configPolicy := range configPolicies.Items {
96-
if len(configPolicy.GetFinalizers()) != 0 {
97-
cleanedUp = false
100+
annos := configPolicy.GetAnnotations()
101+
annos[common.UninstallingAnnotation] = time.Now().Format(time.RFC3339)
98102

99-
annos := configPolicy.GetAnnotations()
100-
annos[common.UninstallingAnnotation] = time.Now().Format(time.RFC3339)
103+
updatedPolicy := configPolicy.DeepCopy()
101104

102-
updatedPolicy := configPolicy.DeepCopy()
105+
updatedPolicy.SetAnnotations(annos)
103106

104-
updatedPolicy.SetAnnotations(annos)
107+
if _, err := policyClient.Update(ctx, updatedPolicy, metav1.UpdateOptions{}); err != nil {
108+
klog.Errorf("Error updating policy %v/%v with an uninstalling annotation: %s",
109+
configPolicy.GetNamespace(), configPolicy.GetName(), err)
105110

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)
111+
return err
112+
}
109113
}
110114
}
111115
}

test/e2e/case29_trigger_uninstall_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,8 @@ var _ = Describe("Clean up during uninstalls", Label("running-in-cluster"), Orde
6868
)
6969
defer ctxCancel()
7070

71-
err = triggeruninstall.TriggerUninstall(ctx, config, deploymentName, deploymentNamespace, testNamespace)
71+
err = triggeruninstall.TriggerUninstall(
72+
ctx, config, deploymentName, deploymentNamespace, []string{testNamespace})
7273
Expect(err).ToNot(HaveOccurred())
7374

7475
By("Verifying that the uninstall annotation was set on the Deployment")

0 commit comments

Comments
 (0)