Skip to content

Commit

Permalink
Merge pull request #2276 from Nordix/fix-noccloudprovider/adil
Browse files Browse the repository at this point in the history
🌱 remove  kubebuilder:default=true for CloudProviderEnabled and set defaults in defaulting webhook
  • Loading branch information
metal3-io-bot authored Jan 27, 2025
2 parents 0fa7817 + 6ebd717 commit 59a0379
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 6 deletions.
2 changes: 1 addition & 1 deletion api/v1beta1/metal3cluster_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ type Metal3ClusterSpec struct {
// If set to false, CAPM3 will use node labels to set providerID on the kubernetes nodes.
// If set to true, providerID is set on nodes by other entities and CAPM3 uses the value of the providerID on the m3m resource.
// TODO: Change the default value to false in release 1.12. Ref: https://github.com/metal3-io/cluster-api-provider-metal3/issues/2255
// +kubebuilder:default=true
// Default value is true, it is set in the webhook.
// +optional
CloudProviderEnabled *bool `json:"cloudProviderEnabled,omitempty"`
}
Expand Down
16 changes: 14 additions & 2 deletions api/v1beta1/metal3cluster_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/validation/field"
"k8s.io/utils/ptr"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/webhook"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
Expand All @@ -39,6 +40,17 @@ func (c *Metal3Cluster) Default() {
if c.Spec.ControlPlaneEndpoint.Port == 0 {
c.Spec.ControlPlaneEndpoint.Port = 6443
}

if c.Spec.CloudProviderEnabled != nil && c.Spec.NoCloudProvider == nil {
c.Spec.NoCloudProvider = ptr.To(!*c.Spec.CloudProviderEnabled)
}
if c.Spec.CloudProviderEnabled == nil && c.Spec.NoCloudProvider != nil {
c.Spec.CloudProviderEnabled = ptr.To(!*c.Spec.NoCloudProvider)
}
if c.Spec.CloudProviderEnabled == nil && c.Spec.NoCloudProvider == nil {
c.Spec.CloudProviderEnabled = ptr.To(true)
c.Spec.NoCloudProvider = ptr.To(false)
}
}

// ValidateCreate implements webhook.Validator so a webhook will be registered for the type.
Expand Down Expand Up @@ -96,7 +108,7 @@ func (c *Metal3Cluster) validate(oldM3C *Metal3Cluster) error {
field.Invalid(
field.NewPath("spec", "cloudProviderEnabled"),
c.Spec.CloudProviderEnabled,
"cloudProviderEnabled conflicts the value of noCloudProvider",
"ValidateUpdate failed, cloudProviderEnabled conflicts the value of noCloudProvider",
),
)
}
Expand All @@ -110,7 +122,7 @@ func (c *Metal3Cluster) validate(oldM3C *Metal3Cluster) error {
field.Invalid(
field.NewPath("spec", "noCloudProvider"),
c.Spec.NoCloudProvider,
"noCloudProvider conflicts the value of cloudProviderEnabled",
"ValidateUpdate failed, noCloudProvider conflicts the value of cloudProviderEnabled",
),
)
}
Expand Down
2 changes: 1 addition & 1 deletion api/v1beta1/metal3cluster_webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func TestMetal3ClusterValidation(t *testing.T) {
Host: "abc.com",
Port: 443,
},
NoCloudProvider: ptr.To(false),
NoCloudProvider: ptr.To(true),
},
},
oldCluster: valid,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,11 @@ spec:
description: Metal3ClusterSpec defines the desired state of Metal3Cluster.
properties:
cloudProviderEnabled:
default: true
description: |-
Determines if the cluster is to be deployed with an external cloud provider.
If set to false, CAPM3 will use node labels to set providerID on the kubernetes nodes.
If set to true, providerID is set on nodes by other entities and CAPM3 uses the value of the providerID on the m3m resource.
Default value is true, it is set in the webhook.
type: boolean
controlPlaneEndpoint:
description: ControlPlaneEndpoint represents the endpoint used to
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,11 @@ spec:
description: Metal3ClusterSpec defines the desired state of Metal3Cluster.
properties:
cloudProviderEnabled:
default: true
description: |-
Determines if the cluster is to be deployed with an external cloud provider.
If set to false, CAPM3 will use node labels to set providerID on the kubernetes nodes.
If set to true, providerID is set on nodes by other entities and CAPM3 uses the value of the providerID on the m3m resource.
Default value is true, it is set in the webhook.
type: boolean
controlPlaneEndpoint:
description: ControlPlaneEndpoint represents the endpoint
Expand Down

0 comments on commit 59a0379

Please sign in to comment.