Skip to content

Choosing the API group

The NarwhalFleet resource is served as version v1alpha1 of the API group narwhal-inference.com, a domain the project holds. This page is for a maintainer who changes the group, and for an operator whose cluster holds fleets of the earlier group narwhal.example.com.

Why the group needs a domain

Kubernetes tells custom resources apart by group and resource name, and a cluster holds one CRD of each name, <plural>.<group>. Only DNS ownership keeps two projects from picking the same group. RFC 2606 reserves example.com, so any project may use a name under it and none can claim one. Two projects could then collide on the CRD name, the label and annotation keys, and the RBAC rules and admission policy that the chart writes for the group.

What to register

Kubernetes never resolves the group. The API server compares it as a string, so the name needs no DNS record, web server or certificate. It only has to sit under a domain the project holds, for as long as the API exists. Any one of these works:

  • A domain registered for the project.
  • A subdomain of a domain the maintainer already owns.
  • A subdomain of <account>.github.io, which GitHub assigns to an account. It costs nothing, but it changes if the account is renamed.

Choose the group itself by these rules:

  • Under a domain that holds more than the project, use a subdomain for the API, <product>.<domain>, such as narwhal.<domain>. A domain registered for the project alone can be the group itself. The API server refuses a group without a dot (CRD validation).
  • Write it in lower case letters, digits, - and ., as a DNS subdomain of at most 253 characters with labels of at most 63.
  • Keep it short. It prefixes every label and annotation key, and operators type it in kubectl commands.
  • Stay out of k8s.io and kubernetes.io. The API server refuses a CRD in those groups unless it carries the api-approved.kubernetes.io annotation, which is for Kubernetes projects.
  • Keep it for the life of the API, through v1beta1 and v1. A cluster cannot move to another group in place, as Moving a cluster to the new group shows.

What derives from the group

Every Kubernetes name that Narwhal defines derives from the group:

  • the CRD narwhalfleets.<group> and the apiVersion <group>/v1alpha1
  • each label and annotation key, such as <group>/gate on a gate Job, <group>/placement as the engine pods' scheduling gate, and <group>/requested-at on the fleet
  • the annotations <group>/held-engines and <group>/released-by on the routers' Lease
  • the controller's Lease controller.<group> and the stage conditions stage.<group>/<stage>
  • the chart's RBAC rules, the ClusterRole's grant on the CRD by name, the admission policy's annotation and label checks, and the PodMonitor's relabelling of the attestation port annotation

The fleet has no finalizer. One added later takes the name <group>/<name>. The API server warns about a finalizer name without a / (custom resource validation).

Changing the group in this repository

API_GROUP in src/narwhal/contracts.py is the one definition. deployment/fleet_resource.py, the engine pod launch and the routers' Lease derive their names from it, and the chart reads it from generated templates. tests/deployment/test_api_group.py fails while any code, test or chart file writes the group itself.

  1. Set API_GROUP to the new group, and update its comment. Rewrite the opening of this page, and the group paragraph of the NarwhalFleet reference, which test_the_page_states_the_group_and_that_the_version_will_change in tests/maintenance/test_fleet_reference_doc.py reads.
  2. Regenerate the files that hold the group:

    .venv/bin/python tools/maintenance/crd_schema.py
    .venv/bin/python tools/maintenance/fleet_reference_doc.py
    .venv/bin/python -m tests.deployment.layouts
    
  3. Replace the group where a page or example file states it for a reader to type. Escape each . of the old group in the expression:

    git grep -l -F '<old group>' -- docs README.md CONTRIBUTING.md config deploy \
      | xargs sed -i 's/<old group>/<new group>/g'
    

    test_api_group fails on a page line that still states the old group.

  4. Run the checks:

    PYTHONPATH=src:. .venv/bin/python -m unittest tests.deployment.test_api_group \
      tests.deployment.test_crd_chart tests.deployment.test_fleet_resource \
      tests.maintenance.test_fleet_reference_doc tests.deployment.test_reference_layouts
    make chart-check
    make docs-build
    make check
    make chart-e2e
    

The change breaks every installed fleet. Give the PR title a !, and put the steps of the next section in the release notes.

Moving a cluster to the new group

No release shipped the controller or its CRD under narwhal.example.com. Only a cluster installed from a development build holds fleets of that group, and it moves by these steps.

The API server cannot change a CRD's group in place. The group is part of the CRD's name, and an object's name never changes. A release with the new group therefore installs a second CRD beside the old one. Its controller reconciles only fleets of the new group. A fleet in the new group is a new object with a new uid, so it starts a new run directory and runs every stage again. The fleet serves nothing from the moment its old copy stops until router-start passes again.

The old CRD serves the namespaces still on the old release, so you can move one namespace at a time. In each namespace:

  1. Keep the fleet file you applied, from your GitOps repository or the workstation. Without one, save the live fleet:

    kubectl get narwhalfleets.<old group> fleet --namespace <namespace> -o yaml > fleet.yaml
    

    Then remove its status, the server's metadata fields (uid, resourceVersion, generation, creationTimestamp and managedFields) and its annotations under the old group.

  2. While the old release still runs, stop the fleet and delete it. Its controller stops the routers, HAProxy, the engines and the Jobs in order, then sets Stopped:

    kubectl patch narwhalfleets.<old group> fleet --namespace <namespace> \
      --type merge -p '{"spec":{"stopped":true}}'
    kubectl wait narwhalfleets.<old group>/fleet --namespace <namespace> \
      --for=condition=Stopped --timeout=30m
    kubectl delete narwhalfleets.<old group> fleet --namespace <namespace>
    

    For a fleet that Helm or a GitOps tool owns, set spec.stopped and remove the fleet there. The garbage collector deletes what the fleet owned, including the Secrets the controller generated. The state claim keeps the old run directory.

  3. Delete the old controller's Lease, controller.<old group>, which has no owner. Delete the routers' Lease narwhal-router too, so that no annotation under the old group stays on it. The upgrade makes it again:

    kubectl delete lease controller.<old group> narwhal-router --namespace <namespace>
    
  4. Apply the new release's CRD, then upgrade the release, as in Upgrading the chart and its CRD. The upgrade replaces the controller, its RBAC and the admission policy with ones for the new group. From then on nothing in the namespace reconciles a fleet of the old group.

  5. Set apiVersion in the fleet file to <new group>/v1alpha1, leave out spec.stopped, and apply the file as before. A workstation needs a new --run directory, because the old one names the old fleet's uid.

Roles that you wrote yourself and that name the old group need the new one. The chart's own Roles change with the upgrade.

Once no namespace holds a fleet of the old group, delete the old CRD. Deleting a CRD deletes every object of it, so check that the list is empty first:

kubectl get narwhalfleets.<old group> --all-namespaces
kubectl delete crd narwhalfleets.<old group>