From 5162ba9e83923e3cc01b2d718a9b1086eac3c7c7 Mon Sep 17 00:00:00 2001 From: Sam Dowell Date: Thu, 17 Oct 2024 16:17:20 -0700 Subject: [PATCH] feat: stall nomos migrate if HNC is enabled (#1454) This requires for the user to disable HNC before removing Config Management, so that ACM helps with the uninstallation of HNC before it is absent from the cluster. This is intended to streamline the experience of migrating to OSS HNC. --- cmd/nomos/migrate/configmanagement.go | 5 +++++ cmd/nomos/util/configmanagement.go | 9 ++++++++ e2e/testcases/cli_test.go | 30 +++++++++++++++++++++++++++ 3 files changed, 44 insertions(+) diff --git a/cmd/nomos/migrate/configmanagement.go b/cmd/nomos/migrate/configmanagement.go index 0be276f0ff..230265c67c 100644 --- a/cmd/nomos/migrate/configmanagement.go +++ b/cmd/nomos/migrate/configmanagement.go @@ -40,6 +40,11 @@ func migrateConfigManagement(ctx context.Context, cc *status.ClusterClient, kube printNotice("The cluster is already running as an OSS installation.") return nil } + if isHNCEnabled, err := cc.ConfigManagement.IsHNCEnabled(ctx); err != nil { + return err + } else if isHNCEnabled { + return fmt.Errorf("Hierarchy Controller is enabled on the ConfigManagement object. It must be disabled before migrating.") + } fmt.Printf("Removing ConfigManagement on cluster %q ...\n", kubeCtx) cmYamlFile, err := saveConfigManagementOperatorYaml(ctx, cc, kubeCtx) diff --git a/cmd/nomos/util/configmanagement.go b/cmd/nomos/util/configmanagement.go index b4c26d3d28..bdd4c4cc07 100644 --- a/cmd/nomos/util/configmanagement.go +++ b/cmd/nomos/util/configmanagement.go @@ -202,6 +202,15 @@ func (c *ConfigManagementClient) RemoveFinalizers(ctx context.Context) error { return err } +// IsHNCEnabled returns if the ConfigManagement object has HNC enabled. +func (c *ConfigManagementClient) IsHNCEnabled(ctx context.Context) (bool, error) { + isHNCEnabled, err := c.NestedBool(ctx, "spec", "hierarchyController", "enabled") + if err != nil { + return false, err + } + return isHNCEnabled, nil +} + // IsOssInstallation will check for the existence of ConfigManagement object, Operator deployment, and RootSync CRD // If RootSync CRD exist but ConfigManagement and Operator doesn't, it indicates an OSS installation func IsOssInstallation(ctx context.Context, c *ConfigManagementClient, cl client.Client, ck *kubernetes.Clientset) (bool, error) { diff --git a/e2e/testcases/cli_test.go b/e2e/testcases/cli_test.go index 7a38846ffe..831b1de55b 100644 --- a/e2e/testcases/cli_test.go +++ b/e2e/testcases/cli_test.go @@ -1378,6 +1378,9 @@ func TestNomosMigrate(t *testing.T) { }, "spec": map[string]interface{}{ "enableMultiRepo": true, + "hierarchyController": map[string]interface{}{ + "enabled": true, + }, }, }, } @@ -1395,6 +1398,33 @@ func TestNomosMigrate(t *testing.T) { ), ))) + nt.T.Log("Running nomos migrate should fail when HNC is enabled") + out, err := nt.Shell.Command("nomos", "migrate", "--remove-configmanagement").CombinedOutput() + if err != nil { + nt.T.Fatal(err) + } + assert.Contains(t, string(out), "Hierarchy Controller is enabled on the ConfigManagement object. It must be disabled before migrating.") + + nt.T.Log("Disabling HNC") + cmObj = &unstructured.Unstructured{ + Object: map[string]interface{}{ + "apiVersion": "configmanagement.gke.io/v1", + "kind": "ConfigManagement", + "metadata": map[string]interface{}{ + "name": "config-management", + }, + "spec": map[string]interface{}{ + "enableMultiRepo": true, + "hierarchyController": map[string]interface{}{ + "enabled": false, + }, + }, + }, + } + nt.Must(nt.KubeClient.Apply(cmObj)) + nt.Must(nt.Watcher.WatchForNotFound(kinds.Namespace(), "hnc-system", "")) + nt.Must(nt.Validate(util.ACMOperatorDeployment, configsync.ControllerNamespace, k8sobjects.DeploymentObject())) + nt.T.Log("Running nomos migrate to migrate from ConfigManagement to OSS install") nt.Must(nt.Shell.Command("nomos", "migrate", "--remove-configmanagement").CombinedOutput())