Skip to content
LF Cert Trainer

Lesson 92 · Section 7: Security

Lesson 92: Operator Framework: building, installing and upgrading operators

What an operator adds to a CRD and a controller, the three parts of the Operator Framework (Operator SDK, Operator Lifecycle Manager, OperatorHub.io), how a Subscription becomes a running operator, and the exam routine: install an operator, then create its custom resource. · 17 min read · 4 min video

Official competency this lesson covers

1/10Operator Framework
0:00 / 3:47

Narrated by the local narrator voice. Keys: Space plays and pauses, J and L step, C captions, F full screen.

Why operators exist

The last two lessons gave you two building blocks. A CustomResourceDefinition (CRD) teaches the API server a new kind of object. A custom controller watches objects of that kind and acts on them. Neither is much use alone: a CRD without a controller is only a place to store YAML, and a controller without a CRD has nothing to watch.

An operator is the two together, built for one specific application. The name comes from the job it replaces. A human operator who runs a database knows how to bootstrap it, add a replica, take a backup before an upgrade and promote a standby when the primary dies. A Deployment or a StatefulSet knows none of that; they only know how to start and replace Pods. An operator is that human knowledge written as a controller.

An operator turns one small object into a managed applicationcreatewatchcreatescreatescreatesYoukubectl applyPrometheus maincustom resourceOperator Podwatches, reconcilesSecretgenerated configServiceprometheus-operatedStatefulSetprometheus-main
An operator turns one small object into a managed application. You describe what you want in a custom resource. The operator, a controller running in a Pod, creates the built-in objects and keeps looking after them.

Parts:

  • You (kubectl apply)
  • Prometheus main (custom resource)
  • Operator Pod (watches, reconciles)
  • Secret (generated config)
  • Service (prometheus-operated)
  • StatefulSet (prometheus-main)

Connections:

  • You to Prometheus main: create
  • Prometheus main to Operator Pod: watch
  • Operator Pod to StatefulSet: creates
  • Operator Pod to Service: creates
  • Operator Pod to Secret: creates

The diagram uses the Prometheus operator as the example. You create a 12-line object of kind Prometheus. The operator answers with a StatefulSet, a Service, a generated configuration Secret, and it keeps all three in line with your object for as long as it exists. Change replicas in the custom resource and the StatefulSet changes; delete the StatefulSet and the operator recreates it.

That leaves three practical questions, and they are what the Operator Framework answers:

  • How do you write an operator without starting from an empty file?
  • How do you find an operator someone else has written, and trust that it is packaged properly?
  • How do you install and upgrade it, with its CRDs and RBAC, without breaking the custom resources that depend on it?

What the Operator Framework is

The Operator Framework is an open source toolkit, a CNCF project, with one part for each question.

The three parts of the Operator Frameworkwritespackspublished inreadsdeploysDeveloperGo, Ansible or HelmOperator SDKscaffold, build, testBundle imageCSV and CRDsOperatorHub.iocatalog of bundlesOLMinstalls, upgradesOperatorrunning in a Pod
The three parts of the Operator Framework. The SDK builds an operator and packs it into a bundle. A catalog such as OperatorHub.io lists bundles. The Operator Lifecycle Manager installs and upgrades them in your cluster.

Parts:

  • Developer (Go, Ansible or Helm)
  • Operator SDK (scaffold, build, test)
  • Bundle image (CSV and CRDs)
  • OperatorHub.io (catalog of bundles)
  • OLM (installs, upgrades)
  • Operator (running in a Pod)

Connections:

  • Developer to Operator SDK: writes
  • Operator SDK to Bundle image: packs
  • Bundle image to OperatorHub.io: published in
  • OperatorHub.io to OLM: reads
  • OLM to Operator: deploys
PartWhat it isWho uses it
Operator SDKA command line tool (operator-sdk) that scaffolds, builds, tests and packages operatorsDevelopers who write operators
Operator Lifecycle Manager (OLM)A set of controllers and CRDs that runs in your cluster and installs, upgrades and removes operatorsCluster administrators
OperatorHub.ioA public catalog of packaged operators, with install instructions for each oneAnyone looking for an operator

As a CKA candidate you sit on the administrator side. You need to recognise the SDK, but your work is installing an operator, checking that it runs and creating the custom resources it manages.

Operator SDK: three ways to write an operator

The SDK supports three kinds of operator. They differ in how much the operator can do.

TypeYou writeGood for
HelmNothing new: the SDK wraps an existing Helm chart. The custom resource's spec becomes the chart's valuesInstall and upgrade of stateless applications
AnsibleAnsible roles and playbooks that run on every reconcileTeams that already automate with Ansible
GoA reconcile function in Go, on top of the same libraries the built-in controllers useAnything, including failover and automatic tuning

The SDK also packs the result into a bundle: a small image holding the operator's CRDs and a ClusterServiceVersion (CSV), the manifest that describes one version of the operator. You will meet the CSV again in a moment.

Capability levels: how much an operator does for you

Not every operator is equally clever. The framework defines five capability levels, and OperatorHub.io shows the level of each operator:

LevelNameThe operator can
1Basic InstallInstall and configure the application from a custom resource
2Seamless UpgradesUpgrade the application and itself to new versions
3Full LifecycleTake backups, restore, and recover from failures
4Deep InsightsExpose metrics, alerts and log processing
5Auto PilotScale, tune and heal by itself

Helm-based operators stop at level 2, because a chart can install and upgrade but cannot react to what the application is doing. Ansible and Go operators can reach all five.

How OLM installs an operator

An operator is more than a Deployment. It needs its CRDs in place first, a ServiceAccount with exactly the right RBAC rules, sometimes webhooks and certificates, and a safe route from each version to the next. OLM treats all of that as one unit.

The objects OLM adds

OLM is itself built from CRDs and controllers. Once it is installed you have these new kinds:

KindShort nameWhat it means
CatalogSourcecatsrcA catalog of operators the cluster can install from. A default install has operatorhubio-catalog
PackageManifestOne installable package in a catalog, with its channels. Read-only: OLM generates it
Subscriptionsub"Keep this package, from this channel of this catalog, installed and up to date"
InstallPlanipThe list of resources OLM will create for one install or upgrade, with an approval switch
ClusterServiceVersioncsvOne version of one operator: its Deployment, permissions and the CRDs it owns
OperatorGroupogWhich namespaces the operators installed in this namespace may watch

From Subscription to running Pod

From a Subscription to a running operatorlooks upplanswritesinstallsstartsSubscriptionpackage and channelInstallPlanlist of stepsCSVphase: SucceededCatalogSourceoperatorhubio-catalogCRDsnew API kindsDeploymentthe operator Pod
From a Subscription to a running operator. You create one object, the Subscription. OLM looks the package up in a catalog, writes an InstallPlan, and carries it out: CRDs first, then the ClusterServiceVersion, which starts the operator Deployment.

Parts:

  • Subscription (package and channel)
  • InstallPlan (list of steps)
  • CSV (phase: Succeeded)
  • CatalogSource (operatorhubio-catalog)
  • CRDs (new API kinds)
  • Deployment (the operator Pod)

Connections:

  • Subscription to CatalogSource: looks up
  • Subscription to InstallPlan: plans
  • InstallPlan to CSV: writes
  • InstallPlan to CRDs: installs
  • CSV to Deployment: starts

You create exactly one object, the Subscription. The rest follows:

  1. OLM's catalog operator sees the Subscription and looks up the package in the named CatalogSource. It picks the newest version in the channel and works out anything that version depends on.
  2. It writes an InstallPlan listing every resource to create.
  3. If the Subscription says installPlanApproval: Automatic (the default), the plan is carried out at once. With Manual, it waits until someone sets spec.approved: true on it.
  4. Carrying out the plan creates the CRDs, the ServiceAccount and RBAC objects, and the CSV.
  5. The OLM operator sees the CSV, checks that its requirements are met, and creates the operator's Deployment. When the Pods are ready, the CSV reaches phase: Succeeded.

Upgrades reuse the same path. When the catalog gains a newer version in your channel, the Subscription produces a new InstallPlan, and a new CSV replaces the old one. This is the main thing OLM gives you over a plain manifest: the operator stays current, and you can put a manual approval in front of each upgrade.

Under the hood · Where OLM lives, and what OperatorGroups are for

A default OLM install creates two namespaces:

  • olm holds OLM itself: the olm-operator, the catalog-operator, the packageserver (which serves the PackageManifest API) and one Pod per CatalogSource.
  • operators is where you put Subscriptions for operators that should watch the whole cluster. It contains an OperatorGroup called global-operators that selects all namespaces.

Every namespace that holds a Subscription needs exactly one OperatorGroup. It tells the operators installed there which namespaces to watch. With no OperatorGroup, or with two of them, the CSV does not install and its status says so. To install an operator that watches only its own namespace, create the namespace, an OperatorGroup whose spec.targetNamespaces lists it, and then the Subscription.

Under the hood · OLM v1: ClusterExtension and ClusterCatalog

The objects above belong to the original OLM, often called OLM v0, with the API group operators.coreos.com. The project has also released a redesigned OLM v1 with a much smaller API in the group olm.operatorframework.io:

  • A ClusterCatalog points at a catalog image, in place of a CatalogSource.
  • A ClusterExtension asks for a package, optionally with a channel or a version range, and names the namespace to install into. It takes the place of the Subscription, the InstallPlan and the CSV.
apiVersion: olm.operatorframework.io/v1
kind: ClusterExtension
metadata:
  name: cert-manager
spec:
  namespace: cert-manager
  source:
    sourceType: Catalog
    catalog:
      packageName: cert-manager
      version: "1.x"

Both generations are in use and the catalogs work with either. If you meet a cluster, check which API group is present with kubectl api-resources | grep -i -e operators.coreos -e operatorframework before you write YAML.

Three ways to install an operator

OLM is optional. It is not part of Kubernetes and a kubeadm cluster does not have it. Many operators are installed without it.

WayWhat you runUpgradesWhen you meet it
Plain manifestskubectl create -f <file or URL> with the CRDs, RBAC and Deployment in one fileYou apply the next version's fileMost upstream projects publish one. The likeliest form in the exam
Helm charthelm install <release> <chart>helm upgrade; note that Helm does not upgrade CRDs kept in a chart's crds/ folderClusters that already use Helm for components
OLMA Subscription (or the install file from OperatorHub.io)Automatic or approved by hand, following the channelClusters that have OLM, and every OpenShift cluster

All three end in the same place: CRDs in the API, a controller Pod running with a ServiceAccount, and nothing managed yet. The application appears only when you create a custom resource.

Using it

Example · Install an operator from a manifest and create its custom resource

The task: "Install the Prometheus operator and run a Prometheus server with two replicas in the namespace default." No OLM is involved.

Install the operator. The project publishes one file, bundle.yaml, with every CRD, the RBAC objects and the Deployment:

LATEST=$(curl -s https://api.github.com/repos/prometheus-operator/prometheus-operator/releases/latest | jq -cr .tag_name)
curl -sL https://github.com/prometheus-operator/prometheus-operator/releases/download/${LATEST}/bundle.yaml \
  | kubectl create -f -

Check the two things every operator install gives you, the controller and the new kinds:

kubectl get deployment prometheus-operator
kubectl api-resources --api-group=monitoring.coreos.com
NAME                  SHORTNAMES   APIVERSION                 NAMESPACED   KIND
alertmanagers         am           monitoring.coreos.com/v1   true         Alertmanager
podmonitors           pmon         monitoring.coreos.com/v1   true         PodMonitor
prometheuses          prom         monitoring.coreos.com/v1   true         Prometheus
servicemonitors       smon         monitoring.coreos.com/v1   true         ServiceMonitor
...

Nothing is monitoring anything yet. Create the custom resource:

apiVersion: monitoring.coreos.com/v1
kind: Prometheus
metadata:
  name: main
spec:
  replicas: 2
  resources:
    requests:
      memory: 400Mi
kubectl apply -f prometheus.yaml
kubectl get prometheus main
kubectl get statefulset,pods -l app.kubernetes.io/instance=main
NAME                               READY   AGE
statefulset.apps/prometheus-main   2/2     50s

NAME                    READY   STATUS    RESTARTS   AGE
pod/prometheus-main-0   2/2     Running   0          50s
pod/prometheus-main-1   2/2     Running   0          50s

You never wrote a StatefulSet. To scale, edit the custom resource, not the StatefulSet: kubectl patch prometheus main --type merge -p '{"spec":{"replicas":3}}'.

Example · Install OLM, then an operator from OperatorHub.io

Install OLM on a practice cluster. Either command works; the first needs a release tag, which you take from the project's releases page:

OLM=v0.28.0     # use the newest tag from the releases page
curl -sL https://github.com/operator-framework/operator-lifecycle-manager/releases/download/${OLM}/install.sh | bash -s ${OLM}
# or, with the SDK installed:
operator-sdk olm install
kubectl get pods -n olm
NAME                                READY   STATUS    RESTARTS   AGE
catalog-operator-6b8c5d7f9c-4jq2m   1/1     Running   0          60s
olm-operator-7d9f6c8b5d-n8w2x       1/1     Running   0          60s
operatorhubio-catalog-k7xvd         1/1     Running   0          45s
packageserver-5c9d8b7f6d-2ptzl      1/1     Running   0          40s
packageserver-5c9d8b7f6d-hq9vw      1/1     Running   0          40s

Look the package up before you subscribe, so the channel name is not a guess:

kubectl get packagemanifests | grep cert-manager
kubectl get packagemanifest cert-manager -o jsonpath='{.status.defaultChannel}{"\n"}'

Subscribe in the operators namespace, which already has a cluster-wide OperatorGroup:

apiVersion: operators.coreos.com/v1alpha1
kind: Subscription
metadata:
  name: my-cert-manager
  namespace: operators
spec:
  name: cert-manager                 # the package
  channel: stable                    # the release track
  source: operatorhubio-catalog      # the CatalogSource...
  sourceNamespace: olm               # ...and where it lives
kubectl apply -f subscription.yaml
kubectl get sub,ip,csv -n operators
NAME                                                PACKAGE        SOURCE                  CHANNEL
subscription.operators.coreos.com/my-cert-manager   cert-manager   operatorhubio-catalog   stable

NAME                                             CSV                     APPROVAL    APPROVED
installplan.operators.coreos.com/install-8xk2f   cert-manager.v1.16.5    Automatic   true

NAME                                                              DISPLAY        VERSION   PHASE
clusterserviceversion.operators.coreos.com/cert-manager.v1.16.5   cert-manager   1.16.5    Succeeded

Succeeded on the CSV is the line to wait for. The version you see will be newer. The operator's Pods run in operators, and the new kinds (Certificate, Issuer, ClusterIssuer) are ready to use. OperatorHub.io gives the same Subscription as a one-line install for each operator, under its Install button.

Example · Hold upgrades until you approve them

On a production cluster you may not want an operator to upgrade itself overnight. Set manual approval in the Subscription:

spec:
  name: cert-manager
  channel: stable
  source: operatorhubio-catalog
  sourceNamespace: olm
  installPlanApproval: Manual

Now every install and every upgrade stops at an unapproved InstallPlan:

kubectl get ip -n operators
NAME            CSV                    APPROVAL   APPROVED
install-m4t9c   cert-manager.v1.17.0   Manual     false

Read what it would change, then approve it:

kubectl get ip install-m4t9c -n operators -o jsonpath='{.spec.clusterServiceVersionNames}{"\n"}'
kubectl patch ip install-m4t9c -n operators --type merge -p '{"spec":{"approved":true}}'

A common surprise: with Manual, the first install waits too. A Subscription that seems to do nothing is often waiting for this patch.

Example · Find out why a custom resource does nothing

You created a custom resource and no Pods appear. Work from the object towards the controller:

kubectl get prometheus main -o yaml | grep -A 12 '^status:'   # many operators report conditions here
kubectl describe prometheus main | tail -n 8                  # events recorded by the operator
kubectl get deployment -A | grep -i operator                  # is the controller running at all?
kubectl logs deployment/prometheus-operator --tail=30         # what did it try to do?

The usual causes, in order of likelihood:

  • The operator is not running. Its Deployment has 0 ready replicas, often an image pull or RBAC problem. Without the controller, a custom resource is only stored data.
  • The operator does not watch your namespace. Some operators watch one namespace, set by a flag, an environment variable such as WATCH_NAMESPACE, or an OperatorGroup.
  • The operator lacks permission. Its log shows forbidden errors: the ServiceAccount's ClusterRole misses a rule, for example after someone edited it.
  • The custom resource is invalid in a way the schema allows. The log names the field.

Tricks

Common mistakes

  • Installing the operator and stopping. The operator manages nothing until a custom resource exists. Read the task for the kind it wants you to create.
  • Editing what the operator owns. Scaling the StatefulSet or editing the generated ConfigMap works for a few seconds, then the operator puts it back. Edit the custom resource.
  • Guessing the channel or catalog in a Subscription. A wrong channel, source or sourceNamespace gives no error on creation; the Subscription only reports the problem in its status. Look them up with kubectl get packagemanifest <name>.
  • Subscribing in a namespace with no OperatorGroup. The CSV is created but never succeeds. Use operators, or create an OperatorGroup first.
  • Expecting OLM in every cluster. If kubectl get csv answers the server doesn't have a resource type "csv", OLM is not installed. Use the operator's manifest or Helm chart.
  • Deleting CRDs to "clean up". That deletes every custom resource of the kind and everything those own.

Recap

  • An operator is a CRD plus a controller that carries the knowledge needed to run one application.
  • The Operator Framework has three parts: the Operator SDK builds operators (Helm, Ansible or Go), a catalog such as OperatorHub.io lists them, and OLM installs and upgrades them.
  • With OLM you create a Subscription; it becomes an InstallPlan, then a CSV in phase Succeeded, then a running Deployment. installPlanApproval: Manual puts you in front of each upgrade.
  • Without OLM, a manifest (kubectl create -f) or a Helm chart installs the same CRDs, RBAC and Deployment.
  • The install is half the job: create the custom resource, change it instead of the objects the operator owns, and read the operator's logs when nothing happens.

Official docs

Quiz: check what you learned

5 questions on this lesson · about 3 min · instant feedback. Missed questions come back in your review queue.

Sign in to take the quiz and keep your score.

Practise: try it on a real cluster

A hands-on lab for this lesson. You get goals, not steps: any solution that reaches them passes.

  • Section capstone. Prepare a namespace for an audit: a short-lived login for the auditor, read-only access for her group, an identity of its own for one app, a hardened container, and network policies that close the namespace.

    Build · Level 3 · challenge · target 40 min

Discussion

Learners ask and answer questions here. Sign in to read and join the discussion.