Skip to content

Commit

Permalink
Handle additional namespace in trigger-uninstall
Browse files Browse the repository at this point in the history
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 <jkulikau@redhat.com>
  • Loading branch information
JustinKuli committed Oct 9, 2024
1 parent d2ddfb5 commit 83a423c
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 24 deletions.
13 changes: 11 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -663,7 +663,7 @@ func main() {
func handleTriggerUninstall() {
triggerUninstallFlagSet := pflag.NewFlagSet("trigger-uninstall", pflag.ExitOnError)

var deploymentName, deploymentNamespace, policyNamespace string
var deploymentName, deploymentNamespace, policyNamespace, additionalNamespace string
var timeoutSeconds uint32

triggerUninstallFlagSet.StringVar(
Expand All @@ -678,6 +678,9 @@ func handleTriggerUninstall() {
triggerUninstallFlagSet.StringVar(
&policyNamespace, "policy-namespace", "", "The namespace of where ConfigurationPolicy objects are stored",
)
triggerUninstallFlagSet.StringVar(
&additionalNamespace, "additional-namespace", "", "An additional namespace for ConfigurationPolicy objects",
)
triggerUninstallFlagSet.Uint32Var(
&timeoutSeconds, "timeout-seconds", 300, "The number of seconds before the operation is canceled",
)
Expand All @@ -695,6 +698,12 @@ func handleTriggerUninstall() {
os.Exit(1)
}

namespacesToClean := []string{policyNamespace}

if additionalNamespace != "" {
namespacesToClean = append(namespacesToClean, additionalNamespace)
}

terminatingCtx := ctrl.SetupSignalHandler()
ctx, cancelCtx := context.WithDeadline(terminatingCtx, time.Now().Add(time.Duration(timeoutSeconds)*time.Second))

Expand All @@ -707,7 +716,7 @@ func handleTriggerUninstall() {
os.Exit(1)
}

err = triggeruninstall.TriggerUninstall(ctx, cfg, deploymentName, deploymentNamespace, policyNamespace)
err = triggeruninstall.TriggerUninstall(ctx, cfg, deploymentName, deploymentNamespace, namespacesToClean)
if err != nil {
klog.Errorf("Failed to trigger the uninstall due to the error: %s", err)
os.Exit(1)
Expand Down
46 changes: 25 additions & 21 deletions pkg/triggeruninstall/triggeruninstall.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@ import (
// TriggerUninstall will add an annotation to the controller's Deployment indicating that the controller needs to
// prepare to be uninstalled. This function will run until all ConfigurationPolicy objects have no finalizers.
func TriggerUninstall(
ctx context.Context, config *rest.Config, deploymentName, deploymentNamespace, policyNamespace string,
ctx context.Context,
config *rest.Config,
deploymentName string,
deploymentNamespace string,
policyNamespaces []string,
) error {
client := kubernetes.NewForConfigOrDie(config)
dynamicClient := dynamic.NewForConfigOrDie(config)
Expand Down Expand Up @@ -77,35 +81,35 @@ func TriggerUninstall(
default:
}

policyClient := dynamicClient.Resource(configPolicyGVR).Namespace(policyNamespace)
cleanedUp := true

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

continue
}
configPolicies, err := policyClient.List(ctx, metav1.ListOptions{})
if err != nil {
klog.Errorf("Error listing policies: %s", err)

return err
}
return err
}

cleanedUp := true
for _, configPolicy := range configPolicies.Items {
if len(configPolicy.GetFinalizers()) != 0 {
cleanedUp = false

for _, configPolicy := range configPolicies.Items {
if len(configPolicy.GetFinalizers()) != 0 {
cleanedUp = false
annos := configPolicy.GetAnnotations()
annos[common.UninstallingAnnotation] = time.Now().Format(time.RFC3339)

annos := configPolicy.GetAnnotations()
annos[common.UninstallingAnnotation] = time.Now().Format(time.RFC3339)
updatedPolicy := configPolicy.DeepCopy()

updatedPolicy := configPolicy.DeepCopy()
updatedPolicy.SetAnnotations(annos)

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

if _, err := policyClient.Update(ctx, updatedPolicy, metav1.UpdateOptions{}); err != nil {
klog.Errorf("Error updating policy %v/%v with an uninstalling annotation: %s",
configPolicy.GetNamespace(), configPolicy.GetName(), err)
return err
}
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion test/e2e/case29_trigger_uninstall_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ var _ = Describe("Clean up during uninstalls", Label("running-in-cluster"), Orde
)
defer ctxCancel()

err = triggeruninstall.TriggerUninstall(ctx, config, deploymentName, deploymentNamespace, testNamespace)
err = triggeruninstall.TriggerUninstall(
ctx, config, deploymentName, deploymentNamespace, []string{testNamespace})
Expect(err).ToNot(HaveOccurred())

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

0 comments on commit 83a423c

Please sign in to comment.