Skip to content

Commit 4c05168

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 4c05168

File tree

61 files changed

+2373
-47
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

+2373
-47
lines changed

test/testdata/generate.sh

+40-8
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,17 @@ function scaffold_test_project {
4040
header_text 'Creating APIs ...'
4141
$kb create api --group crew --version v1 --kind Captain --controller=true --resource=true --make=false
4242
$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
43+
$kb create webhook --group crew --version v1 --kind Captain --defaulting --programmatic-validation --make=false
44+
45+
# Create API to test conversion from v1 to v2
4446
$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
47+
$kb create api --group crew --version v2 --kind FirstMate --controller=false --resource=true --make=false
48+
$kb create webhook --group crew --version v1 --kind FirstMate --conversion --make=false
49+
50+
# TODO: Remove it when we have the hub and spoke scaffolded by Kubebuilder
51+
# Apply the sed command based on project type
52+
insert_kubebuilder_annotations "api/v1/firstmate_types.go"
53+
4654
$kb create api --group crew --version v1 --kind Admiral --plural=admirales --controller=true --resource=true --namespaced=false --make=false
4755
$kb create webhook --group crew --version v1 --kind Admiral --plural=admirales --defaulting
4856
# Controller for External types
@@ -59,14 +67,13 @@ function scaffold_test_project {
5967

6068
header_text 'Creating APIs ...'
6169
$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
70+
$kb create webhook --group crew --version v1 --kind Captain --defaulting --programmatic-validation --make=false
6371

6472
$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
6673
$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
74+
$kb create webhook --group ship --version v1 --kind Destroyer --defaulting --make=false
6875
$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
76+
$kb create webhook --group ship --version v2alpha1 --kind Cruiser --programmatic-validation --make=false
7077

7178
$kb create api --group sea-creatures --version v1beta1 --kind Kraken --controller=true --resource=true --make=false
7279
$kb create api --group sea-creatures --version v1beta2 --kind Leviathan --controller=true --resource=true --make=false
@@ -80,15 +87,30 @@ function scaffold_test_project {
8087
# Webhook for External types
8188
$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
8289
# Webhook for Core type
83-
$kb create webhook --group core --version v1 --kind Pod --programmatic-validation
90+
$kb create webhook --group core --version v1 --kind Pod --programmatic-validation --make=false
8491
fi
8592

8693
if [[ $project =~ multigroup ]] || [[ $project =~ with-plugins ]] ; then
8794
header_text 'With Optional Plugins ...'
8895
header_text 'Creating APIs with deploy-image plugin ...'
8996
$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
9097
$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
98+
# Create only validation webhook for Memcached
99+
$kb create webhook --group example.com --version v1alpha1 --kind Memcached --programmatic-validation --make=false
100+
# Create API to check webhook --conversion from v1 to v2
101+
$kb create api --group example.com --version v1 --kind Wordpress --controller=true --resource=true --make=false
102+
$kb create api --group example.com --version v2 --kind Wordpress --controller=false --resource=true --make=false
103+
$kb create webhook --group example.com --version v1 --kind Wordpress --conversion --make=false
104+
105+
# TODO: Remove it when we have the hub and spoke scaffolded by Kubebuilder
106+
# Apply the sed command based on project type
107+
if [[ $project =~ multigroup ]]; then
108+
insert_kubebuilder_annotations "api/example.com/v1/wordpress_types.go"
109+
fi
110+
if [[ $project =~ with-plugins ]]; then
111+
insert_kubebuilder_annotations "api/v1/wordpress_types.go"
112+
fi
113+
92114
header_text 'Editing project with Grafana plugin ...'
93115
$kb edit --plugins=grafana.kubebuilder.io/v1-alpha
94116
fi
@@ -101,6 +123,16 @@ function scaffold_test_project {
101123
popd
102124
}
103125

126+
# TODO: Remove when hub and spoke be scaffolded by Kubebuilder
127+
function insert_kubebuilder_annotations {
128+
local file=$1
129+
local line=43 # The target line to insert text before
130+
local annotations="// +kubebuilder:storageversion\n// +kubebuilder:conversion:hub"
131+
132+
# Create a temporary file to avoid using -i flag, which varies between macOS and Linux
133+
awk -v insert="$annotations" -v line=$line 'NR==line{print insert} 1' "$file" > "$file.tmp" && mv "$file.tmp" "$file"
134+
}
135+
104136
build_kb
105137

106138
scaffold_test_project project-v4 --plugins="go/v4"

testdata/project-v4-multigroup/PROJECT

+20-3
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"
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+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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+
46+
// Wordpress is the Schema for the wordpresses API.
47+
type Wordpress struct {
48+
metav1.TypeMeta `json:",inline"`
49+
metav1.ObjectMeta `json:"metadata,omitempty"`
50+
51+
Spec WordpressSpec `json:"spec,omitempty"`
52+
Status WordpressStatus `json:"status,omitempty"`
53+
}
54+
55+
// +kubebuilder:object:root=true
56+
57+
// WordpressList contains a list of Wordpress.
58+
type WordpressList struct {
59+
metav1.TypeMeta `json:",inline"`
60+
metav1.ListMeta `json:"metadata,omitempty"`
61+
Items []Wordpress `json:"items"`
62+
}
63+
64+
func init() {
65+
SchemeBuilder.Register(&Wordpress{}, &WordpressList{})
66+
}

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

+114
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
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)