@@ -3,11 +3,13 @@ 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/util/wait"
1113 corev1alpha1 "kubesphere.io/api/core/v1alpha1"
1214 runtimeclient "sigs.k8s.io/controller-runtime/pkg/client"
1315
@@ -33,6 +35,7 @@ func unpublishExtensionCmd() *cobra.Command {
3335}
3436
3537func (o * unpublishOptions ) unpublish (cmd * cobra.Command , args []string ) error {
38+ ctx := context .Background ()
3639 name := args [0 ]
3740 fmt .Printf ("unpublish extension %s\n " , name )
3841
@@ -45,11 +48,25 @@ func (o *unpublishOptions) unpublish(cmd *cobra.Command, args []string) error {
4548 }
4649
4750 extensionVersions := & corev1alpha1.ExtensionVersionList {}
48- if err = genericClient .List (context . Background () , extensionVersions , runtimeclient.MatchingLabels {
51+ if err = genericClient .List (ctx , extensionVersions , runtimeclient.MatchingLabels {
4952 corev1alpha1 .ExtensionReferenceLabel : name ,
5053 }); err != nil {
5154 return err
5255 }
56+
57+ installPlan := & corev1alpha1.InstallPlan {
58+ TypeMeta : metav1.TypeMeta {
59+ APIVersion : "kubesphere.io/v1alpha1" ,
60+ Kind : "InstallPlan" ,
61+ },
62+ ObjectMeta : metav1.ObjectMeta {
63+ Name : name ,
64+ },
65+ }
66+ if err := deleteObjAndWait (ctx , genericClient , installPlan , time .Minute , 2 * time .Second ); err != nil {
67+ return err
68+ }
69+
5370 objs := make ([]runtimeclient.Object , 0 )
5471 for i := range extensionVersions .Items {
5572 version := & extensionVersions .Items [i ]
@@ -65,15 +82,7 @@ func (o *unpublishOptions) unpublish(cmd *cobra.Command, args []string) error {
6582 }, version )
6683 }
6784
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 {
85+ return deleteObjs (ctx , genericClient , append (objs , & corev1alpha1.Extension {
7786 TypeMeta : metav1.TypeMeta {
7887 APIVersion : "kubesphere.io/v1alpha1" ,
7988 Kind : "Extension" ,
@@ -84,12 +93,38 @@ func (o *unpublishOptions) unpublish(cmd *cobra.Command, args []string) error {
8493 })... )
8594}
8695
87- func deleteObjs (c runtimeclient.Client , objs ... runtimeclient.Object ) error {
96+ func deleteObjs (ctx context. Context , c runtimeclient.Client , objs ... runtimeclient.Object ) error {
8897 for _ , obj := range objs {
8998 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 ) {
99+ if err := c .Delete (ctx , obj ); err != nil && ! errors .IsNotFound (err ) {
91100 return err
92101 }
93102 }
94103 return nil
95104}
105+
106+ func deleteObjAndWait (ctx context.Context , c runtimeclient.Client , obj runtimeclient.Object , timeout , interval time.Duration ) error {
107+ fmt .Printf ("deleting %s %s\n " , obj .GetObjectKind ().GroupVersionKind ().Kind , obj .GetName ())
108+ if err := c .Delete (ctx , obj ); err != nil {
109+ if errors .IsNotFound (err ) {
110+ return nil
111+ }
112+ return err
113+ }
114+
115+ key := runtimeclient .ObjectKeyFromObject (obj )
116+ current , ok := obj .DeepCopyObject ().(runtimeclient.Object )
117+ if ! ok {
118+ return fmt .Errorf ("object %T does not implement client.Object" , obj )
119+ }
120+
121+ return wait .PollUntilContextTimeout (ctx , interval , timeout , true , func (ctx context.Context ) (bool , error ) {
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