Skip to content

Commit 8f86fba

Browse files
fix: optimize and improve testdata scaffolding for webhook conversion
- Set for all steps to speed up project generation - Adjust mock data to include webhook conversion scenarios NOTE: This update addresses bug fixes and enhancements requiring these mock datasets, including future support for hub-and-spoke webhook scaffolding and Helm plugin compatibility. Ensuring comprehensive coverage in testdata helps validate these scenarios effectively.
1 parent d500f48 commit 8f86fba

File tree

61 files changed

+2371
-48
lines changed

Some content is hidden

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

61 files changed

+2371
-48
lines changed

test/testdata/generate.sh

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,17 @@ function scaffold_test_project {
3131
rm -rf $testdata_dir/$project/*
3232
pushd $testdata_dir/$project
3333

34+
# Define the sed command based on the OS
35+
if [[ "$OSTYPE" == "darwin"* ]]; then
36+
# macOS sed syntax
37+
sed_storage_version="sed -i '' '43i\\
38+
// +kubebuilder:storageversion\\
39+
// +kubebuilder:conversion:hub'"
40+
else
41+
# Linux sed syntax
42+
sed_storage_version="sed -i '43i // +kubebuilder:storageversion\n// +kubebuilder:conversion:hub'"
43+
fi
44+
3445
header_text "Generating project ${project} with flags: ${init_flags}"
3546
go mod init sigs.k8s.io/kubebuilder/testdata/$project # our repo autodetection will traverse up to the kb module if we don't do this
3647
header_text "Initializing project ..."
@@ -40,9 +51,16 @@ function scaffold_test_project {
4051
header_text 'Creating APIs ...'
4152
$kb create api --group crew --version v1 --kind Captain --controller=true --resource=true --make=false
4253
$kb create api --group crew --version v1 --kind Captain --controller=true --resource=true --make=false --force
43-
$kb create webhook --group crew --version v1 --kind Captain --defaulting --programmatic-validation
54+
$kb create webhook --group crew --version v1 --kind Captain --defaulting --programmatic-validation --make=false
55+
56+
# Create API to test conversion from v1 to v2
4457
$kb create api --group crew --version v1 --kind FirstMate --controller=true --resource=true --make=false
45-
$kb create webhook --group crew --version v1 --kind FirstMate --conversion
58+
$kb create api --group crew --version v2 --kind FirstMate --controller=false --resource=true --make=false
59+
$kb create webhook --group crew --version v1 --kind FirstMate --conversion --make=false
60+
# TODO: Remove it when we have the hub and spoke scaffolded by Kubebuilder
61+
# Apply the sed command based on project type
62+
eval "$sed_storage_version api/v1/firstmate_types.go"
63+
4664
$kb create api --group crew --version v1 --kind Admiral --plural=admirales --controller=true --resource=true --namespaced=false --make=false
4765
$kb create webhook --group crew --version v1 --kind Admiral --plural=admirales --defaulting
4866
# Controller for External types
@@ -59,14 +77,13 @@ function scaffold_test_project {
5977

6078
header_text 'Creating APIs ...'
6179
$kb create api --group crew --version v1 --kind Captain --controller=true --resource=true --make=false
62-
$kb create webhook --group crew --version v1 --kind Captain --defaulting --programmatic-validation
80+
$kb create webhook --group crew --version v1 --kind Captain --defaulting --programmatic-validation --make=false
6381

6482
$kb create api --group ship --version v1beta1 --kind Frigate --controller=true --resource=true --make=false
65-
$kb create webhook --group ship --version v1beta1 --kind Frigate --conversion
6683
$kb create api --group ship --version v1 --kind Destroyer --controller=true --resource=true --namespaced=false --make=false
67-
$kb create webhook --group ship --version v1 --kind Destroyer --defaulting
84+
$kb create webhook --group ship --version v1 --kind Destroyer --defaulting --make=false
6885
$kb create api --group ship --version v2alpha1 --kind Cruiser --controller=true --resource=true --namespaced=false --make=false
69-
$kb create webhook --group ship --version v2alpha1 --kind Cruiser --programmatic-validation
86+
$kb create webhook --group ship --version v2alpha1 --kind Cruiser --programmatic-validation --make=false
7087

7188
$kb create api --group sea-creatures --version v1beta1 --kind Kraken --controller=true --resource=true --make=false
7289
$kb create api --group sea-creatures --version v1beta2 --kind Leviathan --controller=true --resource=true --make=false
@@ -80,15 +97,30 @@ function scaffold_test_project {
8097
# Webhook for External types
8198
$kb create webhook --group "cert-manager" --version v1 --kind Issuer --defaulting --external-api-path=github.com/cert-manager/cert-manager/pkg/apis/certmanager/v1 --external-api-domain=io
8299
# Webhook for Core type
83-
$kb create webhook --group core --version v1 --kind Pod --programmatic-validation
100+
$kb create webhook --group core --version v1 --kind Pod --programmatic-validation --make=false
84101
fi
85102

86103
if [[ $project =~ multigroup ]] || [[ $project =~ with-plugins ]] ; then
87104
header_text 'With Optional Plugins ...'
88105
header_text 'Creating APIs with deploy-image plugin ...'
89106
$kb create api --group example.com --version v1alpha1 --kind Memcached --image=memcached:1.6.26-alpine3.19 --image-container-command="memcached,--memory-limit=64,-o,modern,-v" --image-container-port="11211" --run-as-user="1001" --plugins="deploy-image/v1-alpha" --make=false
90107
$kb create api --group example.com --version v1alpha1 --kind Busybox --image=busybox:1.36.1 --plugins="deploy-image/v1-alpha" --make=false
91-
$kb create webhook --group example.com --version v1alpha1 --kind Memcached --programmatic-validation
108+
# Create only validation webhook for Memcached
109+
$kb create webhook --group example.com --version v1alpha1 --kind Memcached --programmatic-validation --make=false
110+
# Create API to check webhook --conversion from v1 to v2
111+
$kb create api --group example.com --version v1 --kind Wordpress --controller=true --resource=true --make=false
112+
$kb create api --group example.com --version v2 --kind Wordpress --controller=false --resource=true --make=false
113+
$kb create webhook --group example.com --version v1 --kind Wordpress --conversion --make=false
114+
115+
# TODO: Remove it when we have the hub and spoke scaffolded by Kubebuilder
116+
# Apply the sed command based on project type
117+
if [[ $project =~ multigroup ]]; then
118+
eval "$sed_storage_version api/example.com/v1/wordpress_types.go"
119+
fi
120+
if [[ $project =~ with-plugins ]]; then
121+
eval "$sed_storage_version api/v1/wordpress_types.go"
122+
fi
123+
92124
header_text 'Editing project with Grafana plugin ...'
93125
$kb edit --plugins=grafana.kubebuilder.io/v1-alpha
94126
fi

testdata/project-v4-multigroup/PROJECT

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,6 @@ resources:
5050
kind: Frigate
5151
path: sigs.k8s.io/kubebuilder/testdata/project-v4-multigroup/api/ship/v1beta1
5252
version: v1beta1
53-
webhooks:
54-
conversion: true
55-
webhookVersion: v1
5653
- api:
5754
crdVersion: v1
5855
controller: true
@@ -171,4 +168,24 @@ resources:
171168
kind: Busybox
172169
path: sigs.k8s.io/kubebuilder/testdata/project-v4-multigroup/api/example.com/v1alpha1
173170
version: v1alpha1
171+
- api:
172+
crdVersion: v1
173+
namespaced: true
174+
controller: true
175+
domain: testproject.org
176+
group: example.com
177+
kind: Wordpress
178+
path: sigs.k8s.io/kubebuilder/testdata/project-v4-multigroup/api/example.com/v1
179+
version: v1
180+
webhooks:
181+
conversion: true
182+
webhookVersion: v1
183+
- api:
184+
crdVersion: v1
185+
namespaced: true
186+
domain: testproject.org
187+
group: example.com
188+
kind: Wordpress
189+
path: sigs.k8s.io/kubebuilder/testdata/project-v4-multigroup/api/example.com/v2
190+
version: v2
174191
version: "3"
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
Copyright 2024 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 v1 contains API Schema definitions for the example.com v1 API group.
18+
// +kubebuilder:object:generate=true
19+
// +groupName=example.com.testproject.org
20+
package v1
21+
22+
import (
23+
"k8s.io/apimachinery/pkg/runtime/schema"
24+
"sigs.k8s.io/controller-runtime/pkg/scheme"
25+
)
26+
27+
var (
28+
// GroupVersion is group version used to register these objects.
29+
GroupVersion = schema.GroupVersion{Group: "example.com.testproject.org", Version: "v1"}
30+
31+
// SchemeBuilder is used to add go types to the GroupVersionKind scheme.
32+
SchemeBuilder = &scheme.Builder{GroupVersion: GroupVersion}
33+
34+
// AddToScheme adds the types in this group-version to the given scheme.
35+
AddToScheme = SchemeBuilder.AddToScheme
36+
)
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
Copyright 2024 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 v1
18+
19+
import (
20+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
21+
)
22+
23+
// EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN!
24+
// NOTE: json tags are required. Any new fields you add must have json tags for the fields to be serialized.
25+
26+
// WordpressSpec defines the desired state of Wordpress.
27+
type WordpressSpec struct {
28+
// INSERT ADDITIONAL SPEC FIELDS - desired state of cluster
29+
// Important: Run "make" to regenerate code after modifying this file
30+
31+
// Foo is an example field of Wordpress. Edit wordpress_types.go to remove/update
32+
Foo string `json:"foo,omitempty"`
33+
}
34+
35+
// WordpressStatus defines the observed state of Wordpress.
36+
type WordpressStatus struct {
37+
// INSERT ADDITIONAL STATUS FIELD - define observed state of cluster
38+
// Important: Run "make" to regenerate code after modifying this file
39+
}
40+
41+
// +kubebuilder:object:root=true
42+
// +kubebuilder:subresource:status
43+
// +kubebuilder:storageversion
44+
// +kubebuilder:conversion:hub
45+
// Wordpress is the Schema for the wordpresses API.
46+
type Wordpress struct {
47+
metav1.TypeMeta `json:",inline"`
48+
metav1.ObjectMeta `json:"metadata,omitempty"`
49+
50+
Spec WordpressSpec `json:"spec,omitempty"`
51+
Status WordpressStatus `json:"status,omitempty"`
52+
}
53+
54+
// +kubebuilder:object:root=true
55+
56+
// WordpressList contains a list of Wordpress.
57+
type WordpressList struct {
58+
metav1.TypeMeta `json:",inline"`
59+
metav1.ListMeta `json:"metadata,omitempty"`
60+
Items []Wordpress `json:"items"`
61+
}
62+
63+
func init() {
64+
SchemeBuilder.Register(&Wordpress{}, &WordpressList{})
65+
}

testdata/project-v4-multigroup/api/example.com/v1/zz_generated.deepcopy.go

Lines changed: 114 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
Copyright 2024 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 v2 contains API Schema definitions for the example.com v2 API group.
18+
// +kubebuilder:object:generate=true
19+
// +groupName=example.com.testproject.org
20+
package v2
21+
22+
import (
23+
"k8s.io/apimachinery/pkg/runtime/schema"
24+
"sigs.k8s.io/controller-runtime/pkg/scheme"
25+
)
26+
27+
var (
28+
// GroupVersion is group version used to register these objects.
29+
GroupVersion = schema.GroupVersion{Group: "example.com.testproject.org", Version: "v2"}
30+
31+
// SchemeBuilder is used to add go types to the GroupVersionKind scheme.
32+
SchemeBuilder = &scheme.Builder{GroupVersion: GroupVersion}
33+
34+
// AddToScheme adds the types in this group-version to the given scheme.
35+
AddToScheme = SchemeBuilder.AddToScheme
36+
)

0 commit comments

Comments
 (0)