diff --git a/.gitignore b/.gitignore index 2d53820e8..854605259 100644 --- a/.gitignore +++ b/.gitignore @@ -49,5 +49,5 @@ community-operators-prod/ bundle.Dockerfile -# Ignore .env file -.env \ No newline at end of file +# Ignore python virtual env +.venv/ diff --git a/Makefile b/Makefile index e69f588e3..9ff03bf62 100644 --- a/Makefile +++ b/Makefile @@ -9,6 +9,7 @@ VERSION ?= 3.2.0 # Default bundle image tag IMAGE_TAG_BASE ?= quay.io/project-koku/koku-metrics-operator BUNDLE_IMG ?= $(IMAGE_TAG_BASE)-bundle:v$(VERSION) +PREVIOUS_BUNDLE_IMG ?= $(IMAGE_TAG_BASE)-bundle:v$(PREVIOUS_VERSION) CATALOG_IMG ?= quay.io/project-koku/kmc-test-catalog:v$(VERSION) # Image URL to use all building/pushing image targets @@ -43,14 +44,6 @@ IMAGE_SHA=$(shell docker inspect --format='{{index .RepoDigests 0}}' ${IMG}) OS = $(shell go env GOOS) ARCH = $(shell go env GOARCH) -# DOCKER := $(shell which docker 2>/dev/null) -export DOCKER_DEFAULT_PLATFORM = linux/x86_64 - -# 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.33.0 -OPERATOR_REGISTRY_VERSION ?= v1.34.0 - # CONTAINER_TOOL defines the container tool to be used for building images. # Be aware that the target commands are only tested with Docker which is # scaffolded by default. However, you might want to replace it to use other @@ -272,33 +265,41 @@ get-token-and-cert: ## Get a token from a running K8s cluster for local develop NAMESPACE ?= "" .PHONY: bundle -bundle: manifests kustomize ## Generate bundle manifests and metadata, then validate generated files. +bundle: operator-sdk manifests kustomize ## Generate bundle manifests and metadata, then validate generated files. mkdir -p koku-metrics-operator/$(VERSION)/ rm -rf ./bundle koku-metrics-operator/$(VERSION)/ - operator-sdk generate kustomize manifests - cd config/manager && $(KUSTOMIZE) edit set image controller=${IMAGE_SHA} - $(KUSTOMIZE) build config/manifests | operator-sdk generate bundle --overwrite --version $(VERSION) $(BUNDLE_METADATA_OPTS) - operator-sdk bundle validate ./bundle + $(OPERATOR_SDK) generate kustomize manifests + cd config/manager && $(KUSTOMIZE) edit set image controller=$(IMAGE_SHA) + $(KUSTOMIZE) build config/manifests | $(OPERATOR_SDK) generate bundle --overwrite --version $(VERSION) $(BUNDLE_METADATA_OPTS) cp -r ./bundle/ koku-metrics-operator/$(VERSION)/ cp bundle.Dockerfile koku-metrics-operator/$(VERSION)/Dockerfile - scripts/txt_replace.py $(VERSION) $(PREVIOUS_VERSION) ${IMAGE_SHA} --namespace=${NAMESPACE} + .venv/bin/python scripts/txt_replace.py $(VERSION) $(PREVIOUS_VERSION) $(IMAGE_SHA) --namespace=${NAMESPACE} + $(OPERATOR_SDK) bundle validate koku-metrics-operator/$(VERSION) --select-optional name=multiarch + $(OPERATOR_SDK) bundle validate koku-metrics-operator/$(VERSION) --select-optional suite=operatorframework .PHONY: bundle-build bundle-build: ## Build the bundle image. - cd koku-metrics-operator/$(VERSION) && $(CONTAINER_TOOL) build -t $(BUNDLE_IMG) . + cd koku-metrics-operator/$(VERSION) && $(CONTAINER_TOOL) build --platform linux/x86_64 -t $(BUNDLE_IMG) . .PHONY: bundle-push bundle-push: ## Push the bundle image. $(CONTAINER_TOOL) push $(BUNDLE_IMG) -.PHONY: test-catalog -test-catalog: opm ## Build a test-catalog - $(OPM) index add --from-index quay.io/project-koku/kmc-test-catalog:v${PREVIOUS_VERSION} --bundles ${BUNDLE_IMG} --tag ${CATALOG_IMG} --container-tool docker +.PHONY: bundle-deploy-previous +bundle-deploy-previous: operator-sdk ## Deploy previous bundle into a cluster. + $(OPERATOR_SDK) run bundle $(PREVIOUS_BUNDLE_IMG) --namespace=koku-metrics-operator --install-mode=OwnNamespace -.PHONY: test-catalog-push -test-catalog-push: ## Push the test-catalog - $(CONTAINER_TOOL) push ${CATALOG_IMG} +.PHONY: bundle-deploy +bundle-deploy: operator-sdk ## Deploy current bundle into a cluster. + $(OPERATOR_SDK) run bundle $(BUNDLE_IMG) --namespace=koku-metrics-operator --install-mode=OwnNamespace --security-context-config restricted +.PHONY: bundle-deploy-upgrade +bundle-deploy-upgrade: operator-sdk ## Test a bundle upgrade. The previous bundle must have been deployed first. + $(OPERATOR_SDK) run bundle-upgrade $(BUNDLE_IMG) --namespace=koku-metrics-operator + +.PHONY: bundle-deploy-cleanup +bundle-deploy-cleanup: operator-sdk ## Delete the entirety of the deployed bundle + $(OPERATOR_SDK) cleanup koku-metrics-operator --delete-crds --delete-all --namespace=koku-metrics-operator ##@ Generate downstream file changes @@ -342,6 +343,10 @@ ENVTEST ?= $(LOCALBIN)/setup-envtest KUSTOMIZE_VERSION ?= v5.1.1 CONTROLLER_TOOLS_VERSION ?= v0.13.0 +# 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.33.0 + .PHONY: kustomize kustomize: $(KUSTOMIZE) ## Download kustomize locally if necessary. If wrong version is installed, it will be removed before downloading. $(KUSTOMIZE): $(LOCALBIN) @@ -362,23 +367,6 @@ 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: opm -OPM = $(LOCALBIN)/opm -opm: ## Download opm locally if necessary. -ifeq (,$(wildcard $(OPM))) -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/$(OPERATOR_REGISTRY_VERSION)/$${OS}-$${ARCH}-opm ;\ - chmod +x $(OPM) ;\ - } -else -OPM = $(shell which opm) -endif -endif - .PHONY: operator-sdk OPERATOR_SDK ?= $(LOCALBIN)/operator-sdk operator-sdk: ## Download operator-sdk locally if necessary. @@ -395,3 +383,9 @@ else OPERATOR_SDK = $(shell which operator-sdk) endif endif + +.PHONY: venv +venv: ## create venv for txt_replace script + @python3 -m venv .venv + @.venv/bin/python -m pip install -U pip + @.venv/bin/python -m pip install -r scripts/requirements.txt diff --git a/docs/release-testing.md b/docs/release-testing.md index 725472a19..c12d8cc30 100644 --- a/docs/release-testing.md +++ b/docs/release-testing.md @@ -1,137 +1,75 @@ ## Generating and testing release bundles -A full overview of community operator testing can be found [here](https://operator-framework.github.io/community-operators/testing-operators/). The following steps were parsed from the testing document and are specific to testing the `koku-metrics-operator`. - ### Pre-requisites -* Access to a 4.5+ OpenShift cluster -* opm - -To install opm, clone the operator-registry repository: - -``` -git clone https://github.com/operator-framework/operator-registry -``` -Change into the directory and run `make build`. This will generate an `opm` executable in the `operator-registry/bin/` directory. Add the bin to your path, or substitute `opm` in the following commands with the full path to your `opm` executable. - +* Access to an OpenShift cluster (any currently supported version) ### Testing an operator upgrade - Overview -Testing an upgrade is a complicated and an involved process. The general steps are as follows: -1. Create an opm index (test-catalog) for the most recent release and push to Quay (quay.io/project-koku/kmc-test-catalog contains previous versions and can be used instead of creating your own initial catalog). -2. Create a CatalogSource in OCP, and install the operator (this should be the version that will be upgraded). -3. Generate the new controller image, and push to Quay. -4. Generate the new bundle. -5. Build the bundle image and push to Quay. -6. Create an opm index (test-catalog) that contains the last release and the new release, and push to Quay. -7. Update the CatalogSource in OCP to point to the new test-catalog image. -8. Observe the installed operator, and ensure it upgrades automatically. - - -### Testing an operator upgrade -0. set version numbers: -```sh -$ VERSION_BEFORE_PREVIOUS_VERSION=0.9.7 -$ PREVIOUS_VERSION=0.9.8 -$ VERSION=0.9.9 -``` - -1. Check `quay.io/project-koku/kmc-test-catalog` for the most recent operator release. If the index does not exist, create it: - -Check `quay.io/project-koku/koku-metrics-operator-bundle` for the most recent operator release bundle. Create it if it does not exist: - -```sh -$ cd koku-metrics-operator/$PREVIOUS_VERSION -$ docker build -f Dockerfile . -t quay.io/project-koku/koku-metrics-operator-bundle:v$PREVIOUS_VERSION; docker push quay.io/project-koku/koku-metrics-operator-bundle:v$PREVIOUS_VERSION -``` - -Use `opm` to build a catalog image with the koku-metrics-operator and then push the image: +Reference: https://sdk.operatorframework.io/docs/olm-integration/tutorial-bundle +OpenShift comes with OLM enabled, so the "Enable OLM" steps can be omitted. -```sh -$ opm index add --from-index quay.io/project-koku/kmc-test-catalog:v$VERSION_BEFORE_PREVIOUS_VERSION --bundles quay.io/project-koku/koku-metrics-operator-bundle:v$PREVIOUS_VERSION --tag quay.io/project-koku/kmc-test-catalog:v$PREVIOUS_VERSION --container-tool docker -$ docker push quay.io/project-koku/kmc-test-catalog:v$PREVIOUS_VERSION -``` +Testing an upgrade is composed of the following general steps: +1. Create a namespace (koku-metrics-operator). +1. Install the previous bundle (make bundle-deploy-previous). +1. Generate the new controller image, and push to Quay. +1. Generate the new bundle. +1. Build the bundle image and push to Quay. +1. Deploy the upgraded bundle (make bundle-deploy-upgrade). +1. Observe the installed operator, and ensure it upgrades automatically. +1. Cleanup. -2. Create a CatalogSource in OCP, and install the operator (this should be the version that will be upgraded): -Create a catalog source by copying the following into a file called `catalog-source.yaml`: -```sh -apiVersion: operators.coreos.com/v1alpha1 -kind: CatalogSource -metadata: - name: my-test-catalog - namespace: openshift-marketplace -spec: - sourceType: grpc - image: quay.io/project-koku/kmc-test-catalog:v$PREVIOUS_VERSION - updateStrategy: - registryPoll: - interval: 3m -``` - -Deploy the catalog source to the cluster: - -```sh -$ oc apply -f catalog-source.yaml -``` - -Verify that the catalog source was created without errors in the `openshift-marketplace` project. - -Search OperatorHub for the koku-metrics-operator. It should be available under the `custom` or `my-test-catalog` provider type depending on your OCP version. - -Install the koku-metrics-operator in the `koku-metrics-operator` namespace, and test as normal. - - -3. Generate the new controller image, and push to Quay: - -```sh -$ USERNAME= -$ VERSION= -$ docker build . -t quay.io/$USERNAME/koku-metrics-operator:v$VERSION -$ docker push quay.io/$USERNAME/koku-metrics-operator:v$VERSION -``` - -4. Generate the new bundle: +### Testing an operator upgrade +1. Set version numbers in the Makefile: + ```sh + $ PREVIOUS_VERSION=0.9.8 + $ VERSION=0.9.9 + ``` -Update the release versions at the top of the `Makefile` to match the release version of the operator: +1. Create the namespace in the cluster and deploy the previous bundle: + ```sh + $ oc new-project koku-metrics-operator + $ make bundle-deploy-previous + ``` + Check the `koku-metrics-operator` namespace and ensure the previous version deployed correctly. Create a KokuMetricsConfig so that the PVC is created and data is collected, if available. -``` -# Current Operator version -PREVIOUS_VERSION ?= -VERSION ?= -``` +1. Generate the new controller image, and push to your Quay repo: -Run the following command to generate the bundle: + ```sh + $ USERNAME= + $ VERSION= + $ make docker-buildx IMG=quay.io/$USERNAME/koku-metrics-operator:v$VERSION + ``` -```sh -$ make bundle CHANNELS=alpha,beta DEFAULT_CHANNEL=beta IMG=quay.io/$USERNAME/koku-metrics-operator:v$VERSION -``` +1. Generate the new bundle: -This will generate a new `` bundle inside of the `koku-metrics-operator` directory within the repository. + Run the following command to generate the bundle: -5. Build the bundle image and push to Quay: + ```sh + $ docker pull quay.io/$USERNAME/koku-metrics-operator:v$VERSION + $ make bundle CHANNELS=alpha,beta DEFAULT_CHANNEL=beta IMG=quay.io/$USERNAME/koku-metrics-operator:v$VERSION + ``` -Copy the generated bundle to the testing directory: + This will generate a new `$VERSION` bundle inside of the `koku-metrics-operator` directory within the repository. -```sh -$ mv koku-metrics-operator/$VERSION testing -$ cd testing/$VERSION -``` +1. Build the bundle image and push to Quay: -Build and push bundle to your Quay repo: + ```sh + $ make bundle-build BUNDLE_IMG=quay.io/$USERNAME/koku-metrics-operator-bundle:v$VERSION bundle-push + ``` -```sh -$ docker build -f Dockerfile . -t quay.io/$USERNAME/koku-metrics-operator-bundle:v$VERSION; docker push quay.io/$USERNAME/koku-metrics-operator-bundle:v$VERSION -``` +1. Once the bundle is available in Quay, deploy it to your cluster: + ```sh + $ make bundle-deploy-upgrade BUNDLE_IMG=quay.io/$USERNAME/koku-metrics-operator-bundle:v$VERSION + ``` -6. Create an opm index (test-catalog) that contains the last release and the new release, and push to Quay: +1. Observe the installed operator, and ensure it upgrades automatically. -```sh -$ opm index add --from-index quay.io/project-koku/kmc-test-catalog:v$PREVIOUS_VERSION --bundles quay.io/$USERNAME/koku-metrics-operator-bundle:v$VERSION --tag quay.io/$USERNAME/test-catalog:v$VERSION --container-tool docker -$ docker push quay.io/$USERNAME/test-catalog:v$VERSION -``` + Once the operator has upgraded, run through manual tests as normal. -7. Update the image in the CatalogSource to point to the new test-catalog version (i.e. `image: quay.io/$USERNAME/test-catalog:v$VERSION`). -8. Observe the installed operator, and ensure it upgrades automatically. +1. When done with testing, the bundle can be deleted from the cluster with: -Once the operator has upgraded, run through manual tests as normal. + ```sh + $ make bundle-deploy-cleanup + ```