Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

istioctl: support deleting multiple waypoints in one command #47539

Merged
merged 2 commits into from
Oct 25, 2023
Merged
Show file tree
Hide file tree
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: 37 additions & 22 deletions istioctl/pkg/waypoint/waypoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ import (
"context"
"fmt"
"strings"
"sync"
"text/tabwriter"

"github.com/hashicorp/go-multierror"
"github.com/spf13/cobra"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -157,38 +159,51 @@ func Cmd(ctx cli.Context) *cobra.Command {
# Delete a waypoint by name, which can obtain from istioctl x waypoint list
istioctl x waypoint delete waypoint-name --namespace default`,
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) > 1 {
return fmt.Errorf("too many arguments, expected 0 or 1")
}
kubeClient, err := ctx.CLIClient()
if err != nil {
return fmt.Errorf("failed to create Kubernetes client: %v", err)
}
if len(args) == 1 {
name := args[0]
ns := ctx.NamespaceOrDefault(ctx.Namespace())
gw, err := kubeClient.GatewayAPI().GatewayV1beta1().Gateways(ns).Get(context.Background(), name, metav1.GetOptions{})
if err != nil {
if errors.IsNotFound(err) {
fmt.Fprintf(cmd.OutOrStdout(), "waypoint %v/%v not found\n", ns, name)
return nil
}
return err
}
if err := kubeClient.GatewayAPI().GatewayV1beta1().Gateways(ns).
if len(args) == 0 {
gw := makeGateway(true)
if err = kubeClient.GatewayAPI().GatewayV1beta1().Gateways(gw.Namespace).
Delete(context.Background(), gw.Name, metav1.DeleteOptions{}); err != nil {
return err
}
fmt.Fprintf(cmd.OutOrStdout(), "waypoint %v/%v deleted\n", ns, name)
fmt.Fprintf(cmd.OutOrStdout(), "waypoint %v/%v deleted\n", gw.Namespace, gw.Name)
return nil
}
gw := makeGateway(true)
if err = kubeClient.GatewayAPI().GatewayV1beta1().Gateways(gw.Namespace).
Delete(context.Background(), gw.Name, metav1.DeleteOptions{}); err != nil {
return err
ns := ctx.NamespaceOrDefault(ctx.Namespace())

wg := sync.WaitGroup{}
var mu sync.Mutex
multiErr := &multierror.Error{}
for _, name := range args {
wg.Add(1)
go func(name string) {
defer wg.Done()
gw, err := kubeClient.GatewayAPI().GatewayV1beta1().Gateways(ns).Get(context.Background(), name, metav1.GetOptions{})
if err != nil {
if errors.IsNotFound(err) {
fmt.Fprintf(cmd.OutOrStdout(), "waypoint %v/%v not found\n", ns, name)
return
}
mu.Lock()
multiErr = multierror.Append(multiErr, err)
mu.Unlock()
return
}
if err := kubeClient.GatewayAPI().GatewayV1beta1().Gateways(ns).
Delete(context.Background(), gw.Name, metav1.DeleteOptions{}); err != nil {
mu.Lock()
multiErr = multierror.Append(multiErr, err)
mu.Unlock()
return
}
fmt.Fprintf(cmd.OutOrStdout(), "waypoint %v/%v deleted\n", ns, name)
}(name)
}
fmt.Fprintf(cmd.OutOrStdout(), "waypoint %v/%v deleted\n", gw.Namespace, gw.Name)
return nil
wg.Wait()
return multiErr.ErrorOrNil()
},
}
waypointListCmd := &cobra.Command{
Expand Down
6 changes: 6 additions & 0 deletions releasenotes/notes/47539.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
apiVersion: release-notes/v2
kind: feature
area: istioctl
releaseNotes:
- |
**Added** support for deleting multiple waypoints at once via `istioctl x waypoint delete <waypoint1> <waypoint2> ...`