Grafana Operator
repository·master·Indexed 23 days ago
https://github.com/grafana/grafana-operatorA Kubernetes operator that automates the lifecycle management of Grafana instances and associated resources, such as dashboards and data sources. It supports both in-cluster and external deployments, providing installation options via Helm, Terraform, and Kustomize. Key features include support for Grafonnet for programmatic dashboard generation, cross-namespace resource syncing, and flexible CRD management modes (immutable and mutable).
What's inside grafana-operator
- Grafana Operator is a Kubernetes operator designed to manage Grafana instances and their associated resources both inside and outside of Kubernetes. It simplifies the installation, configuration, and maintenance of Grafana. It is specifically designed to support Infrastructure as Code (IaC) and GitOps workflows using tools such as ArgoCD and Flux CD.
Overview of Grafana Operator capabilities
masterThe Grafana Operator is a Kubernetes-native tool designed to manage Grafana instances and resources (like dashboards, data sources, and plugins) both inside and outside of Kubernetes. It is optimized for Infrastructure as Code (IaC) and GitOps workflows (e.g., using ArgoCD or Flux CD).
Key benefits include:
- Effortless multi-instance and multi-namespace deployments.
- Management of dashboards, data sources, and plugins via code.
- Support for Kubernetes and OpenShift.
- Ability to manage external Grafana instances.
- Multi-architecture support.
- One-click installation via Operatorhub/OLM.
Impact of the Grafana Operator migration to upstream Grafana
masterThe
grafana-operatorhas migrated to the officialgrafanaGitHub organization. For existing users, this migration is designed to be non-breaking:- API Stability: There are no plans to introduce new API versions or breaking changes to the existing API.
- Deployment Compatibility: Existing deployments are not expected to be affected.
- Licensing: The license remains exactly the same.
- Repository Access: The primary repository address has moved from
https://github.com/grafana/grafana-operatortohttps://github.com/grafana/grafana-operator(within thegrafanaorganization). GitHub provides automatic redirects for the old address. - OCI Artifacts: Future OCI URIs will reflect the new organization. While existing artifacts will be copied to the new location, note that their digests will change.
- Documentation: The documentation URL is changing, but a redirect from the old location is provided.
Understand the selection model change in v5
masterIn Grafana Operator v4, the
Grafanaresource was responsible for finding its dashboards via thedashboardLabelSelectorfield in its spec.In v5, this relationship is inverted: the
GrafanaDashboardresource is responsible for selecting its targetGrafanainstance using theinstanceSelectorfield. This allows a single Grafana instance to manage multiple dashboards and enables support for multiple, potentially external, Grafana instances.v4 Pattern (Push-based selection):
Grafanaspec defines which labels to look for inGrafanaDashboardresources.v5 Pattern (Pull-based selection):
GrafanaDashboardspec defines whichGrafanainstance labels to match viainstanceSelector.# v4: Selection is in the Grafana spec apiVersion: integreatly.org/v1alpha1 kind: Grafana spec: dashboardLabelSelector: - matchExpressions: - { key: app, operator: In, values: [grafana] } --- apiVersion: integreatly.org/v1alpha1 kind: GrafanaDashboard metadata: labels: app: grafana # v5: Selection is in the GrafanaDashboard spec apiVersion: grafana.integreatly.org/v1beta1 kind: Grafana metadata: labels: dashboards: "grafana" --- apiVersion: grafana.integreatly.org/v1beta1 kind: GrafanaDashboard spec: instanceSelector: matchLabels: dashboards: "grafana"Parameterize dashboards with Jsonnet external variables (std.extVar)
masterYou can use Jsonnet external variables (std.extVar) to inject values into your dashboards. To define these, you must modify your JaaS deployment to include environment variables following the patternJAAS_EXT_VAR_*(for example,JAAS_EXT_VAR_description).Use GrafanaManifest to manage unsupported resources
masterTheGrafanaManifestresource allows you to manage Kubernetes resources that are not natively implemented in the Grafana operator. It utilizes Kubernetes-style APIs to support resources provided by plugins or other extensions. You can also use thepatchcapability within aGrafanaManifestto dynamically replace values in the manifest.Sync dashboards to external Grafana instances using instanceSelectors
masterTo sync dashboards to a specific Grafana instance (whether internal or external), use the
instanceSelectorfield on theGrafanaDashboardresource. This selector must match the labels defined on theGrafanainstance.For example, if a
Grafanainstance has the labeldashboards: "external-grafana", the correspondingGrafanaDashboardmust have aninstanceSelectorthat selects that label to ensure it is applied to the correct instance.Understand Grafana status conditions
masterThe
Grafana.status.conditionsarray contains details about specific aspects of the Grafana resource's state. Each condition follows the standard Kubernetes condition pattern.Fields:
type(string): The type of condition (e.g.,Ready) in CamelCase.status(enum): The state of the condition. Values:True,False,Unknown.reason(string): A CamelCase programmatic identifier for the last transition.message(string): A human-readable message describing the transition.lastTransitionTime(string): The timestamp of the last status change (date-time format).observedGeneration(integer): The.metadata.generationthat the condition was based on. If this is lower than the current metadata generation, the condition is out of date.
Manage Custom Resource Definitions (CRDs) in Helm
masterThe Helm chart supports two modes for managing Custom Resource Definitions (CRDs) via the
crds.immutableboolean key:- Immutable CRDs (
crds.immutable: true, default): CRDs are installed once from thecrds/directory. They are not updated by Helm upgrades and require manual updates usingkubectl apply. - Mutable CRDs (
crds.immutable: false): CRDs are managed and upgraded alongside the Helm chart using thetemplates/directory. This allows for automatic upgrades duringhelm upgrade.
Important: If you are switching from immutable to mutable CRDs for the first time, you must run:
helm upgrade -i --take-ownership.Both types of CRDs are protected from deletion during a
helm uninstallto prevent accidental cascading deletions of managed resources.- Immutable CRDs (
Define dashboards using Grafonnet
masterThe Grafana Operator supports Grafonnet, a Jsonnet framework that allows you to define Grafana dashboards using code. This enables programmatic dashboard generation and version control of dashboard definitions via the Jsonnet language.Manage Grafana Organizations
masterThe Grafana Operator does not provide a native abstraction for managing Grafana Organizations. The recommended pattern is to deploy multiple independent Grafana instances and manage them via CI/CD.
If you must use organizations within a single instance, you can configure the
spec.client.headersmap in yourGrafanaCR to set theX-Grafana-Org-Idheader for API calls.Select Grafana instances for Alert Rule Groups
masterUse
spec.instanceSelectorto define which Grafana instances theGrafanaAlertRuleGroupshould be imported into. This field is immutable.You can select instances using two methods:
matchLabels: A map of{key, value}pairs. All pairs must match (ANDed logic).matchExpressions: A list of label selector requirements. All requirements are ANDed.
matchExpressionsoperators:In:valuesmust be non-empty.NotIn:valuesmust be non-empty.Exists:valuesmust be empty.DoesNotExist:valuesmust be empty.