1
0
Fork 0
mirror of https://github.com/kyverno/kyverno.git synced 2025-03-06 07:57:07 +00:00
kyverno/pkg/utils/util.go

76 lines
1.8 KiB
Go
Raw Normal View History

2019-07-03 11:52:10 -07:00
package utils
2019-07-31 17:43:46 -07:00
import (
2019-08-22 11:18:13 -07:00
"reflect"
2019-07-31 17:43:46 -07:00
2019-08-22 11:18:13 -07:00
"github.com/golang/glog"
2019-07-31 17:43:46 -07:00
"github.com/minio/minio/pkg/wildcard"
2019-08-22 11:18:13 -07:00
client "github.com/nirmata/kyverno/pkg/dclient"
"k8s.io/apimachinery/pkg/runtime/schema"
2019-08-14 14:56:53 -07:00
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
2019-07-31 17:43:46 -07:00
)
2019-08-14 10:01:47 -07:00
//Contains Check if strint is contained in a list of string
func contains(list []string, element string, fn func(string, string) bool) bool {
2019-07-03 11:52:10 -07:00
for _, e := range list {
if fn(e, element) {
2019-07-03 11:52:10 -07:00
return true
}
}
return false
}
2019-07-31 17:43:46 -07:00
//ContainsNamepace check if namespace satisfies any list of pattern(regex)
func ContainsNamepace(patterns []string, ns string) bool {
return contains(patterns, ns, compareNamespaces)
}
//ContainsString check if the string is contains in a list
func ContainsString(list []string, element string) bool {
return contains(list, element, compareString)
}
func compareNamespaces(pattern, ns string) bool {
return wildcard.Match(pattern, ns)
}
func compareString(str, name string) bool {
return str == name
}
2019-08-14 14:56:53 -07:00
//NewKubeClient returns a new kubernetes client
func NewKubeClient(config *rest.Config) (kubernetes.Interface, error) {
kclient, err := kubernetes.NewForConfig(config)
if err != nil {
return nil, err
}
return kclient, nil
}
2019-08-20 16:40:20 -07:00
//Btoi converts boolean to int
func Btoi(b bool) int {
if b {
return 1
}
return 0
}
2019-08-22 11:18:13 -07:00
//CRDInstalled to check if the CRD is installed or not
func CRDInstalled(discovery client.IDiscovery) bool {
check := func(kind string) bool {
gvr := discovery.GetGVRFromKind(kind)
if reflect.DeepEqual(gvr, (schema.GroupVersionResource{})) {
glog.Errorf("%s CRD not installed", kind)
return false
}
glog.Infof("CRD %s found ", kind)
return true
}
if !check("ClusterPolicy") || !check("ClusterPolicyViolation") || !check("PolicyViolation") {
2019-08-22 11:18:13 -07:00
return false
}
return true
}