Skip to content

🌱 Recover v1.10 util packages for conditions, patch and paused to util/deprecated/v1beta1 for provider migrations #12224

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

Merged
Show file tree
Hide file tree
Changes from 9 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
4 changes: 4 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ linters:
- zz_generated.*\.go$
- vendored_openapi\.go$
- internal/apis/.*
- util/deprecated
rules:
# Specific exclude rules for deprecated fields that are still part of the codebase. These
# should be removed as the referenced deprecated item is removed from the project.
Expand Down Expand Up @@ -365,6 +366,9 @@ linters:
- linters:
- govet
text: non-constant format string in call to sigs\.k8s\.io\/cluster-api\/util\/conditions\.
- linters:
- govet
text: non-constant format string in call to sigs\.k8s\.io\/cluster-api\/util\/deprecated\/v1beta1\/conditions\.
- linters:
- goconst
path: (.+)_test\.go
Expand Down
5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,10 @@ generate-manifests-core: $(CONTROLLER_GEN) $(KUSTOMIZE) ## Generate manifests e.
paths=./controllers/crdmigrator/test/t4/... \
crd:crdVersions=v1 \
output:crd:dir=./controllers/crdmigrator/test/t4/crd
$(CONTROLLER_GEN) \
paths=./util/deprecated/v1beta1/test/builder/... \
crd:crdVersions=v1 \
output:crd:dir=./util/deprecated/v1beta1/test/builder/crd

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

.PHONY: generate-go-deepcopy-kubeadm-bootstrap
Expand Down
64 changes: 46 additions & 18 deletions internal/test/envtest/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,13 @@ import (
"github.com/onsi/ginkgo/v2"
"github.com/pkg/errors"
admissionv1 "k8s.io/api/admissionregistration/v1"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
rbacv1 "k8s.io/api/rbac/v1"
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
kerrors "k8s.io/apimachinery/pkg/util/errors"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/apimachinery/pkg/util/wait"
Expand Down Expand Up @@ -104,7 +107,7 @@ func init() {
// Add logger for ginkgo.
klog.SetOutput(ginkgo.GinkgoWriter)

// Calculate the scheme.
// Calculate the global scheme used by fakeclients.
utilruntime.Must(apiextensionsv1.AddToScheme(scheme.Scheme))
utilruntime.Must(admissionv1.AddToScheme(scheme.Scheme))
utilruntime.Must(clusterv1.AddToScheme(scheme.Scheme))
Expand All @@ -114,19 +117,20 @@ func init() {
utilruntime.Must(admissionv1.AddToScheme(scheme.Scheme))
utilruntime.Must(runtimev1.AddToScheme(scheme.Scheme))
utilruntime.Must(ipamv1.AddToScheme(scheme.Scheme))
utilruntime.Must(builder.AddTransitionV1Beta2ToScheme(scheme.Scheme))
utilruntime.Must(addonsv1.AddToScheme(scheme.Scheme))
}

// RunInput is the input for Run.
type RunInput struct {
M *testing.M
ManagerUncachedObjs []client.Object
ManagerCacheOptions cache.Options
SetupIndexes func(ctx context.Context, mgr ctrl.Manager)
SetupReconcilers func(ctx context.Context, mgr ctrl.Manager)
SetupEnv func(e *Environment)
MinK8sVersion string
M *testing.M
ManagerUncachedObjs []client.Object
ManagerCacheOptions cache.Options
SetupIndexes func(ctx context.Context, mgr ctrl.Manager)
SetupReconcilers func(ctx context.Context, mgr ctrl.Manager)
SetupEnv func(e *Environment)
MinK8sVersion string
AdditionalSchemeBuilder runtime.SchemeBuilder
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: should this be

Suggested change
AdditionalSchemeBuilder runtime.SchemeBuilder
AdditionalSchemeBuilders []runtime.SchemeBuilder

(not blocking it is an internal util, we can always iterate in future)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

runtime.Schemebuilder is already an []func(*Scheme) error which is a collection of AddScheme funcs :-)

AdditionalCRDDirectoryPaths []string
}

// Run executes the tests of the given testing.M in a test environment.
Expand All @@ -147,8 +151,28 @@ func Run(ctx context.Context, input RunInput) int {
return input.M.Run()
}

// Calculate the scheme.
scheme := runtime.NewScheme()
utilruntime.Must(appsv1.AddToScheme(scheme))
utilruntime.Must(corev1.AddToScheme(scheme))
utilruntime.Must(rbacv1.AddToScheme(scheme))

utilruntime.Must(addonsv1.AddToScheme(scheme))
utilruntime.Must(admissionv1.AddToScheme(scheme))
utilruntime.Must(admissionv1.AddToScheme(scheme))
utilruntime.Must(apiextensionsv1.AddToScheme(scheme))
utilruntime.Must(bootstrapv1.AddToScheme(scheme))
utilruntime.Must(clusterv1.AddToScheme(scheme))
utilruntime.Must(controlplanev1.AddToScheme(scheme))
utilruntime.Must(expv1.AddToScheme(scheme))
utilruntime.Must(ipamv1.AddToScheme(scheme))
utilruntime.Must(runtimev1.AddToScheme(scheme))
if input.AdditionalSchemeBuilder != nil {
utilruntime.Must(input.AdditionalSchemeBuilder.AddToScheme(scheme))
}

// Bootstrapping test environment
env := newEnvironment(input.ManagerCacheOptions, input.ManagerUncachedObjs...)
env := newEnvironment(scheme, input.AdditionalCRDDirectoryPaths, input.ManagerCacheOptions, input.ManagerUncachedObjs...)

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

crdDirectoryPaths := []string{
filepath.Join(root, "config", "crd", "bases"),
filepath.Join(root, "controlplane", "kubeadm", "config", "crd", "bases"),
filepath.Join(root, "bootstrap", "kubeadm", "config", "crd", "bases"),
}
for _, path := range additionalCRDDirectoryPaths {
crdDirectoryPaths = append(crdDirectoryPaths, filepath.Join(root, path))
}

// Create the test environment.
env := &envtest.Environment{
ErrorIfCRDPathMissing: true,
CRDDirectoryPaths: []string{
filepath.Join(root, "config", "crd", "bases"),
filepath.Join(root, "controlplane", "kubeadm", "config", "crd", "bases"),
filepath.Join(root, "bootstrap", "kubeadm", "config", "crd", "bases"),
filepath.Join(root, "util", "test", "builder", "crd"),
},
CRDDirectoryPaths: crdDirectoryPaths,
CRDs: []*apiextensionsv1.CustomResourceDefinition{
builder.GenericBootstrapConfigCRD.DeepCopy(),
builder.GenericBootstrapConfigTemplateCRD.DeepCopy(),
Expand Down Expand Up @@ -296,7 +324,7 @@ func newEnvironment(managerCacheOptions cache.Options, uncachedObjs ...client.Ob
Controller: config.Controller{
UsePriorityQueue: ptr.To[bool](feature.Gates.Enabled(feature.PriorityQueue)),
},
Scheme: scheme.Scheme,
Scheme: scheme,
Metrics: metricsserver.Options{
BindAddress: "0",
},
Expand Down
20 changes: 20 additions & 0 deletions util/deprecated/v1beta1/conditions/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
Copyright 2020 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 conditions implements condition utilities.
//
// 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.
package conditions
Loading
Loading