-
Notifications
You must be signed in to change notification settings - Fork 199
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
OCM-8075 | fix: Ensure users can only supply a single kubeletconfig f…
…or HCP MachinePools
- Loading branch information
Showing
6 changed files
with
120 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package machinepool | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/AlecAivazis/survey/v2/core" | ||
) | ||
|
||
func ValidateKubeletConfig(input interface{}) error { | ||
if strings, ok := input.([]string); ok { | ||
return validateCount(strings) | ||
} else if answers, ok := input.([]core.OptionAnswer); ok { | ||
return validateCount(answers) | ||
} | ||
|
||
return fmt.Errorf("Input for kubelet config flag is not valid") | ||
} | ||
|
||
func validateCount[K any](kubeletConfigs []K) error { | ||
if len(kubeletConfigs) > 1 { | ||
return fmt.Errorf("Only a single kubelet config is supported for Machine Pools") | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package machinepool | ||
|
||
import ( | ||
"github.com/AlecAivazis/survey/v2/core" | ||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
var _ = Describe("MachinePool validation", func() { | ||
Context("KubeletConfigs", func() { | ||
|
||
It("Fails if customer requests more than 1 kubelet config via []string", func() { | ||
kubeletConfigs := []string{"foo", "bar"} | ||
err := ValidateKubeletConfig(kubeletConfigs) | ||
Expect(err).To(HaveOccurred()) | ||
Expect(err.Error()).To(Equal("Only a single kubelet config is supported for Machine Pools")) | ||
}) | ||
|
||
It("Fails if customer requests more than 1 kubelet config via []core.OptionAnswer", func() { | ||
kubeletConfigs := []core.OptionAnswer{ | ||
{ | ||
Value: "foo", | ||
Index: 0, | ||
}, | ||
{ | ||
Value: "bar", | ||
Index: 1, | ||
}, | ||
} | ||
err := ValidateKubeletConfig(kubeletConfigs) | ||
Expect(err).To(HaveOccurred()) | ||
Expect(err.Error()).To(Equal("Only a single kubelet config is supported for Machine Pools")) | ||
}) | ||
|
||
It("Passes if a customer selects only a single kubelet config via []core.OptionAnswer", func() { | ||
kubeletConfigs := []core.OptionAnswer{ | ||
{ | ||
Value: "foo", | ||
Index: 0, | ||
}, | ||
} | ||
err := ValidateKubeletConfig(kubeletConfigs) | ||
Expect(err).NotTo(HaveOccurred()) | ||
}) | ||
|
||
It("Passes if a customer selects only a single kubelet config via []string", func() { | ||
kubeletConfigs := []string{"foo"} | ||
err := ValidateKubeletConfig(kubeletConfigs) | ||
Expect(err).NotTo(HaveOccurred()) | ||
}) | ||
|
||
It("Passes with empty selection via []string", func() { | ||
err := ValidateKubeletConfig([]string{}) | ||
Expect(err).NotTo(HaveOccurred()) | ||
}) | ||
|
||
It("Passes with empty selection via []core.OptionAnswer", func() { | ||
err := ValidateKubeletConfig([]core.OptionAnswer{}) | ||
Expect(err).NotTo(HaveOccurred()) | ||
}) | ||
|
||
It("Fails if the input is not a []string or []core.OptionAnswer", func() { | ||
err := ValidateKubeletConfig("foo") | ||
Expect(err).To(HaveOccurred()) | ||
Expect(err.Error()).To(Equal("Input for kubelet config flag is not valid")) | ||
}) | ||
}) | ||
}) |