Skip to content

Commit

Permalink
feat: stall nomos migrate if HNC is enabled (#1454)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
sdowell authored Oct 17, 2024
1 parent 182128c commit 5162ba9
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 0 deletions.
5 changes: 5 additions & 0 deletions cmd/nomos/migrate/configmanagement.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
9 changes: 9 additions & 0 deletions cmd/nomos/util/configmanagement.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
30 changes: 30 additions & 0 deletions e2e/testcases/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1378,6 +1378,9 @@ func TestNomosMigrate(t *testing.T) {
},
"spec": map[string]interface{}{
"enableMultiRepo": true,
"hierarchyController": map[string]interface{}{
"enabled": true,
},
},
},
}
Expand All @@ -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())

Expand Down

0 comments on commit 5162ba9

Please sign in to comment.