This is a cache of https://docs.okd.io/4.12/operators/operator_sdk/golang/osdk-golang-updating-projects.html. It is a snapshot of the page at 2024-11-28T18:45:28.417+0000.
Updating Go-based projects - Developing Operators | Operators | OKD 4.12
×

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

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

Updating Go-based Operator projects for Operator SDK 1.25.4

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

Prerequisites
  • Operator SDK 1.25.4 installed

  • An Operator project created or maintained with Operator SDK 1.22.2

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

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: controller-manager
      namespace: system
    spec:
      template:
        spec:
          containers:
          - name: kube-rbac-proxy
            image: registry.redhat.io/openshift4/ose-kube-rbac-proxy:v4.12 (1)
            args:
            - "--secure-listen-address=0.0.0.0:8443"
            - "--upstream=http://127.0.0.1:8080/"
            - "--logtostderr=true"
            - "--v=0"
    ...
    1 Update 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
      # PLATFORMS defines the target platforms for  the manager image be build to provide support to multiple
      # architectures. (i.e. make docker-buildx IMG=myregistry/mypoperator:0.0.1). To use this option you need to:
      # - able to use docker buildx . More info: https://docs.docker.com/build/buildx/
      # - have enable BuildKit, More info: https://docs.docker.com/develop/develop-images/build_enhancements/
      # - 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)
      # To properly provided solutions that supports more than one platform you should use this option.
      PLATFORMS ?= linux/arm64,linux/amd64,linux/s390x,linux/ppc64le
      .PHONY: docker-buildx
      docker-buildx: test ## Build and push docker image for the manager for cross-platform support
      	# copy existing Dockerfile and insert --platform=${BUILDPLATFORM} into Dockerfile.cross, and preserve the original Dockerfile
      	sed -e '1 s/\(^FROM\)/FROM --platform=\$$\{BUILDPLATFORM\}/; t' -e ' 1,// s//FROM --platform=\$$\{BUILDPLATFORM\}/' Dockerfile > Dockerfile.cross
      	- docker buildx create --name project-v3-builder
      	docker buildx use project-v3-builder
      	- docker buildx build --push --platform=$(PLATFORMS) --tag ${IMG} -f Dockerfile.cross
      	- docker buildx rm project-v3-builder
      	rm Dockerfile.cross
    2. Update your project scaffolding to support changes in kubebuilder as shown in the following example:

      Old Makefile
      .PHONY: test
      test: manifests generate fmt vet envtest ## Run tests.
       	KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) -p path)" go test ./... -coverprofile cover.out
      New Makefile
      .PHONY: test
      test: manifests generate fmt vet envtest ## Run tests.
      	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
      KUSTOMIZE_INSTALL_SCRIPT ?= "https://raw.githubusercontent.com/kubernetes-sigs/kustomize/master/hack/install_kustomize.sh"
      .PHONY: kustomize
      kustomize: $(KUSTOMIZE) ## Download kustomize locally if necessary.
      $(KUSTOMIZE): $(LOCALBIN)
      	{ curl -s $(KUSTOMIZE_INSTALL_SCRIPT) | bash -s -- $(subst v,,$(KUSTOMIZE_VERSION)) $(LOCALBIN); }
      
      .PHONY: controller-gen
      controller-gen: $(CONTROLLER_GEN) ## Download controller-gen locally if necessary.
      $(CONTROLLER_GEN): $(LOCALBIN)
      	test -s $(LOCALBIN)/controller-gen || GOBIN=$(LOCALBIN) go install sigs.k8s.io/controller-tools/cmd/controller-gen@$(CONTROLLER_TOOLS_VERSION
      	GOBIN=$(LOCALBIN) go install sigs.k8s.io/controller-tools/cmd/controller-gen@$(CONTROLLER_TOOLS_VERSION)
      
      .PHONY: envtest
      envtest: $(ENVTEST) ## Download envtest-setup locally if necessary.
      $(ENVTEST): $(LOCALBIN)
       	GOBIN=$(LOCALBIN) go install sigs.k8s.io/controller-runtime/tools/setup-envtest@latest
      New Makefile
      KUSTOMIZE_INSTALL_SCRIPT ?= "https://raw.githubusercontent.com/kubernetes-sigs/kustomize/master/hack/install_kustomize.sh"
      .PHONY: kustomize
      kustomize: $(KUSTOMIZE) ## Download kustomize locally if necessary.
      $(KUSTOMIZE): $(LOCALBIN)
      	test -s $(LOCALBIN)/kustomize || { curl -s $(KUSTOMIZE_INSTALL_SCRIPT) | bash -s -- $(subst v,,$(KUSTOMIZE_VERSION)) $(LOCALBIN); } (1)
      
      .PHONY: controller-gen
      controller-gen: $(CONTROLLER_GEN) ## Download controller-gen locally if necessary.
      $(CONTROLLER_GEN): $(LOCALBIN)
      	test -s $(LOCALBIN)/controller-gen || GOBIN=$(LOCALBIN) go install sigs.k8s.io/controller-tools/cmd/controller-gen@$(CONTROLLER_TOOLS_VERSION) (2)
      
      .PHONY: envtest
      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 (3)
      1 Add test -s $(LOCALBIN)/<binary-name> || before the instruction to download a binary.
      2 Add test -s $(LOCALBIN)/<binary-name> || before the instruction to download a binary.
      3 Add 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
      ## Tool Versions
      KUSTOMIZE_VERSION ?= v3.8.7
      CONTROLLER_TOOLS_VERSION ?= v0.9.2 (1)
      1 Update version v0.9.0 to v0.9.2.
    5. To apply the changes to your Makefile and rebuild your Operator, enter the following command:

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

    go 1.19 (1)
    
    require (
      github.com/onsi/ginkgo/v2 v2.1.4 (2)
      github.com/onsi/gomega v1.19.0 (3)
      k8s.io/api v0.25.0 (4)
      k8s.io/apimachinery v0.25.0 (5)
      k8s.io/client-go v0.25.0 (6)
      sigs.k8s.io/controller-runtime v0.13.0 (7)
    )
    1 Update version 1.18 to 1.19.
    2 Update version v1.16.5 to v2.1.4.
    3 Update version v1.18.1 to v1.19.0.
    4 Update version v0.24.0 to v0.25.0.
    5 Update version v0.24.0 to v0.25.0.
    6 Update version v0.24.0 to v0.25.0.
    7 Update 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:

    $ go mod tidy