1
0
Fork 0
mirror of https://github.com/kyverno/kyverno.git synced 2024-12-14 11:57:48 +00:00

refactor: move some helpers in utils package (#3539)

Signed-off-by: Charles-Edouard Brétéché <charled.breteche@gmail.com>

Co-authored-by: shuting <shuting@nirmata.com>
This commit is contained in:
Charles-Edouard Brétéché 2022-04-04 20:58:22 +02:00 committed by GitHub
parent cb6f55cdcd
commit a93ac45586
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 49 additions and 56 deletions

View file

@ -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 {

View file

@ -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"`
}

View file

@ -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"`
}

View file

@ -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 != "" {

View file

@ -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)

View file

@ -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
}

View file

@ -0,0 +1,7 @@
package string
import "strings"
func ContainsWildcard(v string) bool {
return strings.Contains(v, "*") || strings.Contains(v, "?")
}