Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions cli/cmd/deploy_components.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
27 changes: 27 additions & 0 deletions cli/cmd/deploy_kates_operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
38 changes: 38 additions & 0 deletions cli/cmd/deploy_kates_operator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
})
}
}
2 changes: 1 addition & 1 deletion docs/book/kates-operator.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
16 changes: 10 additions & 6 deletions docs/tutorials/16-running-kates-under-the-operator.md
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Loading