Skip to content

Commit

Permalink
Add proper validation for nodepool name
Browse files Browse the repository at this point in the history
Signed-off-by: Tapajit Chandra Paul <[email protected]>
  • Loading branch information
tapojit047 committed Sep 24, 2023
1 parent b195e9a commit 66860ab
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 9 deletions.
54 changes: 50 additions & 4 deletions api/v1beta1/azuremanagedmachinepool_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"regexp"
"strconv"
"strings"
"unicode"

"github.com/pkg/errors"
apierrors "k8s.io/apimachinery/pkg/api/errors"
Expand Down Expand Up @@ -363,14 +364,59 @@ func (m *AzureManagedMachinePool) validateOSType() error {
}

func (m *AzureManagedMachinePool) validateName() error {
if m.Spec.OSType != nil && *m.Spec.OSType == WindowsOS &&
m.Spec.Name != nil && len(*m.Spec.Name) > 6 {
var name *string
var fieldNameMessage string
if m.Spec.Name == nil || *m.Spec.Name == "" {
name = &m.Name
fieldNameMessage = "when spec.name is empty, metadata.name"
} else {
name = m.Spec.Name
fieldNameMessage = "spec.name"
}

if err := validateNameLength(m.Spec.OSType, name, fieldNameMessage); err != nil {
return err
}
return validateNamePattern(name, fieldNameMessage)
}

func validateNameLength(osType *string, name *string, fieldNameMessage string) error {
if osType != nil && *osType == WindowsOS &&
name != nil && len(*name) > 6 {
return field.Invalid(
field.NewPath("Spec", "Name"),
name,
fmt.Sprintf("For OSType Windows, %s can not be longer than 6 characters.", fieldNameMessage))
} else if (osType == nil || *osType == LinuxOS) &&
(name != nil && len(*name) > 12) {
return field.Invalid(
field.NewPath("Spec", "Name"),
m.Spec.Name,
"Windows agent pool name can not be longer than 6 characters.")
osType,
fmt.Sprintf("For OSType Linux, %s can not be longer than 12 characters.", fieldNameMessage))

Check warning on line 395 in api/v1beta1/azuremanagedmachinepool_webhook.go

View check run for this annotation

Codecov / codecov/patch

api/v1beta1/azuremanagedmachinepool_webhook.go#L394-L395

Added lines #L394 - L395 were not covered by tests
}
return nil
}

func validateNamePattern(name *string, fieldNameMessage string) error {
if name == nil || *name == "" {
return nil
}

if !unicode.IsLower(rune((*name)[0])) {
return field.Invalid(
field.NewPath("Spec", "Name"),
name,
fmt.Sprintf("%s must begin with a lowercase letter.", fieldNameMessage))
}

Check warning on line 410 in api/v1beta1/azuremanagedmachinepool_webhook.go

View check run for this annotation

Codecov / codecov/patch

api/v1beta1/azuremanagedmachinepool_webhook.go#L406-L410

Added lines #L406 - L410 were not covered by tests

for _, char := range *name {
if !(unicode.IsLower(char) || unicode.IsNumber(char)) {
return field.Invalid(
field.NewPath("Spec", "Name"),
name,
fmt.Sprintf("%s may only contain lowercase alphanumeric characters.", fieldNameMessage))
}

Check warning on line 418 in api/v1beta1/azuremanagedmachinepool_webhook.go

View check run for this annotation

Codecov / codecov/patch

api/v1beta1/azuremanagedmachinepool_webhook.go#L414-L418

Added lines #L414 - L418 were not covered by tests
}
return nil
}

Expand Down
10 changes: 5 additions & 5 deletions api/v1beta1/azuremanagedmachinepool_webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func TestAzureManagedMachinePoolDefaultingWebhook(t *testing.T) {
t.Logf("Testing ammp defaulting webhook with mode system")
ammp := &AzureManagedMachinePool{
ObjectMeta: metav1.ObjectMeta{
Name: "fooName",
Name: "fooname",
},
Spec: AzureManagedMachinePoolSpec{
Mode: "System",
Expand All @@ -57,22 +57,22 @@ func TestAzureManagedMachinePoolDefaultingWebhook(t *testing.T) {
val, ok := ammp.Labels[LabelAgentPoolMode]
g.Expect(ok).To(BeTrue())
g.Expect(val).To(Equal("System"))
g.Expect(*ammp.Spec.Name).To(Equal("fooName"))
g.Expect(*ammp.Spec.Name).To(Equal("fooname"))
g.Expect(*ammp.Spec.OSType).To(Equal(LinuxOS))

t.Logf("Testing ammp defaulting webhook with empty string name specified in Spec")
emptyName := ""
ammp.Spec.Name = &emptyName
err = mw.Default(context.Background(), ammp)
g.Expect(err).NotTo(HaveOccurred())
g.Expect(*ammp.Spec.Name).To(Equal("fooName"))
g.Expect(*ammp.Spec.Name).To(Equal("fooname"))

t.Logf("Testing ammp defaulting webhook with normal name specified in Spec")
normalName := "barName"
normalName := "barname"
ammp.Spec.Name = &normalName
err = mw.Default(context.Background(), ammp)
g.Expect(err).NotTo(HaveOccurred())
g.Expect(*ammp.Spec.Name).To(Equal("barName"))
g.Expect(*ammp.Spec.Name).To(Equal("barname"))

t.Logf("Testing ammp defaulting webhook with normal OsDiskType specified in Spec")
normalOsDiskType := "Ephemeral"
Expand Down

0 comments on commit 66860ab

Please sign in to comment.