From bf49d552b003072c6bc64ae838a4699c1f4028bd Mon Sep 17 00:00:00 2001 From: Kiran Kamineni Date: Tue, 16 Apr 2019 18:09:13 -0700 Subject: Replace Kind with GroupVersionKind Kind is not unique to track resources in Kubernetes GroupVersionKind is unique. We are just using that to track our data. It is abstracted behind a couple of new types for templates and resources. This change makes a lot of the old kind based operations redundant and simplified. Issue-ID: MULTICLOUD-573 Change-Id: I8f4ded2ba6a0821a8fbd679dc99ce3a44d805524 Signed-off-by: Kiran Kamineni --- src/k8splugin/plugins/generic/plugin.go | 28 ++++++++++++---------------- 1 file changed, 12 insertions(+), 16 deletions(-) (limited to 'src/k8splugin/plugins/generic/plugin.go') diff --git a/src/k8splugin/plugins/generic/plugin.go b/src/k8splugin/plugins/generic/plugin.go index b0cf609c..9073535c 100644 --- a/src/k8splugin/plugins/generic/plugin.go +++ b/src/k8splugin/plugins/generic/plugin.go @@ -24,22 +24,12 @@ import ( utils "k8splugin/internal" "k8splugin/internal/app" + "k8splugin/internal/helm" ) type genericPlugin struct { } -var kindToGVRMap = map[string]schema.GroupVersionResource{ - "ConfigMap": schema.GroupVersionResource{Group: "", Version: "v1", Resource: "configmaps"}, - "StatefulSet": schema.GroupVersionResource{Group: "apps", Version: "v1", Resource: "statefulsets"}, - "Job": schema.GroupVersionResource{Group: "batch", Version: "v1", Resource: "jobs"}, - "Pod": schema.GroupVersionResource{Group: "", Version: "v1", Resource: "pods"}, - "DaemonSet": schema.GroupVersionResource{Group: "apps", Version: "v1", Resource: "daemonsets"}, - "CustomResourceDefinition": schema.GroupVersionResource{ - Group: "apiextensions.k8s.io", Version: "v1beta1", Resource: "customresourcedefinitions", - }, -} - // Create deployment object in a specific Kubernetes cluster func (g genericPlugin) Create(yamlFilePath string, namespace string, client *app.KubernetesClient) (string, error) { if namespace == "" { @@ -79,7 +69,7 @@ func (g genericPlugin) Create(yamlFilePath string, namespace string, client *app } // Delete an existing deployment hosted in a specific Kubernetes cluster -func (g genericPlugin) Delete(kind string, name string, namespace string, client *app.KubernetesClient) error { +func (g genericPlugin) Delete(resource helm.KubernetesResource, namespace string, client *app.KubernetesClient) error { if namespace == "" { namespace = "default" } @@ -90,14 +80,20 @@ func (g genericPlugin) Delete(kind string, name string, namespace string, client } dynClient := client.GetDynamicClient() - gvr, ok := kindToGVRMap[kind] - if !ok { - return pkgerrors.New("GVR not found for: " + kind) + mapper := client.GetMapper() + + mapping, err := mapper.RESTMapping(schema.GroupKind{ + Group: resource.GVK.Group, + Kind: resource.GVK.Kind, + }, resource.GVK.Version) + if err != nil { + return pkgerrors.Wrap(err, "Mapping kind to resource error") } + gvr := mapping.Resource log.Printf("Using gvr: %s, %s, %s", gvr.Group, gvr.Version, gvr.Resource) - err := dynClient.Resource(gvr).Namespace(namespace).Delete(name, opts) + err = dynClient.Resource(gvr).Namespace(namespace).Delete(resource.Name, opts) if err != nil { return pkgerrors.Wrap(err, "Delete object error") } -- cgit 1.2.3-korg