mirror of
https://github.com/kyverno/kyverno.git
synced 2025-03-31 03:45:17 +00:00
added validation for set flag
This commit is contained in:
parent
11e73381cf
commit
0927e7a732
2 changed files with 26 additions and 4 deletions
|
@ -73,10 +73,12 @@ func Command() *cobra.Command {
|
|||
}
|
||||
}()
|
||||
|
||||
kvpairs := strings.Split(strings.Trim(variablesString, " "), ",")
|
||||
for _, kvpair := range kvpairs {
|
||||
kvs := strings.Split(strings.Trim(kvpair, " "), "=")
|
||||
variables[strings.Trim(kvs[0], " ")] = strings.Trim(kvs[1], " ")
|
||||
if variablesString != "" {
|
||||
kvpairs := strings.Split(strings.Trim(variablesString, " "), ",")
|
||||
for _, kvpair := range kvpairs {
|
||||
kvs := strings.Split(strings.Trim(kvpair, " "), "=")
|
||||
variables[strings.Trim(kvs[0], " ")] = strings.Trim(kvs[1], " ")
|
||||
}
|
||||
}
|
||||
|
||||
if len(resourcePaths) == 0 && !cluster {
|
||||
|
@ -110,6 +112,18 @@ func Command() *cobra.Command {
|
|||
return err
|
||||
}
|
||||
|
||||
for _, policy := range policies {
|
||||
err := policy2.Validate(utils.MarshalPolicy(*policy), nil, true, openAPIController)
|
||||
if err != nil {
|
||||
fmt.Printf("Policy %v is not valid: %v\n", policy.Name, err)
|
||||
os.Exit(1)
|
||||
}
|
||||
if common.PolicyHasVariables(*policy) && variablesString == "" {
|
||||
return sanitizedError.NewWithError(fmt.Sprintf("policy %s have variables. pass the values for the variables using set flag", policy.Name), err)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
var dClient *client.Client
|
||||
if cluster {
|
||||
restConfig, err := kubernetesConfig.ToRESTConfig()
|
||||
|
|
|
@ -9,6 +9,7 @@ import (
|
|||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
|
||||
jsonpatch "github.com/evanphx/json-patch"
|
||||
"github.com/go-logr/logr"
|
||||
|
@ -186,3 +187,10 @@ func MutatePolicy(policy *v1.ClusterPolicy, logger logr.Logger) (*v1.ClusterPoli
|
|||
|
||||
return &p, nil
|
||||
}
|
||||
|
||||
// PolicyHasVariables - check for variables in policy
|
||||
func PolicyHasVariables(policy v1.ClusterPolicy) bool {
|
||||
policyRaw, _ := json.Marshal(policy)
|
||||
regex := regexp.MustCompile(`\{\{([^{}]*)\}\}`)
|
||||
return len(regex.FindAllStringSubmatch(string(policyRaw), -1)) > 0
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue