# Adopt running resources into a Helm release without downtime

> helm install fails with "exists and cannot be imported into the current release". Hand a running Deployment and Service to Helm without deleting them.

Level: Advanced · Topic: Operations · About 15 minutes
Try it in the browser: https://k8wiz.com/playground/adopt-existing-resources-into-a-helm-release

The playground is a simulated Kubernetes cluster that runs in the browser. kubectl output and errors follow the real ones.

## The situation

`orders` has run in the `legacy` namespace for years, created with `kubectl apply`. The team now manages everything with the `k8wiz/web` chart and wants `orders` to become a Helm release too.

`helm install orders k8wiz/web --version 1.4.1 -f values-orders.yaml` refuses to touch objects it didn't create. Deleting them first would work and would also take `orders` down. Hand the running Deployment and Service over to Helm instead, then prove Helm owns them by scaling through a `helm upgrade`.

`values-orders.yaml` and the original manifest, `legacy/orders.yaml`, are in the editor. `helm template` renders the chart without touching the cluster.

## Objectives

1. See why Helm refuses the objects that already exist (`helm install orders k8wiz/web --version 1.4.1 -f values-orders.yaml`)
2. Release orders is deployed and owns the Deployment and the Service
3. Scale orders to 4 replicas with a helm upgrade (Keep the other values: `--reuse-values --set replicaCount=4`)
4. The Deployment and Service are the ones that already ran, not new ones
5. orders answers through its Service

## Hints

1. The error lists what Helm checks before it adopts an object: the label `app.kubernetes.io/managed-by=Helm` and the annotations `meta.helm.sh/release-name` and `meta.helm.sh/release-namespace`.
2. `kubectl label deployment/orders service/orders app.kubernetes.io/managed-by=Helm` and `kubectl annotate deployment/orders service/orders meta.helm.sh/release-name=orders meta.helm.sh/release-namespace=legacy`.
3. Don't install yet. `helm template orders k8wiz/web --version 1.4.1 -f values-orders.yaml` shows the chart selects Pods by `app.kubernetes.io/name` and `app.kubernetes.io/instance`. The running Deployment selects `app: orders`, and a Deployment's selector is immutable.
4. `helm show values k8wiz/web --version 1.4.1` documents `selectorLabels`, which replaces the chart's selector. Add `--set selectorLabels.app=orders` or put it in values-orders.yaml.
5. If an install already failed halfway, the release exists as `failed` and the Service selects nothing. `helm upgrade orders k8wiz/web --version 1.4.1 -f values-orders.yaml --set selectorLabels.app=orders` repairs both.

## Solution

```bash
helm install orders k8wiz/web --version 1.4.1 -f values-orders.yaml
```
```bash
helm template orders k8wiz/web --version 1.4.1 -f values-orders.yaml
```
```bash
kubectl label deployment/orders service/orders app.kubernetes.io/managed-by=Helm
```
```bash
kubectl annotate deployment/orders service/orders meta.helm.sh/release-name=orders meta.helm.sh/release-namespace=legacy
```
```bash
helm install orders k8wiz/web --version 1.4.1 -f values-orders.yaml --set selectorLabels.app=orders --wait
```
```bash
helm upgrade orders k8wiz/web --version 1.4.1 --reuse-values --set replicaCount=4 --wait
```

## What it teaches

- Helm adopts an existing object only when it carries `app.kubernetes.io/managed-by: Helm` and the two `meta.helm.sh/release-*` annotations with the release's name and namespace. Without them an install stops before it changes anything.
- Adoption doesn't recreate the object. Helm patches it towards the chart's manifest, so fields the chart doesn't set stay as they were.
- A Deployment's `spec.selector` can't change. When a chart's selector labels differ from the running ones, either the chart lets you override them or the workload has to be replaced side by side.
- Helm applies objects one by one. A patch that fails on the Deployment leaves the Service already changed, so render with `helm template` or `helm diff upgrade` before you install over live objects.
