Skip to content

Commit

Permalink
OCM-10009 | feat: Adding taints effect validation
Browse files Browse the repository at this point in the history
  • Loading branch information
den-rgb committed Jul 31, 2024
1 parent 4e4a45f commit b9424e0
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 1 deletion.
24 changes: 24 additions & 0 deletions pkg/helper/machinepools/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package machinepools
import (
"fmt"
"os"
"slices"
"strconv"
"strings"

Expand Down Expand Up @@ -31,6 +32,12 @@ const (
MaxNodeDrainTimeInHours = 168
)

var allowedTaintEffects = []string{
"NoSchedule",
"NoExecute",
"PreferNoSchedule",
}

func MinNodePoolReplicaValidator(autoscaling bool) interactive.Validator {
return func(val interface{}) error {
minReplicas, err := strconv.Atoi(fmt.Sprintf("%v", val))
Expand Down Expand Up @@ -149,6 +156,10 @@ func ParseTaints(taints string) ([]*cmv1.TaintBuilder, error) {
errs = append(errs, fmt.Errorf("Expected a not empty effect"))
continue
}
if err := validateMachinePoolTaintEffect(taint); err != nil {
errs = append(errs, err)
continue
}
taintBuilders = append(taintBuilders, newTaintBuilder)
}

Expand All @@ -159,6 +170,19 @@ func ParseTaints(taints string) ([]*cmv1.TaintBuilder, error) {
return taintBuilders, nil
}

func validateMachinePoolTaintEffect(taint string) error {
parts := strings.Split(taint, ":")
if len(parts) != 2 {
return fmt.Errorf("Invalid taint format: '%s'. Expected format is '<key>=<value>:<effect>'", taint)
}
effect := parts[1]
if !slices.Contains(allowedTaintEffects, effect) {
return fmt.Errorf("Invalid taint effect '%s', only the following effects are supported:"+
" 'NoExecute', 'NoSchedule', 'PreferNoSchedule'", effect)
}
return nil
}

func ValidateTaintKeyValuePair(key, value string) error {
return ValidateKeyValuePair(key, value, "taint")
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/helper/machinepools/helpers_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
. "github.com/onsi/gomega"
)

func TestIdp(t *testing.T) {
func TestMachinePools(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "MachinePools Suite")
}
17 changes: 17 additions & 0 deletions pkg/helper/machinepools/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,23 @@ var _ = Describe("Validate node drain grace period", func() {
)
})

var _ = Describe("ValidateMachinePoolTaintEffect", func() {
It("should return nil for recognized taint effects", func() {
Expect(validateMachinePoolTaintEffect("key=value:NoSchedule")).To(Succeed())
Expect(validateMachinePoolTaintEffect("key=value:NoExecute")).To(Succeed())
Expect(validateMachinePoolTaintEffect("key=value:PreferNoSchedule")).To(Succeed())
})

It("should return an error for unrecognized taint effects", func() {
Expect(validateMachinePoolTaintEffect("key=value:unrecognized")).To(MatchError(
MatchRegexp("Invalid taint effect 'unrecognized', only the following" +
" effects are supported: 'NoExecute', 'NoSchedule', 'PreferNoSchedule'")))
Expect(validateMachinePoolTaintEffect("key=value:unrecognized:")).To(MatchError(
MatchRegexp("Invalid taint format: 'key=value:unrecognized:'. Expected format" +
" is '<key>=<value>:<effect>'")))
})
})

var _ = Describe("Validate MaxSurge and MaxUnavailable", func() {
DescribeTable("Validate MaxSurge and MaxUnavailable",
func(value interface{}, errMsg string) {
Expand Down

0 comments on commit b9424e0

Please sign in to comment.