From 3166d434c07a9d009412b1d60d93bcdf3ff0294b Mon Sep 17 00:00:00 2001 From: Fabian Mueller Date: Fri, 8 Sep 2023 15:58:37 +0200 Subject: [PATCH] Get Grafana Image from env; bundle cmd for redhat marketplace --- Makefile | 48 ++++++++++++++++--- PREPARE_RELEASE.md | 25 +++++----- ...rafana-operator.clusterserviceversion.yaml | 2 + config/default/kustomization.yaml | 6 +-- config/manager/kustomization.yaml | 6 ++- config/manager/manager.yaml | 2 + .../grafana/deployment_reconciler.go | 11 ++++- .../templates/deployment.yaml | 4 ++ deploy/helm/grafana-operator/values.yaml | 4 ++ .../cluster_scoped/kustomization.yaml | 4 +- .../namespace_scoped/kustomization.yaml | 4 +- 11 files changed, 85 insertions(+), 31 deletions(-) diff --git a/Makefile b/Makefile index 78d7d00ac..8d87d75fa 100644 --- a/Makefile +++ b/Makefile @@ -4,6 +4,20 @@ # Current Operator version VERSION ?= 5.4.2 +# BUNDLE_GEN_FLAGS are the flags passed to the operator-sdk generate bundle command +BUNDLE_GEN_FLAGS ?= -q --overwrite --version $(VERSION) $(BUNDLE_METADATA_OPTS) + +# USE_IMAGE_DIGESTS defines if images are resolved via tags or digests +# You can enable this value if you would like to use SHA Based Digests +# To enable set flag to true +USE_IMAGE_DIGESTS ?= false +ifeq ($(USE_IMAGE_DIGESTS), true) + BUNDLE_GEN_FLAGS += --use-image-digests +endif + +# Set the Operator SDK version to use. By default, what is installed on the system is used. +# This is useful for CI or a project to utilize a specific version of the operator-sdk toolkit. +OPERATOR_SDK_VERSION ?= v1.31.0 # Image URL to use all building/pushing image targets REGISTRY ?= ghcr.io REPO ?= grafana-operator @@ -151,6 +165,23 @@ envtest: $(ENVTEST) ## Download envtest-setup locally if necessary. $(ENVTEST): $(LOCALBIN) test -s $(LOCALBIN)/setup-envtest || GOBIN=$(LOCALBIN) go install sigs.k8s.io/controller-runtime/tools/setup-envtest@latest +.PHONY: operator-sdk +OPERATOR_SDK ?= $(LOCALBIN)/operator-sdk +operator-sdk: ## Download operator-sdk locally if necessary. +ifeq (,$(wildcard $(OPERATOR_SDK))) +ifeq (, $(shell which operator-sdk 2>/dev/null)) + @{ \ + set -e ;\ + mkdir -p $(dir $(OPERATOR_SDK)) ;\ + OS=$(shell go env GOOS) && ARCH=$(shell go env GOARCH) && \ + curl -sSLo $(OPERATOR_SDK) https://github.com/operator-framework/operator-sdk/releases/download/$(OPERATOR_SDK_VERSION)/operator-sdk_$${OS}_$${ARCH} ;\ + chmod +x $(OPERATOR_SDK) ;\ + } +else +OPERATOR_SDK = $(shell which operator-sdk) +endif +endif + ### # END OF kubebuilder SECTION ### @@ -175,14 +206,19 @@ endif BUNDLE_METADATA_OPTS ?= $(BUNDLE_CHANNELS) $(BUNDLE_DEFAULT_CHANNEL) .PHONY: bundle -bundle: manifests kustomize ## Generate bundle manifests and metadata, then validate generated files. - operator-sdk generate kustomize manifests -q +bundle: manifests kustomize operator-sdk ## Generate bundle manifests and metadata, then validate generated files. + $(OPERATOR_SDK) generate kustomize manifests -q cd config/manager && $(KUSTOMIZE) edit set image controller=$(IMG) - $(KUSTOMIZE) build config/manifests | operator-sdk generate bundle -q --overwrite --version $(VERSION) $(BUNDLE_METADATA_OPTS) - operator-sdk bundle validate ./bundle + $(KUSTOMIZE) build config/manifests | $(OPERATOR_SDK) generate bundle $(BUNDLE_GEN_FLAGS) + $(OPERATOR_SDK) bundle validate ./bundle -# e2e +.PHONY: bundle/redhat +bundle/redhat: BUNDLE_GEN_FLAGS += --use-image-digests +bundle/redhat: + $(KUSTOMIZE) build config/manifests | $(OPERATOR_SDK) generate bundle $(BUNDLE_GEN_FLAGS) + $(OPERATOR_SDK) bundle validate ./bundle +# e2e .PHONY: e2e e2e: kuttl install deploy-kuttl ## Run e2e tests using kuttl. $(KUTTL) test @@ -291,7 +327,7 @@ ifeq (,$(shell which opm 2>/dev/null)) set -e ;\ mkdir -p $(dir $(OPM)) ;\ OS=$(shell go env GOOS) && ARCH=$(shell go env GOARCH) && \ - curl -sSLo $(OPM) https://github.com/operator-framework/operator-registry/releases/download/v1.15.1/$${OS}-$${ARCH}-opm ;\ + curl -sSLo $(OPM) https://github.com/operator-framework/operator-registry/releases/download/v1.23.2/$${OS}-$${ARCH}-opm ;\ chmod +x $(OPM) ;\ } else diff --git a/PREPARE_RELEASE.md b/PREPARE_RELEASE.md index 4e368bbd5..bb8c52d49 100644 --- a/PREPARE_RELEASE.md +++ b/PREPARE_RELEASE.md @@ -15,18 +15,17 @@ After that you need to run `make helm/docs` which will generate the changes to b There is a lot of information on what is needed to manage OLM [compatible operators](https://redhat-connect.gitbook.io/certified-operator-guide/ocp-deployment/operator-metadata/creating-the-csv). - Update the `Makefile` version -- `make generate`, `make manifests` & `make bundle` -- Update `containerImage` field in `config/manifests/bases/grafana-operator.clusterserviceversion.yaml` -- Update `replaces` field in `config/manifests/bases/grafana-operator.clusterserviceversion.yaml` -- Update `CreatedAt` field in `config/manifests/bases/grafana-operator.clusterserviceversion.yaml` - You will have to asses when it's going to get merged and you will be able to do a release. - You should make sure it's the same date. If not you will have to change it - manually when creating PR:s to OLM. - - # This is how the time syntax should look. - $ docker inspect ghcr.io/grafana-operator/grafana-operator:v5.0.0 |jq '.[0].Created' - "2023-11-22T10:34:12.173861869Z" - # 2023-11-22T10:34:12Z is enough +- Run `make generate` & `make manifests` +- Update the following fields under `metadata.annotations` in `config/manifests/bases/grafana-operator.clusterserviceversion.yaml`: + - `containerImage` + - `replaces` + - `createdAt`: You will have to asses when it's going to get merged and you will be able to do a release. You should make sure it's the same date. If not you will have to change it manually when creating PR:s to OLM. + ``` + # This is how the time syntax should look. + $ docker inspect ghcr.io/grafana-operator/grafana-operator:v5.0.0 |jq '.[0].Created' + "2023-11-22T10:34:12.173861869Z" + # 2023-11-22T10:34:12Z is enough + ``` - Run `make bundle` - `Helm` look if any rbac rules have been changed in the last release, if so verify that the rbac rules for the helm chart is correct. This should be done in those PRs but it don't hurt take an extra look. - Create a PR and get it merged @@ -50,6 +49,8 @@ Update `operators/grafana-operator/grafana-operator.package.yaml` with the new t ## RedHat operators +- Run `make bundle/redhat` + Create a new version of the operator under [https://github.com/redhat-openshift-ecosystem/community-operators-prod/tree/main/operators/grafana-operator](https://github.com/redhat-openshift-ecosystem/community-operators-prod/tree/main/operators/grafana-operator) that matches the new tag. diff --git a/bundle/manifests/grafana-operator.clusterserviceversion.yaml b/bundle/manifests/grafana-operator.clusterserviceversion.yaml index 1b92c88fd..fb23d0176 100644 --- a/bundle/manifests/grafana-operator.clusterserviceversion.yaml +++ b/bundle/manifests/grafana-operator.clusterserviceversion.yaml @@ -323,6 +323,8 @@ spec: - --metrics-bind-address=0.0.0.0:9090 - --leader-elect env: + - name: RELATED_IMAGE_GRAFANA + value: docker.io/grafana/grafana:9.1.6 - name: WATCH_NAMESPACE valueFrom: fieldRef: diff --git a/config/default/kustomization.yaml b/config/default/kustomization.yaml index 827fb3e52..4e2932030 100644 --- a/config/default/kustomization.yaml +++ b/config/default/kustomization.yaml @@ -12,7 +12,7 @@ namePrefix: grafana-operator- #commonLabels: # someName: someValue -bases: +resources: - ../crd - ../rbac - ../manager @@ -23,10 +23,8 @@ bases: #- ../certmanager # [PROMETHEUS] To enable prometheus monitor, uncomment all sections with 'PROMETHEUS'. #- ../prometheus - -resources: # Add metrics service - - metrics_service.yaml +- metrics_service.yaml patchesStrategicMerge: # Protect the /metrics endpoint by putting it behind auth. diff --git a/config/manager/kustomization.yaml b/config/manager/kustomization.yaml index ef986be44..64e2deff7 100644 --- a/config/manager/kustomization.yaml +++ b/config/manager/kustomization.yaml @@ -1,3 +1,6 @@ +apiVersion: kustomize.config.k8s.io/v1beta1 +kind: Kustomization + resources: - manager.yaml @@ -8,8 +11,7 @@ configMapGenerator: - files: - controller_manager_config.yaml name: manager-config -apiVersion: kustomize.config.k8s.io/v1beta1 -kind: Kustomization + images: - name: controller newName: ghcr.io/grafana-operator/grafana-operator diff --git a/config/manager/manager.yaml b/config/manager/manager.yaml index 637544fe8..ed4844aab 100644 --- a/config/manager/manager.yaml +++ b/config/manager/manager.yaml @@ -58,6 +58,8 @@ spec: cpu: 100m memory: 20Mi env: + - name: RELATED_IMAGE_GRAFANA + value: "docker.io/grafana/grafana:9.1.6" - name: WATCH_NAMESPACE valueFrom: fieldRef: diff --git a/controllers/reconcilers/grafana/deployment_reconciler.go b/controllers/reconcilers/grafana/deployment_reconciler.go index ea7b97856..a57bc9569 100644 --- a/controllers/reconcilers/grafana/deployment_reconciler.go +++ b/controllers/reconcilers/grafana/deployment_reconciler.go @@ -3,6 +3,7 @@ package grafana import ( "context" "fmt" + "os" "github.com/grafana-operator/grafana-operator/v5/api/v1beta1" config2 "github.com/grafana-operator/grafana-operator/v5/controllers/config" @@ -132,10 +133,18 @@ func getVolumeMounts(cr *v1beta1.Grafana, scheme *runtime.Scheme) []v1.VolumeMou return mounts } +func setGrafanaImage() string { + grafanaImg := os.Getenv("RELATED_IMAGE_GRAFANA") + if grafanaImg == "" { + grafanaImg = fmt.Sprintf("%s@%s", config2.GrafanaImage, config2.GrafanaVersion) + } + return grafanaImg +} + func getContainers(cr *v1beta1.Grafana, scheme *runtime.Scheme, vars *v1beta1.OperatorReconcileVars, openshiftPlatform bool) []v1.Container { var containers []v1.Container - image := fmt.Sprintf("%s:%s", config2.GrafanaImage, config2.GrafanaVersion) + image := setGrafanaImage() plugins := model.GetPluginsConfigMap(cr, scheme) // env var to restart containers if plugins change diff --git a/deploy/helm/grafana-operator/templates/deployment.yaml b/deploy/helm/grafana-operator/templates/deployment.yaml index db3f2ff0f..77d1a204d 100644 --- a/deploy/helm/grafana-operator/templates/deployment.yaml +++ b/deploy/helm/grafana-operator/templates/deployment.yaml @@ -42,6 +42,10 @@ spec: image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}" imagePullPolicy: {{ .Values.image.pullPolicy }} env: + {{- if .Values.env.grafanaImage }} + - name: RELATED_IMAGE_GRAFANA + value: {{ .Values.env.grafanaImage }} + {{- end }} - name: WATCH_NAMESPACE {{- if and .Values.namespaceScope (eq .Values.watchNamespaces "") }} value: {{ .Release.Namespace }} diff --git a/deploy/helm/grafana-operator/values.yaml b/deploy/helm/grafana-operator/values.yaml index ae3caa1f0..e7aceb76d 100644 --- a/deploy/helm/grafana-operator/values.yaml +++ b/deploy/helm/grafana-operator/values.yaml @@ -26,6 +26,10 @@ image: # -- image pull secrets imagePullSecrets: [] +env: + # -- grafana image, e.g. docker.io/grafana/grafana:9.1.6 + grafanaImage: "" + nameOverride: "" fullnameOverride: "" diff --git a/deploy/kustomize/overlays/cluster_scoped/kustomization.yaml b/deploy/kustomize/overlays/cluster_scoped/kustomization.yaml index 55b278edb..0fc201889 100644 --- a/deploy/kustomize/overlays/cluster_scoped/kustomization.yaml +++ b/deploy/kustomize/overlays/cluster_scoped/kustomization.yaml @@ -1,7 +1,5 @@ -bases: - - ../../base - namespace: grafana resources: + - ../../base - rbac.yaml diff --git a/deploy/kustomize/overlays/namespace_scoped/kustomization.yaml b/deploy/kustomize/overlays/namespace_scoped/kustomization.yaml index 97971dbda..dd6479d11 100644 --- a/deploy/kustomize/overlays/namespace_scoped/kustomization.yaml +++ b/deploy/kustomize/overlays/namespace_scoped/kustomization.yaml @@ -1,9 +1,7 @@ -bases: - - ../../base - namespace: grafana resources: + - ../../base - rbac.yaml patches: