@@ -3,11 +3,14 @@ package cmd
33import (
44 "context"
55 "fmt"
6+ "time"
67
78 "github.com/spf13/cobra"
89 corev1 "k8s.io/api/core/v1"
910 "k8s.io/apimachinery/pkg/api/errors"
1011 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
12+ "k8s.io/apimachinery/pkg/types"
13+ "k8s.io/apimachinery/pkg/util/wait"
1114 corev1alpha1 "kubesphere.io/api/core/v1alpha1"
1215 runtimeclient "sigs.k8s.io/controller-runtime/pkg/client"
1316
@@ -33,6 +36,7 @@ func unpublishExtensionCmd() *cobra.Command {
3336}
3437
3538func (o * unpublishOptions ) unpublish (cmd * cobra.Command , args []string ) error {
39+ ctx := context .Background ()
3640 name := args [0 ]
3741 fmt .Printf ("unpublish extension %s\n " , name )
3842
@@ -45,11 +49,25 @@ func (o *unpublishOptions) unpublish(cmd *cobra.Command, args []string) error {
4549 }
4650
4751 extensionVersions := & corev1alpha1.ExtensionVersionList {}
48- if err = genericClient .List (context . Background () , extensionVersions , runtimeclient.MatchingLabels {
52+ if err = genericClient .List (ctx , extensionVersions , runtimeclient.MatchingLabels {
4953 corev1alpha1 .ExtensionReferenceLabel : name ,
5054 }); err != nil {
5155 return err
5256 }
57+
58+ installPlan := & corev1alpha1.InstallPlan {
59+ TypeMeta : metav1.TypeMeta {
60+ APIVersion : "kubesphere.io/v1alpha1" ,
61+ Kind : "InstallPlan" ,
62+ },
63+ ObjectMeta : metav1.ObjectMeta {
64+ Name : name ,
65+ },
66+ }
67+ if err := deleteObjAndWait (ctx , genericClient , installPlan , time .Minute , 2 * time .Second ); err != nil {
68+ return err
69+ }
70+
5371 objs := make ([]runtimeclient.Object , 0 )
5472 for i := range extensionVersions .Items {
5573 version := & extensionVersions .Items [i ]
@@ -65,15 +83,7 @@ func (o *unpublishOptions) unpublish(cmd *cobra.Command, args []string) error {
6583 }, version )
6684 }
6785
68- return deleteObjs (genericClient , append (objs , & corev1alpha1.InstallPlan {
69- TypeMeta : metav1.TypeMeta {
70- APIVersion : "kubesphere.io/v1alpha1" ,
71- Kind : "InstallPlan" ,
72- },
73- ObjectMeta : metav1.ObjectMeta {
74- Name : name ,
75- },
76- }, & corev1alpha1.Extension {
86+ return deleteObjs (ctx , genericClient , append (objs , & corev1alpha1.Extension {
7787 TypeMeta : metav1.TypeMeta {
7888 APIVersion : "kubesphere.io/v1alpha1" ,
7989 Kind : "Extension" ,
@@ -84,12 +94,37 @@ func (o *unpublishOptions) unpublish(cmd *cobra.Command, args []string) error {
8494 })... )
8595}
8696
87- func deleteObjs (c runtimeclient.Client , objs ... runtimeclient.Object ) error {
97+ func deleteObjs (ctx context. Context , c runtimeclient.Client , objs ... runtimeclient.Object ) error {
8898 for _ , obj := range objs {
8999 fmt .Printf ("deleting %s %s\n " , obj .GetObjectKind ().GroupVersionKind ().Kind , obj .GetName ())
90- if err := c .Delete (context . Background () , obj ); err != nil && ! errors .IsNotFound (err ) {
100+ if err := c .Delete (ctx , obj ); err != nil && ! errors .IsNotFound (err ) {
91101 return err
92102 }
93103 }
94104 return nil
95105}
106+
107+ func deleteObjAndWait (ctx context.Context , c runtimeclient.Client , obj runtimeclient.Object , timeout , interval time.Duration ) error {
108+ fmt .Printf ("deleting %s %s\n " , obj .GetObjectKind ().GroupVersionKind ().Kind , obj .GetName ())
109+ if err := c .Delete (ctx , obj ); err != nil {
110+ if errors .IsNotFound (err ) {
111+ return nil
112+ }
113+ return err
114+ }
115+
116+ key := types.NamespacedName {Name : obj .GetName (), Namespace : obj .GetNamespace ()}
117+ return wait .PollUntilContextTimeout (ctx , interval , timeout , true , func (ctx context.Context ) (bool , error ) {
118+ current , ok := obj .DeepCopyObject ().(runtimeclient.Object )
119+ if ! ok {
120+ return false , fmt .Errorf ("object %T does not implement client.Object" , obj )
121+ }
122+ if err := c .Get (ctx , key , current ); err != nil {
123+ if errors .IsNotFound (err ) {
124+ return true , nil
125+ }
126+ return false , err
127+ }
128+ return false , nil
129+ })
130+ }
0 commit comments