Skip to content

Commit

Permalink
Use CustomDefaulter to default KThreesControlPlane spec (#121)
Browse files Browse the repository at this point in the history
* Use CustomDefaulter to default KThreesControlPlane spec

Signed-off-by: Andrea Mazzotti <[email protected]>

* Fix wrong comment reference

Signed-off-by: Andrea Mazzotti <[email protected]>

* Add KThreesConfig defaults

Signed-off-by: Andrea Mazzotti <[email protected]>

* Fix wrong comment reference

Signed-off-by: Andrea Mazzotti <[email protected]>

---------

Signed-off-by: Andrea Mazzotti <[email protected]>
  • Loading branch information
anmazzotti authored Jun 4, 2024
1 parent c4b652c commit 888216c
Show file tree
Hide file tree
Showing 11 changed files with 62 additions and 30 deletions.
9 changes: 5 additions & 4 deletions bootstrap/api/v1beta1/conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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
Expand Down
6 changes: 2 additions & 4 deletions bootstrap/api/v1beta2/kthreesconfig_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
25 changes: 20 additions & 5 deletions bootstrap/api/v1beta2/kthreesconfig_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand All @@ -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
}
Expand All @@ -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
}
8 changes: 4 additions & 4 deletions bootstrap/api/v1beta2/kthreesconfigtemplate_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand All @@ -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
}
Expand All @@ -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
}
10 changes: 10 additions & 0 deletions bootstrap/api/v1beta2/zz_generated.deepcopy.go

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
Expand Up @@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
11 changes: 10 additions & 1 deletion controlplane/api/v1beta2/kthreescontrolplane_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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)
Expand All @@ -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")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -205,7 +204,6 @@ spec:
"cluster.local")'
type: string
disableCloudController:
default: true
description: 'DisableCloudController disables k3s
default cloud controller manager. (default: true)'
type: boolean
Expand Down
15 changes: 11 additions & 4 deletions pkg/k3s/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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...),
Expand Down Expand Up @@ -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
}

0 comments on commit 888216c

Please sign in to comment.