Skip to content

Commit

Permalink
Add default valid nodepool name generator and Update validator for th…
Browse files Browse the repository at this point in the history
…e nodepool name

Signed-off-by: Tapajit Chandra Paul <[email protected]>
Signed-off-by: tapojit047 <[email protected]>
  • Loading branch information
tapojit047 committed Sep 9, 2023
1 parent 5ea468e commit 20b341d
Showing 1 changed file with 51 additions and 2 deletions.
53 changes: 51 additions & 2 deletions api/v1beta1/azuremanagedmachinepool_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,13 @@ import (
"regexp"
"strconv"
"strings"
"unicode"

"github.com/pkg/errors"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
kerrors "k8s.io/apimachinery/pkg/util/errors"
utilrand "k8s.io/apimachinery/pkg/util/rand"
"k8s.io/apimachinery/pkg/util/validation/field"
"k8s.io/utils/ptr"
"sigs.k8s.io/cluster-api-provider-azure/feature"
Expand Down Expand Up @@ -72,16 +74,34 @@ func (mw *azureManagedMachinePoolWebhook) Default(ctx context.Context, obj runti
m.Labels[LabelAgentPoolMode] = m.Spec.Mode

if m.Spec.Name == nil || *m.Spec.Name == "" {
m.Spec.Name = &m.Name
aksNodePoolName := generateAKSNodePoolName(m.Name)
m.Spec.Name = &aksNodePoolName
}

if m.Spec.OSType == nil {
m.Spec.OSType = ptr.To(DefaultOSType)
}

return nil
}

// Generates a valid unique nodepool name following limitations of Azure.
func generateAKSNodePoolName(name string) string {
name = strings.ToLower(name)
name = strings.Map(func(r rune) rune {
if unicode.IsNumber(r) || unicode.IsLetter(r) {
return r
}
return -1
}, name)
name = strings.TrimLeftFunc(name, unicode.IsNumber)
if len(name) > 6 {
name = name[:6]
} else if name == "" {
name = "pool"
}
return fmt.Sprintf("%s%s", name, utilrand.String(6))
}

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

// ValidateCreate implements webhook.Validator so a webhook will be registered for the type.
Expand Down Expand Up @@ -371,6 +391,35 @@ func (m *AzureManagedMachinePool) validateName() error {
"Windows agent pool name can not be longer than 6 characters.")
}

// Length check for linux following restrictions of AKS.
if (m.Spec.OSType == nil || *m.Spec.OSType == LinuxOS) &&
(m.Spec.Name != nil && len(*m.Spec.Name) > 12) {
return field.Invalid(
field.NewPath("Spec", "Name"),
m.Spec.Name,
"Linux agent pool name can not be longer than 12 characters.")
}

// Common Checks for both Windows & Linux following restrictions of AKS.
if m.Spec.Name != nil {
for _, char := range *m.Spec.Name {
if !unicode.IsLower(char) && !unicode.IsNumber(char) {
return field.Invalid(
field.NewPath("Spec", "Name"),
m.Spec.Name,
"Agent pool name may only contain lowercase alphanumeric characters.")
}
}

nodePoolName := *m.Spec.Name
if !unicode.IsLower(rune(nodePoolName[0])) {
return field.Invalid(
field.NewPath("Spec", "Name"),
m.Spec.Name,
"Linux agent pool name must begin with a lowercase letter.")
}
}

return nil
}

Expand Down

0 comments on commit 20b341d

Please sign in to comment.