Updating Go-based Operator projects for newer Operator SDK versions

OKD 4.12 supports Operator SDK v1.25.0. If you already have the v1.22.0 CLI installed on your workstation, you can update the CLI to v1.25.0 by installing the latest version.

However, to ensure your existing Operator projects maintain compatibility with Operator SDK v1.25.0, update steps are required for the associated breaking changes introduced since v1.22.0. You must perform the update steps manually in any of your Operator projects that were previously created or maintained with v1.22.0.

Updating Go-based Operator projects for Operator SDK v1.25.0

The following procedure updates an existing Go-based Operator project for compatibility with v1.25.0.

Prerequisites

  • Operator SDK v1.25.0 installed

  • An Operator project created or maintained with Operator SDK v1.22.0

Procedure

  1. Make the following changes to the config/default/manager_auth_proxy_patch.yaml file:

    1. apiVersion: apps/v1
    2. kind: Deployment
    3. metadata:
    4. name: controller-manager
    5. namespace: system
    6. spec:
    7. template:
    8. spec:
    9. containers:
    10. - name: kube-rbac-proxy
    11. image: registry.redhat.io/openshift4/ose-kube-rbac-proxy:v4.12 (1)
    12. args:
    13. - "--secure-listen-address=0.0.0.0:8443"
    14. - "--upstream=http://127.0.0.1:8080/"
    15. - "--logtostderr=true"
    16. - "--v=0"
    17. ...
    1Update the tag version from v4.11 to v4.12.
  2. Make the following changes to your Makefile:

    1. To enable multi-architecture build support, add the docker-buildx target to your project Makefile:

      Example Makefile

      1. # PLATFORMS defines the target platforms for the manager image be build to provide support to multiple
      2. # architectures. (i.e. make docker-buildx IMG=myregistry/mypoperator:0.0.1). To use this option you need to:
      3. # - able to use docker buildx . More info: https://docs.docker.com/build/buildx/
      4. # - have enable BuildKit, More info: https://docs.docker.com/develop/develop-images/build_enhancements/
      5. # - be able to push the image for your registry (i.e. if you do not inform a valid value via IMG=<myregistry/image:<tag>> than the export will fail)
      6. # To properly provided solutions that supports more than one platform you should use this option.
      7. PLATFORMS ?= linux/arm64,linux/amd64,linux/s390x,linux/ppc64le
      8. .PHONY: docker-buildx
      9. docker-buildx: test ## Build and push docker image for the manager for cross-platform support
      10. # copy existing Dockerfile and insert --platform=${BUILDPLATFORM} into Dockerfile.cross, and preserve the original Dockerfile
      11. sed -e '1 s/\(^FROM\)/FROM --platform=\$$\{BUILDPLATFORM\}/; t' -e ' 1,// s//FROM --platform=\$$\{BUILDPLATFORM\}/' Dockerfile > Dockerfile.cross
      12. - docker buildx create --name project-v3-builder
      13. docker buildx use project-v3-builder
      14. - docker buildx build --push --platform=$(PLATFORMS) --tag ${IMG} -f Dockerfile.cross
      15. - docker buildx rm project-v3-builder
      16. rm Dockerfile.cross
    2. Update your project scaffolding to support changes in kubebuilder as shown in the following example:

      Old Makefile

      1. .PHONY: test
      2. test: manifests generate fmt vet envtest ## Run tests.
      3. KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) -p path)" go test ./... -coverprofile cover.out

      New Makefile

      1. .PHONY: test
      2. test: manifests generate fmt vet envtest ## Run tests.
      3. KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) -p path)" go test $(go list ./... | grep -v /test/) -coverprofile cover.out
    3. To ensure Makefile targets do not download binaries already in your binary path, make the following changes to your Makefile:

      Old Makefile

      1. KUSTOMIZE_INSTALL_SCRIPT ?= "https://raw.githubusercontent.com/kubernetes-sigs/kustomize/master/hack/install_kustomize.sh"
      2. .PHONY: kustomize
      3. kustomize: $(KUSTOMIZE) ## Download kustomize locally if necessary.
      4. $(KUSTOMIZE): $(LOCALBIN)
      5. { curl -s $(KUSTOMIZE_INSTALL_SCRIPT) | bash -s -- $(subst v,,$(KUSTOMIZE_VERSION)) $(LOCALBIN); }
      6. .PHONY: controller-gen
      7. controller-gen: $(CONTROLLER_GEN) ## Download controller-gen locally if necessary.
      8. $(CONTROLLER_GEN): $(LOCALBIN)
      9. test -s $(LOCALBIN)/controller-gen || GOBIN=$(LOCALBIN) go install sigs.k8s.io/controller-tools/cmd/controller-gen@$(CONTROLLER_TOOLS_VERSION
      10. GOBIN=$(LOCALBIN) go install sigs.k8s.io/controller-tools/cmd/controller-gen@$(CONTROLLER_TOOLS_VERSION)
      11. .PHONY: envtest
      12. envtest: $(ENVTEST) ## Download envtest-setup locally if necessary.
      13. $(ENVTEST): $(LOCALBIN)
      14. GOBIN=$(LOCALBIN) go install sigs.k8s.io/controller-runtime/tools/setup-envtest@latest

      New Makefile

      1. KUSTOMIZE_INSTALL_SCRIPT ?= "https://raw.githubusercontent.com/kubernetes-sigs/kustomize/master/hack/install_kustomize.sh"
      2. .PHONY: kustomize
      3. kustomize: $(KUSTOMIZE) ## Download kustomize locally if necessary.
      4. $(KUSTOMIZE): $(LOCALBIN)
      5. test -s $(LOCALBIN)/kustomize || { curl -s $(KUSTOMIZE_INSTALL_SCRIPT) | bash -s -- $(subst v,,$(KUSTOMIZE_VERSION)) $(LOCALBIN); } (1)
      6. .PHONY: controller-gen
      7. controller-gen: $(CONTROLLER_GEN) ## Download controller-gen locally if necessary.
      8. $(CONTROLLER_GEN): $(LOCALBIN)
      9. test -s $(LOCALBIN)/controller-gen || GOBIN=$(LOCALBIN) go install sigs.k8s.io/controller-tools/cmd/controller-gen@$(CONTROLLER_TOOLS_VERSION) (1)
      10. .PHONY: envtest
      11. envtest: $(ENVTEST) ## Download envtest-setup locally if necessary.
      12. $(ENVTEST): $(LOCALBIN)
      13. test -s $(LOCALBIN)/setup-envtest || GOBIN=$(LOCALBIN) go install sigs.k8s.io/controller-runtime/tools/setup-envtest@latest (1)
      1Add test -s $(LOCALBIN)/<binary-name> || before the instruction to download a binary.
    4. Update controller-tools to version v0.9.2 as shown in the following example:

      Example `Makefile

      1. ## Tool Versions
      2. KUSTOMIZE_VERSION ?= v3.8.7
      3. CONTROLLER_TOOLS_VERSION ?= v0.9.2 (1)
      1Update version v0.9.0 to v0.9.2.
    5. To apply the changes to your Makefile and rebuild your Operator, enter the following command:

      1. $ make
  3. To update Go and its dependencies, make the following changes to your go.mod file:

    1. go 1.19 (1)
    2. require (
    3. github.com/onsi/ginkgo/v2 v2.1.4 (2)
    4. github.com/onsi/gomega v1.19.0 (3)
    5. k8s.io/api v0.25.0 (4)
    6. k8s.io/apimachinery v0.25.0 (4)
    7. k8s.io/client-go v0.25.0 (4)
    8. sigs.k8s.io/controller-runtime v0.13.0 (5)
    9. )
    1Update version 1.18 to 1.19.
    2Update version v1.16.5 to v2.1.4.
    3Update version v1.18.1 to v1.19.0.
    4Update version v0.24.0 to v0.25.0.
    5Update version v0.12.1 to v0.13.0.
  4. To download the updated versions, clean up the dependencies, and apply the changes in your go.mod file, run the following command:

    1. $ go mod tidy

Additional resources