Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 47 additions & 12 deletions cmd/unpublish.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ package cmd
import (
"context"
"fmt"
"time"

"github.com/spf13/cobra"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/wait"
corev1alpha1 "kubesphere.io/api/core/v1alpha1"
runtimeclient "sigs.k8s.io/controller-runtime/pkg/client"

Expand All @@ -33,6 +35,7 @@ func unpublishExtensionCmd() *cobra.Command {
}

func (o *unpublishOptions) unpublish(cmd *cobra.Command, args []string) error {
ctx := context.Background()
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

It is recommended to use cmd.Context() instead of context.Background(). This allows the command to respect context cancellation, such as when a user interrupts the process (e.g., via Ctrl+C), ensuring a more responsive CLI experience.

Suggested change
ctx := context.Background()
ctx := cmd.Context()

name := args[0]
fmt.Printf("unpublish extension %s\n", name)

Expand All @@ -45,11 +48,25 @@ func (o *unpublishOptions) unpublish(cmd *cobra.Command, args []string) error {
}

extensionVersions := &corev1alpha1.ExtensionVersionList{}
if err = genericClient.List(context.Background(), extensionVersions, runtimeclient.MatchingLabels{
if err = genericClient.List(ctx, extensionVersions, runtimeclient.MatchingLabels{
corev1alpha1.ExtensionReferenceLabel: name,
}); err != nil {
return err
}

installPlan := &corev1alpha1.InstallPlan{
TypeMeta: metav1.TypeMeta{
APIVersion: "kubesphere.io/v1alpha1",
Kind: "InstallPlan",
},
ObjectMeta: metav1.ObjectMeta{
Name: name,
},
}
if err := deleteObjAndWait(ctx, genericClient, installPlan, time.Minute, 2*time.Second); err != nil {
return err
}

objs := make([]runtimeclient.Object, 0)
for i := range extensionVersions.Items {
version := &extensionVersions.Items[i]
Expand All @@ -65,15 +82,7 @@ func (o *unpublishOptions) unpublish(cmd *cobra.Command, args []string) error {
}, version)
}

return deleteObjs(genericClient, append(objs, &corev1alpha1.InstallPlan{
TypeMeta: metav1.TypeMeta{
APIVersion: "kubesphere.io/v1alpha1",
Kind: "InstallPlan",
},
ObjectMeta: metav1.ObjectMeta{
Name: name,
},
}, &corev1alpha1.Extension{
return deleteObjs(ctx, genericClient, append(objs, &corev1alpha1.Extension{
TypeMeta: metav1.TypeMeta{
APIVersion: "kubesphere.io/v1alpha1",
Kind: "Extension",
Expand All @@ -84,12 +93,38 @@ func (o *unpublishOptions) unpublish(cmd *cobra.Command, args []string) error {
})...)
}

func deleteObjs(c runtimeclient.Client, objs ...runtimeclient.Object) error {
func deleteObjs(ctx context.Context, c runtimeclient.Client, objs ...runtimeclient.Object) error {
for _, obj := range objs {
fmt.Printf("deleting %s %s\n", obj.GetObjectKind().GroupVersionKind().Kind, obj.GetName())
if err := c.Delete(context.Background(), obj); err != nil && !errors.IsNotFound(err) {
if err := c.Delete(ctx, obj); err != nil && !errors.IsNotFound(err) {
return err
}
}
return nil
}

func deleteObjAndWait(ctx context.Context, c runtimeclient.Client, obj runtimeclient.Object, timeout, interval time.Duration) error {
fmt.Printf("deleting %s %s\n", obj.GetObjectKind().GroupVersionKind().Kind, obj.GetName())
if err := c.Delete(ctx, obj); err != nil {
if errors.IsNotFound(err) {
return nil
}
return err
}

key := runtimeclient.ObjectKeyFromObject(obj)
current, ok := obj.DeepCopyObject().(runtimeclient.Object)
if !ok {
return fmt.Errorf("object %T does not implement client.Object", obj)
}

return wait.PollUntilContextTimeout(ctx, interval, timeout, true, func(ctx context.Context) (bool, error) {
if err := c.Get(ctx, key, current); err != nil {
if errors.IsNotFound(err) {
return true, nil
}
return false, err
}
return false, nil
})
Comment on lines +121 to +129
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The object cloning and type assertion are performed inside the polling loop. Moving these operations outside the loop improves efficiency by avoiding redundant allocations and checks in every iteration.

Suggested change
return wait.PollUntilContextTimeout(ctx, interval, timeout, true, func(ctx context.Context) (bool, error) {
current, ok := obj.DeepCopyObject().(runtimeclient.Object)
if !ok {
return false, fmt.Errorf("object %T does not implement client.Object", obj)
}
if err := c.Get(ctx, key, current); err != nil {
if errors.IsNotFound(err) {
return true, nil
}
return false, err
}
return false, nil
})
current, ok := obj.DeepCopyObject().(runtimeclient.Object)
if !ok {
return fmt.Errorf("object %T does not implement client.Object", obj)
}
return wait.PollUntilContextTimeout(ctx, interval, timeout, true, func(ctx context.Context) (bool, error) {
if err := c.Get(ctx, key, current); err != nil {
if errors.IsNotFound(err) {
return true, nil
}
return false, err
}
return false, nil
})

}
Loading