Skip to content

Commit

Permalink
Fix delete issue (#268)
Browse files Browse the repository at this point in the history
Signed-off-by: Maggie Chen <magchen@redhat.com>

Signed-off-by: Maggie Chen <magchen@redhat.com>
  • Loading branch information
chenz4027 authored Oct 21, 2022
1 parent 33433fd commit a596832
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 25 deletions.
64 changes: 39 additions & 25 deletions pkg/synchronizer/kubernetes/sync_appsubstatus.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
52 changes: 52 additions & 0 deletions pkg/synchronizer/kubernetes/sync_appsubstatus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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())

})
})

0 comments on commit a596832

Please sign in to comment.