Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Blocked do not merge] ⚠️ (kustomize/v1,go/v3): upgrade kustomize 3.x to 4.x and add test to ensure backwards compatibility #2758

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ check-testdata: ## Run the script to ensure that the testdata is updated
.PHONY: test-testdata
test-testdata: ## Run the tests of the testdata directory
./test/testdata/test.sh
./test/testdata/test_bk_compatibility_with_3x.sh

#todo(remove the test-legacy whne the go/v2 be removed from kubebuilder)

Expand Down
47 changes: 3 additions & 44 deletions pkg/plugins/common/kustomize/v1/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,8 @@ import (
"fmt"
"os"
"path/filepath"
"runtime"
"strings"

log "github.com/sirupsen/logrus"
"github.com/spf13/pflag"

"sigs.k8s.io/kubebuilder/v3/pkg/config"
Expand All @@ -35,11 +33,6 @@ import (

var _ plugin.InitSubcommand = &initSubcommand{}

// Verify if the local environment is supported by this plugin
var supportedArchs = []string{"linux/amd64",
"linux/arm64",
"darwin/amd64"}

type initSubcommand struct {
config config.Config

Expand All @@ -54,12 +47,9 @@ func (p *initSubcommand) UpdateMetadata(cliMeta plugin.CLIMetadata, subcmdMeta *
- a "PROJECT" file that stores project configuration
- several YAML files for project deployment under the "config" directory

NOTE: The kustomize/v1 plugin used to do this scaffold uses the v3 release (%s).
Therefore, darwin/arm64 is not supported since Kustomize does not provide v3
binaries for this architecture. The currently supported architectures are %q.
More info: https://github.com/kubernetes-sigs/kustomize/issues/4612.

`, KustomizeVersion, supportedArchs)
NOTE: The kustomize/v1 plugin used to do this scaffold uses the v4 release (%s).
However, its default scaffolds and files should still be backwards compatible with 3.x.
`, KustomizeVersion)

subcmdMeta.Examples = fmt.Sprintf(` # Initialize a common project with your domain and name in copyright
%[1]s init --plugins common/v3 --domain example.org
Expand Down Expand Up @@ -108,37 +98,6 @@ func (p *initSubcommand) InjectConfig(c config.Config) error {
return nil
}

func (p *initSubcommand) PreScaffold(machinery.Filesystem) error {
arch := runtime.GOARCH
// It probably will never return x86_64. However, we are here checking the support for the binaries
// So that, x86_64 means getting the Linux/amd64 binary. Then, we just keep this line to ensure
// that it complies with the same code implementation that we have in the targets. In case someone
// call the command inform the GOARCH=x86_64 then, we will properly handle the scenario
// since it will work successfully and will instal the Linux/amd64 binary via the Makefile target.
arch = strings.Replace(arch, "x86_64", "amd64", -1)
localPlatform := fmt.Sprintf("%s/%s", strings.TrimSpace(runtime.GOOS), strings.TrimSpace(arch))

if !hasSupportFor(localPlatform) {
log.Warnf("the platform of this environment (%s) is not suppported by kustomize v3 (%s) which is "+
"used in this scaffold. You will be unable to download a binary for the kustomize version supported "+
"and used by this plugin. The currently supported platforms are: %q",
localPlatform,
KustomizeVersion,
supportedArchs)
}

return nil
}

func hasSupportFor(localPlatform string) bool {
for _, value := range supportedArchs {
if value == localPlatform {
return true
}
}
return false
}

func (p *initSubcommand) Scaffold(fs machinery.Filesystem) error {
scaffolder := scaffolds.NewInitScaffolder(p.config)
scaffolder.InjectFS(fs)
Expand Down
2 changes: 1 addition & 1 deletion pkg/plugins/common/kustomize/v1/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
)

// KustomizeVersion is the kubernetes-sigs/kustomize version to be used in the project
const KustomizeVersion = "v3.8.7"
const KustomizeVersion = "v4.5.5"

const pluginName = "kustomize.common." + plugins.DefaultNameQualifier

Expand Down
22 changes: 20 additions & 2 deletions test/e2e/v3/plugin_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ import (
"strings"
"time"

"sigs.k8s.io/kubebuilder/v3/pkg/plugin/util"

//nolint:golint
//nolint:revive
. "github.com/onsi/ginkgo"
Expand All @@ -35,6 +33,8 @@ import (
//nolint:revive
. "github.com/onsi/gomega"

"sigs.k8s.io/kubebuilder/v3/pkg/plugin/util"
kustomizecommonv1 "sigs.k8s.io/kubebuilder/v3/pkg/plugins/common/kustomize/v1"
"sigs.k8s.io/kubebuilder/v3/test/e2e/utils"
)

Expand Down Expand Up @@ -120,6 +120,24 @@ var _ = Describe("kubebuilder", func() {
kbc.UninstallCertManager(false)
})

It("should generate a runnable project go/v3 using kustomize 3.x", func() {
camilamacedo86 marked this conversation as resolved.
Show resolved Hide resolved
// Skip if cluster version < 1.16, when v1 CRDs and webhooks did not exist.
if srvVer := kbc.K8sVersion.ServerVersion; srvVer.GetMajorInt() <= 1 && srvVer.GetMinorInt() < 16 {
Skip(fmt.Sprintf("cluster version %s does not support v1 CRDs or webhooks",
srvVer.GitVersion))
}

GenerateV3(kbc, "v1", false)

// replace the version to 3x
makefile := filepath.Join(kbc.Dir, "Makefile")
if err := util.ReplaceInFile(makefile, kustomizecommonv1.KustomizeVersion, `v3.8.7`); err != nil {
ExpectWithOffset(1, err).NotTo(HaveOccurred())
}

Run(kbc)
})

It("should generate a runnable project go/v3 with v1 CRDs and Webhooks", func() {
// Skip if cluster version < 1.16, when v1 CRDs and webhooks did not exist.
if srvVer := kbc.K8sVersion.ServerVersion; srvVer.GetMajorInt() <= 1 && srvVer.GetMinorInt() < 16 {
Expand Down
68 changes: 68 additions & 0 deletions test/testdata/test_bk_compatibility_with_3x.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#!/usr/bin/env bash

# Copyright 2022 The Kubernetes Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# The go/v3 stable plugin with kustomize/v1 was upgraded
# to move forward from its version 3.X to 4.X.
# However, we need to ensure the backwords compability
# and we cannot change the scaffolds mainly of create api and
# webhook commands to use the new features provide with kustomize 4.X
# in order to not introduce a breaking change for thoso who scaffold
# the projects with Kubebuilder CLI 3.X when they will use new versions
# of the tool. So, this test is for we ensure and does not
# allow maintainers break the stable plugin.
#
# Changes on the syntax for kustomize v4 must be addressed
# ONLY in the new plugin version kustomize/v2 instead.
#
# For further information see the doc about Plugin Versioning

source "$(dirname "$0")/../common.sh"

# Executes the test of the testdata directories
function test_bk_with_projects {
rm -f "$(command -v controller-gen)"
rm -f "$(command -v kustomize)"

header_text "Performing tests in dir $1"
pushd "$(dirname "$0")/../../testdata/$1"

make kustomize

header_text "Remove the kustomize bin if exists"
rm -rf "bin/kustomize"

header_text "Installing kustomize version v3.8.7"
curl -s "https://raw.githubusercontent.com/kubernetes-sigs/kustomize/master/hack/install_kustomize.sh" | bash -s -- 3.8.7 "./bin"
./bin/kustomize version

header_text "Testing kustomize build manifests with 3.x to ensure backwords compability"
./bin/kustomize build "config/crd"
./bin/kustomize build "config/default"
./bin/kustomize build "config/webhook"
./bin/kustomize build "config/certmanager"

popd
header_text "No changes on the scaffold were done that breaks those who scaffold the projects with Kubebuilder 3.x and go/v3"
}

build_kb

# Test backwards compatibility of the scaffolds done with project v3 and kustomize v3.x
# IMPORTANT: Do not remove the test if it fails when you do changes in the scaffolds
# This test was done with the purpose to ensure that we will NOT change
# the kustomize/v1 scaffolds in a way that it no longer work with 3.x
test_bk_with_projects project-v3
test_bk_with_projects project-v3-multigroup
2 changes: 1 addition & 1 deletion testdata/project-v3-addon/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ CONTROLLER_GEN ?= $(LOCALBIN)/controller-gen
ENVTEST ?= $(LOCALBIN)/setup-envtest

## Tool Versions
KUSTOMIZE_VERSION ?= v3.8.7
KUSTOMIZE_VERSION ?= v4.5.5
CONTROLLER_TOOLS_VERSION ?= v0.9.0

KUSTOMIZE_INSTALL_SCRIPT ?= "https://raw.githubusercontent.com/kubernetes-sigs/kustomize/master/hack/install_kustomize.sh"
Expand Down
2 changes: 1 addition & 1 deletion testdata/project-v3-config/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ CONTROLLER_GEN ?= $(LOCALBIN)/controller-gen
ENVTEST ?= $(LOCALBIN)/setup-envtest

## Tool Versions
KUSTOMIZE_VERSION ?= v3.8.7
KUSTOMIZE_VERSION ?= v4.5.5
CONTROLLER_TOOLS_VERSION ?= v0.9.0

KUSTOMIZE_INSTALL_SCRIPT ?= "https://raw.githubusercontent.com/kubernetes-sigs/kustomize/master/hack/install_kustomize.sh"
Expand Down
2 changes: 1 addition & 1 deletion testdata/project-v3-multigroup/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ CONTROLLER_GEN ?= $(LOCALBIN)/controller-gen
ENVTEST ?= $(LOCALBIN)/setup-envtest

## Tool Versions
KUSTOMIZE_VERSION ?= v3.8.7
KUSTOMIZE_VERSION ?= v4.5.5
CONTROLLER_TOOLS_VERSION ?= v0.9.0

KUSTOMIZE_INSTALL_SCRIPT ?= "https://raw.githubusercontent.com/kubernetes-sigs/kustomize/master/hack/install_kustomize.sh"
Expand Down
2 changes: 1 addition & 1 deletion testdata/project-v3/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ CONTROLLER_GEN ?= $(LOCALBIN)/controller-gen
ENVTEST ?= $(LOCALBIN)/setup-envtest

## Tool Versions
KUSTOMIZE_VERSION ?= v3.8.7
KUSTOMIZE_VERSION ?= v4.5.5
CONTROLLER_TOOLS_VERSION ?= v0.9.0

KUSTOMIZE_INSTALL_SCRIPT ?= "https://raw.githubusercontent.com/kubernetes-sigs/kustomize/master/hack/install_kustomize.sh"
Expand Down