-
Notifications
You must be signed in to change notification settings - Fork 104
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Error out in instance controller when a parameter has an undefined pl…
…an trigger (#1336) * Verify plans are referenced and referenced plans are defined * error out in instance controller when a parameter has an undefined plan trigger Signed-off-by: Andreas Neumann <aneumann@mesosphere.com> Signed-off-by: Thomas Runyon <runyontr@gmail.com>
- Loading branch information
1 parent
ff98bcd
commit 6051a14
Showing
7 changed files
with
168 additions
and
11 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
Warnings | ||
plan "validation" defined but not used | ||
parameter "Cpus" defined but not used. | ||
parameter "comma," defined but not used. | ||
Errors | ||
parameter "Cpus" has a duplicate | ||
parameter "comma," contains invalid character ',' | ||
Errors | ||
parameter "Cpus" has a duplicate | ||
parameter "comma," contains invalid character ',' | ||
plan "not-existing-plan" used in parameter "comma," is not defined |
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,60 @@ | ||
package plan | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/kudobuilder/kudo/pkg/apis/kudo/v1beta1" | ||
"github.com/kudobuilder/kudo/pkg/kudoctl/packages" | ||
"github.com/kudobuilder/kudo/pkg/kudoctl/packages/verifier" | ||
) | ||
|
||
// ReferenceVerifier verifies plans producing errors for plans referenced in param triggers that do not exist and warnings for plans which are not used in a param | ||
type ReferenceVerifier struct{} | ||
|
||
func (ReferenceVerifier) Verify(pf *packages.Files) verifier.Result { | ||
res := verifier.NewResult() | ||
res.Merge(plansNotDefined(pf)) | ||
res.Merge(plansDefinedNotUsed(pf)) | ||
|
||
return res | ||
} | ||
|
||
func plansDefinedNotUsed(pf *packages.Files) verifier.Result { | ||
res := verifier.NewResult() | ||
usedPlans := make(map[string]bool) | ||
|
||
// Mark reserved plan names as used | ||
for _, plan := range v1beta1.ReservedPlanNames { | ||
usedPlans[plan] = true | ||
} | ||
|
||
// Mark plans in param triggers as used | ||
for _, param := range pf.Params.Parameters { | ||
if param.Trigger != "" { | ||
usedPlans[param.Trigger] = true | ||
} | ||
} | ||
|
||
for name := range pf.Operator.Plans { | ||
if _, ok := usedPlans[name]; !ok { | ||
res.AddWarnings(fmt.Sprintf("plan %q defined but not used", name)) | ||
} | ||
} | ||
|
||
return res | ||
} | ||
|
||
func plansNotDefined(pf *packages.Files) verifier.Result { | ||
res := verifier.NewResult() | ||
plans := pf.Operator.Plans | ||
|
||
for _, param := range pf.Params.Parameters { | ||
if param.Trigger != "" { | ||
if _, ok := plans[param.Trigger]; !ok { | ||
res.AddErrors(fmt.Sprintf("plan %q used in parameter %q is not defined", param.Trigger, param.Name)) | ||
} | ||
} | ||
} | ||
|
||
return res | ||
} |
76 changes: 76 additions & 0 deletions
76
pkg/kudoctl/packages/verifier/plan/verify_references_test.go
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,76 @@ | ||
package plan | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/kudobuilder/kudo/pkg/apis/kudo/v1beta1" | ||
"github.com/kudobuilder/kudo/pkg/kudoctl/packages" | ||
) | ||
|
||
func TestPlanReferenceVerifier(t *testing.T) { | ||
|
||
resources := []string{"sally.yaml"} | ||
tasks := []v1beta1.Task{{ | ||
Name: "thingOne", | ||
Kind: "Apply", | ||
Spec: v1beta1.TaskSpec{ | ||
ResourceTaskSpec: v1beta1.ResourceTaskSpec{Resources: resources}, | ||
}, | ||
}} | ||
|
||
steps := []v1beta1.Step{{ | ||
Name: "cat-in-hat", | ||
Tasks: []string{"thingOne"}, | ||
}} | ||
|
||
phases := []v1beta1.Phase{{ | ||
Name: "parents leave", | ||
Strategy: "serial", | ||
Steps: steps, | ||
}} | ||
|
||
plans := make(map[string]v1beta1.Plan) | ||
plans["deploy"] = v1beta1.Plan{ | ||
Strategy: "serial", | ||
Phases: phases, | ||
} | ||
plans["used-plan"] = v1beta1.Plan{ | ||
Strategy: "serial", | ||
Phases: phases, | ||
} | ||
plans["unused-plan"] = v1beta1.Plan{ | ||
Strategy: "serial", | ||
Phases: phases, | ||
} | ||
|
||
operator := packages.OperatorFile{ | ||
Tasks: tasks, | ||
Plans: plans, | ||
} | ||
params := packages.ParamsFile{ | ||
Parameters: packages.Parameter{ | ||
v1beta1.Parameter{ | ||
Name: "PARAM1", | ||
Trigger: "used-plan", | ||
}, | ||
v1beta1.Parameter{ | ||
Name: "PARAM2", | ||
Trigger: "not-existing-plan", | ||
}, | ||
}, | ||
} | ||
|
||
pf := packages.Files{ | ||
Operator: &operator, | ||
Params: ¶ms, | ||
} | ||
verifier := ReferenceVerifier{} | ||
res := verifier.Verify(&pf) | ||
|
||
assert.Equal(t, 1, len(res.Warnings)) | ||
assert.Equal(t, `plan "unused-plan" defined but not used`, res.Warnings[0]) | ||
assert.Equal(t, 1, len(res.Errors)) | ||
assert.Equal(t, `plan "not-existing-plan" used in parameter "PARAM2" is not defined`, res.Errors[0]) | ||
} |