Skip to content

Commit

Permalink
update deps to use k8s 1.22
Browse files Browse the repository at this point in the history
Description
Update deps to use k8s 1.22
Update controller-tools to 0.7.0
Update sigs.k8s.io/controller-runtime to 0.10.0
Update ENV TEST to 1.22
  • Loading branch information
Camila Macedo committed Sep 20, 2021
1 parent 9817db7 commit c85d992
Show file tree
Hide file tree
Showing 118 changed files with 2,093 additions and 189 deletions.
7 changes: 4 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ require (
github.com/gobuffalo/flect v0.2.2
github.com/joelanford/go-apidiff v0.1.0
github.com/onsi/ginkgo v1.16.4
github.com/onsi/gomega v1.13.0
github.com/onsi/gomega v1.15.0
github.com/sirupsen/logrus v1.8.1
github.com/spf13/afero v1.2.2
github.com/spf13/cobra v1.1.3
github.com/spf13/pflag v1.0.5
golang.org/x/tools v0.1.2
k8s.io/apimachinery v0.21.2 // for `kubebuilder alpha config-gen`
sigs.k8s.io/controller-runtime v0.9.2
k8s.io/apimachinery v0.22.1 // for `kubebuilder alpha config-gen`
sigs.k8s.io/controller-runtime v0.10.0
sigs.k8s.io/controller-tools v0.6.0 // for `kubebuilder alpha config-gen`
sigs.k8s.io/kustomize/kyaml v0.10.21 // for `kubebuilder alpha config-gen`
sigs.k8s.io/yaml v1.2.0
Expand Down
144 changes: 117 additions & 27 deletions go.sum

Large diffs are not rendered by default.

5 changes: 4 additions & 1 deletion pkg/model/resource/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,13 @@ import (
"github.com/gobuffalo/flect"
)

const V1beta1 = "v1beta1"
const V1 = "v1"

// validateAPIVersion validates CRD or Webhook versions
func validateAPIVersion(version string) error {
switch version {
case "v1beta1", "v1":
case V1beta1, V1:
return nil
default:
return fmt.Errorf("API version must be one of: v1beta1, v1")
Expand Down
14 changes: 14 additions & 0 deletions pkg/plugins/golang/v3/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,10 @@ func (p *createAPISubcommand) BindFlags(fs *pflag.FlagSet) {
fs.BoolVar(&p.options.DoController, "controller", true,
"if set, generate the controller without prompting the user")
p.controllerFlag = fs.Lookup("controller")

// (not required raise an error in this case)
// nolint:errcheck,gosec
fs.MarkDeprecated("crd-version", deprecateMsg)
}

func (p *createAPISubcommand) InjectConfig(c config.Config) error {
Expand Down Expand Up @@ -182,6 +186,16 @@ func (p *createAPISubcommand) Scaffold(fs machinery.Filesystem) error {
}

func (p *createAPISubcommand) PostScaffold() error {

// Update the makefile to allow generate Webhooks to ensure backwards compatibility
// todo: it should be removed for go/v4
// nolint:lll,gosec
if p.resource.API.CRDVersion == "v1beta1" {
if err := applyScaffoldCustomizationsForVbeta1(); err != nil {
return err
}
}

err := util.RunCmd("Update dependencies", "go", "mod", "tidy")
if err != nil {
return err
Expand Down
122 changes: 122 additions & 0 deletions pkg/plugins/golang/v3/commons.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
/*
Copyright 2021 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.
*/

package v3

import (
"fmt"
"io/ioutil"
"path/filepath"
"strings"

log "github.com/sirupsen/logrus"

"sigs.k8s.io/kubebuilder/v3/pkg/plugin/util"
"sigs.k8s.io/kubebuilder/v3/pkg/plugins/golang/v3/scaffolds"
"sigs.k8s.io/kubebuilder/v3/pkg/utils"
)

const deprecateMsg = "The v1beta1 API version for CRDs and Webhooks are deprecated and are no longer supported since " +
"the Kubernetes release 1.22. This flag no longer required to exist in future releases. Also, we would like to " +
"recommend you no longer use these API versions." +
"More info: https://kubernetes.io/docs/reference/using-api/deprecation-guide/#v1-22"

// Update the makefile to allow generate CRDs/Webhooks with v1beta1 to ensure backwards compatibility
// nolint:lll,gosec
func applyScaffoldCustomizationsForVbeta1() error {
makefilePath := filepath.Join("Makefile")
bs, err := ioutil.ReadFile(makefilePath)
if err != nil {
return err
}
if !strings.Contains(string(bs), "CRD_OPTIONS") {

log.Warn("The v1beta1 API version for CRDs and Webhooks are deprecated and are no longer supported " +
"since the Kubernetes release 1.22. In order to help you out use these versions" +
"we will need to try to update the Makefile and go.mod files of this project. Please," +
"ensure that these changes were done accordingly with your customizations.\n" +
"Also, we would like to recommend you no longer use these API versions." +
"More info: https://kubernetes.io/docs/reference/using-api/deprecation-guide/#v1-22")

const makefileTarget = `$(CONTROLLER_GEN) rbac:roleName=manager-role crd webhook paths="./..." output:crd:artifacts:config=config/crd/bases`
const makefileTargetForV1beta1 = `$(CONTROLLER_GEN) $(CRD_OPTIONS) rbac:roleName=manager-role webhook paths="./..." output:crd:artifacts:config=config/crd/bases`

if err := utils.ReplaceInFile("Makefile", makefileTarget, makefileTargetForV1beta1); err != nil {
fmt.Printf("unable to update the makefile to allow the usage of v1beta1: %s", err)
}

const makegentarget = `
manifests: controller-gen`
const makegenV1beta1Options = `# Produce CRDs that work back to Kubernetes 1.11 (no version conversion)
CRD_OPTIONS ?= "crd:crdVersions={v1beta1},trivialVersions=true,preserveUnknownFields=false"
manifests: controller-gen`

if err := utils.ReplaceInFile("Makefile", makegentarget, makegenV1beta1Options); err != nil {
log.Warnf("unable to update the Makefile with %s: %s", makegenV1beta1Options, err)
}

// latest version of controller-tools where v1beta1 is supported
const controllerToolsVersionForVBeta1 = "v0.6.2"
if err := utils.ReplaceInFile("Makefile",
fmt.Sprintf("controller-gen@%s",
scaffolds.ControllerToolsVersion),
fmt.Sprintf("controller-gen@%s",
controllerToolsVersionForVBeta1)); err != nil {
log.Warnf("unable to update the Makefile with %s: %s", fmt.Sprintf("controller-gen@%s",
controllerToolsVersionForVBeta1), err)
}

if err := utils.ReplaceInFile("Makefile",
"ENVTEST_K8S_VERSION = 1.22",
"ENVTEST_K8S_VERSION = 1.21"); err != nil {
log.Warnf("unable to update the Makefile with %s: %s", "ENVTEST_K8S_VERSION = 1.21", err)
}

// latest version of controller-runtime where v1beta1 is supported
const controllerRuntimeVersionForVBeta1 = "v0.9.2"

if err := utils.ReplaceInFile("go.mod",
fmt.Sprintf("sigs.k8s.io/controller-runtime %s", scaffolds.ControllerRuntimeVersion),
fmt.Sprintf("sigs.k8s.io/controller-runtime %s", controllerRuntimeVersionForVBeta1)); err != nil {
log.Warnf("unable to update the go.mod with sigs.k8s.io/controller-runtime %s: %s",
controllerRuntimeVersionForVBeta1, err)
}

if err := utils.ReplaceInFile("go.mod",
"k8s.io/api v0.22.1",
"k8s.io/api v0.21.2"); err != nil {
log.Warnf("unable to update the go.mod with k8s.io/api v0.21.2: %s", err)
}

if err := utils.ReplaceInFile("go.mod",
"k8s.io/apimachinery v0.22.1",
"k8s.io/apimachinery v0.21.2"); err != nil {
log.Warnf("unable to update the go.mod with k8s.io/apimachinery v0.21.2: %s", err)
}

if err := utils.ReplaceInFile("go.mod",
"k8s.io/apimachinery v0.22.1",
"k8s.io/apimachinery v0.21.2"); err != nil {
log.Warnf("unable to update the go.mod with k8s.io/apimachinery v0.21.2: %s", err)
}

err = util.RunCmd("Update dependencies", "go", "mod", "tidy")
if err != nil {
return err
}
}
return nil
}
8 changes: 5 additions & 3 deletions pkg/plugins/golang/v3/scaffolds/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ import (

const (
// ControllerRuntimeVersion is the kubernetes-sigs/controller-runtime version to be used in the project
ControllerRuntimeVersion = "v0.9.2"
ControllerRuntimeVersion = "v0.10.0"
// ControllerToolsVersion is the kubernetes-sigs/controller-tools version to be used in the project
ControllerToolsVersion = "v0.6.1"
ControllerToolsVersion = "master"
// KustomizeVersion is the kubernetes-sigs/kustomize version to be used in the project
KustomizeVersion = "v3.8.7"

Expand Down Expand Up @@ -99,7 +99,9 @@ func (s *initScaffolder) Scaffold() error {

return scaffold.Execute(
&templates.Main{},
&templates.GoMod{ControllerRuntimeVersion: ControllerRuntimeVersion},
&templates.GoMod{
ControllerRuntimeVersion: ControllerRuntimeVersion,
},
&templates.GitIgnore{},
&templates.Makefile{
Image: imageName,
Expand Down
14 changes: 10 additions & 4 deletions pkg/plugins/golang/v3/scaffolds/internal/templates/api/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ type Webhook struct { // nolint:maligned
// Is the Group domain for the Resource replacing '.' with '-'
QualifiedGroupWithDash string

// Define value for AdmissionReviewVersions marker
AdmissionReviewVersions string

Force bool
}

Expand Down Expand Up @@ -70,6 +73,11 @@ func (f *Webhook) SetTemplateDefaults() error {
f.IfExistsAction = machinery.Error
}

f.AdmissionReviewVersions = "v1"
if f.Resource.Webhooks.WebhookVersion == "v1beta1" {
f.AdmissionReviewVersions = "{v1,v1beta1}"
}

f.QualifiedGroupWithDash = strings.Replace(f.Resource.QualifiedGroup(), ".", "-", -1)

return nil
Expand Down Expand Up @@ -103,10 +111,9 @@ func (r *{{ .Resource.Kind }}) SetupWebhookWithManager(mgr ctrl.Manager) error {
// EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN!
`

// TODO(estroz): update admissionReviewVersions to include v1 when controller-runtime supports that version.
//nolint:lll
defaultingWebhookTemplate = `
//+kubebuilder:webhook:{{ if ne .Resource.Webhooks.WebhookVersion "v1" }}webhookVersions={{"{"}}{{ .Resource.Webhooks.WebhookVersion }}{{"}"}},{{ end }}path=/mutate-{{ .QualifiedGroupWithDash }}-{{ .Resource.Version }}-{{ lower .Resource.Kind }},mutating=true,failurePolicy=fail,sideEffects=None,groups={{ .Resource.QualifiedGroup }},resources={{ .Resource.Plural }},verbs=create;update,versions={{ .Resource.Version }},name=m{{ lower .Resource.Kind }}.kb.io,admissionReviewVersions={v1,v1beta1}
//+kubebuilder:webhook:{{ if ne .Resource.Webhooks.WebhookVersion "v1" }}webhookVersions={{"{"}}{{ .Resource.Webhooks.WebhookVersion }}{{"}"}},{{ end }}path=/mutate-{{ .QualifiedGroupWithDash }}-{{ .Resource.Version }}-{{ lower .Resource.Kind }},mutating=true,failurePolicy=fail,sideEffects=None,groups={{ .Resource.QualifiedGroup }},resources={{ .Resource.Plural }},verbs=create;update,versions={{ .Resource.Version }},name=m{{ lower .Resource.Kind }}.kb.io,admissionReviewVersions={{ .AdmissionReviewVersions }}
var _ webhook.Defaulter = &{{ .Resource.Kind }}{}
Expand All @@ -118,11 +125,10 @@ func (r *{{ .Resource.Kind }}) Default() {
}
`

// TODO(estroz): update admissionReviewVersions to include v1 when controller-runtime supports that version.
//nolint:lll
validatingWebhookTemplate = `
// TODO(user): change verbs to "verbs=create;update;delete" if you want to enable deletion validation.
//+kubebuilder:webhook:{{ if ne .Resource.Webhooks.WebhookVersion "v1" }}webhookVersions={{"{"}}{{ .Resource.Webhooks.WebhookVersion }}{{"}"}},{{ end }}path=/validate-{{ .QualifiedGroupWithDash }}-{{ .Resource.Version }}-{{ lower .Resource.Kind }},mutating=false,failurePolicy=fail,sideEffects=None,groups={{ .Resource.QualifiedGroup }},resources={{ .Resource.Plural }},verbs=create;update,versions={{ .Resource.Version }},name=v{{ lower .Resource.Kind }}.kb.io,admissionReviewVersions={v1,v1beta1}
//+kubebuilder:webhook:{{ if ne .Resource.Webhooks.WebhookVersion "v1" }}webhookVersions={{"{"}}{{ .Resource.Webhooks.WebhookVersion }}{{"}"}},{{ end }}path=/validate-{{ .QualifiedGroupWithDash }}-{{ .Resource.Version }}-{{ lower .Resource.Kind }},mutating=false,failurePolicy=fail,sideEffects=None,groups={{ .Resource.QualifiedGroup }},resources={{ .Resource.Plural }},verbs=create;update,versions={{ .Resource.Version }},name=v{{ lower .Resource.Kind }}.kb.io,admissionReviewVersions={{ .AdmissionReviewVersions }}
var _ webhook.Validator = &{{ .Resource.Kind }}{}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,8 @@ func (f *Makefile) SetTemplateDefaults() error {
const makefileTemplate = `
# Image URL to use all building/pushing image targets
IMG ?= {{ .Image }}
# Produce CRDs that work back to Kubernetes 1.11 (no version conversion)
CRD_OPTIONS ?= "crd:trivialVersions=true,preserveUnknownFields=false"
# ENVTEST_K8S_VERSION refers to the version of kubebuilder assets to be downloaded by envtest binary.
ENVTEST_K8S_VERSION = 1.21
ENVTEST_K8S_VERSION = 1.22
# Get the currently used golang install path (in GOPATH/bin, unless GOBIN is set)
ifeq (,$(shell go env GOBIN))
Expand Down Expand Up @@ -99,7 +97,7 @@ help: ## Display this help.
##@ Development
manifests: controller-gen ## Generate WebhookConfiguration, ClusterRole and CustomResourceDefinition objects.
$(CONTROLLER_GEN) $(CRD_OPTIONS) rbac:roleName=manager-role webhook paths="./..." output:crd:artifacts:config=config/crd/bases
$(CONTROLLER_GEN) rbac:roleName=manager-role crd webhook paths="./..." output:crd:artifacts:config=config/crd/bases
generate: controller-gen ## Generate code containing DeepCopy, DeepCopyInto, and DeepCopyObject method implementations.
$(CONTROLLER_GEN) object:headerFile={{printf "%q" .BoilerplatePath}} paths="./..."
Expand Down
25 changes: 25 additions & 0 deletions pkg/plugins/golang/v3/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ func (p *createWebhookSubcommand) BindFlags(fs *pflag.FlagSet) {

fs.BoolVar(&p.force, "force", false,
"attempt to create resource even if it already exists")

// (not required raise an error in this case)
// nolint:errcheck,gosec
fs.MarkDeprecated("webhook-version", deprecateMsg)
}

func (p *createWebhookSubcommand) InjectConfig(c config.Config) error {
Expand Down Expand Up @@ -122,3 +126,24 @@ func (p *createWebhookSubcommand) Scaffold(fs machinery.Filesystem) error {
scaffolder.InjectFS(fs)
return scaffolder.Scaffold()
}

func (p *createWebhookSubcommand) PostScaffold() error {
if p.resource.Webhooks.WebhookVersion == "v1beta1" {
if err := applyScaffoldCustomizationsForVbeta1(); err != nil {
return err
}
}

err := pluginutil.RunCmd("Update dependencies", "go", "mod", "tidy")
if err != nil {
return err
}

err = pluginutil.RunCmd("Running make", "make", "generate")
if err != nil {
return err
}
fmt.Print("Next: implement your new Webhook and generate the manifests with:\n$ make manifests\n")

return nil
}
Loading

0 comments on commit c85d992

Please sign in to comment.