Skip to content
Install the CLI

How-to guide Adopt existing resources

Preserve existing selectors

These docs describe Kix's main branch, which can include changes not yet released. Read the docs for the latest release.

Kubernetes does not allow a Deployment or StatefulSet selector to change in place. Before Kix takes ownership of an existing workload, configure its instance to retain that selector.

Get the selector from the live workload:

❱ kubectl get deployment legacy-web -n apps -o jsonpath='{.spec.selector.matchLabels}'

Use the labels from spec.selector.matchLabels, not every label on the workload. Confirm that the Pod template has the same labels before continuing.

Copy those labels into selectorLabels:

how-to/adoption/cluster.nix
apps.legacy-web = {
package = webPackage;
selectorLabels = {
app = "legacy-web";
tier = "frontend";
};
config = {
message = "Kix now manages this workload";
environment = "production";
};
};

View source on GitHub ↗

Kix passes this value to package builders as scope.selectorLabels. Workload helpers use it for both spec.selector.matchLabels and the Pod template labels, and Service helpers use it when selecting the workload.

The package must build its selectors from scope.selectorLabels for this instance option to take effect.

Evaluate the cluster:

kix-examples/
❱ kix check how-to-adoption
 TOOL       RESULT  DETAILS                       
 eval       pass    19 manifests evaluated        
 scorecard  pass    0 errors, 14 warnings, 2 info 

architecture.annotationDomainOwnership (warning), 1:
  apps/client: 'client' uses annotations under domain 'example.com' (ConfigMap/legacy-api-client) but no package in this cluster declares that it handles them. Add the operator package that handles these annotations. If something outside Kix consumes this domain, declare it in `scorecard.knownAnnotationDomains`.

governance.hasOwner (warning), 4:
  apps/client: package 'client' has no meta.owner
  apps/helm-web: package 'helm-web' has no meta.owner
  apps/legacy-api-contract: package 'legacy-api-contract' has no meta.owner
  apps/legacy-web: package 'legacy-web' has no meta.owner

reliability.boundedResources (warning), 2:
  apps/helm-web/helm-web: container 'nginx' is missing resource settings: limits.cpu. Set CPU and memory requests and limits.
  apps/legacy-web/legacy-web: container 'nginx' is missing resource settings: limits.cpu. Set CPU and memory requests and limits.

reliability.hasProbes (warning), 2:
  apps/helm-web/helm-web: container 'nginx' has no livenessProbe
  apps/legacy-web/legacy-web: container 'nginx' has no livenessProbe

security.hasNetworkPolicy (warning), 1:
  apps: namespace 'apps' has workloads but no network policy

security.nonRoot (warning), 2:
  apps/helm-web/helm-web: container 'nginx' may run as root: neither it nor the pod sets securityContext.runAsNonRoot, and no non-zero runAsUser resolves for it
  apps/legacy-web/legacy-web: container 'nginx' may run as root: neither it nor the pod sets securityContext.runAsNonRoot, and no non-zero runAsUser resolves for it

security.readOnlyRootfs (warning), 2:
  apps/helm-web/helm-web: container 'nginx' does not set readOnlyRootFilesystem
  apps/legacy-web/legacy-web: container 'nginx' does not set readOnlyRootFilesystem

2 info finding(s) not listed; run with -v to list them.

Render the workload and compare both label sets with the live Deployment:

kix-examples/ output excerpt
❱ kix build how-to-adoption --output json
{
  "apiVersion": "apps/v1",
  "kind": "Deployment",
  "metadata": {
    "name": "legacy-web",
    "namespace": "apps"
  },
  "spec": {
    "selector": {
      "matchLabels": {
        "app": "legacy-web",
        "tier": "frontend"
      }
    },
    "template": {
      "metadata": {
        "labels": {
          "app": "legacy-web",
          "app.kubernetes.io/name": "legacy-web",
          "tier": "frontend"
        }
      }
    }
  }
}

The rendered selector and Pod labels contain the existing app and tier values. Compare them against the kubectl get output from the first step.

kix plan --read-only cannot make this comparison. It finds live resources through Kix’s management labels, which the existing workload does not yet have. The read-only plan therefore shows the workload as a new resource and never compares the selectors.

Ask the API server instead:

kix-examples/
❱ kix plan how-to-adoption

Deployment and StatefulSet selectors are immutable. A mismatch therefore appears among the writes the deploy would stop on, either as an immutable-field refusal or as a field ownership conflict when another manager owns the selector.