From 258e59bf8563021f4eded42b33c6cc61a6ffebd8 Mon Sep 17 00:00:00 2001 From: Lukasz Rajewski Date: Tue, 5 Oct 2021 14:36:01 +0200 Subject: Fixed issue with order of deleted resources For delete operation order of resources is reverse to the order used for creation Issue-ID: MULTICLOUD-1398 Signed-off-by: Lukasz Rajewski Change-Id: I3f34c6000222e82c34f59042e99d2c37a343dfa5 --- src/k8splugin/internal/app/client.go | 17 ++++++----- src/k8splugin/internal/app/config_backend.go | 2 +- src/k8splugin/internal/app/instance.go | 6 ++-- src/k8splugin/internal/helm/helm.go | 10 +++++++ src/k8splugin/internal/helm/helm_test.go | 43 ++++++++++++++++++++++++++++ 5 files changed, 65 insertions(+), 13 deletions(-) diff --git a/src/k8splugin/internal/app/client.go b/src/k8splugin/internal/app/client.go index 06c4c464..f70dc0a5 100644 --- a/src/k8splugin/internal/app/client.go +++ b/src/k8splugin/internal/app/client.go @@ -550,18 +550,17 @@ func (k *KubernetesClient) DeleteKind(resource helm.KubernetesResource, namespac return pkgerrors.Wrap(err, "Error loading plugin") } - name, err := pluginImpl.Get(resource, namespace, k) + err = pluginImpl.Delete(resource, namespace, k) - if (err == nil && name == resource.Name) || (err != nil && strings.Contains(err.Error(), "not found") == false) { - err = pluginImpl.Delete(resource, namespace, k) - if err != nil { + if err != nil { + if strings.Contains(err.Error(), "not found") == false { return pkgerrors.Wrap(err, "Error deleting "+resource.Name) + } else { + log.Warn("Resource already does not exist", log.Fields{ + "gvk": resource.GVK, + "resource": resource.Name, + }) } - } else { - log.Warn("Resource does not exist, Skipping delete", log.Fields{ - "gvk": resource.GVK, - "resource": resource.Name, - }) } return nil diff --git a/src/k8splugin/internal/app/config_backend.go b/src/k8splugin/internal/app/config_backend.go index be11e8ce..1f22922a 100644 --- a/src/k8splugin/internal/app/config_backend.go +++ b/src/k8splugin/internal/app/config_backend.go @@ -551,7 +551,7 @@ func scheduleResources(c chan configResourceList) { //Move onto the next cloud region continue } - err = k8sClient.deleteResources(data.resources, inst.Namespace) + err = k8sClient.deleteResources(helm.GetReverseK8sResources(data.resources), inst.Namespace) if err != nil { log.Printf("Error Deleting resources: %s", err.Error()) continue diff --git a/src/k8splugin/internal/app/instance.go b/src/k8splugin/internal/app/instance.go index 63fe042b..c4cce5ed 100644 --- a/src/k8splugin/internal/app/instance.go +++ b/src/k8splugin/internal/app/instance.go @@ -322,7 +322,7 @@ func (v *InstanceClient) Create(i InstanceRequest) (InstanceResponse, error) { if err != nil { if len(createdResources) > 0 { log.Printf("[Instance] Reverting created resources on Error: %s", err.Error()) - k8sClient.deleteResources(createdResources, profile.Namespace) + k8sClient.deleteResources(helm.GetReverseK8sResources(createdResources), profile.Namespace) } log.Printf(" Instance: %s, Main rss are failed, skip post-install and remove instance in DB", id) //main rss creation failed -> remove instance in DB @@ -764,7 +764,7 @@ func (v *InstanceClient) Delete(id string) error { return pkgerrors.Wrap(err, "Cleanup Config Resources") } - err = k8sClient.deleteResources(inst.Resources, inst.Namespace) + err = k8sClient.deleteResources(helm.GetReverseK8sResources(inst.Resources), inst.Namespace) if err != nil { return pkgerrors.Wrap(err, "Deleting Instance Resources") } @@ -872,7 +872,7 @@ func (v *InstanceClient) RecoverCreateOrDelete(id string) error { return } - err = k8sClient.deleteResources(instance.Resources, instance.Namespace) + err = k8sClient.deleteResources(helm.GetReverseK8sResources(instance.Resources), instance.Namespace) if err != nil { log.Printf(" Error running deleting instance resources, error: %s", err) return diff --git a/src/k8splugin/internal/helm/helm.go b/src/k8splugin/internal/helm/helm.go index 6064b2c8..b27c8aee 100644 --- a/src/k8splugin/internal/helm/helm.go +++ b/src/k8splugin/internal/helm/helm.go @@ -249,3 +249,13 @@ func getGroupVersionKind(data string) (schema.GroupVersionKind, error) { return *gvk, nil } + +//GetReverseK8sResources reverse list of resources for delete purpose +func GetReverseK8sResources(resources []KubernetesResource) []KubernetesResource { + reversed := []KubernetesResource{} + + for i := len(resources) - 1; i >= 0; i-- { + reversed = append(reversed, resources[i]) + } + return reversed +} diff --git a/src/k8splugin/internal/helm/helm_test.go b/src/k8splugin/internal/helm/helm_test.go index b805b59b..951ff92b 100644 --- a/src/k8splugin/internal/helm/helm_test.go +++ b/src/k8splugin/internal/helm/helm_test.go @@ -26,6 +26,7 @@ import ( "testing" "gopkg.in/yaml.v2" + "k8s.io/apimachinery/pkg/runtime/schema" ) func TestProcessValues(t *testing.T) { @@ -265,3 +266,45 @@ func TestGenerateKubernetesArtifacts(t *testing.T) { }) } } + +func TestReverseResources(t *testing.T) { + + t.Run("Successfully reverse resources", func(t *testing.T) { + data := []KubernetesResource{ + { + GVK: schema.GroupVersionKind{ + Group: "apps", + Version: "v1", + Kind: "Deployment"}, + Name: "deployment-1", + }, + { + GVK: schema.GroupVersionKind{ + Group: "apps", + Version: "v1", + Kind: "Deployment"}, + Name: "deployment-2", + }, + { + GVK: schema.GroupVersionKind{ + Group: "", + Version: "v1", + Kind: "Service"}, + Name: "service-1", + }, + { + GVK: schema.GroupVersionKind{ + Group: "", + Version: "v1", + Kind: "Service"}, + Name: "service-2", + }, + } + + reversed := GetReverseK8sResources(data) + + if reversed[0] != data[len(data)-1] { + t.Fatalf("Unexpected k8s resource at position 0 %s", reversed[0]) + } + }) +} -- cgit 1.2.3-korg