Skip to content

Commit

Permalink
Merge pull request #2531 from gdbranco/fix/ocm-11560
Browse files Browse the repository at this point in the history
OCM-11560 | fix: allow trailing comma for taints/labels
  • Loading branch information
gdbranco authored Oct 8, 2024
2 parents 0177b97 + df3a4fb commit 33c4277
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
17 changes: 14 additions & 3 deletions pkg/helper/machinepools/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,13 @@ func ParseLabels(labels string) (map[string]string, error) {
if labels == "" || labels == interactiveModeEmptyLabels {
return labelMap, nil
}

for _, label := range strings.Split(labels, ",") {
possibleLabels := strings.Split(labels, ",")
for i, label := range possibleLabels {
// If it is empty and it is the last one
// can be disregarded to still continue with the ones up to it
if label == "" && i == len(possibleLabels)-1 {
continue
}
if !strings.Contains(label, "=") {
return nil, fmt.Errorf("Expected key=value format for labels")
}
Expand Down Expand Up @@ -105,7 +110,13 @@ func ParseTaints(taints string) ([]*cmv1.TaintBuilder, error) {
return taintBuilders, nil
}
var errs []error
for _, taint := range strings.Split(taints, ",") {
possibleTaints := strings.Split(taints, ",")
for i, taint := range possibleTaints {
// If it is empty and it is the last one
// can be disregarded to still continue with the ones up to it
if taint == "" && i == len(possibleTaints)-1 {
continue
}
if !strings.Contains(taint, "=") || !strings.Contains(taint, ":") {
return nil, fmt.Errorf("Expected key=value:scheduleType format for taints. Got '%s'", taint)
}
Expand Down
6 changes: 6 additions & 0 deletions pkg/helper/machinepools/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ var _ = Describe("MachinePool", func() {
Entry(
"2 well formed taints",
"node-role.kubernetes.io/infra=bar:NoSchedule,node-role.kubernetes.io/master=val:NoSchedule", "", 2),
Entry(
"Trailing ',' is parsed correctly",
"node-role.kubernetes.io/infra=bar:NoSchedule,node-role.kubernetes.io/master=val:NoSchedule,", "", 2),
Entry(
"Empty value taint bad format",
"node-role.kubernetes.io/infraNoSchedule",
Expand Down Expand Up @@ -76,6 +79,9 @@ var _ = Describe("MachinePool", func() {
Entry("Multiple labels are parsed correctly",
"com.example.foo=bar,com.example.baz=bob", "", 2,
),
Entry("Trailing ',' is parsed correctly",
"com.example.foo=bar,com.example.baz=bob,", "", 2,
),
Entry("Labels with no value are parsed correctly",
"com.example.foo=,com.example.baz=bob", "", 2,
),
Expand Down

0 comments on commit 33c4277

Please sign in to comment.