From a596832875b28dc506c0778dca6f314889b331fa Mon Sep 17 00:00:00 2001 From: Maggie Chen Date: Fri, 21 Oct 2022 19:42:11 -0400 Subject: [PATCH] Fix delete issue (#268) Signed-off-by: Maggie Chen Signed-off-by: Maggie Chen --- .../kubernetes/sync_appsubstatus.go | 64 +++++++++++-------- .../kubernetes/sync_appsubstatus_test.go | 52 +++++++++++++++ 2 files changed, 91 insertions(+), 25 deletions(-) diff --git a/pkg/synchronizer/kubernetes/sync_appsubstatus.go b/pkg/synchronizer/kubernetes/sync_appsubstatus.go index 279971d9..a30e1bd6 100644 --- a/pkg/synchronizer/kubernetes/sync_appsubstatus.go +++ b/pkg/synchronizer/kubernetes/sync_appsubstatus.go @@ -309,35 +309,49 @@ func (sync *KubeSynchronizer) SyncAppsubClusterStatus(appsub *appv1.Subscription failedUnitStatuses := []v1alpha1.SubscriptionUnitStatus{} newUnitStatus := []v1alpha1.SubscriptionUnitStatus{} - for _, resource := range appsubClusterStatus.SubscriptionPackageStatus { - uS := &v1alpha1.SubscriptionUnitStatus{ - Name: resource.Name, - APIVersion: resource.APIVersion, - Kind: resource.Kind, - Namespace: resource.Namespace, - Phase: v1alpha1.PackagePhase(resource.Phase), - Message: resource.Message, - LastUpdateTime: metaV1.Time{Time: time.Now()}, - } - newUnitStatus = append(newUnitStatus, *uS) + if appsub != nil { + // Check if the subscription still exist + subscriptionExist := true + + if err := sync.LocalClient.Get(context.TODO(), + client.ObjectKey{Name: appsub.Name, Namespace: appsub.Namespace}, appsub); err != nil { + if errors.IsNotFound(err) { + klog.Errorf("failed to get appsub err: %v", err) - if resource.Phase == string(v1alpha1.PackageDeployFailed) { - uS := &v1alpha1.SubscriptionUnitStatus{ - Name: resource.Name, - Namespace: appsubClusterStatus.AppSub.Namespace, - Phase: v1alpha1.PackagePhase(resource.Phase), - Message: resource.Message, - LastUpdateTime: metaV1.Time{ - Time: time.Now(), - }, + subscriptionExist = false } - failedUnitStatuses = append(failedUnitStatuses, *uS) - } - } + if subscriptionExist { + for _, resource := range appsubClusterStatus.SubscriptionPackageStatus { + uS := &v1alpha1.SubscriptionUnitStatus{ + Name: resource.Name, + APIVersion: resource.APIVersion, + Kind: resource.Kind, + Namespace: resource.Namespace, + Phase: v1alpha1.PackagePhase(resource.Phase), + Message: resource.Message, + LastUpdateTime: metaV1.Time{Time: time.Now()}, + } + newUnitStatus = append(newUnitStatus, *uS) + + if resource.Phase == string(v1alpha1.PackageDeployFailed) { + uS := &v1alpha1.SubscriptionUnitStatus{ + Name: resource.Name, + Namespace: appsubClusterStatus.AppSub.Namespace, + Phase: v1alpha1.PackagePhase(resource.Phase), + Message: resource.Message, + LastUpdateTime: metaV1.Time{ + Time: time.Now(), + }, + } + + failedUnitStatuses = append(failedUnitStatuses, *uS) + } + } + } - if appsub != nil { - sync.recordAppSubStatusEvents(appsub, "Delete", newUnitStatus) + sync.recordAppSubStatusEvents(appsub, "Delete", newUnitStatus) + } } if len(failedUnitStatuses) == 0 { diff --git a/pkg/synchronizer/kubernetes/sync_appsubstatus_test.go b/pkg/synchronizer/kubernetes/sync_appsubstatus_test.go index b2f9e9d3..d34cc13d 100644 --- a/pkg/synchronizer/kubernetes/sync_appsubstatus_test.go +++ b/pkg/synchronizer/kubernetes/sync_appsubstatus_test.go @@ -65,6 +65,30 @@ var ( }, } + toBeDeletedType = types.NamespacedName{ + Name: "hive-clusterimagesets-subscription-to-be-deleted", + Namespace: "default", + } + + appToBeDeleted = &appv1.Subscription{ + ObjectMeta: metav1.ObjectMeta{ + Name: "hive-clusterimagesets-subscription-to-be-deleted", + Namespace: "default", + Annotations: map[string]string{ + "apps.open-cluster-management.io/git-branch": "release-2.6", + "apps.open-cluster-management.io/git-path": "clusterImageSets/fast", + "meta.helm.sh/release-namespace": "default", + }, + Generation: 1, + Labels: map[string]string{ + "app": "hive-clusterimagesets", + "app.kubernetes.io/managed-by": "Helm", + "subscription-pause": "false", + }, + }, + Spec: appv1.SubscriptionSpec{}, + } + legacyAppSubNoChannel = &appv1.Subscription{ ObjectMeta: metav1.ObjectMeta{ Name: "hive-clusterimagesets-subscription-fast-10", @@ -143,6 +167,15 @@ var ( Phase: string(appSubStatusV1alpha1.PackageDeployFailed), Message: "Success", } + + rmAppSubUnitStatus = SubscriptionUnitStatus{ + Name: "pkg2", + Namespace: appToBeDeleted.Namespace, + APIVersion: "v1", + Kind: "ConfigMap", + Phase: string(appSubStatusV1alpha1.PackageDeployFailed), + Message: "Success", + } ) var _ = Describe("test create/update/delete appsub status for standalone and managed cluster", func() { @@ -498,5 +531,24 @@ var _ = Describe("test create/update/delete appsub status for standalone and man appsubStatuses = s.getResourcesByLegacySubStatus(appsubInvalidresource) Expect(appsubStatuses).To(Equal(expectedAppSubStatuses)) + // create appsub for a to be deleted appsub + appsubToBeDeleted := appToBeDeleted.DeepCopy() + Expect(k8sClient.Create(context.TODO(), appsubToBeDeleted)).NotTo(HaveOccurred()) + time.Sleep(4 * time.Second) + + appSubUnitStatuses = []SubscriptionUnitStatus{} + appSubUnitStatuses = append(appSubUnitStatuses, rmAppSubUnitStatus) + + // Delete + rmClusterStatus := SubscriptionClusterStatus{ + Cluster: "cluster1", + AppSub: toBeDeletedType, + Action: "DELETE", + SubscriptionPackageStatus: appSubUnitStatuses, + } + + err = s.SyncAppsubClusterStatus(appsubToBeDeleted, rmClusterStatus, nil, nil) + Expect(err).NotTo(HaveOccurred()) + }) })