mirror of
https://github.com/kyverno/kyverno.git
synced 2025-03-31 03:45:17 +00:00
Merge pull request #55 from nirmata/46_Support_anyResourceKind
support all registered GVK for policy application in admission-contro…
This commit is contained in:
commit
65010c4178
4 changed files with 26 additions and 37 deletions
|
@ -4,6 +4,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
types "github.com/nirmata/kube-policy/pkg/apis/policy/v1alpha1"
|
types "github.com/nirmata/kube-policy/pkg/apis/policy/v1alpha1"
|
||||||
|
@ -307,13 +308,10 @@ func (c *Client) waitUntilNamespaceIsCreated(name string) error {
|
||||||
return lastError
|
return lastError
|
||||||
}
|
}
|
||||||
|
|
||||||
//GetSupportedKinds provides list of supported types
|
// KindIsSupported checks if the kind is a registerd GVK
|
||||||
func GetSupportedKinds() []string {
|
func (c *Client) KindIsSupported(kind string) bool {
|
||||||
return supportedTypes
|
kind = strings.ToLower(kind) + "s"
|
||||||
}
|
buildGVKMapper(c.clientConfig, false)
|
||||||
|
_, ok := getValue(kind)
|
||||||
var supportedTypes = []string{
|
return ok
|
||||||
"ConfigMap", "Pods", "Deployment", "CronJob", "Endpoints", "HorizontalPodAutoscaler",
|
|
||||||
"Ingress", "Job", "LimitRange", "Namespace", "NetworkPolicy", "PersistentVolumeClaim",
|
|
||||||
"PodDisruptionBudget", "PodTemplate", "ResourceQuota", "Secret", "Service", "StatefulSet",
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,26 +17,32 @@ const namespaceCreationWaitInterval time.Duration = 100 * time.Millisecond
|
||||||
var groupVersionMapper map[string]schema.GroupVersionResource
|
var groupVersionMapper map[string]schema.GroupVersionResource
|
||||||
|
|
||||||
func getGrpVersionMapper(kind string, clientConfig *rest.Config, refresh bool) schema.GroupVersionResource {
|
func getGrpVersionMapper(kind string, clientConfig *rest.Config, refresh bool) schema.GroupVersionResource {
|
||||||
grpVersionSchema := schema.GroupVersionResource{}
|
// build the GVK mapper
|
||||||
|
buildGVKMapper(clientConfig, refresh)
|
||||||
if groupVersionMapper == nil || refresh {
|
|
||||||
groupVersionMapper = make(map[string]schema.GroupVersionResource)
|
|
||||||
// refesh the mapper
|
|
||||||
if err := refreshRegisteredResources(groupVersionMapper, clientConfig); err != nil {
|
|
||||||
utilruntime.HandleError(err)
|
|
||||||
return grpVersionSchema
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// Query mapper
|
// Query mapper
|
||||||
if val, ok := getValue(kind); ok {
|
if val, ok := getValue(kind); ok {
|
||||||
return *val
|
return *val
|
||||||
}
|
}
|
||||||
utilruntime.HandleError(fmt.Errorf("Resouce '%s' not registered", kind))
|
utilruntime.HandleError(fmt.Errorf("Resouce '%s' not registered", kind))
|
||||||
return grpVersionSchema
|
return schema.GroupVersionResource{}
|
||||||
|
}
|
||||||
|
|
||||||
|
func buildGVKMapper(clientConfig *rest.Config, refresh bool) {
|
||||||
|
if groupVersionMapper == nil || refresh {
|
||||||
|
groupVersionMapper = make(map[string]schema.GroupVersionResource)
|
||||||
|
// refresh the mapper
|
||||||
|
if err := refreshRegisteredResources(groupVersionMapper, clientConfig); err != nil {
|
||||||
|
utilruntime.HandleError(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func getValue(kind string) (*schema.GroupVersionResource, bool) {
|
func getValue(kind string) (*schema.GroupVersionResource, bool) {
|
||||||
|
if groupVersionMapper == nil {
|
||||||
|
utilruntime.HandleError(fmt.Errorf("GroupVersionKind mapper is not loaded"))
|
||||||
|
return nil, false
|
||||||
|
}
|
||||||
if val, ok := groupVersionMapper[kind]; ok {
|
if val, ok := groupVersionMapper[kind]; ok {
|
||||||
return &val, true
|
return &val, true
|
||||||
}
|
}
|
||||||
|
|
|
@ -87,8 +87,7 @@ func (ws *WebhookServer) serve(w http.ResponseWriter, r *http.Request) {
|
||||||
admissionReview.Response = &v1beta1.AdmissionResponse{
|
admissionReview.Response = &v1beta1.AdmissionResponse{
|
||||||
Allowed: true,
|
Allowed: true,
|
||||||
}
|
}
|
||||||
|
if ws.client.KindIsSupported(admissionReview.Request.Kind.Kind) {
|
||||||
if KindIsSupported(admissionReview.Request.Kind.Kind) {
|
|
||||||
switch r.URL.Path {
|
switch r.URL.Path {
|
||||||
case config.MutatingWebhookServicePath:
|
case config.MutatingWebhookServicePath:
|
||||||
admissionReview.Response = ws.HandleMutation(admissionReview.Request)
|
admissionReview.Response = ws.HandleMutation(admissionReview.Request)
|
||||||
|
|
|
@ -1,14 +0,0 @@
|
||||||
package webhooks
|
|
||||||
|
|
||||||
import "github.com/nirmata/kube-policy/client"
|
|
||||||
|
|
||||||
// KindIsSupported checks kind to be prensent in
|
|
||||||
// SupportedKinds defined in config
|
|
||||||
func KindIsSupported(kind string) bool {
|
|
||||||
for _, k := range client.GetSupportedKinds() {
|
|
||||||
if k == kind {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
|
Loading…
Add table
Reference in a new issue