Skip to content

Commit 1f34e41

Browse files
authored
🌱 Recover v1.10 util packages for conditions, patch and paused to util/deprecated/v1beta1 for provider migrations (#12224)
* Recover v1.10 util/conditions and util/patch to util/deprecated/v1beta1 for providers * util/deprecated: add package deprecation information to packages * readd util/deprecated/v1beta1/paused * util/deprecated: use deprecated packages for paused * util/deprecated: use clusterv1.Cluster * fix deprecated tests * util/deprecated/v1beta1/test add ObservedGeneration to Phase1Obj and generate CRD * util/test allow to pass in custom crd paths and scheme builders * envtest: adjust godoc for global scheme * envtest: review fixes * deprecated: rename func parameters * fixup renaming * fixup linter changes
1 parent 113722d commit 1f34e41

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+14312
-28
lines changed

‎.golangci.yml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,19 @@ linters:
277277
- linters:
278278
- errcheck
279279
text: Error return value of .((os\.)?std(out|err)\..*|.*Close|.*Flush|os\.Remove(All)?|.*print(f|ln)?|os\.(Un)?Setenv). is not checked
280+
# v1beta1 deprecated util packages
281+
- linters:
282+
- staticcheck
283+
text: 'SA1019: .* is deprecated'
284+
path: util/deprecated/v1beta1/.*\.go$
285+
- linters:
286+
- staticcheck
287+
text: "QF1001: could apply De Morgan's law"
288+
path: util/deprecated/v1beta1/.*\.go$
289+
- linters:
290+
- importas
291+
text: 'imported as ".*" but must be ".*" according to config'
292+
path: util/deprecated/v1beta1/.*\.go$
280293
# Exclude some packages or code to require comments, for example test code, or fake clients.
281294
- linters:
282295
- revive
@@ -367,9 +380,17 @@ linters:
367380
- linters:
368381
- govet
369382
text: non-constant format string in call to sigs\.k8s\.io\/cluster-api\/util\/conditions\.
383+
- linters:
384+
- govet
385+
text: non-constant format string in call to sigs\.k8s\.io\/cluster-api\/util\/deprecated\/v1beta1\/conditions\.
370386
- linters:
371387
- goconst
372388
path: (.+)_test\.go
389+
# It's clearer to see that a field gets accessed or func gets called on the embedded objects
390+
- linters:
391+
- staticcheck
392+
path: (.+)\.go$
393+
text: 'QF1008: could remove embedded field'
373394
issues:
374395
max-issues-per-linter: 0
375396
max-same-issues: 0

‎Makefile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,10 @@ generate-manifests-core: $(CONTROLLER_GEN) $(KUSTOMIZE) ## Generate manifests e.
324324
paths=./controllers/crdmigrator/test/t4/... \
325325
crd:crdVersions=v1 \
326326
output:crd:dir=./controllers/crdmigrator/test/t4/crd
327+
$(CONTROLLER_GEN) \
328+
paths=./util/deprecated/v1beta1/test/builder/... \
329+
crd:crdVersions=v1 \
330+
output:crd:dir=./util/deprecated/v1beta1/test/builder/crd
327331

328332
.PHONY: generate-manifests-kubeadm-bootstrap
329333
generate-manifests-kubeadm-bootstrap: $(CONTROLLER_GEN) ## Generate manifests e.g. CRD, RBAC etc. for kubeadm bootstrap
@@ -400,6 +404,7 @@ generate-go-deepcopy-core: $(CONTROLLER_GEN) ## Generate deepcopy go code for co
400404
paths=./internal/runtime/test/... \
401405
paths=./cmd/clusterctl/... \
402406
paths=./util/test/builder/... \
407+
paths=./util/deprecated/v1beta1/test/builder/... \
403408
paths=./controllers/crdmigrator/test/...
404409

405410
.PHONY: generate-go-deepcopy-kubeadm-bootstrap

‎internal/test/envtest/environment.go

Lines changed: 52 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,13 @@ import (
3232
"github.com/onsi/ginkgo/v2"
3333
"github.com/pkg/errors"
3434
admissionv1 "k8s.io/api/admissionregistration/v1"
35+
appsv1 "k8s.io/api/apps/v1"
3536
corev1 "k8s.io/api/core/v1"
37+
rbacv1 "k8s.io/api/rbac/v1"
3638
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
3739
apierrors "k8s.io/apimachinery/pkg/api/errors"
3840
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
41+
"k8s.io/apimachinery/pkg/runtime"
3942
kerrors "k8s.io/apimachinery/pkg/util/errors"
4043
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
4144
"k8s.io/apimachinery/pkg/util/wait"
@@ -104,29 +107,34 @@ func init() {
104107
// Add logger for ginkgo.
105108
klog.SetOutput(ginkgo.GinkgoWriter)
106109

107-
// Calculate the scheme.
108-
utilruntime.Must(apiextensionsv1.AddToScheme(scheme.Scheme))
109-
utilruntime.Must(admissionv1.AddToScheme(scheme.Scheme))
110-
utilruntime.Must(clusterv1.AddToScheme(scheme.Scheme))
111-
utilruntime.Must(bootstrapv1.AddToScheme(scheme.Scheme))
112-
utilruntime.Must(expv1.AddToScheme(scheme.Scheme))
113-
utilruntime.Must(controlplanev1.AddToScheme(scheme.Scheme))
114-
utilruntime.Must(admissionv1.AddToScheme(scheme.Scheme))
115-
utilruntime.Must(runtimev1.AddToScheme(scheme.Scheme))
116-
utilruntime.Must(ipamv1.AddToScheme(scheme.Scheme))
117-
utilruntime.Must(builder.AddTransitionV1Beta2ToScheme(scheme.Scheme))
118-
utilruntime.Must(addonsv1.AddToScheme(scheme.Scheme))
110+
// Calculate the global scheme used by fakeclients.
111+
registerSchemes(scheme.Scheme)
112+
}
113+
114+
func registerSchemes(s *runtime.Scheme) {
115+
utilruntime.Must(admissionv1.AddToScheme(s))
116+
utilruntime.Must(apiextensionsv1.AddToScheme(s))
117+
118+
utilruntime.Must(addonsv1.AddToScheme(s))
119+
utilruntime.Must(bootstrapv1.AddToScheme(s))
120+
utilruntime.Must(clusterv1.AddToScheme(s))
121+
utilruntime.Must(controlplanev1.AddToScheme(s))
122+
utilruntime.Must(expv1.AddToScheme(s))
123+
utilruntime.Must(ipamv1.AddToScheme(s))
124+
utilruntime.Must(runtimev1.AddToScheme(s))
119125
}
120126

121127
// RunInput is the input for Run.
122128
type RunInput struct {
123-
M *testing.M
124-
ManagerUncachedObjs []client.Object
125-
ManagerCacheOptions cache.Options
126-
SetupIndexes func(ctx context.Context, mgr ctrl.Manager)
127-
SetupReconcilers func(ctx context.Context, mgr ctrl.Manager)
128-
SetupEnv func(e *Environment)
129-
MinK8sVersion string
129+
M *testing.M
130+
ManagerUncachedObjs []client.Object
131+
ManagerCacheOptions cache.Options
132+
SetupIndexes func(ctx context.Context, mgr ctrl.Manager)
133+
SetupReconcilers func(ctx context.Context, mgr ctrl.Manager)
134+
SetupEnv func(e *Environment)
135+
MinK8sVersion string
136+
AdditionalSchemeBuilder runtime.SchemeBuilder
137+
AdditionalCRDDirectoryPaths []string
130138
}
131139

132140
// Run executes the tests of the given testing.M in a test environment.
@@ -147,8 +155,20 @@ func Run(ctx context.Context, input RunInput) int {
147155
return input.M.Run()
148156
}
149157

158+
// Calculate the scheme.
159+
scheme := runtime.NewScheme()
160+
registerSchemes(scheme)
161+
// Register additional schemes from k8s APIs.
162+
utilruntime.Must(appsv1.AddToScheme(scheme))
163+
utilruntime.Must(corev1.AddToScheme(scheme))
164+
utilruntime.Must(rbacv1.AddToScheme(scheme))
165+
// Register additionally passed schemes.
166+
if input.AdditionalSchemeBuilder != nil {
167+
utilruntime.Must(input.AdditionalSchemeBuilder.AddToScheme(scheme))
168+
}
169+
150170
// Bootstrapping test environment
151-
env := newEnvironment(input.ManagerCacheOptions, input.ManagerUncachedObjs...)
171+
env := newEnvironment(scheme, input.AdditionalCRDDirectoryPaths, input.ManagerCacheOptions, input.ManagerUncachedObjs...)
152172

153173
ctx, cancel := context.WithCancelCause(ctx)
154174
env.cancelManager = cancel
@@ -233,20 +253,24 @@ type Environment struct {
233253
//
234254
// This function should be called only once for each package you're running tests within,
235255
// usually the environment is initialized in a suite_test.go file within a `BeforeSuite` ginkgo block.
236-
func newEnvironment(managerCacheOptions cache.Options, uncachedObjs ...client.Object) *Environment {
256+
func newEnvironment(scheme *runtime.Scheme, additionalCRDDirectoryPaths []string, managerCacheOptions cache.Options, uncachedObjs ...client.Object) *Environment {
237257
// Get the root of the current file to use in CRD paths.
238258
_, filename, _, _ := goruntime.Caller(0) //nolint:dogsled
239259
root := path.Join(path.Dir(filename), "..", "..", "..")
240260

261+
crdDirectoryPaths := []string{
262+
filepath.Join(root, "config", "crd", "bases"),
263+
filepath.Join(root, "controlplane", "kubeadm", "config", "crd", "bases"),
264+
filepath.Join(root, "bootstrap", "kubeadm", "config", "crd", "bases"),
265+
}
266+
for _, path := range additionalCRDDirectoryPaths {
267+
crdDirectoryPaths = append(crdDirectoryPaths, filepath.Join(root, path))
268+
}
269+
241270
// Create the test environment.
242271
env := &envtest.Environment{
243272
ErrorIfCRDPathMissing: true,
244-
CRDDirectoryPaths: []string{
245-
filepath.Join(root, "config", "crd", "bases"),
246-
filepath.Join(root, "controlplane", "kubeadm", "config", "crd", "bases"),
247-
filepath.Join(root, "bootstrap", "kubeadm", "config", "crd", "bases"),
248-
filepath.Join(root, "util", "test", "builder", "crd"),
249-
},
273+
CRDDirectoryPaths: crdDirectoryPaths,
250274
CRDs: []*apiextensionsv1.CustomResourceDefinition{
251275
builder.GenericBootstrapConfigCRD.DeepCopy(),
252276
builder.GenericBootstrapConfigTemplateCRD.DeepCopy(),
@@ -296,7 +320,7 @@ func newEnvironment(managerCacheOptions cache.Options, uncachedObjs ...client.Ob
296320
Controller: config.Controller{
297321
UsePriorityQueue: ptr.To[bool](feature.Gates.Enabled(feature.PriorityQueue)),
298322
},
299-
Scheme: scheme.Scheme,
323+
Scheme: scheme,
300324
Metrics: metricsserver.Options{
301325
BindAddress: "0",
302326
},
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
Copyright 2020 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+
// Package conditions implements condition utilities.
18+
//
19+
// Deprecated: This package is deprecated and is going to be removed when support for v1beta1 will be dropped. Please see https://github.com/kubernetes-sigs/cluster-api/blob/main/docs/proposals/20240916-improve-status-in-CAPI-resources.md for more details.
20+
package conditions

0 commit comments

Comments
 (0)