Operators with OLM: the Grafana Operator
Author: Fabrice JAMMES (LinkedIn). Duration: 15 minutes of demo, 30-40 minutes of lab
Objective
On OpenShift, operators are installed and upgraded by the Operator Lifecycle Manager (OLM), from the catalogs displayed in the web console. This page has two parts:
- Demo, by the trainer: install the Grafana Operator from the web console, in All namespaces mode.
- Lab, by the trainees: find the objects OLM created (
Subscription,InstallPlan,ClusterServiceVersion,OperatorGroup), create Grafana instances in their own namespace, and check that the operator keeps reconciling them.
Both parts run on the OpenShift cluster shared by all trainees: switch to it first with kubectx <my-openshift-context>.
Note
An operator owns cluster-wide objects (its CRDs) and OLM runs a single copy of it per cluster: on a shared cluster, only the trainer installs, upgrades or uninstalls it. Trainees only create custom resources in their own namespace.
Part 1 — Demo: install the Grafana Operator from the web console
The OLM objects
In the order they appear during the demo:
| Object | Namespace | Role |
|---|---|---|
| CatalogSource | openshift-marketplace | A catalog: a pod serving an index of operators (redhat-operators, certified-operators, community-operators, redhat-marketplace). |
| PackageManifest | openshift-marketplace | One operator in a catalog: its channels, the current version of each channel and the CRDs it provides. Read-only virtual resource, computed by the packageserver from the catalogs; the Software Catalog displays it. |
| Subscription | the operator namespace (openshift-operators) | The administrator’s intent: “I want operator X, from catalog Y, on channel Z, with this approval mode”. Created by the Install button. |
| InstallPlan | the operator namespace | The list of objects OLM will create to fulfill the Subscription, read from the operator bundle: the CSV and the CRDs. In Manual mode it waits for approved: true, set by the Approve button; once approved, OLM creates these objects. |
| ClusterServiceVersion (CSV) | the operator namespace, copied in every namespace in AllNamespaces mode | The installation recipe of one operator version (grafana-operator.v5.25.0): the Deployment of the operator, the permissions it needs and the CRDs it owns. Created by the InstallPlan; OLM then reads it and creates the Deployment, ServiceAccount and RBAC. Phase Succeeded means the operator runs. |
| OperatorGroup | the operator namespace | Prerequisite, created before the Subscription: it says which namespaces the operators of this namespace watch. OLM refuses to install a CSV in a namespace without exactly one OperatorGroup, and scopes the RBAC to these namespaces. global-operators, already present in openshift-operators, has no spec.targetNamespaces nor spec.selector: this means AllNamespaces, shown as status.namespaces: [""]. |
The catalog publishes packages. The Subscription picks one, in a namespace that has an OperatorGroup. OLM computes an InstallPlan; once approved, it creates the CSV and the CRDs. OLM then deploys the operator described in the CSV. On an upgrade, a new version appears in the channel, OLM creates a new InstallPlan and, once approved, a new CSV replaces the old one.
Before starting
Logged in to the web console as kubeadmin, in the Administrator perspective. Since OpenShift 4.20, the Operators menu is named Ecosystem and the OperatorHub is the Software Catalog; older versions use Operators > OperatorHub.
Keep a terminal open next to the console, to show the objects each click creates (-w only accepts a single resource type, hence watch):
1. The catalogs
Administration > Cluster Settings > Configuration > OperatorHub > Sources: each catalog is a CatalogSource, a pod serving an index of operators.
Explain the four sources: Red Hat (supported by Red Hat), Certified (tested by Red Hat, supported by the vendor), Community (no support) and Marketplace (paid).
2. Find the operator
Ecosystem > Software Catalog, filter on the Community source and search for Grafana. Open the Grafana Operator card (the console warns that community operators are not supported by Red Hat) and show:
- the capability level, from Basic Install to Auto Pilot
- the channel (
v5) and the available versions. A channel is an update stream published by the operator author: the Subscription follows one channel, and OLM proposes the new versions released in it. Herev5is the major version of the operator (5.25.0), not of Grafana itself; other operators usestable,fastorcandidate. Changing major version means switching channel, on purpose. - the source (
Community) and the provider
The panel does not list the provided APIs (the CRDs): they appear on the next page, after clicking Install, and later in Installed Operators > Grafana Operator > Details. The CLI shows them too, with the channels:
3. Install it
Click Install and fill in the form:
- Update channel:
v5 - Installation mode: All namespaces on the cluster, the operator goes to
openshift-operatorsand watches every namespace. Point out that the operator lists the supported modes: some operators can only watch their own namespace. - Update approval: Manual, so that every new version waits for an administrator
Click Install. The terminal shows a Subscription, then an InstallPlan with APPROVED false: nothing is installed yet.
4. Approve the InstallPlan
The console displays Manual approval required: click Review manual InstallPlan. The InstallPlan lists everything OLM is about to create: the CRDs, the ServiceAccount, the ClusterRoles and the operator Deployment. Click Approve.
The CSV goes through Pending, InstallReady, Installing, then Succeeded.
5. Tour of the installed operator
Ecosystem > Installed Operators > Grafana Operator:
- Details: the status of the CSV, the provided APIs
- YAML: the
ClusterServiceVersion, which holds the operator Deployment, the owned CRDs and the RBAC it needs - Subscription: the channel, the approval mode, the installed version; this is where an upgrade is approved, or the approval mode changed
- Events
Then Workloads > Pods, project openshift-operators: the operator is a plain pod. Finally switch to any other project: Installed Operators also lists the Grafana Operator there, because OLM copies the CSV in every namespace in All namespaces mode.
6. The same installation from the CLI
With Automatic, OLM approves the InstallPlan itself, now and for every new version published in the channel. Manual keeps upgrades under control, at the cost of an approval for each version:
OLM resolves the Subscriptions of a namespace together: one Manual Subscription in openshift-operators can hold back the upgrades of the other operators of this namespace.
7. Uninstall, at the end of the training
Installed Operators > Grafana Operator > Actions > Uninstall Operator. It removes the Subscription and the CSV, so the operator Deployment goes away, but OLM keeps the CRDs and the custom resources of every namespace. Remove the CRDs explicitly:
The solutions are validated by ex1-olm-grafana.sh: -s installs the operator, -c uninstalls it and removes its CRDs.
Part 2 — Lab
Tip
The Grafana Operator has been installed by the trainer in Part 1. Do not install, upgrade or uninstall it.
Setup
Exercise 1 — The objects created by OLM
- Which
Subscriptioninstalled the Grafana Operator, from which catalog and which channel? Is the upgrade approval automatic or manual?
- Which version of the operator is installed? Which CRDs does it own?
- Where does the operator run? Which namespaces does it watch?
- Run
oc get csvin your own namespace. Why does the Grafana Operator CSV appear there?
Exercise 2 — Create a Grafana instance
Create a Grafana custom resource in your namespace, from the console or from the CLI.
From the console: Ecosystem > Installed Operators, select your project grafana-<id> at the top (not openshift-operators), Grafana Operator > Grafana > Create Grafana. Switch to the YAML view, replace its content with the YAML below and click Create. The Form view fails with Required value for field "spec.route.spec.to.name".
From the CLI, the same YAML:
Note
STAGE STATUS stays failed for about 2 minutes, until Grafana answers the operator: be patient.
- Watch the status of the instance. Which objects did the operator create?
- Retrieve the admin credentials and the URL of Grafana, then log in.
Exercise 3 — Reconciliation
Delete the Grafana Deployment, then watch the Deployments of your namespace. What happens, and why?
Exercise 4 — Datasource and dashboard as code
Create a datasource and a dashboard for your instance:
- Check that the dashboard appears in your Grafana. How does the operator know which Grafana instance should receive it?
Exercise 5 — Uninstalling an operator
Do not run it: what would happen to your Grafana instance if the trainer uninstalled the Grafana Operator from the console?