Skip to content
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

🌱 fix: optimize and improve testdata scaffolding for webhook conversion #4276

Merged
Show file tree
Hide file tree
Changes from all 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
48 changes: 40 additions & 8 deletions test/testdata/generate.sh
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,17 @@ function scaffold_test_project {
header_text 'Creating APIs ...'
$kb create api --group crew --version v1 --kind Captain --controller=true --resource=true --make=false
$kb create api --group crew --version v1 --kind Captain --controller=true --resource=true --make=false --force
$kb create webhook --group crew --version v1 --kind Captain --defaulting --programmatic-validation
$kb create webhook --group crew --version v1 --kind Captain --defaulting --programmatic-validation --make=false

# Create API to test conversion from v1 to v2
$kb create api --group crew --version v1 --kind FirstMate --controller=true --resource=true --make=false
$kb create webhook --group crew --version v1 --kind FirstMate --conversion
$kb create api --group crew --version v2 --kind FirstMate --controller=false --resource=true --make=false
$kb create webhook --group crew --version v1 --kind FirstMate --conversion --make=false

# TODO: Remove it when we have the hub and spoke scaffolded by Kubebuilder
# Apply the sed command based on project type
insert_kubebuilder_annotations "api/v1/firstmate_types.go"

$kb create api --group crew --version v1 --kind Admiral --plural=admirales --controller=true --resource=true --namespaced=false --make=false
$kb create webhook --group crew --version v1 --kind Admiral --plural=admirales --defaulting
# Controller for External types
Expand All @@ -59,14 +67,13 @@ function scaffold_test_project {

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

$kb create api --group ship --version v1beta1 --kind Frigate --controller=true --resource=true --make=false
$kb create webhook --group ship --version v1beta1 --kind Frigate --conversion
$kb create api --group ship --version v1 --kind Destroyer --controller=true --resource=true --namespaced=false --make=false
$kb create webhook --group ship --version v1 --kind Destroyer --defaulting
$kb create webhook --group ship --version v1 --kind Destroyer --defaulting --make=false
$kb create api --group ship --version v2alpha1 --kind Cruiser --controller=true --resource=true --namespaced=false --make=false
$kb create webhook --group ship --version v2alpha1 --kind Cruiser --programmatic-validation
$kb create webhook --group ship --version v2alpha1 --kind Cruiser --programmatic-validation --make=false

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

if [[ $project =~ multigroup ]] || [[ $project =~ with-plugins ]] ; then
header_text 'With Optional Plugins ...'
header_text 'Creating APIs with deploy-image plugin ...'
$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
$kb create api --group example.com --version v1alpha1 --kind Busybox --image=busybox:1.36.1 --plugins="deploy-image/v1-alpha" --make=false
$kb create webhook --group example.com --version v1alpha1 --kind Memcached --programmatic-validation
# Create only validation webhook for Memcached
$kb create webhook --group example.com --version v1alpha1 --kind Memcached --programmatic-validation --make=false
# Create API to check webhook --conversion from v1 to v2
$kb create api --group example.com --version v1 --kind Wordpress --controller=true --resource=true --make=false
$kb create api --group example.com --version v2 --kind Wordpress --controller=false --resource=true --make=false
$kb create webhook --group example.com --version v1 --kind Wordpress --conversion --make=false

# TODO: Remove it when we have the hub and spoke scaffolded by Kubebuilder
# Apply the sed command based on project type
if [[ $project =~ multigroup ]]; then
insert_kubebuilder_annotations "api/example.com/v1/wordpress_types.go"
fi
if [[ $project =~ with-plugins ]]; then
insert_kubebuilder_annotations "api/v1/wordpress_types.go"
fi

header_text 'Editing project with Grafana plugin ...'
$kb edit --plugins=grafana.kubebuilder.io/v1-alpha
fi
Expand All @@ -101,6 +123,16 @@ function scaffold_test_project {
popd
}

# TODO: Remove when hub and spoke be scaffolded by Kubebuilder
function insert_kubebuilder_annotations {
local file=$1
local line=43 # The target line to insert text before
local annotations="// +kubebuilder:storageversion\n// +kubebuilder:conversion:hub"

# Create a temporary file to avoid using -i flag, which varies between macOS and Linux
awk -v insert="$annotations" -v line=$line 'NR==line{print insert} 1' "$file" > "$file.tmp" && mv "$file.tmp" "$file"
}

build_kb

scaffold_test_project project-v4 --plugins="go/v4"
Expand Down
23 changes: 20 additions & 3 deletions testdata/project-v4-multigroup/PROJECT
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,6 @@ resources:
kind: Frigate
path: sigs.k8s.io/kubebuilder/testdata/project-v4-multigroup/api/ship/v1beta1
version: v1beta1
webhooks:
conversion: true
webhookVersion: v1
- api:
crdVersion: v1
controller: true
Expand Down Expand Up @@ -171,4 +168,24 @@ resources:
kind: Busybox
path: sigs.k8s.io/kubebuilder/testdata/project-v4-multigroup/api/example.com/v1alpha1
version: v1alpha1
- api:
crdVersion: v1
namespaced: true
controller: true
domain: testproject.org
group: example.com
kind: Wordpress
path: sigs.k8s.io/kubebuilder/testdata/project-v4-multigroup/api/example.com/v1
version: v1
webhooks:
conversion: true
webhookVersion: v1
- api:
crdVersion: v1
namespaced: true
domain: testproject.org
group: example.com
kind: Wordpress
path: sigs.k8s.io/kubebuilder/testdata/project-v4-multigroup/api/example.com/v2
version: v2
version: "3"
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
Copyright 2024 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 v1 contains API Schema definitions for the example.com v1 API group.
// +kubebuilder:object:generate=true
// +groupName=example.com.testproject.org
package v1

import (
"k8s.io/apimachinery/pkg/runtime/schema"
"sigs.k8s.io/controller-runtime/pkg/scheme"
)

var (
// GroupVersion is group version used to register these objects.
GroupVersion = schema.GroupVersion{Group: "example.com.testproject.org", Version: "v1"}

// SchemeBuilder is used to add go types to the GroupVersionKind scheme.
SchemeBuilder = &scheme.Builder{GroupVersion: GroupVersion}

// AddToScheme adds the types in this group-version to the given scheme.
AddToScheme = SchemeBuilder.AddToScheme
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
Copyright 2024 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 v1

import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

// EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN!
// NOTE: json tags are required. Any new fields you add must have json tags for the fields to be serialized.

// WordpressSpec defines the desired state of Wordpress.
type WordpressSpec struct {
// INSERT ADDITIONAL SPEC FIELDS - desired state of cluster
// Important: Run "make" to regenerate code after modifying this file

// Foo is an example field of Wordpress. Edit wordpress_types.go to remove/update
Foo string `json:"foo,omitempty"`
}

// WordpressStatus defines the observed state of Wordpress.
type WordpressStatus struct {
// INSERT ADDITIONAL STATUS FIELD - define observed state of cluster
// Important: Run "make" to regenerate code after modifying this file
}

// +kubebuilder:object:root=true
// +kubebuilder:subresource:status
// +kubebuilder:storageversion
// +kubebuilder:conversion:hub

// Wordpress is the Schema for the wordpresses API.
type Wordpress struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`

Spec WordpressSpec `json:"spec,omitempty"`
Status WordpressStatus `json:"status,omitempty"`
}

// +kubebuilder:object:root=true

// WordpressList contains a list of Wordpress.
type WordpressList struct {
metav1.TypeMeta `json:",inline"`
metav1.ListMeta `json:"metadata,omitempty"`
Items []Wordpress `json:"items"`
}

func init() {
SchemeBuilder.Register(&Wordpress{}, &WordpressList{})
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
Copyright 2024 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 v2 contains API Schema definitions for the example.com v2 API group.
// +kubebuilder:object:generate=true
// +groupName=example.com.testproject.org
package v2

import (
"k8s.io/apimachinery/pkg/runtime/schema"
"sigs.k8s.io/controller-runtime/pkg/scheme"
)

var (
// GroupVersion is group version used to register these objects.
GroupVersion = schema.GroupVersion{Group: "example.com.testproject.org", Version: "v2"}

// SchemeBuilder is used to add go types to the GroupVersionKind scheme.
SchemeBuilder = &scheme.Builder{GroupVersion: GroupVersion}

// AddToScheme adds the types in this group-version to the given scheme.
AddToScheme = SchemeBuilder.AddToScheme
)
Loading
Loading