Skip to content

Commit

Permalink
merge(#3592): node pool name validation
Browse files Browse the repository at this point in the history
Added node pool name validation
  • Loading branch information
akijakya authored Nov 19, 2021
2 parents ddc0081 + 5dc676c commit 0f95b8a
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
5 changes: 5 additions & 0 deletions internal/cluster/distribution/eks/ekscluster/eks.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,11 @@ const (

// Validate checks Amazon's node fields
func (a *NodePool) Validate(npName string) error {
// ---- [ Node pool name validation ] ---- //
if err := pkgCommon.ValidateNodePoolName(npName); err != nil {
return err
}

// ---- [ Node instanceType check ] ---- //
if len(a.InstanceType) == 0 {
return pkgErrors.ErrorInstancetypeFieldIsEmpty
Expand Down
21 changes: 21 additions & 0 deletions pkg/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package common
import (
"fmt"
"net/http"
"regexp"
"strings"
"time"

Expand Down Expand Up @@ -71,6 +72,14 @@ const (
SpotConfigMapKey = "spot-deploy-config"
)

// nodePoolNameRegexp is matching a valid node pool name
// (only lower case alphanumeric characters and single dashes within the string)
//
// Regular expression: https://regex101.com/r/OrRrlw/1
var (
nodePoolNameRegexp = regexp.MustCompile(`^[a-z0-9]([a-z0-9]*[a-z0-9])?(-[a-z0-9]([a-z0-9]*[a-z0-9])?)*$`) // nolint:gochecknoglobals
)

// ErrorResponseWithStatus aborts the http request with a JSON error response with the given status code and error
func ErrorResponseWithStatus(c *gin.Context, status int, err error) {
if c.Writer.Status() != http.StatusOK {
Expand Down Expand Up @@ -103,3 +112,15 @@ func ValidateNodePoolLabels(nodePoolName string, labels map[string]string) error

return nil
}

// ValidateNodePoolName checks the length and validity of the node pool name
func ValidateNodePoolName(nodePoolName string) error {
if len(nodePoolName) > 63 {
return errors.New(fmt.Sprintf("node pool name '%s' too long: over 63 characters", nodePoolName))
}

if nodePoolNameRegexp.MatchString(nodePoolName) {
return nil
}
return errors.New(fmt.Sprintf("node pool name '%s' invalid: must consist of lower case alphanumeric characters and single dashes within the string", nodePoolName))
}

0 comments on commit 0f95b8a

Please sign in to comment.