# Upgrade and operate

> Run operator commands, change values, upgrade to a new version, add or remove a search node and uninstall a Corfedo release that the Helm chart installed.

Source: https://docs.corfedo.ai/operate/kubernetes/upgrade-and-operate/

## Run operator commands

Operator commands run in the toolbox pod through `kubectl exec`. A command reads its configuration from standard input, `artifact-storage status` from a file of the pod.

```sh
printf '%s' '{"NodeId": "corfedo-search-default-0"}' | kubectl exec --namespace corfedo --stdin \
  deployment/corfedo-toolbox -- dotnet Corfedo.Host.dll engine-node status --configuration /dev/stdin
kubectl exec --namespace corfedo deployment/corfedo-toolbox -- dotnet Corfedo.Host.dll \
  artifact-storage status --configuration /etc/corfedo/toolbox/artifact-storage.json
```

A search node's `NodeId` is its pod name. `recovery issue` and `network-policy clear` do what [Recover sign-in](https://docs.corfedo.ai/operate/security/#recover-sign-in) and [Clear a network list](https://docs.corfedo.ai/operate/security/#clear-a-network-list) describe.

## Change values and scale

Edit `values.yaml` and run `helm upgrade`. While the chart version and both images stay the same, Corfedo needs no stop, and only the pods whose configuration changed are replaced.

```sh
helm upgrade corfedo oci://ghcr.io/corfedo/charts/corfedo --version CHART_VERSION \
  --namespace corfedo --values values.yaml
```

- Change the replicas of a role or a pool only in the values, such as `roles.gateway.replicas`. A count set with `kubectl scale` can make a later upgrade with a different count fail with a conflict. The scale to zero before a [version upgrade](#upgrade-to-a-new-version) does not.
- A role with one replica, and the bundled PostgreSQL, are unavailable while their pod is replaced. A second replica of `gateway`, `web` and `api` removes that gap.
- More replicas need more [database connections](https://docs.corfedo.ai/operate/kubernetes/prepare-the-cluster/#postgresql). `helm upgrade` stops when `database.maxConnections` is too low for them.

## Upgrade to a new version

A new chart version, or a new platform or engine image, needs a stop. Corfedo is unavailable from the stop until the roles are ready again. First set the new image tags or digests in `values.yaml`, because `--version` selects only the chart.

```sh
# Scale the roles to zero, wait until their pods are gone, then upgrade.
kubectl scale deployment --namespace corfedo \
  --selector app.kubernetes.io/instance=corfedo,corfedo.ai/group=platform --replicas=0 &&
kubectl wait pod --namespace corfedo \
  --selector app.kubernetes.io/instance=corfedo,corfedo.ai/group=platform --for=delete --timeout=5m &&
helm upgrade corfedo oci://ghcr.io/corfedo/charts/corfedo --version CHART_VERSION \
  --namespace corfedo --values values.yaml
```

The setup Job migrates the database before the roles start again.

**Check:** every pod in `kubectl get pods --namespace corfedo` is ready. A search pool with `OnDelete` or a partition keeps the old engine image until you [update that pool](https://docs.corfedo.ai/operate/kubernetes/sizing-and-search-nodes/#pools).

With a deployment tool that applies the chart's manifests without `helm upgrade`, keep the same order on a version upgrade: stop the roles, let the setup Job complete, then start the roles.

## Add or remove a search node

To add a node, raise the pool's `replicas`, such as `search.pools.default.replicas`, and run `helm upgrade`. Copies already placed are not moved to it.

To remove a node, drain the one with the highest ordinal in its pool, here the third.

1. Drain the node.

   ```sh
   printf '%s' '{"NodeId": "corfedo-search-default-2"}' | kubectl exec --namespace corfedo --stdin \
     deployment/corfedo-toolbox -- dotnet Corfedo.Host.dll engine-node drain --configuration /dev/stdin
   ```

2. Repeat `engine-node status` for it until it prints `"SafeToRemove": true`.
3. Lower `search.pools.default.replicas` and run `helm upgrade`.

The node becomes safe to remove once each repository it serves has all its copies ready on other search nodes that aren't draining.

- With two nodes and two [copies](https://docs.corfedo.ai/operate/kubernetes/sizing-and-search-nodes/#copies-and-search-nodes), a node that holds copies never becomes safe, and the search nodes can't be fewer than the copies. Add a node or lower the copies first.
- If `SafeToRemove` stays `false` with enough nodes, `serving-status` for a repository (`TenantId`, `RepositoryId`) names what that repository waits for, such as no node with room.
- A repository's previous index keeps its copies for `servingPolicy.retentionSeconds` after it is replaced, so a drain can last that long.
- `engine-node resume` cancels a drain. A draining pod that restarts within about a minute stays draining. When a later scale-up brings a removed node's pod name back after that, the node starts active without `engine-node resume`. Check that `engine-node status` shows `"State": "Active"`, and run `engine-node resume` if it still shows `"Draining"`.
- After a scale-down without a drain, a repository whose only ready copy was on the node cannot be searched until a copy is placed elsewhere.

## Uninstall

```sh
helm uninstall corfedo --namespace corfedo
```

It keeps the Secrets, the trust bundle, the database, the bucket's contents and:

- The volume claims of the search nodes and indexers, also of pods removed earlier.
- The Secrets of the workload certificates.
- With `postgresql.enabled`, the bundled PostgreSQL's claim, `data-corfedo-postgresql-0`.

These commands remove the first two:

```sh
kubectl delete pvc --namespace corfedo --selector \
  'app.kubernetes.io/name=corfedo,app.kubernetes.io/instance=corfedo,app.kubernetes.io/component in (search, indexer)'
kubectl delete secret --namespace corfedo \
  corfedo-ai-api-workload corfedo-ai-executor-workload corfedo-ai-scripts-workload \
  corfedo-api-workload corfedo-control-workload corfedo-gateway-workload \
  corfedo-integrations-workload corfedo-issuer-workload corfedo-mcp-workload corfedo-web-workload
```

> **Warning:** `kubectl delete pvc --namespace corfedo data-corfedo-postgresql-0` deletes the bundled PostgreSQL's database. This cannot be undone.
