mirror of
https://github.com/kyverno/kyverno.git
synced 2025-01-20 18:52:16 +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:
parent
cb6f55cdcd
commit
a93ac45586
7 changed files with 49 additions and 56 deletions
|
@ -3,6 +3,7 @@ package v1
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
|
kubeutils "github.com/kyverno/kyverno/pkg/utils/kube"
|
||||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
"k8s.io/apimachinery/pkg/util/sets"
|
"k8s.io/apimachinery/pkg/util/sets"
|
||||||
"k8s.io/apimachinery/pkg/util/validation/field"
|
"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 {
|
if r.Name != "" && len(r.Names) > 0 {
|
||||||
errs = append(errs, field.Invalid(path, r, "Both name and names can not be specified together"))
|
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 {
|
if selector, err := metav1.LabelSelectorAsSelector(r.Selector); err != nil {
|
||||||
errs = append(errs, field.Invalid(path.Child("selector"), r.Selector, err.Error()))
|
errs = append(errs, field.Invalid(path.Child("selector"), r.Selector, err.Error()))
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -1,11 +1,8 @@
|
||||||
package v1
|
package v1
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"strings"
|
|
||||||
|
|
||||||
"k8s.io/apiextensions-apiserver/pkg/apis/apiextensions"
|
"k8s.io/apiextensions-apiserver/pkg/apis/apiextensions"
|
||||||
apiextv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
|
apiextv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
|
||||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
||||||
"k8s.io/apimachinery/pkg/util/validation/field"
|
"k8s.io/apimachinery/pkg/util/validation/field"
|
||||||
log "sigs.k8s.io/controller-runtime/pkg/log"
|
log "sigs.k8s.io/controller-runtime/pkg/log"
|
||||||
)
|
)
|
||||||
|
@ -48,35 +45,3 @@ func ValidatePolicyName(path *field.Path, name string) (errs field.ErrorList) {
|
||||||
}
|
}
|
||||||
return errs
|
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"`
|
|
||||||
}
|
|
||||||
|
|
17
api/kyverno/v1/violated_rule_types.go
Executable file
17
api/kyverno/v1/violated_rule_types.go
Executable 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"`
|
||||||
|
}
|
|
@ -3,9 +3,9 @@ package wildcards
|
||||||
import (
|
import (
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
commonAnchor "github.com/kyverno/kyverno/pkg/engine/anchor"
|
|
||||||
|
|
||||||
wildcard "github.com/kyverno/go-wildcard"
|
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"
|
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 {
|
func replaceWildcardsInMapKeyValues(patternMap map[string]string, resourceMap map[string]string) map[string]string {
|
||||||
result := map[string]string{}
|
result := map[string]string{}
|
||||||
for k, v := range patternMap {
|
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)
|
matchK, matchV := expandWildcards(k, v, resourceMap, true, true)
|
||||||
result[matchK] = matchV
|
result[matchK] = matchV
|
||||||
} else {
|
} else {
|
||||||
|
@ -32,10 +32,6 @@ func replaceWildcardsInMapKeyValues(patternMap map[string]string, resourceMap ma
|
||||||
return result
|
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) {
|
func expandWildcards(k, v string, resourceMap map[string]string, matchValue, replace bool) (key string, val string) {
|
||||||
for k1, v1 := range resourceMap {
|
for k1, v1 := range resourceMap {
|
||||||
if wildcard.Match(k, k1) {
|
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
|
// replaceWildCardChars will replace '*' and '?' characters which are not
|
||||||
// supported by Kubernetes with a '0'.
|
// supported by Kubernetes with a '0'.
|
||||||
func replaceWildCardChars(s string) string {
|
func replaceWildCardChars(s string) string {
|
||||||
s = strings.Replace(s, "*", "0", -1)
|
s = strings.ReplaceAll(s, "*", "0")
|
||||||
s = strings.Replace(s, "?", "0", -1)
|
s = strings.ReplaceAll(s, "?", "0")
|
||||||
return s
|
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{} {
|
func replaceWildcardsInMapKeys(patternData, resourceData map[string]string) map[string]interface{} {
|
||||||
results := map[string]interface{}{}
|
results := map[string]interface{}{}
|
||||||
for k, v := range patternData {
|
for k, v := range patternData {
|
||||||
if hasWildcards(k) {
|
if stringutils.ContainsWildcard(k) {
|
||||||
anchorFreeKey, anchorPrefix := commonAnchor.RemoveAnchor(k)
|
anchorFreeKey, anchorPrefix := commonAnchor.RemoveAnchor(k)
|
||||||
matchK, _ := expandWildcards(anchorFreeKey, v, resourceData, false, false)
|
matchK, _ := expandWildcards(anchorFreeKey, v, resourceData, false, false)
|
||||||
if anchorPrefix != "" {
|
if anchorPrefix != "" {
|
||||||
|
|
|
@ -10,6 +10,7 @@ import (
|
||||||
kyverno "github.com/kyverno/kyverno/api/kyverno/v1"
|
kyverno "github.com/kyverno/kyverno/api/kyverno/v1"
|
||||||
"github.com/kyverno/kyverno/pkg/config"
|
"github.com/kyverno/kyverno/pkg/config"
|
||||||
"github.com/kyverno/kyverno/pkg/utils"
|
"github.com/kyverno/kyverno/pkg/utils"
|
||||||
|
stringutils "github.com/kyverno/kyverno/pkg/utils/string"
|
||||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
||||||
"k8s.io/apimachinery/pkg/labels"
|
"k8s.io/apimachinery/pkg/labels"
|
||||||
|
@ -69,7 +70,7 @@ func (pc *PolicyController) getNamespacesForRule(rule *kyverno.Rule, log logr.Lo
|
||||||
|
|
||||||
var wildcards []string
|
var wildcards []string
|
||||||
for _, nsName := range rule.MatchResources.Namespaces {
|
for _, nsName := range rule.MatchResources.Namespaces {
|
||||||
if HasWildcard(nsName) {
|
if stringutils.ContainsWildcard(nsName) {
|
||||||
wildcards = append(wildcards, nsName)
|
wildcards = append(wildcards, nsName)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -84,15 +85,6 @@ func (pc *PolicyController) getNamespacesForRule(rule *kyverno.Rule, log logr.Lo
|
||||||
return pc.configHandler.FilterNamespaces(matchedNS)
|
return pc.configHandler.FilterNamespaces(matchedNS)
|
||||||
}
|
}
|
||||||
|
|
||||||
// HasWildcard ...
|
|
||||||
func HasWildcard(s string) bool {
|
|
||||||
if s == "" {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
return strings.Contains(s, "*") || strings.Contains(s, "?")
|
|
||||||
}
|
|
||||||
|
|
||||||
// GetMatchingNamespaces ...
|
// GetMatchingNamespaces ...
|
||||||
func GetMatchingNamespaces(wildcards []string, nslister listerv1.NamespaceLister, log logr.Logger) []string {
|
func GetMatchingNamespaces(wildcards []string, nslister listerv1.NamespaceLister, log logr.Logger) []string {
|
||||||
all := GetAllNamespaces(nslister, log)
|
all := GetAllNamespaces(nslister, log)
|
||||||
|
|
15
pkg/utils/kube/wildcard.go
Normal file
15
pkg/utils/kube/wildcard.go
Normal 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
|
||||||
|
}
|
7
pkg/utils/string/wildcard.go
Normal file
7
pkg/utils/string/wildcard.go
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
package string
|
||||||
|
|
||||||
|
import "strings"
|
||||||
|
|
||||||
|
func ContainsWildcard(v string) bool {
|
||||||
|
return strings.Contains(v, "*") || strings.Contains(v, "?")
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue