From 888216ce79ad816fd988eb0f533b74f60fceceb9 Mon Sep 17 00:00:00 2001 From: Andrea Mazzotti Date: Tue, 4 Jun 2024 09:12:05 +0200 Subject: [PATCH] Use CustomDefaulter to default KThreesControlPlane spec (#121) * Use CustomDefaulter to default KThreesControlPlane spec Signed-off-by: Andrea Mazzotti * Fix wrong comment reference Signed-off-by: Andrea Mazzotti * Add KThreesConfig defaults Signed-off-by: Andrea Mazzotti * Fix wrong comment reference Signed-off-by: Andrea Mazzotti --------- Signed-off-by: Andrea Mazzotti --- bootstrap/api/v1beta1/conversion.go | 9 ++++--- bootstrap/api/v1beta2/kthreesconfig_types.go | 6 ++--- .../api/v1beta2/kthreesconfig_webhook.go | 25 +++++++++++++++---- .../v1beta2/kthreesconfigtemplate_webhook.go | 8 +++--- .../api/v1beta2/zz_generated.deepcopy.go | 10 ++++++++ ...strap.cluster.x-k8s.io_kthreesconfigs.yaml | 2 -- ...uster.x-k8s.io_kthreesconfigtemplates.yaml | 2 -- .../v1beta2/kthreescontrolplane_webhook.go | 11 +++++++- ...cluster.x-k8s.io_kthreescontrolplanes.yaml | 2 -- ...x-k8s.io_kthreescontrolplanetemplates.yaml | 2 -- pkg/k3s/config.go | 15 ++++++++--- 11 files changed, 62 insertions(+), 30 deletions(-) diff --git a/bootstrap/api/v1beta1/conversion.go b/bootstrap/api/v1beta1/conversion.go index 261dcc9e..0a66a88b 100644 --- a/bootstrap/api/v1beta1/conversion.go +++ b/bootstrap/api/v1beta1/conversion.go @@ -17,6 +17,7 @@ import ( "fmt" "k8s.io/apimachinery/pkg/conversion" + "k8s.io/utils/ptr" utilconversion "sigs.k8s.io/cluster-api/util/conversion" ctrlconversion "sigs.k8s.io/controller-runtime/pkg/conversion" @@ -136,11 +137,11 @@ func Convert_v1beta1_KThreesServerConfig_To_v1beta2_KThreesServerConfig(in *KThr out.DeprecatedDisableExternalCloudProvider = in.DisableExternalCloudProvider if !in.DisableExternalCloudProvider { - out.CloudProviderName = "external" - out.DisableCloudController = true + out.CloudProviderName = ptr.To("external") + out.DisableCloudController = ptr.To(true) } else { - out.CloudProviderName = "" - out.DisableCloudController = false + out.CloudProviderName = ptr.To("") + out.DisableCloudController = ptr.To(false) } return nil diff --git a/bootstrap/api/v1beta2/kthreesconfig_types.go b/bootstrap/api/v1beta2/kthreesconfig_types.go index cd470717..e622757e 100644 --- a/bootstrap/api/v1beta2/kthreesconfig_types.go +++ b/bootstrap/api/v1beta2/kthreesconfig_types.go @@ -115,13 +115,11 @@ type KThreesServerConfig struct { // DisableCloudController disables k3s default cloud controller manager. (default: true) // +optional - // +kubebuilder:default=true - DisableCloudController bool `json:"disableCloudController,omitempty"` + DisableCloudController *bool `json:"disableCloudController,omitempty"` // CloudProviderName defines the --cloud-provider= kubelet extra arg. (default: "external") // +optional - // +kubebuilder:default=external - CloudProviderName string `json:"cloudProviderName,omitempty"` + CloudProviderName *string `json:"cloudProviderName,omitempty"` } type KThreesAgentConfig struct { diff --git a/bootstrap/api/v1beta2/kthreesconfig_webhook.go b/bootstrap/api/v1beta2/kthreesconfig_webhook.go index 1ba815e9..a441741f 100644 --- a/bootstrap/api/v1beta2/kthreesconfig_webhook.go +++ b/bootstrap/api/v1beta2/kthreesconfig_webhook.go @@ -18,13 +18,16 @@ package v1beta2 import ( "context" + "fmt" + apierrors "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/runtime" + "k8s.io/utils/ptr" ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/webhook/admission" ) -// SetupWebhookWithManager will setup the webhooks for the KThreesControlPlane. +// SetupWebhookWithManager will setup the webhooks for the KThreesConfig. func (c *KThreesConfig) SetupWebhookWithManager(mgr ctrl.Manager) error { return ctrl.NewWebhookManagedBy(mgr). For(c). @@ -39,12 +42,12 @@ func (c *KThreesConfig) SetupWebhookWithManager(mgr ctrl.Manager) error { var _ admission.CustomDefaulter = &KThreesConfig{} var _ admission.CustomValidator = &KThreesConfig{} -// ValidateCreate will do any extra validation when creating a KThreesControlPlane. +// ValidateCreate will do any extra validation when creating a KThreesConfig. func (c *KThreesConfig) ValidateCreate(_ context.Context, _ runtime.Object) (admission.Warnings, error) { return []string{}, nil } -// ValidateUpdate will do any extra validation when updating a KThreesControlPlane. +// ValidateUpdate will do any extra validation when updating a KThreesConfig. func (c *KThreesConfig) ValidateUpdate(_ context.Context, _, _ runtime.Object) (admission.Warnings, error) { return []string{}, nil } @@ -54,7 +57,19 @@ func (c *KThreesConfig) ValidateDelete(_ context.Context, _ runtime.Object) (adm return []string{}, nil } -// Default will set default values for the KThreesControlPlane. -func (c *KThreesConfig) Default(_ context.Context, _ runtime.Object) error { +// Default will set default values for the KThreesConfig. +func (c *KThreesConfig) Default(_ context.Context, obj runtime.Object) error { + c, ok := obj.(*KThreesConfig) + if !ok { + return apierrors.NewBadRequest(fmt.Sprintf("expected a KThreesConfig but got a %T", obj)) + } + + if c.Spec.ServerConfig.DisableCloudController == nil { + c.Spec.ServerConfig.DisableCloudController = ptr.To(true) + } + + if c.Spec.ServerConfig.CloudProviderName == nil { + c.Spec.ServerConfig.CloudProviderName = ptr.To("external") + } return nil } diff --git a/bootstrap/api/v1beta2/kthreesconfigtemplate_webhook.go b/bootstrap/api/v1beta2/kthreesconfigtemplate_webhook.go index 5c1d001b..f6bc6cc6 100644 --- a/bootstrap/api/v1beta2/kthreesconfigtemplate_webhook.go +++ b/bootstrap/api/v1beta2/kthreesconfigtemplate_webhook.go @@ -24,7 +24,7 @@ import ( "sigs.k8s.io/controller-runtime/pkg/webhook/admission" ) -// SetupWebhookWithManager will setup the webhooks for the KThreesControlPlane. +// SetupWebhookWithManager will setup the webhooks for the KThreesConfigTemplate. func (c *KThreesConfigTemplate) SetupWebhookWithManager(mgr ctrl.Manager) error { return ctrl.NewWebhookManagedBy(mgr). For(c). @@ -39,12 +39,12 @@ func (c *KThreesConfigTemplate) SetupWebhookWithManager(mgr ctrl.Manager) error var _ admission.CustomDefaulter = &KThreesConfigTemplate{} var _ admission.CustomValidator = &KThreesConfigTemplate{} -// ValidateCreate will do any extra validation when creating a KThreesControlPlane. +// ValidateCreate will do any extra validation when creating a KThreesConfigTemplate. func (c *KThreesConfigTemplate) ValidateCreate(_ context.Context, _ runtime.Object) (admission.Warnings, error) { return []string{}, nil } -// ValidateUpdate will do any extra validation when updating a KThreesControlPlane. +// ValidateUpdate will do any extra validation when updating a KThreesConfigTemplate. func (c *KThreesConfigTemplate) ValidateUpdate(_ context.Context, _, _ runtime.Object) (admission.Warnings, error) { return []string{}, nil } @@ -54,7 +54,7 @@ func (c *KThreesConfigTemplate) ValidateDelete(_ context.Context, _ runtime.Obje return []string{}, nil } -// Default will set default values for the KThreesControlPlane. +// Default will set default values for the KThreesConfigTemplate. func (c *KThreesConfigTemplate) Default(_ context.Context, _ runtime.Object) error { return nil } diff --git a/bootstrap/api/v1beta2/zz_generated.deepcopy.go b/bootstrap/api/v1beta2/zz_generated.deepcopy.go index 06b83495..d4bc8ca7 100644 --- a/bootstrap/api/v1beta2/zz_generated.deepcopy.go +++ b/bootstrap/api/v1beta2/zz_generated.deepcopy.go @@ -339,6 +339,16 @@ func (in *KThreesServerConfig) DeepCopyInto(out *KThreesServerConfig) { *out = make([]string, len(*in)) copy(*out, *in) } + if in.DisableCloudController != nil { + in, out := &in.DisableCloudController, &out.DisableCloudController + *out = new(bool) + **out = **in + } + if in.CloudProviderName != nil { + in, out := &in.CloudProviderName, &out.CloudProviderName + *out = new(string) + **out = **in + } } // DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new KThreesServerConfig. diff --git a/bootstrap/config/crd/bases/bootstrap.cluster.x-k8s.io_kthreesconfigs.yaml b/bootstrap/config/crd/bases/bootstrap.cluster.x-k8s.io_kthreesconfigs.yaml index 89b82a4c..febbf886 100644 --- a/bootstrap/config/crd/bases/bootstrap.cluster.x-k8s.io_kthreesconfigs.yaml +++ b/bootstrap/config/crd/bases/bootstrap.cluster.x-k8s.io_kthreesconfigs.yaml @@ -442,7 +442,6 @@ spec: description: 'BindAddress k3s bind address (default: 0.0.0.0)' type: string cloudProviderName: - default: external description: 'CloudProviderName defines the --cloud-provider= kubelet extra arg. (default: "external")' type: string @@ -458,7 +457,6 @@ spec: description: 'ClusterDomain Cluster Domain (default: "cluster.local")' type: string disableCloudController: - default: true description: 'DisableCloudController disables k3s default cloud controller manager. (default: true)' type: boolean diff --git a/bootstrap/config/crd/bases/bootstrap.cluster.x-k8s.io_kthreesconfigtemplates.yaml b/bootstrap/config/crd/bases/bootstrap.cluster.x-k8s.io_kthreesconfigtemplates.yaml index c07c8bc8..bea8c859 100644 --- a/bootstrap/config/crd/bases/bootstrap.cluster.x-k8s.io_kthreesconfigtemplates.yaml +++ b/bootstrap/config/crd/bases/bootstrap.cluster.x-k8s.io_kthreesconfigtemplates.yaml @@ -403,7 +403,6 @@ spec: description: 'BindAddress k3s bind address (default: 0.0.0.0)' type: string cloudProviderName: - default: external description: 'CloudProviderName defines the --cloud-provider= kubelet extra arg. (default: "external")' type: string @@ -419,7 +418,6 @@ spec: description: 'ClusterDomain Cluster Domain (default: "cluster.local")' type: string disableCloudController: - default: true description: 'DisableCloudController disables k3s default cloud controller manager. (default: true)' type: boolean diff --git a/controlplane/api/v1beta2/kthreescontrolplane_webhook.go b/controlplane/api/v1beta2/kthreescontrolplane_webhook.go index d8177074..d8f18c0d 100644 --- a/controlplane/api/v1beta2/kthreescontrolplane_webhook.go +++ b/controlplane/api/v1beta2/kthreescontrolplane_webhook.go @@ -22,6 +22,7 @@ import ( apierrors "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/runtime" + "k8s.io/utils/ptr" ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/webhook/admission" ) @@ -60,7 +61,7 @@ func (in *KThreesControlPlane) ValidateDelete(_ context.Context, _ runtime.Objec func (in *KThreesControlPlane) Default(_ context.Context, obj runtime.Object) error { c, ok := obj.(*KThreesControlPlane) if !ok { - return apierrors.NewBadRequest(fmt.Sprintf("expected a KubeadmConfig but got a %T", obj)) + return apierrors.NewBadRequest(fmt.Sprintf("expected a KThreesControlPlane but got a %T", obj)) } defaultKThreesControlPlaneSpec(&c.Spec, c.Namespace) @@ -76,4 +77,12 @@ func defaultKThreesControlPlaneSpec(s *KThreesControlPlaneSpec, namespace string if s.MachineTemplate.InfrastructureRef.Namespace == "" { s.MachineTemplate.InfrastructureRef.Namespace = namespace } + + if s.KThreesConfigSpec.ServerConfig.DisableCloudController == nil { + s.KThreesConfigSpec.ServerConfig.DisableCloudController = ptr.To(true) + } + + if s.KThreesConfigSpec.ServerConfig.CloudProviderName == nil { + s.KThreesConfigSpec.ServerConfig.CloudProviderName = ptr.To("external") + } } diff --git a/controlplane/config/crd/bases/controlplane.cluster.x-k8s.io_kthreescontrolplanes.yaml b/controlplane/config/crd/bases/controlplane.cluster.x-k8s.io_kthreescontrolplanes.yaml index c0488d0a..42a40faa 100644 --- a/controlplane/config/crd/bases/controlplane.cluster.x-k8s.io_kthreescontrolplanes.yaml +++ b/controlplane/config/crd/bases/controlplane.cluster.x-k8s.io_kthreescontrolplanes.yaml @@ -749,7 +749,6 @@ spec: description: 'BindAddress k3s bind address (default: 0.0.0.0)' type: string cloudProviderName: - default: external description: 'CloudProviderName defines the --cloud-provider= kubelet extra arg. (default: "external")' type: string @@ -765,7 +764,6 @@ spec: description: 'ClusterDomain Cluster Domain (default: "cluster.local")' type: string disableCloudController: - default: true description: 'DisableCloudController disables k3s default cloud controller manager. (default: true)' type: boolean diff --git a/controlplane/config/crd/bases/controlplane.cluster.x-k8s.io_kthreescontrolplanetemplates.yaml b/controlplane/config/crd/bases/controlplane.cluster.x-k8s.io_kthreescontrolplanetemplates.yaml index 9d984e20..c7d09398 100644 --- a/controlplane/config/crd/bases/controlplane.cluster.x-k8s.io_kthreescontrolplanetemplates.yaml +++ b/controlplane/config/crd/bases/controlplane.cluster.x-k8s.io_kthreescontrolplanetemplates.yaml @@ -188,7 +188,6 @@ spec: 0.0.0.0)' type: string cloudProviderName: - default: external description: 'CloudProviderName defines the --cloud-provider= kubelet extra arg. (default: "external")' type: string @@ -205,7 +204,6 @@ spec: "cluster.local")' type: string disableCloudController: - default: true description: 'DisableCloudController disables k3s default cloud controller manager. (default: true)' type: boolean diff --git a/pkg/k3s/config.go b/pkg/k3s/config.go index 7a5f0aa4..d2cdb141 100644 --- a/pkg/k3s/config.go +++ b/pkg/k3s/config.go @@ -42,7 +42,7 @@ type K3sAgentConfig struct { func GenerateInitControlPlaneConfig(controlPlaneEndpoint string, token string, serverConfig bootstrapv1.KThreesServerConfig, agentConfig bootstrapv1.KThreesAgentConfig) K3sServerConfig { kubeletExtraArgs := getKubeletExtraArgs(serverConfig) k3sServerConfig := K3sServerConfig{ - DisableCloudController: serverConfig.DisableCloudController, + DisableCloudController: getDisableCloudController(serverConfig), ClusterInit: true, KubeAPIServerArgs: append(serverConfig.KubeAPIServerArgs, "anonymous-auth=true", getTLSCipherSuiteArg()), TLSSan: append(serverConfig.TLSSan, controlPlaneEndpoint), @@ -75,7 +75,7 @@ func GenerateInitControlPlaneConfig(controlPlaneEndpoint string, token string, s func GenerateJoinControlPlaneConfig(serverURL string, token string, controlplaneendpoint string, serverConfig bootstrapv1.KThreesServerConfig, agentConfig bootstrapv1.KThreesAgentConfig) K3sServerConfig { kubeletExtraArgs := getKubeletExtraArgs(serverConfig) k3sServerConfig := K3sServerConfig{ - DisableCloudController: serverConfig.DisableCloudController, + DisableCloudController: getDisableCloudController(serverConfig), KubeAPIServerArgs: append(serverConfig.KubeAPIServerArgs, "anonymous-auth=true", getTLSCipherSuiteArg()), TLSSan: append(serverConfig.TLSSan, controlplaneendpoint), KubeControllerManagerArgs: append(serverConfig.KubeControllerManagerArgs, kubeletExtraArgs...), @@ -158,9 +158,16 @@ func getTLSCipherSuiteArg() string { func getKubeletExtraArgs(serverConfig bootstrapv1.KThreesServerConfig) []string { kubeletExtraArgs := []string{} - if len(serverConfig.CloudProviderName) > 0 { - cloudProviderArg := fmt.Sprintf("cloud-provider=%s", serverConfig.CloudProviderName) + if serverConfig.CloudProviderName != nil && len(*serverConfig.CloudProviderName) > 0 { + cloudProviderArg := fmt.Sprintf("cloud-provider=%s", *serverConfig.CloudProviderName) kubeletExtraArgs = append(kubeletExtraArgs, cloudProviderArg) } return kubeletExtraArgs } + +func getDisableCloudController(serverConfig bootstrapv1.KThreesServerConfig) bool { + if serverConfig.DisableCloudController == nil { + return true + } + return *serverConfig.DisableCloudController +}