Skip to content

⚠️ Add v1beta2 API for ExtensionConfig #12197

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

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ linters:
- pkg: sigs.k8s.io/cluster-api/exp/ipam/api/v1beta2
alias: ipamv1
# CAPI exp runtime
- pkg: sigs.k8s.io/cluster-api/exp/runtime/api/v1alpha1
- pkg: sigs.k8s.io/cluster-api/exp/runtime/api/v1beta2
alias: runtimev1
- pkg: sigs.k8s.io/cluster-api/exp/runtime/hooks/api/v1alpha1
alias: runtimehooksv1
Expand Down
6 changes: 6 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,12 @@ generate-go-conversions-core-runtime: $(CONVERSION_GEN) ## Generate conversions
--go-header-file=./hack/boilerplate/boilerplate.generatego.txt \
./internal/runtime/test/v1alpha1 \
./internal/runtime/test/v1alpha2
$(MAKE) clean-generated-conversions SRC_DIRS="./$(EXP_DIR)/runtime/api/v1alpha1,./$(EXP_DIR)/runtime/api/v1beta2"
$(CONVERSION_GEN) \
--output-file=zz_generated.conversion.go \
--go-header-file=./hack/boilerplate/boilerplate.generatego.txt \
./$(EXP_DIR)/runtime/api/v1alpha1 \
./$(EXP_DIR)/runtime/api/v1beta2

.PHONY: generate-go-conversions-kubeadm-bootstrap
generate-go-conversions-kubeadm-bootstrap: $(CONVERSION_GEN) ## Generate conversions go code for kubeadm bootstrap
Expand Down
351 changes: 351 additions & 0 deletions config/crd/bases/runtime.cluster.x-k8s.io_extensionconfigs.yaml

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions config/crd/kustomization.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ patches:
- path: patches/webhook_in_clusterresourcesetbindings.yaml
- path: patches/webhook_in_ipaddresses.yaml
- path: patches/webhook_in_ipaddressclaims.yaml
- path: patches/webhook_in_extensionconfigs.yaml
# +kubebuilder:scaffold:crdkustomizewebhookpatch

# the following config is for teaching kustomize how to do kustomization for CRDs.
Expand Down
16 changes: 16 additions & 0 deletions config/crd/patches/webhook_in_extensionconfigs.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# The following patch enables conversion webhook for CRD
# CRD conversion requires k8s 1.13 or later.
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
name: extensionconfigs.runtime.cluster.x-k8s.io
spec:
conversion:
strategy: Webhook
webhook:
conversionReviewVersions: ["v1", "v1beta1"]
clientConfig:
service:
namespace: system
name: webhook-service
path: /convert
4 changes: 0 additions & 4 deletions config/default/kustomization.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,6 @@ replacements:
delimiter: /
select:
kind: CustomResourceDefinition
reject:
- name: extensionconfigs.runtime.cluster.x-k8s.io
- source:
fieldPath: .metadata.name
group: cert-manager.io
Expand Down Expand Up @@ -90,8 +88,6 @@ replacements:
index: 1
select:
kind: CustomResourceDefinition
reject:
- name: extensionconfigs.runtime.cluster.x-k8s.io
- source:
fieldPath: .metadata.name
kind: Service
Expand Down
8 changes: 4 additions & 4 deletions config/webhook/manifests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -165,15 +165,15 @@ webhooks:
service:
name: webhook-service
namespace: system
path: /mutate-runtime-cluster-x-k8s-io-v1alpha1-extensionconfig
path: /mutate-runtime-cluster-x-k8s-io-v1beta2-extensionconfig
failurePolicy: Fail
matchPolicy: Equivalent
name: default.extensionconfig.runtime.addons.cluster.x-k8s.io
rules:
- apiGroups:
- runtime.cluster.x-k8s.io
apiVersions:
- v1alpha1
- v1beta2
operations:
- CREATE
- UPDATE
Expand Down Expand Up @@ -415,15 +415,15 @@ webhooks:
service:
name: webhook-service
namespace: system
path: /validate-runtime-cluster-x-k8s-io-v1alpha1-extensionconfig
path: /validate-runtime-cluster-x-k8s-io-v1beta2-extensionconfig
failurePolicy: Fail
matchPolicy: Equivalent
name: validation.extensionconfig.runtime.cluster.x-k8s.io
rules:
- apiGroups:
- runtime.cluster.x-k8s.io
apiVersions:
- v1alpha1
- v1beta2
operations:
- CREATE
- UPDATE
Expand Down
104 changes: 104 additions & 0 deletions exp/runtime/api/v1alpha1/conversion.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/*
Copyright 2025 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 v1alpha1

import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
apimachineryconversion "k8s.io/apimachinery/pkg/conversion"
"sigs.k8s.io/controller-runtime/pkg/conversion"

clusterv1 "sigs.k8s.io/cluster-api/api/v1beta2"
runtimev1 "sigs.k8s.io/cluster-api/exp/runtime/api/v1beta2"
)

func (src *ExtensionConfig) ConvertTo(dstRaw conversion.Hub) error {
dst := dstRaw.(*runtimev1.ExtensionConfig)

return Convert_v1alpha1_ExtensionConfig_To_v1beta2_ExtensionConfig(src, dst, nil)
}

func (dst *ExtensionConfig) ConvertFrom(srcRaw conversion.Hub) error {
src := srcRaw.(*runtimev1.ExtensionConfig)

return Convert_v1beta2_ExtensionConfig_To_v1alpha1_ExtensionConfig(src, dst, nil)
}

func Convert_v1beta2_ExtensionConfigStatus_To_v1alpha1_ExtensionConfigStatus(in *runtimev1.ExtensionConfigStatus, out *ExtensionConfigStatus, s apimachineryconversion.Scope) error {
if err := autoConvert_v1beta2_ExtensionConfigStatus_To_v1alpha1_ExtensionConfigStatus(in, out, s); err != nil {
return err
}

// Reset conditions from autogenerated conversions
// NOTE: v1beta2 conditions should not be automatically be converted into legacy conditions (v1beta1).
out.Conditions = nil

// Retrieve legacy conditions (v1beta1) from the deprecated field.
if in.Deprecated != nil && in.Deprecated.V1Beta1 != nil {
if in.Deprecated.V1Beta1.Conditions != nil {
out.Conditions = in.Deprecated.V1Beta1.Conditions
Copy link
Member

Choose a reason for hiding this comment

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

This should not be possible without conversion. v1alpha1 should not be using v1beta2.Conditions. Looks like this was changed on main (on release-1.10 v1alpha1 is using v1beta1.Conditions)

(e.g. CRS is using clusterv1beta1.Convert_v1beta2_Deprecated_V1Beta1_Conditions_To_v1beta1_Conditions(&in.Deprecated.V1Beta1.Conditions, &out.Conditions))

}
}

// Move new conditions (v1beta2) to the v1beta2 field.
if in.Conditions == nil {
return nil
}
out.V1Beta2 = &ExtensionConfigV1Beta2Status{}
out.V1Beta2.Conditions = in.Conditions
return nil
}

func Convert_v1alpha1_ExtensionConfigStatus_To_v1beta2_ExtensionConfigStatus(in *ExtensionConfigStatus, out *runtimev1.ExtensionConfigStatus, s apimachineryconversion.Scope) error {
if err := autoConvert_v1alpha1_ExtensionConfigStatus_To_v1beta2_ExtensionConfigStatus(in, out, s); err != nil {
return err
}

// Reset conditions from autogenerated conversions
// NOTE: v1beta1 conditions should not be automatically be converted into v1beta2 conditions.
out.Conditions = nil

// Retrieve new conditions (v1beta2) from the v1beta2 field.
if in.V1Beta2 != nil {
out.Conditions = in.V1Beta2.Conditions
}

// Move legacy conditions (v1beta1) to the deprecated field.
if in.Conditions == nil {
return nil
}

if out.Deprecated == nil {
out.Deprecated = &runtimev1.ExtensionConfigDeprecatedStatus{}
}
if out.Deprecated.V1Beta1 == nil {
out.Deprecated.V1Beta1 = &runtimev1.ExtensionConfigV1Beta1DeprecatedStatus{}
}
if in.Conditions != nil {
out.Deprecated.V1Beta1.Conditions = in.Conditions
Copy link
Member

Choose a reason for hiding this comment

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

This should also require conversion, e.g.:

clusterv1beta1.Convert_v1beta1_Conditions_To_v1beta2_Deprecated_V1Beta1_Conditions(&in.Conditions, &out.Deprecated.V1Beta1.Conditions)

}
return nil
}

func Convert_v1_Condition_To_v1beta2_Condition(_ *metav1.Condition, _ *clusterv1.Condition, _ apimachineryconversion.Scope) error {
Copy link
Member

Choose a reason for hiding this comment

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

Not sure I follow. Why is this different to other CRDs? e.g.

func Convert_v1_Condition_To_v1beta1_Condition(in *metav1.Condition, out *clusterv1beta1.Condition, s apimachineryconversion.Scope) error {
	return clusterv1beta1.Convert_v1_Condition_To_v1beta1_Condition(in, out, s)
}

func Convert_v1beta1_Condition_To_v1_Condition(in *clusterv1beta1.Condition, out *metav1.Condition, s apimachineryconversion.Scope) error {
	return clusterv1beta1.Convert_v1beta1_Condition_To_v1_Condition(in, out, s)
}

// NOTE: v1beta2 conditions should not be automatically converted into legacy (v1beta2) conditions.
return nil
}

func Convert_v1beta2_Condition_To_v1_Condition(_ *clusterv1.Condition, _ *metav1.Condition, _ apimachineryconversion.Scope) error {
// NOTE: legacy (v1beta2) conditions should not be automatically converted into v1beta2 conditions.
return nil
}
68 changes: 68 additions & 0 deletions exp/runtime/api/v1alpha1/conversion_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
//go:build !race

/*
Copyright 2025 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 v1alpha1

import (
"reflect"
"testing"

fuzz "github.com/google/gofuzz"
"k8s.io/apimachinery/pkg/api/apitesting/fuzzer"
runtimeserializer "k8s.io/apimachinery/pkg/runtime/serializer"

runtimev1 "sigs.k8s.io/cluster-api/exp/runtime/api/v1beta2"
utilconversion "sigs.k8s.io/cluster-api/util/conversion"
)

// Test is disabled when the race detector is enabled (via "//go:build !race" above) because otherwise the fuzz tests would just time out.

func TestFuzzyConversion(t *testing.T) {
t.Run("for ExtensionConfig", utilconversion.FuzzTestFunc(utilconversion.FuzzTestFuncInput{
Hub: &runtimev1.ExtensionConfig{},
Spoke: &ExtensionConfig{},
FuzzerFuncs: []fuzzer.FuzzerFuncs{ExtensionConfigFuzzFuncs},
}))
}

func ExtensionConfigFuzzFuncs(_ runtimeserializer.CodecFactory) []interface{} {
return []interface{}{
hubExtensionConfigStatus,
spokeExtensionConfigStatus,
}
}

func hubExtensionConfigStatus(in *runtimev1.ExtensionConfigStatus, c fuzz.Continue) {
c.FuzzNoCustom(in)
// Drop empty structs with only omit empty fields.
if in.Deprecated != nil {
if in.Deprecated.V1Beta1 == nil || reflect.DeepEqual(in.Deprecated.V1Beta1, &runtimev1.ExtensionConfigV1Beta1DeprecatedStatus{}) {
in.Deprecated = nil
}
}
}

func spokeExtensionConfigStatus(in *ExtensionConfigStatus, c fuzz.Continue) {
c.FuzzNoCustom(in)
// Drop empty structs with only omit empty fields.
if in.V1Beta2 != nil {
if reflect.DeepEqual(in.V1Beta2, &ExtensionConfigV1Beta2Status{}) {
in.V1Beta2 = nil
}
}
}
1 change: 1 addition & 0 deletions exp/runtime/api/v1alpha1/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ limitations under the License.
*/

// Package v1alpha1 contains the v1alpha1 implementation of ExtensionConfig.
// +k8s:conversion-gen=sigs.k8s.io/cluster-api/exp/runtime/api/v1beta2
package v1alpha1
1 change: 0 additions & 1 deletion exp/runtime/api/v1alpha1/extensionconfig_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,6 @@ const (
// +kubebuilder:object:root=true
// +kubebuilder:resource:path=extensionconfigs,shortName=ext,scope=Cluster,categories=cluster-api
// +kubebuilder:subresource:status
// +kubebuilder:storageversion
// +kubebuilder:printcolumn:name="Age",type="date",JSONPath=".metadata.creationTimestamp",description="Time duration since creation of ExtensionConfig"

// ExtensionConfig is the Schema for the ExtensionConfig API.
Expand Down
3 changes: 3 additions & 0 deletions exp/runtime/api/v1alpha1/groupversion_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ var (
// AddToScheme adds the types in this group-version to the given scheme.
AddToScheme = schemeBuilder.AddToScheme

// localSchemeBuilder is used for type conversions.
localSchemeBuilder = schemeBuilder

objectTypes = []runtime.Object{}
)

Expand Down
Loading
Loading