Skip to content

Commit

Permalink
api: make command a single string
Browse files Browse the repository at this point in the history
This is for compatibility with other consumers of recipes.

Signed-off-by: Raghavendra Talur <raghavendra.talur@gmail.com>
Co-Authored-by: Jose A. Rivera <jarrpa@redhat.com>
  • Loading branch information
2 people authored and ShyamsundarR committed Sep 18, 2024
1 parent bd385d7 commit aea39dd
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 18 deletions.
4 changes: 2 additions & 2 deletions api/v1alpha1/recipe_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,8 @@ type Operation struct {
// The container where the command should be executed
Container string `json:"container,omitempty"`
// The command to execute
//+kubebuilder:validation:MinItems=1
Command []string `json:"command"`
//+kubebuilder:validation:MinLength=1
Command string `json:"command"`
// How to handle command returning with non-zero exit code. Defaults to Fail.
OnError string `json:"onError,omitempty"`
// How long to wait for the command to execute, in seconds
Expand Down
45 changes: 29 additions & 16 deletions controllers/recipe_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,20 +133,19 @@ var _ = Describe("RecipeController", func() {

Expect(err).ToNot(BeNil())
})
echoCommand := func(s string) []string {
return []string{"/bin/sh", "-c", "echo", s}
}
emptyCommand := func(string) []string {
return []string{}

echoCommandTemplate := "/bin/sh/ -c echo"
emptyCommandTemplate := ""

commandGenerator := func(commandTemplate, suffix string) string {
return commandTemplate + suffix
}
hookOps := func(command func(string) []string, opNames ...string) []*Recipe.Operation {
ops := make([]*Recipe.Operation, len(opNames))
for i, opName := range opNames {
ops[i] = &Recipe.Operation{Name: opName, Command: command(opName)}
}

return ops
appendOp := func(ops []*Recipe.Operation, opName, commandTemplate, suffix string) []*Recipe.Operation {
op := &Recipe.Operation{Name: opName, Command: commandGenerator(commandTemplate, suffix)}
return append(ops, op)
}

hookOpsRecipe := func(ops []*Recipe.Operation) *Recipe.Recipe {
return &Recipe.Recipe{
TypeMeta: metav1.TypeMeta{Kind: "Recipe", APIVersion: "ramendr.openshift.io/v1alpha1"},
Expand All @@ -164,17 +163,21 @@ var _ = Describe("RecipeController", func() {
}
}
It("error on empty command", func() {
recipe := hookOpsRecipe(hookOps(emptyCommand, "op-1"))
ops := []*Recipe.Operation{}

ops = appendOp(ops, "op-1", emptyCommandTemplate, "")

recipe := hookOpsRecipe(ops)
Expect(k8sClient.Create(context.TODO(), recipe)).To(MatchError(func() *errors.StatusError {
path := field.NewPath("spec", "hooks[0]", "ops[0]", "command")
value := 0
value := ""

return errors.NewInvalid(
schema.GroupKind{Group: Recipe.GroupVersion.Group, Kind: "Recipe"},
recipe.Name,
field.ErrorList{
field.Invalid(
path, value, validationErrors.TooFewItems(
path, value, validationErrors.TooShort(
path.String(),
"body",
1,
Expand All @@ -186,12 +189,22 @@ var _ = Describe("RecipeController", func() {
}()))
})
It("allow unique Ops names", func() {
err := k8sClient.Create(context.TODO(), hookOpsRecipe(hookOps(echoCommand, "op-1", "op-2")))
ops := []*Recipe.Operation{}

ops = appendOp(ops, "op-1", echoCommandTemplate, "aaa")
ops = appendOp(ops, "op-2", echoCommandTemplate, "bbb")

err := k8sClient.Create(context.TODO(), hookOpsRecipe(ops))

Expect(err).To(BeNil())
})
It("error on duplicate Ops names", func() {
err := k8sClient.Create(context.TODO(), hookOpsRecipe(hookOps(echoCommand, "op-1", "op-1")))
ops := []*Recipe.Operation{}

ops = appendOp(ops, "op-1", echoCommandTemplate, "aaa")
ops = appendOp(ops, "op-1", echoCommandTemplate, "bbb")

err := k8sClient.Create(context.TODO(), hookOpsRecipe(ops))

Expect(err).ToNot(BeNil())
})
Expand Down

0 comments on commit aea39dd

Please sign in to comment.