diff --git a/cli/cmd/deploy_components.go b/cli/cmd/deploy_components.go index 4ee84ee4..f1631da6 100644 --- a/cli/cmd/deploy_components.go +++ b/cli/cmd/deploy_components.go @@ -1715,6 +1715,9 @@ func (dc *deployContext) katesComponent() component { // Auto-cleanup stale ClusterRole ownership from previous topology switches cleanupStaleClusterResource(ctx, "clusterrole", "kates", appNS) cleanupStaleClusterResource(ctx, "clusterrolebinding", "kates", appNS) + if err := adoptAPIKeySecret(ctx, appNS); err != nil { + return err + } // Ends at .svc: the pod's search path adds the cluster domain, so a wrong detection cannot break it. katesBootstrap := fmt.Sprintf("%s-kafka-bootstrap.%s.svc:9092", dc.primary.Name, dc.primary.Namespace) diff --git a/cli/cmd/deploy_kates_operator.go b/cli/cmd/deploy_kates_operator.go index 9b2bbc25..77514549 100644 --- a/cli/cmd/deploy_kates_operator.go +++ b/cli/cmd/deploy_kates_operator.go @@ -388,6 +388,33 @@ func randomAlnum(n int) (string, error) { return b.String(), nil } +// adoptAPIKeySecret hands the API key Secret an operator-mode install wrote +// to the kates chart: Helm refuses to install over an object without its +// ownership metadata, and its lookup then keeps the key clients already hold. +// A Secret Helm already manages is left as it is. +func adoptAPIKeySecret(ctx context.Context, namespace string) error { + out, err := runExecOutputFn(ctx, "kubectl", "get", "secret", apiKeySecretName, "-n", namespace, "--ignore-not-found", + "-o", `jsonpath={.metadata.name}|{.metadata.labels.app\.kubernetes\.io/managed-by}`) + if err != nil { + return fmt.Errorf("secret %s in %s could not be read: %w", apiKeySecretName, namespace, err) + } + name, managedBy, _ := strings.Cut(strings.TrimSpace(string(out)), "|") + if name == "" || managedBy != "" { + return nil + } + dl.Printf(" - Handing Secret %s/%s, written for the Kates operator, to Helm release kates\n", namespace, apiKeySecretName) + for _, args := range [][]string{ + {"label", "secret", apiKeySecretName, "-n", namespace, "--overwrite", "app.kubernetes.io/managed-by=Helm"}, + {"annotate", "secret", apiKeySecretName, "-n", namespace, "--overwrite", + "meta.helm.sh/release-name=kates", "meta.helm.sh/release-namespace=" + namespace}, + } { + if out, err := runExecCombinedFn(ctx, "kubectl", args...); err != nil { + return fmt.Errorf("secret %s in %s could not be handed to Helm: %w: %s", apiKeySecretName, namespace, err, bytes.TrimSpace(out)) + } + } + return nil +} + // applySecret writes Secret name in namespace, through stdin so no value is // on a command line. func applySecret(ctx context.Context, namespace, name string, data map[string]string) error { diff --git a/cli/cmd/deploy_kates_operator_test.go b/cli/cmd/deploy_kates_operator_test.go index 7ee93254..44104547 100644 --- a/cli/cmd/deploy_kates_operator_test.go +++ b/cli/cmd/deploy_kates_operator_test.go @@ -270,3 +270,41 @@ func TestOnlyTheCurrentGenerationsConditionCounts(t *testing.T) { t.Fatalf("error = %v after %d reads", err, *reads) } } + +func TestTheOperatorModesAPIKeySecretIsHandedToTheChart(t *testing.T) { + origOut, origComb := runExecOutputFn, runExecCombinedFn + t.Cleanup(func() { runExecOutputFn, runExecCombinedFn = origOut, origComb }) + for _, tc := range []struct { + name, get string + adopted bool + }{ + {"no Secret", "", false}, + {"written for the operator", "kates-api-key|", true}, + {"already the chart's", "kates-api-key|Helm", false}, + } { + t.Run(tc.name, func(t *testing.T) { + var ran []string + runExecOutputFn = func(context.Context, string, ...string) ([]byte, error) { return []byte(tc.get), nil } + runExecCombinedFn = func(_ context.Context, _ string, args ...string) ([]byte, error) { + ran = append(ran, strings.Join(args, " ")) + return nil, nil + } + if err := adoptAPIKeySecret(context.Background(), "kates"); err != nil { + t.Fatal(err) + } + if !tc.adopted { + if len(ran) != 0 { + t.Fatalf("changed a Secret it should leave alone: %q", ran) + } + return + } + all := strings.Join(ran, "\n") + for _, want := range []string{"app.kubernetes.io/managed-by=Helm", + "meta.helm.sh/release-name=kates", "meta.helm.sh/release-namespace=kates"} { + if !strings.Contains(all, want) { + t.Fatalf("lacks %q:\n%s", want, all) + } + } + }) + } +} diff --git a/docs/book/kates-operator.md b/docs/book/kates-operator.md index f4819c8b..66e49da0 100644 --- a/docs/book/kates-operator.md +++ b/docs/book/kates-operator.md @@ -219,7 +219,7 @@ The chart's README, [kates-operator](../../operator/chart/README.md), covers wha ## Switching Modes -You switch modes by removing Kates and deploying again, because `kates deploy` refuses to install one mode over the other. `kates clean` removes either mode. Under the operator, it deletes the `Kates` resource while the operator still runs, so the operator removes what it made, then removes the operator. To leave the rest of the stack in place, remove only Kates: `helm uninstall kates -n kates` for the standalone mode, or, for the operator mode, `kubectl delete kates kates -n kates` and the Secret `kates-api-key`, which the `kates` chart then makes again with a new key. +You switch modes by removing Kates and deploying again, because `kates deploy` refuses to install one mode over the other. `kates clean` removes either mode. Under the operator, it deletes the `Kates` resource while the operator still runs, so the operator removes what it made, then removes the operator. To leave the rest of the stack in place, remove only Kates: `helm uninstall kates -n kates` for the standalone mode, or `kubectl delete kates kates -n kates` for the operator mode. Deploying standalone after that hands the Secret `kates-api-key` to the `kates` chart, so the key your clients hold stays the same. The two modes keep their run history in different databases: the `kates` chart's own PostgreSQL, or `kates-db`. A switch therefore starts with an empty history unless you carry it over. To keep it, dump the old database before you remove it and restore the dump into the new one, as [A Database That Survives a Node](12-deployment.md#a-database-that-survives-a-node) does for a move to CloudNativePG. diff --git a/docs/tutorials/16-running-kates-under-the-operator.md b/docs/tutorials/16-running-kates-under-the-operator.md index 4df45b46..d1cca609 100644 --- a/docs/tutorials/16-running-kates-under-the-operator.md +++ b/docs/tutorials/16-running-kates-under-the-operator.md @@ -256,18 +256,22 @@ replaced again. ## Step 7: Go Back to Standalone, or Clean Up To go back to the standalone mode, delete the `Kates` resource. The operator -removes the members, their Services and their volumes. Also delete the API key -Secret, which deploy made for the operator mode and which the `kates` chart -makes again with a new key. Then deploy without the flag, and run -`kates ports` again so the CLI context picks up the new key: +removes the members, their Services and their volumes. Then deploy without +the flag. Deploy hands the API key Secret it wrote for the operator mode to the +`kates` chart, so the key, and your `ports` context, stay the same: ```bash kubectl delete kates kates -n kates -kubectl delete secret kates-api-key -n kates kates deploy -kates ports ``` +> [!NOTE] +> A `kates` CLI of v1.41.2 or older doesn't hand the Secret over, and the +> standalone install then fails with `invalid ownership metadata` on +> `kates-api-key`. With such a CLI, run +> `kubectl delete secret kates-api-key -n kates` before `kates deploy`, and +> `kates ports` after it to pick up the new key. + The operator and `kates-db` stay installed and unused. Remove them with `helm uninstall kates-operator -n kates-operator` and `helm uninstall kates-db -n kates`. To remove the whole stack instead,