From b2ae45c856454962133ccb5429e1bbad8bbb9cfd Mon Sep 17 00:00:00 2001 From: shravan Date: Fri, 3 Apr 2020 10:30:52 +0530 Subject: [PATCH] 777 tested prototype --- pkg/kyverno/apply/command.go | 41 +++++++++++++----------------------- pkg/kyverno/apply/helper.go | 37 -------------------------------- 2 files changed, 15 insertions(+), 63 deletions(-) delete mode 100644 pkg/kyverno/apply/helper.go diff --git a/pkg/kyverno/apply/command.go b/pkg/kyverno/apply/command.go index 75ee885986..daea727a61 100644 --- a/pkg/kyverno/apply/command.go +++ b/pkg/kyverno/apply/command.go @@ -7,6 +7,9 @@ import ( "os" "path/filepath" "regexp" + "time" + + client "github.com/nirmata/kyverno/pkg/dclient" "github.com/nirmata/kyverno/pkg/utils" @@ -18,8 +21,6 @@ import ( "k8s.io/apimachinery/pkg/runtime/schema" - "k8s.io/client-go/discovery" - "k8s.io/apimachinery/pkg/util/yaml" "github.com/nirmata/kyverno/pkg/engine" @@ -87,15 +88,19 @@ func Command() *cobra.Command { } } - var dClient discovery.CachedDiscoveryInterface + var dClient *client.Client if cluster { - dClient, err = kubernetesConfig.ToDiscoveryClient() + restConfig, err := kubernetesConfig.ToRESTConfig() if err != nil { - return sanitizedError.New(fmt.Errorf("Issues with kubernetes Config").Error()) + return err + } + dClient, err = client.NewClient(restConfig, 10*time.Second, make(chan struct{}), log.Log) + if err != nil { + return err } } - resources, err := getResources(policies, resourcePaths, dClient, openAPIController) + resources, err := getResources(policies, resourcePaths, dClient) if err != nil { return sanitizedError.New(fmt.Errorf("Issues fetching resources").Error()) } @@ -123,7 +128,7 @@ func Command() *cobra.Command { return cmd } -func getResources(policies []*v1.ClusterPolicy, resourcePaths []string, dClient discovery.CachedDiscoveryInterface, openAPIController *openapi.Controller) ([]*unstructured.Unstructured, error) { +func getResources(policies []*v1.ClusterPolicy, resourcePaths []string, dClient *client.Client) ([]*unstructured.Unstructured, error) { var resources []*unstructured.Unstructured var err error @@ -142,7 +147,7 @@ func getResources(policies []*v1.ClusterPolicy, resourcePaths []string, dClient resourceTypes = append(resourceTypes, kind) } - resources, err = getResourcesOfTypeFromCluster(resourceTypes, dClient, openAPIController) + resources, err = getResourcesOfTypeFromCluster(resourceTypes, dClient) if err != nil { return nil, err } @@ -160,27 +165,11 @@ func getResources(policies []*v1.ClusterPolicy, resourcePaths []string, dClient return resources, nil } -func getResourcesOfTypeFromCluster(resourceTypes []string, dClient discovery.CachedDiscoveryInterface, openAPIController *openapi.Controller) ([]*unstructured.Unstructured, error) { +func getResourcesOfTypeFromCluster(resourceTypes []string, dClient *client.Client) ([]*unstructured.Unstructured, error) { var resources []*unstructured.Unstructured for _, kind := range resourceTypes { - // TODO use lister interface - endpoint, err := getListEndpointForKind(kind, openAPIController) - if err != nil { - return nil, err - } - - listObjectRaw, err := dClient.RESTClient().Get().RequestURI(endpoint).Do().Raw() - if err != nil { - return nil, err - } - - listObject, err := engineutils.ConvertToUnstructured(listObjectRaw) - if err != nil { - return nil, err - } - - resourceList, err := listObject.ToList() + resourceList, err := dClient.ListResource(kind, "", nil) if err != nil { return nil, err } diff --git a/pkg/kyverno/apply/helper.go b/pkg/kyverno/apply/helper.go deleted file mode 100644 index 4ad4b1b6d3..0000000000 --- a/pkg/kyverno/apply/helper.go +++ /dev/null @@ -1,37 +0,0 @@ -package apply - -import ( - "fmt" - "strings" - - "github.com/nirmata/kyverno/pkg/openapi" -) - -func getListEndpointForKind(kind string, openAPIController *openapi.Controller) (string, error) { - - definitionName := openAPIController.GetDefinitionNameFromKind(kind) - definitionNameWithoutPrefix := strings.Replace(definitionName, "io.k8s.", "", -1) - - parts := strings.Split(definitionNameWithoutPrefix, ".") - definitionPrefix := strings.Join(parts[:len(parts)-1], ".") - - defPrefixToApiPrefix := map[string]string{ - "api.core.v1": "/api/v1", - "api.apps.v1": "/apis/apps/v1", - "api.batch.v1": "/apis/batch/v1", - "api.admissionregistration.v1": "/apis/admissionregistration.k8s.io/v1", - "kube-aggregator.pkg.apis.apiregistration.v1": "/apis/apiregistration.k8s.io/v1", - "apiextensions-apiserver.pkg.apis.apiextensions.v1": "/apis/apiextensions.k8s.io/v1", - "api.autoscaling.v1": "/apis/autoscaling/v1/", - "api.storage.v1": "/apis/storage.k8s.io/v1", - "api.coordination.v1": "/apis/coordination.k8s.io/v1", - "api.scheduling.v1": "/apis/scheduling.k8s.io/v1", - "api.rbac.v1": "/apis/rbac.authorization.k8s.io/v1", - } - - if defPrefixToApiPrefix[definitionPrefix] == "" { - return "", fmt.Errorf("Unsupported resource type %v", kind) - } - - return defPrefixToApiPrefix[definitionPrefix] + "/" + strings.ToLower(kind) + "s", nil -}