1
0
Fork 0
mirror of https://github.com/kyverno/kyverno.git synced 2024-12-14 11:57:48 +00:00
kyverno/cmd/cli/kubectl-kyverno/policy/variables.go
Charles-Edouard Brétéché ee4e0422ed
refactor: introduce cli variables package (#8285)
* refactor: introduce cli variables package

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>

* fix

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>

* fix

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>

* fix

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>

* fix

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>

* fix

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>

---------

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>
2023-09-06 14:03:51 +00:00

27 lines
701 B
Go

package policy
import (
"encoding/json"
"errors"
kyvernov1 "github.com/kyverno/kyverno/api/kyverno/v1"
"github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/log"
"github.com/kyverno/kyverno/pkg/engine/variables/regex"
)
func ExtractVariables(policy kyvernov1.PolicyInterface) ([]string, error) {
var variables []string
raw, err := json.Marshal(policy)
if err != nil {
return nil, err
}
for _, match := range regex.RegexVariables.FindAllStringSubmatch(string(raw), -1) {
if len(match) != 3 {
err := errors.New("extract variables match has wrong elements number")
log.Log.Error(err, err.Error())
} else {
variables = append(variables, match[2])
}
}
return variables, nil
}