diff --git a/cmd/nomos/migrate/configmanagement.go b/cmd/nomos/migrate/configmanagement.go index 0be276f0f..230265c67 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 b4c26d3d2..bdd4c4cc0 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 7a38846ff..831b1de55 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())