sigs.k8s.io/kubebuilder/v3@v3.14.0/pkg/plugins/golang/v3/commons.go (about) 1 /* 2 Copyright 2022 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 //go:deprecated This package has been deprecated in favor of v4 18 package v3 19 20 import ( 21 "fmt" 22 "os" 23 "path/filepath" 24 "strings" 25 26 log "github.com/sirupsen/logrus" 27 28 "sigs.k8s.io/kubebuilder/v3/pkg/plugin/util" 29 "sigs.k8s.io/kubebuilder/v3/pkg/plugins/golang/v3/scaffolds" 30 ) 31 32 const deprecateMsg = "The v1beta1 API version for CRDs and Webhooks are deprecated and are no longer supported since " + 33 "the Kubernetes release 1.22. This flag no longer required to exist in future releases. Also, we would like to " + 34 "recommend you no longer use these API versions." + 35 "More info: https://kubernetes.io/docs/reference/using-api/deprecation-guide/#v1-22" 36 37 // Update the makefile to allow generate CRDs/Webhooks with v1beta1 to ensure backwards compatibility 38 // nolint:lll,gosec 39 func applyScaffoldCustomizationsForVbeta1() error { 40 makefilePath := filepath.Join("Makefile") 41 bs, err := os.ReadFile(makefilePath) 42 if err != nil { 43 return err 44 } 45 if !strings.Contains(string(bs), "CRD_OPTIONS") { 46 47 log.Warn("The v1beta1 API version for CRDs and Webhooks are deprecated and are no longer supported " + 48 "since the Kubernetes release 1.22. In order to help you out use these versions" + 49 "we will need to try to update the Makefile and go.mod files of this project. Please," + 50 "ensure that these changes were done accordingly with your customizations.\n" + 51 "Also, we would like to recommend you no longer use these API versions." + 52 "More info: https://kubernetes.io/docs/reference/using-api/deprecation-guide/#v1-22") 53 54 const makefileTarget = `$(CONTROLLER_GEN) rbac:roleName=manager-role crd webhook paths="./..." output:crd:artifacts:config=config/crd/bases` 55 const makefileTargetForV1beta1 = `$(CONTROLLER_GEN) $(CRD_OPTIONS) rbac:roleName=manager-role webhook paths="./..." output:crd:artifacts:config=config/crd/bases` 56 57 if err := util.ReplaceInFile("Makefile", makefileTarget, makefileTargetForV1beta1); err != nil { 58 fmt.Printf("unable to update the makefile to allow the usage of v1beta1: %s", err) 59 } 60 61 const makegentarget = `manifests: controller-gen` 62 const makegenV1beta1Options = `# Produce CRDs that work back to Kubernetes 1.11 (no version conversion) 63 CRD_OPTIONS ?= "crd:crdVersions={v1beta1},trivialVersions=true,preserveUnknownFields=false" 64 manifests: controller-gen` 65 66 if err := util.ReplaceInFile("Makefile", makegentarget, makegenV1beta1Options); err != nil { 67 log.Warnf("unable to update the Makefile with %s: %s", makegenV1beta1Options, err) 68 } 69 70 // latest version of controller-tools where v1beta1 is supported 71 const controllerToolsVersionForVBeta1 = "v0.6.2" 72 if err := util.ReplaceInFile("Makefile", 73 fmt.Sprintf("CONTROLLER_TOOLS_VERSION ?= %s", 74 scaffolds.ControllerToolsVersion), 75 fmt.Sprintf("CONTROLLER_TOOLS_VERSION ?= %s", 76 controllerToolsVersionForVBeta1)); err != nil { 77 log.Warnf("unable to update the Makefile with %s: %s", fmt.Sprintf("controller-gen@%s", 78 controllerToolsVersionForVBeta1), err) 79 } 80 81 if err := util.ReplaceInFile("Makefile", 82 "ENVTEST_K8S_VERSION = 1.26.1", 83 "ENVTEST_K8S_VERSION = 1.21"); err != nil { 84 log.Warnf("unable to update the Makefile with %s: %s", "ENVTEST_K8S_VERSION = 1.21", err) 85 } 86 87 // DO NOT UPDATE THIS VERSION 88 // Note that this implementation will update the go.mod to downgrade the versions for those that are 89 // compatible v1beta1 CRD/Webhooks k8s core APIs if/when a user tries to create an API with 90 // create api [options] crd-version=v1beta1. The flag/feature is deprecated. however, to ensure that backwards 91 // compatible we must introduce this logic. Also, note that when we bump the k8s dependencies we need to 92 // ensure that the following replacements will be done accordingly to downgrade the versions. 93 // The next version of the Golang base plugin (go/v4) no longer provide this feature. 94 const controllerRuntimeVersionForVBeta1 = "v0.9.2" 95 96 if err := util.ReplaceInFile("go.mod", 97 fmt.Sprintf("sigs.k8s.io/controller-runtime %s", scaffolds.ControllerRuntimeVersion), 98 fmt.Sprintf("sigs.k8s.io/controller-runtime %s", controllerRuntimeVersionForVBeta1)); err != nil { 99 log.Warnf("unable to update the go.mod with sigs.k8s.io/controller-runtime %s: %s", 100 controllerRuntimeVersionForVBeta1, err) 101 } 102 103 if err := util.ReplaceInFile("go.mod", 104 "k8s.io/api v0.24.2", 105 "k8s.io/api v0.21.2"); err != nil { 106 log.Warnf("unable to update the go.mod with k8s.io/api v0.21.2: %s", err) 107 } 108 109 if err := util.ReplaceInFile("go.mod", 110 "k8s.io/apimachinery v0.24.2", 111 "k8s.io/apimachinery v0.21.2"); err != nil { 112 log.Warnf("unable to update the go.mod with k8s.io/apimachinery v0.21.2: %s", err) 113 } 114 115 if err := util.ReplaceInFile("go.mod", 116 "k8s.io/client-go v0.24.2", 117 "k8s.io/client-go v0.21.2"); err != nil { 118 log.Warnf("unable to update the go.mod with k8s.io/client-go v0.21.2: %s", err) 119 } 120 121 // During the scaffolding phase, this gets added to go.mod file, running go mod tidy bumps back 122 // the version from 21.2 to the latest 123 if err := util.ReplaceInFile("go.mod", 124 "k8s.io/api v0.24.2", 125 "k8s.io/api v0.21.2"); err != nil { 126 log.Warnf("unable to update the go.mod with k8s.io/api v0.21.2: %s", err) 127 } 128 129 if err := util.ReplaceInFile("go.mod", 130 "k8s.io/apiextensions-apiserver v0.24.2", 131 "k8s.io/apiextensions-apiserver v0.21.2"); err != nil { 132 log.Warnf("unable to update the go.mod with k8s.io/apiextensions-apiserver v0.21.2: %s", err) 133 } 134 135 if err := util.ReplaceInFile("go.mod", 136 "k8s.io/component-base v0.24.2", 137 "k8s.io/component-base v0.21.2"); err != nil { 138 log.Warnf("unable to update the go.mod with k8s.io/component-base v0.21.2: %s", err) 139 } 140 141 // Cannot use v1+ unless controller runtime is v0.11 142 if err := util.ReplaceInFile("go.mod", 143 "github.com/go-logr/logr v1.2.0", 144 "github.com/go-logr/logr v0.4.0"); err != nil { 145 log.Warnf("unable to update the go.mod with github.com/go-logr/logr v0.4.0: %s", err) 146 } 147 148 if err := util.ReplaceInFile("go.mod", 149 "github.com/go-logr/zapr v1.2.0", 150 "github.com/go-logr/zapr v0.4.0"); err != nil { 151 log.Warnf("unable to update the go.mod with github.com/go-logr/zapr v0.4.0: %s", err) 152 } 153 154 if err := util.ReplaceInFile("go.mod", 155 "k8s.io/klog/v2 v2.60.1", 156 "k8s.io/klog/v2 v2.9.0"); err != nil { 157 log.Warnf("unable to update the go.mod with k8s.io/klog/v2 v2.9.0: %s", err) 158 } 159 160 // Due to some indirect dependency changes with a bump in k8s packages from 0.23.x --> 0.24.x we need to 161 // clear out all indirect dependencies before we run `go mod tidy` so that the above changes get resolved correctly 162 if err := util.ReplaceRegexInFile("go.mod", `(require \(\n(\t.* \/\/ indirect\n)+\))`, ""); err != nil { 163 log.Warnf("unable to update the go.mod indirect dependencies: %s", err) 164 } 165 166 err = util.RunCmd("Update dependencies", "go", "mod", "tidy") 167 if err != nil { 168 return err 169 } 170 } 171 return nil 172 }