Skip to content

Latest commit

 

History

History
61 lines (48 loc) · 2.83 KB

DEPLOYMENT-CONTROLLERRUNTIME.md

File metadata and controls

61 lines (48 loc) · 2.83 KB

Create a Deployment (and learn controller runtime pkg)

TLDR:

Use the client's Create() method inside the controller's Reconcile() function to create a new Deployment.

1. Read: Navigate the docs for the Kubernetes API pkg.go.dev

  1. Read through Controller Runtime package docs, esp. Clients & Caches
  2. Navigate the docs: append /pkg/client (or manager, etc) after controller-runtime
  3. Look at Client type

2. Read: Deployment type (navigating pkg.go.dev API docs)

Navigate to https://pkg.go.dev/k8s.io/api

3. Imports

appsv1 "k8s.io/api/apps/v1"  ---> for deployments, etc
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ---> ObjectMeta data, fields, etc ...
corev1 "k8s.io/api/core/v1" ---> spec, container fields ...

"k8s.io/apimachinery/pkg/runtime"
"sigs.k8s.io/controller-runtime/pkg/client"

4. Code: Bare bones deployment with label selector (discovery) and container image, name

write labelselector ("discovery") code in controller Reconcile() function

    1. Choose what labels our Deployment will select (help your Deployment find your app)
    1. Add those labels to the metadata of the app as well (so that Deployment can find it)

Write code for container (make, then add fields for name and image to pull for it)

  • See guestbook_controller.go

5. Test: make run, then get deployments and pods

Run controller:

make run

While controller is running, look up the Deployment it created (you could also have watched):

k get deployment -n default guestbook-sample  -owide

// gives us:
NAME               READY   UP-TO-DATE   AVAILABLE   AGE    CONTAINERS   IMAGES                                 SELECTOR
guestbook-sample   1/1     1            1           5m7s   frontend     gcr.io/google-samples/gb-frontend:v4   app=guestbook,tier=frontend

Do pods have those labels? They have to, for discovery purposes for k8s API:

k get pods -A -l app=guestbook

// gives us:
NAMESPACE   NAME                               READY   STATUS    RESTARTS   AGE
default     guestbook-sample-94dbc5dcd-kqqz8   1/1     Running   0          6m20s

6. Think about other things like clients, caching, shared informers, watchers ...

At another later time :)