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

Add ClusterClass Support for Managed Clusters #4155

Merged
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
56 changes: 56 additions & 0 deletions api/v1beta1/azuremanagedclustertemplate_types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
Copyright 2023 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 v1beta1

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

// AzureManagedClusterTemplateSpec defines the desired state of AzureManagedClusterTemplate.
type AzureManagedClusterTemplateSpec struct {
Template AzureManagedClusterTemplateResource `json:"template"`
}

// +kubebuilder:object:root=true
// +kubebuilder:resource:path=azuremanagedclustertemplates,scope=Namespaced,categories=cluster-api,shortName=amct
// +kubebuilder:storageversion

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

Spec AzureManagedClusterTemplateSpec `json:"spec,omitempty"`
}

// +kubebuilder:object:root=true

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

func init() {
SchemeBuilder.Register(&AzureManagedClusterTemplate{}, &AzureManagedClusterTemplateList{})
}

// AzureManagedClusterTemplateResource describes the data needed to create an AzureManagedCluster from a template.
type AzureManagedClusterTemplateResource struct {
Spec AzureManagedClusterTemplateResourceSpec `json:"spec"`
}
61 changes: 61 additions & 0 deletions api/v1beta1/azuremanagedclustertemplate_webhook.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
Copyright 2023 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 v1beta1

import (
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/validation/field"
"sigs.k8s.io/cluster-api-provider-azure/feature"
capifeature "sigs.k8s.io/cluster-api/feature"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/webhook"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
)

// SetupWebhookWithManager sets up and registers the webhook with the manager.
func (r *AzureManagedClusterTemplate) SetupWebhookWithManager(mgr ctrl.Manager) error {
return ctrl.NewWebhookManagedBy(mgr).
For(r).
Complete()

Check warning on line 33 in api/v1beta1/azuremanagedclustertemplate_webhook.go

View check run for this annotation

Codecov / codecov/patch

api/v1beta1/azuremanagedclustertemplate_webhook.go#L30-L33

Added lines #L30 - L33 were not covered by tests
}

// +kubebuilder:webhook:verbs=update,path=/validate-infrastructure-cluster-x-k8s-io-v1beta1-azuremanagedclustertemplate,mutating=false,failurePolicy=fail,groups=infrastructure.cluster.x-k8s.io,resources=azuremanagedclustertemplates,versions=v1beta1,name=validation.azuremanagedclustertemplates.infrastructure.cluster.x-k8s.io,sideEffects=None,admissionReviewVersions=v1;v1beta1

var _ webhook.Validator = &AzureManagedClusterTemplate{}

// ValidateCreate implements webhook.Validator so a webhook will be registered for the type.
func (r *AzureManagedClusterTemplate) ValidateCreate() (admission.Warnings, error) {
// NOTE: AzureManagedClusterTemplate relies upon MachinePools, which is behind a feature gate flag.
// The webhook must prevent creating new objects in case the feature flag is disabled.
if !feature.Gates.Enabled(capifeature.MachinePool) {
return nil, field.Forbidden(
field.NewPath("spec"),
"cannot be set if the Cluster API 'MachinePool' feature flag is not enabled",
)
}
return nil, nil

Check warning on line 50 in api/v1beta1/azuremanagedclustertemplate_webhook.go

View check run for this annotation

Codecov / codecov/patch

api/v1beta1/azuremanagedclustertemplate_webhook.go#L41-L50

Added lines #L41 - L50 were not covered by tests
}

// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type.
func (r *AzureManagedClusterTemplate) ValidateUpdate(_ runtime.Object) (admission.Warnings, error) {
return nil, nil

Check warning on line 55 in api/v1beta1/azuremanagedclustertemplate_webhook.go

View check run for this annotation

Codecov / codecov/patch

api/v1beta1/azuremanagedclustertemplate_webhook.go#L54-L55

Added lines #L54 - L55 were not covered by tests
}

// ValidateDelete implements webhook.Validator so a webhook will be registered for the type.
func (r *AzureManagedClusterTemplate) ValidateDelete() (admission.Warnings, error) {
return nil, nil

Check warning on line 60 in api/v1beta1/azuremanagedclustertemplate_webhook.go

View check run for this annotation

Codecov / codecov/patch

api/v1beta1/azuremanagedclustertemplate_webhook.go#L59-L60

Added lines #L59 - L60 were not covered by tests
}
112 changes: 70 additions & 42 deletions api/v1beta1/azuremanagedcontrolplane_default.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,13 @@
import (
"encoding/base64"
"fmt"
"strings"

"golang.org/x/crypto/ssh"
"k8s.io/utils/ptr"
utilSSH "sigs.k8s.io/cluster-api-provider-azure/util/ssh"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
ctrl "sigs.k8s.io/controller-runtime"
)

const (
Expand All @@ -36,6 +39,15 @@
defaultAKSNodeSubnetCIDRForOverlay = "10.224.0.0/16"
)

// setDefaultResourceGroupName sets the default ResourceGroupName for an AzureManagedControlPlane.
func (m *AzureManagedControlPlane) setDefaultResourceGroupName() {
Copy link
Contributor

Choose a reason for hiding this comment

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

is this new defaulting?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes. I lost track of the exact part in the code this was needed, but the controlle expects there to be a resource group name set and caused the E2E test to fail otherwise. Will try and find the exact issue and update it here

if m.Spec.ResourceGroupName == "" {
if clusterName, ok := m.Labels[clusterv1.ClusterNameLabel]; ok {
m.Spec.ResourceGroupName = clusterName
}

Check warning on line 47 in api/v1beta1/azuremanagedcontrolplane_default.go

View check run for this annotation

Codecov / codecov/patch

api/v1beta1/azuremanagedcontrolplane_default.go#L45-L47

Added lines #L45 - L47 were not covered by tests
}
}

// setDefaultSSHPublicKey sets the default SSHPublicKey for an AzureManagedControlPlane.
func (m *AzureManagedControlPlane) setDefaultSSHPublicKey() error {
if sshKey := m.Spec.SSHPublicKey; sshKey != nil && *sshKey == "" {
Expand Down Expand Up @@ -86,73 +98,89 @@
}
}

func (m *AzureManagedControlPlane) setDefaultSku() {
if m.Spec.SKU == nil {
m.Spec.SKU = &AKSSku{
Tier: FreeManagedControlPlaneTier,
}
func setDefaultSku(sku *AKSSku) *AKSSku {
result := sku.DeepCopy()
if sku == nil {
result = new(AKSSku)
result.Tier = FreeManagedControlPlaneTier
} else if sku.Tier == PaidManagedControlPlaneTier {
result.Tier = StandardManagedControlPlaneTier
ctrl.Log.WithName("AzureManagedControlPlaneWebHookLogger").Info("Paid SKU tier is deprecated and has been replaced by Standard")
}
return result
}

func (m *AzureManagedControlPlane) setDefaultAutoScalerProfile() {
if m.Spec.AutoScalerProfile == nil {
return
func setDefaultVersion(version string) string {
if version != "" && !strings.HasPrefix(version, "v") {
normalizedVersion := "v" + version
version = normalizedVersion
}
return version
}

func setDefaultAutoScalerProfile(autoScalerProfile *AutoScalerProfile) *AutoScalerProfile {
willie-yao marked this conversation as resolved.
Show resolved Hide resolved
if autoScalerProfile == nil {
return nil
}

result := autoScalerProfile.DeepCopy()

// Default values are from https://learn.microsoft.com/en-us/azure/aks/cluster-autoscaler#using-the-autoscaler-profile
// If any values are set, they all need to be set.
if m.Spec.AutoScalerProfile.BalanceSimilarNodeGroups == nil {
m.Spec.AutoScalerProfile.BalanceSimilarNodeGroups = (*BalanceSimilarNodeGroups)(ptr.To(string(BalanceSimilarNodeGroupsFalse)))
if autoScalerProfile.BalanceSimilarNodeGroups == nil {
result.BalanceSimilarNodeGroups = (*BalanceSimilarNodeGroups)(ptr.To(string(BalanceSimilarNodeGroupsFalse)))
}
if m.Spec.AutoScalerProfile.Expander == nil {
m.Spec.AutoScalerProfile.Expander = (*Expander)(ptr.To(string(ExpanderRandom)))
if autoScalerProfile.Expander == nil {
result.Expander = (*Expander)(ptr.To(string(ExpanderRandom)))
}
if m.Spec.AutoScalerProfile.MaxEmptyBulkDelete == nil {
m.Spec.AutoScalerProfile.MaxEmptyBulkDelete = ptr.To("10")
if autoScalerProfile.MaxEmptyBulkDelete == nil {
result.MaxEmptyBulkDelete = ptr.To("10")
}
if m.Spec.AutoScalerProfile.MaxGracefulTerminationSec == nil {
m.Spec.AutoScalerProfile.MaxGracefulTerminationSec = ptr.To("600")
if autoScalerProfile.MaxGracefulTerminationSec == nil {
result.MaxGracefulTerminationSec = ptr.To("600")
}
if m.Spec.AutoScalerProfile.MaxNodeProvisionTime == nil {
m.Spec.AutoScalerProfile.MaxNodeProvisionTime = ptr.To("15m")
if autoScalerProfile.MaxNodeProvisionTime == nil {
result.MaxNodeProvisionTime = ptr.To("15m")
}
if m.Spec.AutoScalerProfile.MaxTotalUnreadyPercentage == nil {
m.Spec.AutoScalerProfile.MaxTotalUnreadyPercentage = ptr.To("45")
if autoScalerProfile.MaxTotalUnreadyPercentage == nil {
result.MaxTotalUnreadyPercentage = ptr.To("45")
}
if m.Spec.AutoScalerProfile.NewPodScaleUpDelay == nil {
m.Spec.AutoScalerProfile.NewPodScaleUpDelay = ptr.To("0s")
if autoScalerProfile.NewPodScaleUpDelay == nil {
result.NewPodScaleUpDelay = ptr.To("0s")
}
if m.Spec.AutoScalerProfile.OkTotalUnreadyCount == nil {
m.Spec.AutoScalerProfile.OkTotalUnreadyCount = ptr.To("3")
if autoScalerProfile.OkTotalUnreadyCount == nil {
result.OkTotalUnreadyCount = ptr.To("3")
}
if m.Spec.AutoScalerProfile.ScanInterval == nil {
m.Spec.AutoScalerProfile.ScanInterval = ptr.To("10s")
if autoScalerProfile.ScanInterval == nil {
result.ScanInterval = ptr.To("10s")
}
if m.Spec.AutoScalerProfile.ScaleDownDelayAfterAdd == nil {
m.Spec.AutoScalerProfile.ScaleDownDelayAfterAdd = ptr.To("10m")
if autoScalerProfile.ScaleDownDelayAfterAdd == nil {
result.ScaleDownDelayAfterAdd = ptr.To("10m")
}
if m.Spec.AutoScalerProfile.ScaleDownDelayAfterDelete == nil {
if autoScalerProfile.ScaleDownDelayAfterDelete == nil {
// Default is the same as the ScanInterval so default to that same value if it isn't set
m.Spec.AutoScalerProfile.ScaleDownDelayAfterDelete = m.Spec.AutoScalerProfile.ScanInterval
result.ScaleDownDelayAfterDelete = result.ScanInterval
}
if m.Spec.AutoScalerProfile.ScaleDownDelayAfterFailure == nil {
m.Spec.AutoScalerProfile.ScaleDownDelayAfterFailure = ptr.To("3m")
if autoScalerProfile.ScaleDownDelayAfterFailure == nil {
result.ScaleDownDelayAfterFailure = ptr.To("3m")
}
if m.Spec.AutoScalerProfile.ScaleDownUnneededTime == nil {
m.Spec.AutoScalerProfile.ScaleDownUnneededTime = ptr.To("10m")
if autoScalerProfile.ScaleDownUnneededTime == nil {
result.ScaleDownUnneededTime = ptr.To("10m")
}
if m.Spec.AutoScalerProfile.ScaleDownUnreadyTime == nil {
m.Spec.AutoScalerProfile.ScaleDownUnreadyTime = ptr.To("20m")
if autoScalerProfile.ScaleDownUnreadyTime == nil {
result.ScaleDownUnreadyTime = ptr.To("20m")
}
if m.Spec.AutoScalerProfile.ScaleDownUtilizationThreshold == nil {
m.Spec.AutoScalerProfile.ScaleDownUtilizationThreshold = ptr.To("0.5")
if autoScalerProfile.ScaleDownUtilizationThreshold == nil {
result.ScaleDownUtilizationThreshold = ptr.To("0.5")
}
if m.Spec.AutoScalerProfile.SkipNodesWithLocalStorage == nil {
m.Spec.AutoScalerProfile.SkipNodesWithLocalStorage = (*SkipNodesWithLocalStorage)(ptr.To(string(SkipNodesWithLocalStorageFalse)))
if autoScalerProfile.SkipNodesWithLocalStorage == nil {
result.SkipNodesWithLocalStorage = (*SkipNodesWithLocalStorage)(ptr.To(string(SkipNodesWithLocalStorageFalse)))
}
if m.Spec.AutoScalerProfile.SkipNodesWithSystemPods == nil {
m.Spec.AutoScalerProfile.SkipNodesWithSystemPods = (*SkipNodesWithSystemPods)(ptr.To(string(SkipNodesWithSystemPodsTrue)))
if autoScalerProfile.SkipNodesWithSystemPods == nil {
result.SkipNodesWithSystemPods = (*SkipNodesWithSystemPods)(ptr.To(string(SkipNodesWithSystemPodsTrue)))
}

return result
}

func (m *AzureManagedControlPlane) setDefaultOIDCIssuerProfile() {
Expand Down
88 changes: 48 additions & 40 deletions api/v1beta1/azuremanagedcontrolplane_default_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,69 +64,77 @@ func TestSetDefaultAutoScalerProfile(t *testing.T) {

defaultAMP := &AzureManagedControlPlane{
Spec: AzureManagedControlPlaneSpec{
AutoScalerProfile: &AutoScalerProfile{
BalanceSimilarNodeGroups: (*BalanceSimilarNodeGroups)(ptr.To(string(BalanceSimilarNodeGroupsFalse))),
Expander: (*Expander)(ptr.To(string(ExpanderRandom))),
MaxEmptyBulkDelete: ptr.To("10"),
MaxGracefulTerminationSec: ptr.To("600"),
MaxNodeProvisionTime: ptr.To("15m"),
MaxTotalUnreadyPercentage: ptr.To("45"),
NewPodScaleUpDelay: ptr.To("0s"),
OkTotalUnreadyCount: ptr.To("3"),
ScanInterval: ptr.To("10s"),
ScaleDownDelayAfterAdd: ptr.To("10m"),
ScaleDownDelayAfterDelete: ptr.To("10s"),
ScaleDownDelayAfterFailure: ptr.To("3m"),
ScaleDownUnneededTime: ptr.To("10m"),
ScaleDownUnreadyTime: ptr.To("20m"),
ScaleDownUtilizationThreshold: ptr.To("0.5"),
SkipNodesWithLocalStorage: (*SkipNodesWithLocalStorage)(ptr.To(string(SkipNodesWithLocalStorageFalse))),
SkipNodesWithSystemPods: (*SkipNodesWithSystemPods)(ptr.To(string(SkipNodesWithSystemPodsTrue))),
AzureManagedControlPlaneClassSpec: AzureManagedControlPlaneClassSpec{
AutoScalerProfile: &AutoScalerProfile{
BalanceSimilarNodeGroups: (*BalanceSimilarNodeGroups)(ptr.To(string(BalanceSimilarNodeGroupsFalse))),
Expander: (*Expander)(ptr.To(string(ExpanderRandom))),
MaxEmptyBulkDelete: ptr.To("10"),
MaxGracefulTerminationSec: ptr.To("600"),
MaxNodeProvisionTime: ptr.To("15m"),
MaxTotalUnreadyPercentage: ptr.To("45"),
NewPodScaleUpDelay: ptr.To("0s"),
OkTotalUnreadyCount: ptr.To("3"),
ScanInterval: ptr.To("10s"),
ScaleDownDelayAfterAdd: ptr.To("10m"),
ScaleDownDelayAfterDelete: ptr.To("10s"),
ScaleDownDelayAfterFailure: ptr.To("3m"),
ScaleDownUnneededTime: ptr.To("10m"),
ScaleDownUnreadyTime: ptr.To("20m"),
ScaleDownUtilizationThreshold: ptr.To("0.5"),
SkipNodesWithLocalStorage: (*SkipNodesWithLocalStorage)(ptr.To(string(SkipNodesWithLocalStorageFalse))),
SkipNodesWithSystemPods: (*SkipNodesWithSystemPods)(ptr.To(string(SkipNodesWithSystemPodsTrue))),
},
},
},
}

allFieldsAreNilTest := test{amcp: &AzureManagedControlPlane{
Spec: AzureManagedControlPlaneSpec{
AutoScalerProfile: &AutoScalerProfile{},
AzureManagedControlPlaneClassSpec: AzureManagedControlPlaneClassSpec{
AutoScalerProfile: &AutoScalerProfile{},
},
},
}}

allFieldsAreNilTest.amcp.setDefaultAutoScalerProfile()
allFieldsAreNilTest.amcp.Spec.AutoScalerProfile = setDefaultAutoScalerProfile(allFieldsAreNilTest.amcp.Spec.AutoScalerProfile)

g.Expect(allFieldsAreNilTest.amcp.Spec.AutoScalerProfile).To(Equal(defaultAMP.Spec.AutoScalerProfile))

expectedNotNil := &AzureManagedControlPlane{
Spec: AzureManagedControlPlaneSpec{
AutoScalerProfile: &AutoScalerProfile{
BalanceSimilarNodeGroups: (*BalanceSimilarNodeGroups)(ptr.To(string(BalanceSimilarNodeGroupsTrue))),
Expander: (*Expander)(ptr.To(string(ExpanderLeastWaste))),
MaxEmptyBulkDelete: ptr.To("5"),
MaxGracefulTerminationSec: ptr.To("300"),
MaxNodeProvisionTime: ptr.To("10m"),
MaxTotalUnreadyPercentage: ptr.To("30"),
NewPodScaleUpDelay: ptr.To("30s"),
OkTotalUnreadyCount: ptr.To("5"),
ScanInterval: ptr.To("20s"),
ScaleDownDelayAfterAdd: ptr.To("5m"),
ScaleDownDelayAfterDelete: ptr.To("1m"),
ScaleDownDelayAfterFailure: ptr.To("2m"),
ScaleDownUnneededTime: ptr.To("5m"),
ScaleDownUnreadyTime: ptr.To("10m"),
ScaleDownUtilizationThreshold: ptr.To("0.4"),
SkipNodesWithLocalStorage: (*SkipNodesWithLocalStorage)(ptr.To(string(SkipNodesWithLocalStorageTrue))),
SkipNodesWithSystemPods: (*SkipNodesWithSystemPods)(ptr.To(string(SkipNodesWithSystemPodsFalse))),
AzureManagedControlPlaneClassSpec: AzureManagedControlPlaneClassSpec{
AutoScalerProfile: &AutoScalerProfile{
BalanceSimilarNodeGroups: (*BalanceSimilarNodeGroups)(ptr.To(string(BalanceSimilarNodeGroupsTrue))),
Expander: (*Expander)(ptr.To(string(ExpanderLeastWaste))),
MaxEmptyBulkDelete: ptr.To("5"),
MaxGracefulTerminationSec: ptr.To("300"),
MaxNodeProvisionTime: ptr.To("10m"),
MaxTotalUnreadyPercentage: ptr.To("30"),
NewPodScaleUpDelay: ptr.To("30s"),
OkTotalUnreadyCount: ptr.To("5"),
ScanInterval: ptr.To("20s"),
ScaleDownDelayAfterAdd: ptr.To("5m"),
ScaleDownDelayAfterDelete: ptr.To("1m"),
ScaleDownDelayAfterFailure: ptr.To("2m"),
ScaleDownUnneededTime: ptr.To("5m"),
ScaleDownUnreadyTime: ptr.To("10m"),
ScaleDownUtilizationThreshold: ptr.To("0.4"),
SkipNodesWithLocalStorage: (*SkipNodesWithLocalStorage)(ptr.To(string(SkipNodesWithLocalStorageTrue))),
SkipNodesWithSystemPods: (*SkipNodesWithSystemPods)(ptr.To(string(SkipNodesWithSystemPodsFalse))),
},
},
},
}

allFieldsAreNotNilTest := test{amcp: &AzureManagedControlPlane{
Spec: AzureManagedControlPlaneSpec{
AutoScalerProfile: expectedNotNil.Spec.AutoScalerProfile,
AzureManagedControlPlaneClassSpec: AzureManagedControlPlaneClassSpec{
AutoScalerProfile: ptr.To(*expectedNotNil.Spec.AutoScalerProfile),
},
},
}}

allFieldsAreNotNilTest.amcp.setDefaultAutoScalerProfile()
allFieldsAreNotNilTest.amcp.Spec.AutoScalerProfile = setDefaultAutoScalerProfile(allFieldsAreNotNilTest.amcp.Spec.AutoScalerProfile)

g.Expect(allFieldsAreNotNilTest.amcp.Spec.AutoScalerProfile).To(Equal(expectedNotNil.Spec.AutoScalerProfile))
willie-yao marked this conversation as resolved.
Show resolved Hide resolved
}
Loading