-
Notifications
You must be signed in to change notification settings - Fork 152
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support blueprint resource for validate sub command to
kanctl
(#1187)
* Support blueprint resource for validate subcommand to kanctl * Validate the mandatory arguments of a function * Add comment for function name checks * Add unit tests * Add license header in new files * Fix lint issues in CI * Refactor render_test.go to fix CI * Address review comments * Add another flag in validate blueprint to specify functionVersion * Make funcitonVersion flag optional * Fix test that failed in CI * Address minor review comment Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
- Loading branch information
1 parent
bfe366c
commit e5a2867
Showing
12 changed files
with
323 additions
and
38 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 |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// Copyright 2022 The Kanister Authors. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package validate | ||
|
||
import ( | ||
"fmt" | ||
|
||
kanister "github.com/kanisterio/kanister/pkg" | ||
crv1alpha1 "github.com/kanisterio/kanister/pkg/apis/cr/v1alpha1" | ||
_ "github.com/kanisterio/kanister/pkg/function" | ||
"github.com/kanisterio/kanister/pkg/param" | ||
"github.com/kanisterio/kanister/pkg/utils" | ||
) | ||
|
||
// Do takes a blueprint and validates if the function names in phases are correct | ||
// and all the required arguments for the kanister functions are provided. This doesn't | ||
// check anything with template params yet. | ||
func Do(bp *crv1alpha1.Blueprint, funcVersion string) error { | ||
for name, action := range bp.Actions { | ||
// GetPhases also checks if the function names referred in the action are correct | ||
phases, err := kanister.GetPhases(*bp, name, funcVersion, param.TemplateParams{}) | ||
if err != nil { | ||
utils.PrintStage(fmt.Sprintf("validation of action %s", name), utils.Fail) | ||
return err | ||
} | ||
|
||
for i, phase := range phases { | ||
// validate function's mandatory arguments | ||
if err := phase.Validate(action.Phases[i].Args); err != nil { | ||
utils.PrintStage(fmt.Sprintf("validation of phase %s in action %s", phase.Name(), name), utils.Fail) | ||
return err | ||
} | ||
utils.PrintStage(fmt.Sprintf("validation of phase %s in action %s", phase.Name(), name), utils.Pass) | ||
} | ||
} | ||
|
||
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,162 @@ | ||
// Copyright 2022 The Kanister Authors. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package validate | ||
|
||
import ( | ||
"strings" | ||
"testing" | ||
|
||
. "gopkg.in/check.v1" | ||
|
||
kanister "github.com/kanisterio/kanister/pkg" | ||
crv1alpha1 "github.com/kanisterio/kanister/pkg/apis/cr/v1alpha1" | ||
) | ||
|
||
func Test(t *testing.T) { TestingT(t) } | ||
|
||
type ValidateBlueprint struct{} | ||
|
||
var _ = Suite(&ValidateBlueprint{}) | ||
|
||
func (v *ValidateBlueprint) TestValidate(c *C) { | ||
for _, tc := range []struct { | ||
phases []crv1alpha1.BlueprintPhase | ||
err Checker | ||
errContains string | ||
}{ | ||
{ | ||
phases: []crv1alpha1.BlueprintPhase{ | ||
{ | ||
Func: "KubeTask", | ||
Name: "00", | ||
Args: map[string]interface{}{ | ||
"image": "", | ||
}, | ||
}, | ||
{ | ||
Func: "KubeExec", | ||
Name: "01", | ||
Args: map[string]interface{}{ | ||
"namespace": "", | ||
"command": "", | ||
}, | ||
}, | ||
{ | ||
Func: "KubeExec", | ||
Name: "01", | ||
Args: map[string]interface{}{ | ||
"namespace": "", | ||
"command": "", | ||
"pod": "", | ||
}, | ||
}, | ||
}, | ||
errContains: "Required arg missing: command", | ||
err: NotNil, | ||
}, | ||
{ | ||
phases: []crv1alpha1.BlueprintPhase{ | ||
{ | ||
Func: "KubeTask", | ||
Name: "10", | ||
Args: map[string]interface{}{ | ||
"image": "", | ||
"command": "", | ||
}, | ||
}, | ||
{ | ||
Func: "KubeExec", | ||
Name: "11", | ||
Args: map[string]interface{}{ | ||
"namespace": "", | ||
"command": "", | ||
"pod": "", | ||
}, | ||
}, | ||
}, | ||
err: IsNil, | ||
}, | ||
{ | ||
// function name is incorrect | ||
phases: []crv1alpha1.BlueprintPhase{ | ||
{ | ||
Func: "KubeTasks", | ||
Name: "20", | ||
Args: map[string]interface{}{ | ||
"image": "", | ||
"command": "", | ||
}, | ||
}, | ||
{ | ||
Func: "KubeExec", | ||
Name: "21", | ||
Args: map[string]interface{}{ | ||
"namespace": "", | ||
"command": "", | ||
"pod": "", | ||
}, | ||
}, | ||
}, | ||
errContains: "Requested function {KubeTasks} has not been registered", | ||
err: NotNil, | ||
}, | ||
{ | ||
phases: []crv1alpha1.BlueprintPhase{ | ||
{ | ||
Func: "PrepareData", | ||
Name: "30", | ||
Args: map[string]interface{}{ | ||
"namespace": "", | ||
"image": "", | ||
"command": "", | ||
}, | ||
}, | ||
}, | ||
err: IsNil, | ||
}, | ||
{ | ||
phases: []crv1alpha1.BlueprintPhase{ | ||
{ | ||
Func: "PrepareData", | ||
Name: "40", | ||
Args: map[string]interface{}{ | ||
"namespace": "", | ||
"image": "", | ||
}, | ||
}, | ||
}, | ||
errContains: "Required arg missing: command", | ||
err: NotNil, | ||
}, | ||
} { | ||
bp := blueprint() | ||
bp.Actions["backup"].Phases = tc.phases | ||
err := Do(bp, kanister.DefaultVersion) | ||
if err != nil { | ||
c.Assert(strings.Contains(err.Error(), tc.errContains), Equals, true) | ||
} | ||
c.Assert(err, tc.err) | ||
} | ||
} | ||
|
||
func blueprint() *crv1alpha1.Blueprint { | ||
return &crv1alpha1.Blueprint{ | ||
Actions: map[string]*crv1alpha1.BlueprintAction{ | ||
"backup": { | ||
Phases: []crv1alpha1.BlueprintPhase{}, | ||
}, | ||
}, | ||
} | ||
} |
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,36 @@ | ||
// Copyright 2022 The Kanister Authors. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package kanctl | ||
|
||
import ( | ||
"errors" | ||
|
||
"github.com/kanisterio/kanister/pkg/blueprint" | ||
"github.com/kanisterio/kanister/pkg/blueprint/validate" | ||
) | ||
|
||
func performBlueprintValidation(p *validateParams) error { | ||
if p.filename == "" { | ||
return errors.New("--name is not supported for blueprint resources, please specify blueprint manifest using -f.") | ||
} | ||
|
||
// read blueprint from specified file | ||
bp, err := blueprint.ReadFromFile(p.filename) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return validate.Do(bp, p.functionVersion) | ||
} |
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
Oops, something went wrong.