Skip to content

Commit

Permalink
🐛 fix controller-runtime setupenv incompability with old versions
Browse files Browse the repository at this point in the history
  • Loading branch information
camilamacedo86 committed Mar 29, 2024
1 parent 011383c commit 2dc2f6d
Show file tree
Hide file tree
Showing 11 changed files with 44 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ GOLANGCI_LINT = $(LOCALBIN)/golangci-lint-$(GOLANGCI_LINT_VERSION)
## Tool Versions
KUSTOMIZE_VERSION ?= v5.3.0
CONTROLLER_TOOLS_VERSION ?= v0.14.0
ENVTEST_VERSION ?= latest
ENVTEST_VERSION ?= release-0.14
GOLANGCI_LINT_VERSION ?= v1.54.2

.PHONY: kustomize
Expand Down
2 changes: 1 addition & 1 deletion docs/book/src/cronjob-tutorial/testdata/project/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ GOLANGCI_LINT = $(LOCALBIN)/golangci-lint-$(GOLANGCI_LINT_VERSION)
## Tool Versions
KUSTOMIZE_VERSION ?= v5.3.0
CONTROLLER_TOOLS_VERSION ?= v0.14.0
ENVTEST_VERSION ?= latest
ENVTEST_VERSION ?= release-0.14
GOLANGCI_LINT_VERSION ?= v1.54.2

.PHONY: kustomize
Expand Down
2 changes: 1 addition & 1 deletion docs/book/src/getting-started/testdata/project/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ GOLANGCI_LINT = $(LOCALBIN)/golangci-lint-$(GOLANGCI_LINT_VERSION)
## Tool Versions
KUSTOMIZE_VERSION ?= v5.3.0
CONTROLLER_TOOLS_VERSION ?= v0.14.0
ENVTEST_VERSION ?= latest
ENVTEST_VERSION ?= release-0.14
GOLANGCI_LINT_VERSION ?= v1.54.2

.PHONY: kustomize
Expand Down
17 changes: 17 additions & 0 deletions pkg/plugins/golang/v4/scaffolds/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package scaffolds

import (
"fmt"

Check failure on line 20 in pkg/plugins/golang/v4/scaffolds/init.go

View workflow job for this annotation

GitHub Actions / golangci-lint

File is not `goimports`-ed (goimports)
log "github.com/sirupsen/logrus"
"github.com/spf13/afero"
"sigs.k8s.io/kubebuilder/v3/pkg/config"
Expand All @@ -29,6 +30,7 @@ import (
"sigs.k8s.io/kubebuilder/v3/pkg/plugins/golang/v4/scaffolds/internal/templates/hack"
"sigs.k8s.io/kubebuilder/v3/pkg/plugins/golang/v4/scaffolds/internal/templates/test/e2e"
"sigs.k8s.io/kubebuilder/v3/pkg/plugins/golang/v4/scaffolds/internal/templates/test/utils"
"strings"
)

const (
Expand Down Expand Up @@ -71,6 +73,20 @@ func (s *initScaffolder) InjectFS(fs machinery.Filesystem) {
s.fs = fs
}

// getControllerRuntimeReleaseBranch converts the ControllerRuntime semantic versioning string to a release branch string.

Check failure on line 76 in pkg/plugins/golang/v4/scaffolds/init.go

View workflow job for this annotation

GitHub Actions / golangci-lint

line is 122 characters (lll)
// Example input: "v0.17.0" -> Output: "release-0.17"
func getControllerRuntimeReleaseBranch() string {
v := strings.TrimPrefix(ControllerToolsVersion, "v")
tmp := strings.Split(v, ".")

if len(tmp) < 2 {
fmt.Println("Invalid version format. Expected at least major and minor version numbers.")
return ""
}
releaseBranch := fmt.Sprintf("release-%s.%s", tmp[0], tmp[1])
return releaseBranch
}

// Scaffold implements cmdutil.Scaffolder
func (s *initScaffolder) Scaffold() error {
log.Println("Writing scaffold for you to edit...")
Expand Down Expand Up @@ -136,6 +152,7 @@ func (s *initScaffolder) Scaffold() error {
ControllerToolsVersion: ControllerToolsVersion,
KustomizeVersion: kustomizeVersion,
ControllerRuntimeVersion: ControllerRuntimeVersion,
EnvtestVersion: getControllerRuntimeReleaseBranch(),
},
&templates.Dockerfile{},
&templates.DockerIgnore{},
Expand Down
14 changes: 9 additions & 5 deletions pkg/plugins/golang/v4/scaffolds/internal/templates/makefile.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,15 @@ func (f *Makefile) SetTemplateDefaults() error {
f.Image = "controller:latest"
}

// TODO: Looking for ways to tag the controller-runtime
// release using tag versions. Note that we cannot
// relay upon the branch since we cannot ensure that
// it will be generated for all releases. We must be
// able to use the tag.
// TODO: Current workaround for setup-envtest compatibility
// Due to past instances where controller-runtime maintainers released
// versions without corresponding branches, directly relying on branches
// poses a risk of breaking the Kubebuilder chain. Such practices may
// change over time, potentially leading to compatibility issues. This
// approach, although not ideal, remains the best solution for ensuring
// compatibility with controller-runtime releases as of now. For more
// details on the quest for a more robust solution, refer to the issue
// raised in the controller-runtime repository: https://github.com/kubernetes-sigs/controller-runtime/issues/2744
if f.EnvtestVersion == "" {
f.EnvtestVersion = "latest"
}
Expand Down
11 changes: 10 additions & 1 deletion test/common.sh
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,16 @@ function fetch_tools {
if ! is_installed setup-envtest; then
header_text "Installing setup-envtest to $(go env GOPATH)/bin"

go install sigs.k8s.io/controller-runtime/tools/setup-envtest@latest
# TODO: Current workaround for setup-envtest compatibility
# Due to past instances where controller-runtime maintainers released
# versions without corresponding branches, directly relying on branches
# poses a risk of breaking the Kubebuilder chain. Such practices may
# change over time, potentially leading to compatibility issues. This
# approach, although not ideal, remains the best solution for ensuring
# compatibility with controller-runtime releases as of now. For more
# details on the quest for a more robust solution, refer to the issue
# raised in the controller-runtime repository: https://github.com/kubernetes-sigs/controller-runtime/issues/2744
go install sigs.k8s.io/controller-runtime/tools/setup-envtest@release-0.17
fi

if [ -z "$SKIP_FETCH_TOOLS" ]; then
Expand Down
2 changes: 1 addition & 1 deletion testdata/project-v4-multigroup-with-deploy-image/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ GOLANGCI_LINT = $(LOCALBIN)/golangci-lint-$(GOLANGCI_LINT_VERSION)
## Tool Versions
KUSTOMIZE_VERSION ?= v5.3.0
CONTROLLER_TOOLS_VERSION ?= v0.14.0
ENVTEST_VERSION ?= latest
ENVTEST_VERSION ?= release-0.14
GOLANGCI_LINT_VERSION ?= v1.54.2

.PHONY: kustomize
Expand Down
2 changes: 1 addition & 1 deletion testdata/project-v4-multigroup/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ GOLANGCI_LINT = $(LOCALBIN)/golangci-lint-$(GOLANGCI_LINT_VERSION)
## Tool Versions
KUSTOMIZE_VERSION ?= v5.3.0
CONTROLLER_TOOLS_VERSION ?= v0.14.0
ENVTEST_VERSION ?= latest
ENVTEST_VERSION ?= release-0.14
GOLANGCI_LINT_VERSION ?= v1.54.2

.PHONY: kustomize
Expand Down
2 changes: 1 addition & 1 deletion testdata/project-v4-with-deploy-image/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ GOLANGCI_LINT = $(LOCALBIN)/golangci-lint-$(GOLANGCI_LINT_VERSION)
## Tool Versions
KUSTOMIZE_VERSION ?= v5.3.0
CONTROLLER_TOOLS_VERSION ?= v0.14.0
ENVTEST_VERSION ?= latest
ENVTEST_VERSION ?= release-0.14
GOLANGCI_LINT_VERSION ?= v1.54.2

.PHONY: kustomize
Expand Down
2 changes: 1 addition & 1 deletion testdata/project-v4-with-grafana/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ GOLANGCI_LINT = $(LOCALBIN)/golangci-lint-$(GOLANGCI_LINT_VERSION)
## Tool Versions
KUSTOMIZE_VERSION ?= v5.3.0
CONTROLLER_TOOLS_VERSION ?= v0.14.0
ENVTEST_VERSION ?= latest
ENVTEST_VERSION ?= release-0.14
GOLANGCI_LINT_VERSION ?= v1.54.2

.PHONY: kustomize
Expand Down
2 changes: 1 addition & 1 deletion testdata/project-v4/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ GOLANGCI_LINT = $(LOCALBIN)/golangci-lint-$(GOLANGCI_LINT_VERSION)
## Tool Versions
KUSTOMIZE_VERSION ?= v5.3.0
CONTROLLER_TOOLS_VERSION ?= v0.14.0
ENVTEST_VERSION ?= latest
ENVTEST_VERSION ?= release-0.14
GOLANGCI_LINT_VERSION ?= v1.54.2

.PHONY: kustomize
Expand Down

0 comments on commit 2dc2f6d

Please sign in to comment.