Skip to content

Commit

Permalink
⚠️ upgrading controller-runtime from 0.9.2 to 0.10.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Camila Macedo committed Sep 19, 2021
1 parent 9817db7 commit 2f1588d
Show file tree
Hide file tree
Showing 67 changed files with 383 additions and 170 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
36 changes: 35 additions & 1 deletion pkg/plugins/golang/v3/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ import (
"fmt"
"os"

log "github.com/sirupsen/logrus"
"github.com/spf13/pflag"
"sigs.k8s.io/kubebuilder/v3/pkg/utils"

"sigs.k8s.io/kubebuilder/v3/pkg/config"
"sigs.k8s.io/kubebuilder/v3/pkg/machinery"
Expand Down Expand Up @@ -182,6 +184,37 @@ func (p *createAPISubcommand) Scaffold(fs machinery.Filesystem) error {
}

func (p *createAPISubcommand) PostScaffold() error {

// Update the makefile to allow generate CRD to ensure backwards compatibility
// todo: it should be removed for go/v4
if p.config.ResourcesLength() == 1 && p.resource.API.CRDVersion == "V1beta1" {
//nolint:lll
const makefileTarget = `$(CONTROLLER_GEN) rbac:roleName=manager-role crd webhook paths="./..." output:crd:artifacts:config=config/crd/bases`
//nolint:lll
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 {
log.Warnf("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:trivialVersions=true,preserveUnknownFields=false"
manifests: controller-gen`

if err := utils.ReplaceInFile("makefile", makegentarget, makegenV1beta1Options); err != nil {
log.Warnf("unable to update the makefile to allow the usage of v1beta1: %s", 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), controllerToolsVersionForVBeta1); err != nil {
log.Warnf("unable to update the makefile to allow the usage of v1beta1: %s", err)
}
}

err := util.RunCmd("Update dependencies", "go", "mod", "tidy")
if err != nil {
return err
Expand All @@ -191,7 +224,8 @@ func (p *createAPISubcommand) PostScaffold() error {
if err != nil {
return err
}
fmt.Print("Next: implement your new API and generate the manifests (e.g. CRDs,CRs) with:\n$ make manifests\n")
log.Info("Next: implement your new API and generate the manifests " +
"(e.g. CRDs,CRs) with:\n$ make manifests\n")
}

return nil
Expand Down
3 changes: 2 additions & 1 deletion pkg/plugins/golang/v3/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"strings"
"unicode"

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

"sigs.k8s.io/kubebuilder/v3/pkg/config"
Expand Down Expand Up @@ -150,7 +151,7 @@ func (p *initSubcommand) PostScaffold() error {
return err
}

fmt.Printf("Next: define a resource with:\n$ %s create api\n", p.commandName)
log.Infof("Next: define a resource with:\n$ %s create api\n", p.commandName)
return nil
}

Expand Down
4 changes: 2 additions & 2 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
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 @@ -33,6 +33,9 @@ type Makefile struct {
BoilerplatePath string
// Controller tools version to use in the project
ControllerToolsVersion string
// Controller tools version to use when the v1beta1 is scaffold to ensure
// backwards compatibility
ControllerToolsVersionForVBeta1 string
// Kustomize version to use in the project
KustomizeVersion string
// ControllerRuntimeVersion version to be used to download the envtest setup script
Expand Down Expand Up @@ -60,8 +63,6 @@ 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
Expand Down Expand Up @@ -99,7 +100,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
1 change: 0 additions & 1 deletion pkg/plugins/golang/v3/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"fmt"

"github.com/spf13/pflag"

"sigs.k8s.io/kubebuilder/v3/pkg/config"
"sigs.k8s.io/kubebuilder/v3/pkg/machinery"
"sigs.k8s.io/kubebuilder/v3/pkg/model/resource"
Expand Down
79 changes: 75 additions & 4 deletions test/e2e/utils/util.go → pkg/utils/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,15 @@ limitations under the License.
package utils

import (
"bufio"
"bytes"
"crypto/rand"
"errors"
"fmt"
"io/ioutil"
"math/big"
"os"
"regexp"
"strings"
)

Expand Down Expand Up @@ -61,6 +65,8 @@ func GetNonEmptyLines(output string) []string {

// InsertCode searches target content in the file and insert `toInsert` after the target.
func InsertCode(filename, target, code string) error {
// false positive
// nolint:gosec
contents, err := ioutil.ReadFile(filename)
if err != nil {
return err
Expand All @@ -75,6 +81,8 @@ func InsertCode(filename, target, code string) error {
// UncommentCode searches for target in the file and remove the comment prefix
// of the target content. The target content may span multiple lines.
func UncommentCode(filename, target, prefix string) error {
// false positive
// nolint:gosec
content, err := ioutil.ReadFile(filename)
if err != nil {
return err
Expand All @@ -83,7 +91,7 @@ func UncommentCode(filename, target, prefix string) error {

idx := strings.Index(strContent, target)
if idx < 0 {
return nil
return fmt.Errorf("unable to find the code %s to be uncomment", target)
}

out := new(bytes.Buffer)
Expand All @@ -92,12 +100,22 @@ func UncommentCode(filename, target, prefix string) error {
return err
}

strs := strings.Split(target, "\n")
for _, str := range strs {
_, err := out.WriteString(strings.TrimPrefix(str, prefix) + "\n")
scanner := bufio.NewScanner(bytes.NewBufferString(target))
if !scanner.Scan() {
return nil
}
for {
_, err := out.WriteString(strings.TrimPrefix(scanner.Text(), prefix))
if err != nil {
return err
}
// Avoid writing a newline in case the previous line was the last in target.
if !scanner.Scan() {
break
}
if _, err := out.WriteString("\n"); err != nil {
return err
}
}

_, err = out.Write(content[idx+len(target):])
Expand All @@ -111,6 +129,8 @@ func UncommentCode(filename, target, prefix string) error {

// ImplementWebhooks will mock an webhook data
func ImplementWebhooks(filename string) error {
// false positive
// nolint:gosec
bs, err := ioutil.ReadFile(filename)
if err != nil {
return err
Expand Down Expand Up @@ -168,3 +188,54 @@ func EnsureExistAndReplace(input, match, replace string) (string, error) {
}
return strings.Replace(input, match, replace, -1), nil
}

// ReplaceInFile replaces all instances of old with new in the file at path.
func ReplaceInFile(path, old, new string) error {
info, err := os.Stat(path)
if err != nil {
return err
}
// false positive
// nolint:gosec
b, err := ioutil.ReadFile(path)
if err != nil {
return err
}
if !strings.Contains(string(b), old) {
return errors.New("unable to find the content to be replaced")
}
s := strings.Replace(string(b), old, new, -1)
err = ioutil.WriteFile(path, []byte(s), info.Mode())
if err != nil {
return err
}
return nil
}

// ReplaceRegexInFile finds all strings that match `match` and replaces them
// with `replace` in the file at path.
func ReplaceRegexInFile(path, match, replace string) error {
matcher, err := regexp.Compile(match)
if err != nil {
return err
}
info, err := os.Stat(path)
if err != nil {
return err
}
// false positive
// nolint:gosec
b, err := ioutil.ReadFile(path)
if err != nil {
return err
}
s := matcher.ReplaceAllString(string(b), replace)
if s == string(b) {
return errors.New("unable to find the content to be replaced")
}
err = ioutil.WriteFile(path, []byte(s), info.Mode())
if err != nil {
return err
}
return nil
}
23 changes: 22 additions & 1 deletion test/e2e/utils/test_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,14 @@ package utils
import (
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"strings"

"sigs.k8s.io/kubebuilder/v3/pkg/utils"

. "github.com/onsi/ginkgo" //nolint:golint,revive
)

Expand All @@ -45,7 +48,7 @@ type TestContext struct {
// NewTestContext init with a random suffix for test TestContext stuff,
// to avoid conflict when running tests synchronously.
func NewTestContext(binaryName string, env ...string) (*TestContext, error) {
testSuffix, err := RandomSuffix()
testSuffix, err := utils.RandomSuffix()
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -274,3 +277,21 @@ func (cc *CmdContext) Run(cmd *exec.Cmd) ([]byte, error) {

return output, nil
}

// AllowProjectBeMultiGroup will update the PROJECT file with the information to allow we scaffold
// apis with different groups. be available.
func (t *TestContext) AllowProjectBeMultiGroup() error {
const multiGroup = `multigroup: true
`
projectBytes, err := ioutil.ReadFile(filepath.Join(t.Dir, "PROJECT"))
if err != nil {
return err
}

projectBytes = append([]byte(multiGroup), projectBytes...)
err = ioutil.WriteFile(filepath.Join(t.Dir, "PROJECT"), projectBytes, 0644)
if err != nil {
return err
}
return nil
}
Loading

0 comments on commit 2f1588d

Please sign in to comment.