1
0
Fork 0
mirror of https://github.com/kyverno/kyverno.git synced 2025-03-31 03:45:17 +00:00

move commonly used code to pkg/utils

This commit is contained in:
Shuting Zhao 2019-07-03 11:52:10 -07:00
parent f10d82f555
commit 3a92bde097
4 changed files with 52 additions and 10 deletions

10
pkg/utils/util.go Normal file
View file

@ -0,0 +1,10 @@
package utils
func Contains(list []string, element string) bool {
for _, e := range list {
if e == element {
return true
}
}
return false
}

40
pkg/utils/util_test.go Normal file
View file

@ -0,0 +1,40 @@
package utils
import (
"testing"
"gotest.tools/assert"
)
func Test_allEmpty(t *testing.T) {
var list []string
var element string
res := Contains(list, element)
assert.Assert(t, res == false)
}
func Test_emptyList(t *testing.T) {
var list []string
element := "foo"
res := Contains(list, element)
assert.Assert(t, res == false)
}
func Test_emptyElement(t *testing.T) {
list := []string{"foo", "bar"}
var element string
res := Contains(list, element)
assert.Assert(t, res == false)
}
func Test_emptyElementInList(t *testing.T) {
list := []string{"foo", "bar", ""}
var element string
res := Contains(list, element)
assert.Assert(t, res == true)
list = []string{"foo", "bar", "bar"}
element = "bar"
res = Contains(list, element)
assert.Assert(t, res == true)
}

View file

@ -21,6 +21,7 @@ import (
"github.com/nirmata/kyverno/pkg/info"
"github.com/nirmata/kyverno/pkg/sharedinformer"
tlsutils "github.com/nirmata/kyverno/pkg/tls"
"github.com/nirmata/kyverno/pkg/utils"
v1beta1 "k8s.io/api/admission/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
@ -399,7 +400,7 @@ func (ws *WebhookServer) validateUniqueRuleName(rawPolicy []byte) *v1beta1.Admis
json.Unmarshal(rawPolicy, &policy)
for _, rule := range policy.Spec.Rules {
if contains(ruleNames, rule.Name) {
if utils.Contains(ruleNames, rule.Name) {
msg := fmt.Sprintf(`The policy "%s" is invalid: duplicate rule name: "%s"`, policy.Name, rule.Name)
glog.Errorln(msg)

View file

@ -63,12 +63,3 @@ func getApplicableKindsForPolicy(p *v1alpha1.Policy) []string {
}
return kinds
}
func contains(ruleNames []string, ruleName string) bool {
for _, rn := range ruleNames {
if rn == ruleName {
return true
}
}
return false
}