Skip to content

Commit d0ad572

Browse files
committed
test: Add envtest for controllers that work with clusterclass and templates
1 parent 6f6d921 commit d0ad572

29 files changed

+29137
-4
lines changed

.pre-commit-config.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ repos:
118118
name: License headers - Go
119119
stages: [commit]
120120
files: "(.*\\.go|go.mod)$"
121-
exclude: ^api/external/
121+
exclude: ^(api/external/|internal/test)
122122
args:
123123
- --license-filepath
124124
- hack/license-header.txt
@@ -129,7 +129,7 @@ repos:
129129
name: License headers - YAML and Makefiles
130130
stages: [commit]
131131
files: (^Makefile|\.(ya?ml|mk))$
132-
exclude: ^(pkg/handlers/.+/embedded|examples|charts/cluster-api-runtime-extensions-nutanix/defaultclusterclasses)/.+\.ya?ml|docs/static/helm/index\.yaml|charts/cluster-api-runtime-extensions-nutanix/templates/helm-config.yaml$
132+
exclude: ^(internal/test|pkg/handlers/.+/embedded|examples|charts/cluster-api-runtime-extensions-nutanix/defaultclusterclasses)/.+\.ya?ml|docs/static/helm/index\.yaml|charts/cluster-api-runtime-extensions-nutanix/templates/helm-config.yaml$
133133
args:
134134
- --license-filepath
135135
- hack/license-header.txt

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,14 @@ replace (
1515
require (
1616
github.com/blang/semver/v4 v4.0.0
1717
github.com/go-logr/logr v1.4.2
18+
github.com/gobuffalo/flect v1.0.2
1819
github.com/google/go-cmp v0.6.0
1920
github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/api v0.0.0-00010101000000-000000000000
2021
github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/common v0.7.0
2122
github.com/nutanix-cloud-native/prism-go-client v0.4.0
2223
github.com/onsi/ginkgo/v2 v2.19.0
2324
github.com/onsi/gomega v1.33.1
25+
github.com/pkg/errors v0.9.1
2426
github.com/spf13/pflag v1.0.5
2527
github.com/stretchr/testify v1.9.0
2628
gopkg.in/yaml.v2 v2.4.0
@@ -71,7 +73,6 @@ require (
7173
github.com/go-openapi/jsonreference v0.20.2 // indirect
7274
github.com/go-openapi/swag v0.22.3 // indirect
7375
github.com/go-task/slim-sprig/v3 v3.0.0 // indirect
74-
github.com/gobuffalo/flect v1.0.2 // indirect
7576
github.com/gogo/protobuf v1.3.2 // indirect
7677
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
7778
github.com/golang/protobuf v1.5.4 // indirect
@@ -105,7 +106,6 @@ require (
105106
github.com/opencontainers/image-spec v1.1.0-rc5 // indirect
106107
github.com/pelletier/go-toml v1.9.5 // indirect
107108
github.com/pelletier/go-toml/v2 v2.1.0 // indirect
108-
github.com/pkg/errors v0.9.1 // indirect
109109
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
110110
github.com/prometheus/client_golang v1.18.0 // indirect
111111
github.com/prometheus/client_model v0.5.0 // indirect

internal/test/README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<!--
2+
Copyright 2024 Nutanix. All rights reserved.
3+
SPDX-License-Identifier: Apache-2.0
4+
-->
5+
6+
# Test Framework
7+
8+
## Origin
9+
10+
This directory is a copy, with modifications, of the [upstream test
11+
package](https://github.com/kubernetes-sigs/cluster-api/tree/v1.7.2/internal/test).
12+
13+
## Purpose
14+
15+
The namespacesync controller reads and writes Templates of various types. The
16+
upstream test package creates "generic" Template types and CRDs. This allows us
17+
to test the controller using envtest, with real types and CRDs.

internal/test/bootstrap/kubeadm/config/crd/bases/bootstrap.cluster.x-k8s.io_kubeadmconfigs.yaml

Lines changed: 3180 additions & 0 deletions
Large diffs are not rendered by default.

internal/test/bootstrap/kubeadm/config/crd/bases/bootstrap.cluster.x-k8s.io_kubeadmconfigtemplates.yaml

Lines changed: 3086 additions & 0 deletions
Large diffs are not rendered by default.

internal/test/builder/bootstrap.go

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
/*
2+
Copyright 2021 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+
//nolint:lll // Code is copied from upstream.
18+
package builder
19+
20+
import (
21+
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
22+
"k8s.io/apimachinery/pkg/runtime/schema"
23+
)
24+
25+
var (
26+
// BootstrapGroupVersion is group version used for bootstrap objects.
27+
BootstrapGroupVersion = schema.GroupVersion{
28+
Group: "bootstrap.cluster.x-k8s.io",
29+
Version: "v1beta1",
30+
}
31+
32+
// GenericBootstrapConfigKind is the Kind for the GenericBootstrapConfig.
33+
GenericBootstrapConfigKind = "GenericBootstrapConfig"
34+
// GenericBootstrapConfigCRD is a generic bootstrap CRD.
35+
GenericBootstrapConfigCRD = untypedCRD(
36+
BootstrapGroupVersion.WithKind(GenericBootstrapConfigKind),
37+
)
38+
39+
// GenericBootstrapConfigTemplateKind is the Kind for the GenericBootstrapConfigTemplate.
40+
GenericBootstrapConfigTemplateKind = "GenericBootstrapConfigTemplate"
41+
// GenericBootstrapConfigTemplateCRD is a generic bootstrap template CRD.
42+
GenericBootstrapConfigTemplateCRD = untypedCRD(
43+
BootstrapGroupVersion.WithKind(GenericBootstrapConfigTemplateKind),
44+
)
45+
46+
// TODO: drop generic CRDs in favour of typed test CRDs.
47+
48+
// TestBootstrapConfigTemplateKind is the kind for the TestBootstrapConfigTemplate type.
49+
TestBootstrapConfigTemplateKind = "TestBootstrapConfigTemplate"
50+
// TestBootstrapConfigTemplateCRD is a test bootstrap config template CRD.
51+
TestBootstrapConfigTemplateCRD = testBootstrapConfigTemplateCRD(
52+
BootstrapGroupVersion.WithKind(TestBootstrapConfigTemplateKind),
53+
)
54+
55+
// TestBootstrapConfigKind is the kind for the TestBootstrapConfig type.
56+
TestBootstrapConfigKind = "TestBootstrapConfig"
57+
// TestBootstrapConfigCRD is a test bootstrap config CRD.
58+
TestBootstrapConfigCRD = testBootstrapConfigCRD(
59+
BootstrapGroupVersion.WithKind(TestBootstrapConfigKind),
60+
)
61+
)
62+
63+
func testBootstrapConfigTemplateCRD(
64+
gvk schema.GroupVersionKind,
65+
) *apiextensionsv1.CustomResourceDefinition {
66+
return generateCRD(gvk, map[string]apiextensionsv1.JSONSchemaProps{
67+
"metadata": {
68+
// NOTE: in CRD there is only a partial definition of metadata schema.
69+
// Ref https://github.com/kubernetes-sigs/controller-tools/blob/59485af1c1f6a664655dad49543c474bb4a0d2a2/pkg/crd/gen.go#L185
70+
Type: "object",
71+
},
72+
"spec": {
73+
Type: "object",
74+
Properties: map[string]apiextensionsv1.JSONSchemaProps{
75+
// Mandatory field from the Cluster API contract
76+
"template": {
77+
Type: "object",
78+
Properties: map[string]apiextensionsv1.JSONSchemaProps{
79+
"spec": bootstrapConfigSpecSchema,
80+
},
81+
},
82+
},
83+
},
84+
})
85+
}
86+
87+
func testBootstrapConfigCRD(gvk schema.GroupVersionKind) *apiextensionsv1.CustomResourceDefinition {
88+
return generateCRD(gvk, map[string]apiextensionsv1.JSONSchemaProps{
89+
"metadata": {
90+
// NOTE: in CRD there is only a partial definition of metadata schema.
91+
// Ref https://github.com/kubernetes-sigs/controller-tools/blob/59485af1c1f6a664655dad49543c474bb4a0d2a2/pkg/crd/gen.go#L185
92+
Type: "object",
93+
},
94+
"spec": bootstrapConfigSpecSchema,
95+
"status": {
96+
Type: "object",
97+
Properties: map[string]apiextensionsv1.JSONSchemaProps{
98+
// mandatory field from the Cluster API contract
99+
"ready": {Type: "boolean"},
100+
"dataSecretName": {Type: "string"},
101+
// General purpose fields to be used in different test scenario.
102+
"foo": {Type: "string"},
103+
"bar": {Type: "string"},
104+
},
105+
},
106+
})
107+
}
108+
109+
var bootstrapConfigSpecSchema = apiextensionsv1.JSONSchemaProps{
110+
Type: "object",
111+
Properties: map[string]apiextensionsv1.JSONSchemaProps{
112+
// General purpose fields to be used in different test scenario.
113+
"foo": {Type: "string"},
114+
"bar": {Type: "string"},
115+
},
116+
}

0 commit comments

Comments
 (0)