TLDR:
Use the client's
Create()
method inside the controller's Reconcile() function to create a new Deployment.
- Read through Controller Runtime package docs, esp. Clients & Caches
- Navigate the docs: append
/pkg/client
(ormanager
, etc) aftercontroller-runtime
- Look at Client type
Navigate to https://pkg.go.dev/k8s.io/api
- Note the
apps
section and different versions. Recall how client-go and go pkgs work with kubernetes API; alpha, beta, and then GA. Theappsv1
is hence the "GA" of what we want; so go there - Click on
Deployment
. That'll lead us here: [https://pkg.go.dev/k8s.io/[email protected]/apps/v1#Deployment]https://pkg.go.dev/k8s.io/[email protected]/apps/v1#Deployment)
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"
write labelselector ("discovery") code in controller Reconcile() function
-
- Choose what labels our Deployment will select (help your Deployment find your
app
)
- Choose what labels our Deployment will select (help your Deployment find your
-
- Add those labels to the metadata of the
app
as well (so that Deployment can find it)
- Add those labels to the metadata of the
Write code for container (
make
, then add fields forname
andimage
to pull for it)
- See
guestbook_controller.go
Run controller:
make run
While controller is running, look up the Deployment it created (you could also have
watch
ed):
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
At another later time :)