From a93ac45586ca80a90e5be34b2a8ef58c8d7eeb46 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Charles-Edouard=20Br=C3=A9t=C3=A9ch=C3=A9?= Date: Mon, 4 Apr 2022 20:58:22 +0200 Subject: [PATCH] refactor: move some helpers in utils package (#3539) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Charles-Edouard Brétéché Co-authored-by: shuting --- api/kyverno/v1/resource_description_types.go | 3 +- api/kyverno/v1/utils.go | 35 -------------------- api/kyverno/v1/violated_rule_types.go | 17 ++++++++++ pkg/engine/wildcards/wildcards.go | 16 ++++----- pkg/policy/common.go | 12 ++----- pkg/utils/kube/wildcard.go | 15 +++++++++ pkg/utils/string/wildcard.go | 7 ++++ 7 files changed, 49 insertions(+), 56 deletions(-) create mode 100755 api/kyverno/v1/violated_rule_types.go create mode 100644 pkg/utils/kube/wildcard.go create mode 100644 pkg/utils/string/wildcard.go diff --git a/api/kyverno/v1/resource_description_types.go b/api/kyverno/v1/resource_description_types.go index e92802fe94..17fac7b3c3 100644 --- a/api/kyverno/v1/resource_description_types.go +++ b/api/kyverno/v1/resource_description_types.go @@ -3,6 +3,7 @@ package v1 import ( "fmt" + kubeutils "github.com/kyverno/kyverno/pkg/utils/kube" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/util/sets" "k8s.io/apimachinery/pkg/util/validation/field" @@ -57,7 +58,7 @@ func (r *ResourceDescription) Validate(path *field.Path, namespaced bool, cluste if r.Name != "" && len(r.Names) > 0 { errs = append(errs, field.Invalid(path, r, "Both name and names can not be specified together")) } - if r.Selector != nil && !labelSelectorContainsWildcard(r.Selector) { + if r.Selector != nil && !kubeutils.LabelSelectorContainsWildcard(r.Selector) { if selector, err := metav1.LabelSelectorAsSelector(r.Selector); err != nil { errs = append(errs, field.Invalid(path.Child("selector"), r.Selector, err.Error())) } else { diff --git a/api/kyverno/v1/utils.go b/api/kyverno/v1/utils.go index 1c96d86d95..5282870c50 100755 --- a/api/kyverno/v1/utils.go +++ b/api/kyverno/v1/utils.go @@ -1,11 +1,8 @@ package v1 import ( - "strings" - "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions" apiextv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/util/validation/field" log "sigs.k8s.io/controller-runtime/pkg/log" ) @@ -48,35 +45,3 @@ func ValidatePolicyName(path *field.Path, name string) (errs field.ErrorList) { } return errs } - -func labelSelectorContainsWildcard(v *metav1.LabelSelector) bool { - for k, v := range v.MatchLabels { - if isWildcardPresent(k) || isWildcardPresent(v) { - return true - } - } - return false -} - -func isWildcardPresent(v string) bool { - if strings.Contains(v, "*") || strings.Contains(v, "?") { - return true - } - return false -} - -// ViolatedRule stores the information regarding the rule. -type ViolatedRule struct { - // Name specifies violated rule name. - Name string `json:"name" yaml:"name"` - - // Type specifies violated rule type. - Type string `json:"type" yaml:"type"` - - // Message specifies violation message. - // +optional - Message string `json:"message" yaml:"message"` - - // Status shows the rule response status - Status string `json:"status" yaml:"status"` -} diff --git a/api/kyverno/v1/violated_rule_types.go b/api/kyverno/v1/violated_rule_types.go new file mode 100755 index 0000000000..945cf419cb --- /dev/null +++ b/api/kyverno/v1/violated_rule_types.go @@ -0,0 +1,17 @@ +package v1 + +// ViolatedRule stores the information regarding the rule. +type ViolatedRule struct { + // Name specifies violated rule name. + Name string `json:"name" yaml:"name"` + + // Type specifies violated rule type. + Type string `json:"type" yaml:"type"` + + // Message specifies violation message. + // +optional + Message string `json:"message" yaml:"message"` + + // Status shows the rule response status + Status string `json:"status" yaml:"status"` +} diff --git a/pkg/engine/wildcards/wildcards.go b/pkg/engine/wildcards/wildcards.go index 721aa401eb..cc653c82f5 100644 --- a/pkg/engine/wildcards/wildcards.go +++ b/pkg/engine/wildcards/wildcards.go @@ -3,9 +3,9 @@ package wildcards import ( "strings" - commonAnchor "github.com/kyverno/kyverno/pkg/engine/anchor" - wildcard "github.com/kyverno/go-wildcard" + commonAnchor "github.com/kyverno/kyverno/pkg/engine/anchor" + stringutils "github.com/kyverno/kyverno/pkg/utils/string" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) @@ -21,7 +21,7 @@ func ReplaceInSelector(labelSelector *metav1.LabelSelector, resourceLabels map[s func replaceWildcardsInMapKeyValues(patternMap map[string]string, resourceMap map[string]string) map[string]string { result := map[string]string{} for k, v := range patternMap { - if hasWildcards(k) || hasWildcards(v) { + if stringutils.ContainsWildcard(k) || stringutils.ContainsWildcard(v) { matchK, matchV := expandWildcards(k, v, resourceMap, true, true) result[matchK] = matchV } else { @@ -32,10 +32,6 @@ func replaceWildcardsInMapKeyValues(patternMap map[string]string, resourceMap ma return result } -func hasWildcards(s string) bool { - return strings.Contains(s, "*") || strings.Contains(s, "?") -} - func expandWildcards(k, v string, resourceMap map[string]string, matchValue, replace bool) (key string, val string) { for k1, v1 := range resourceMap { if wildcard.Match(k, k1) { @@ -58,8 +54,8 @@ func expandWildcards(k, v string, resourceMap map[string]string, matchValue, rep // replaceWildCardChars will replace '*' and '?' characters which are not // supported by Kubernetes with a '0'. func replaceWildCardChars(s string) string { - s = strings.Replace(s, "*", "0", -1) - s = strings.Replace(s, "?", "0", -1) + s = strings.ReplaceAll(s, "*", "0") + s = strings.ReplaceAll(s, "?", "0") return s } @@ -145,7 +141,7 @@ func getValueAsStringMap(key string, data interface{}) (string, map[string]strin func replaceWildcardsInMapKeys(patternData, resourceData map[string]string) map[string]interface{} { results := map[string]interface{}{} for k, v := range patternData { - if hasWildcards(k) { + if stringutils.ContainsWildcard(k) { anchorFreeKey, anchorPrefix := commonAnchor.RemoveAnchor(k) matchK, _ := expandWildcards(anchorFreeKey, v, resourceData, false, false) if anchorPrefix != "" { diff --git a/pkg/policy/common.go b/pkg/policy/common.go index 92992d564b..7245bde59c 100644 --- a/pkg/policy/common.go +++ b/pkg/policy/common.go @@ -10,6 +10,7 @@ import ( kyverno "github.com/kyverno/kyverno/api/kyverno/v1" "github.com/kyverno/kyverno/pkg/config" "github.com/kyverno/kyverno/pkg/utils" + stringutils "github.com/kyverno/kyverno/pkg/utils/string" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" "k8s.io/apimachinery/pkg/labels" @@ -69,7 +70,7 @@ func (pc *PolicyController) getNamespacesForRule(rule *kyverno.Rule, log logr.Lo var wildcards []string for _, nsName := range rule.MatchResources.Namespaces { - if HasWildcard(nsName) { + if stringutils.ContainsWildcard(nsName) { wildcards = append(wildcards, nsName) } @@ -84,15 +85,6 @@ func (pc *PolicyController) getNamespacesForRule(rule *kyverno.Rule, log logr.Lo return pc.configHandler.FilterNamespaces(matchedNS) } -// HasWildcard ... -func HasWildcard(s string) bool { - if s == "" { - return false - } - - return strings.Contains(s, "*") || strings.Contains(s, "?") -} - // GetMatchingNamespaces ... func GetMatchingNamespaces(wildcards []string, nslister listerv1.NamespaceLister, log logr.Logger) []string { all := GetAllNamespaces(nslister, log) diff --git a/pkg/utils/kube/wildcard.go b/pkg/utils/kube/wildcard.go new file mode 100644 index 0000000000..5565292d40 --- /dev/null +++ b/pkg/utils/kube/wildcard.go @@ -0,0 +1,15 @@ +package kube + +import ( + stringutils "github.com/kyverno/kyverno/pkg/utils/string" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" +) + +func LabelSelectorContainsWildcard(v *metav1.LabelSelector) bool { + for k, v := range v.MatchLabels { + if stringutils.ContainsWildcard(k) || stringutils.ContainsWildcard(v) { + return true + } + } + return false +} diff --git a/pkg/utils/string/wildcard.go b/pkg/utils/string/wildcard.go new file mode 100644 index 0000000000..4d4112241e --- /dev/null +++ b/pkg/utils/string/wildcard.go @@ -0,0 +1,7 @@ +package string + +import "strings" + +func ContainsWildcard(v string) bool { + return strings.Contains(v, "*") || strings.Contains(v, "?") +}