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

783 tested prototype

This commit is contained in:
shravan 2020-04-02 12:19:32 +05:30
parent f0300b7c49
commit c0dbd1d6e6
2 changed files with 19 additions and 16 deletions

View file

@ -129,6 +129,11 @@ func (c *Client) PatchResource(kind string, namespace string, name string, patch
return c.getResourceInterface(kind, namespace).Patch(name, patchTypes.JSONPatchType, patch, meta.PatchOptions{})
}
// GetDynamicInterface fetches underlying dynamic interface
func (c *Client) GetDynamicInterface() dynamic.Interface {
return c.client
}
// ListResource returns the list of resources in unstructured/json format
// Access items using []Items
func (c *Client) ListResource(kind string, namespace string, lselector *meta.LabelSelector) (*unstructured.UnstructuredList, error) {

View file

@ -5,6 +5,10 @@ import (
"fmt"
"time"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
runtimeSchema "k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"gopkg.in/yaml.v2"
@ -49,12 +53,16 @@ func (c *crdSync) Run(workers int, stopCh <-chan struct{}) {
c.sync()
for i := 0; i < workers; i++ {
go wait.Until(c.sync, time.Second*10, stopCh)
go wait.Until(c.sync, time.Second*25, stopCh)
}
}
func (c *crdSync) sync() {
crds, err := c.client.ListResource("CustomResourceDefinition", "", nil)
crds, err := c.client.GetDynamicInterface().Resource(runtimeSchema.GroupVersionResource{
Group: "apiextensions.k8s.io",
Version: "v1beta1",
Resource: "customresourcedefinitions",
}).List(v1.ListOptions{})
if err != nil {
log.Log.Error(err, "could not fetch crd's from server")
return
@ -85,11 +93,9 @@ func (o *Controller) parseCRD(crd unstructured.Unstructured) {
Names struct {
Kind string `json:"kind"`
} `json:"names"`
Versions []struct {
Schema struct {
OpenAPIV3Schema interface{} `json:"openAPIV3Schema"`
} `json:"schema"`
} `json:"versions"`
Validation struct {
OpenAPIV3Schema interface{} `json:"openAPIV3Schema"`
} `json:"validation"`
} `json:"spec"`
}
@ -97,13 +103,9 @@ func (o *Controller) parseCRD(crd unstructured.Unstructured) {
_ = json.Unmarshal(crdRaw, &crdDefinition)
crdName := crdDefinition.Spec.Names.Kind
if len(crdDefinition.Spec.Versions) < 1 {
log.Log.V(4).Info("could not parse crd schema, no versions present")
return
}
var schema yaml.MapSlice
schemaRaw, _ := json.Marshal(crdDefinition.Spec.Versions[0].Schema.OpenAPIV3Schema)
schemaRaw, _ := json.Marshal(crdDefinition.Spec.Validation.OpenAPIV3Schema)
if len(schemaRaw) < 1 {
log.Log.V(4).Info("could not parse crd schema")
return
@ -131,10 +133,6 @@ func addingDefaultFieldsToSchema(schemaRaw []byte) []byte {
}
_ = json.Unmarshal(schemaRaw, &schema)
if len(schemaRaw) < 1 {
schema.Properties = make(map[string]interface{})
}
if schema.Properties["apiVersion"] == nil {
apiVersionDefRaw := `{"description":"APIVersion defines the versioned schema of this representation of an object. Servers should convert recognized schemas to the latest internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources","type":"string"}`
apiVersionDef := make(map[string]interface{})