Skip to content

Commit

Permalink
refactor: Using cobra contexte
Browse files Browse the repository at this point in the history
Signed-off-by: Vincent Boutour <[email protected]>
  • Loading branch information
ViBiOh committed Jan 8, 2023
1 parent b9efc07 commit 46608f3
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 24 deletions.
6 changes: 3 additions & 3 deletions cmd/choose.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,16 @@ func getCommonNamespace(kube client.Kube, namespace string) string {
return ""
}

func getCommonObjects(namespace string, lister resource.Lister) []string {
func getCommonObjects(ctx context.Context, namespace string, lister resource.Lister) []string {
output := make(chan string, len(clients))
successChan := make(chan bool, len(clients))

go func() {
defer close(output)
defer close(successChan)

clients.Execute(func(kube client.Kube) error {
items, err := lister(context.Background(), kube, getCommonNamespace(kube, namespace), metav1.ListOptions{})
clients.Execute(ctx, func(ctx context.Context, kube client.Kube) error {
items, err := lister(ctx, kube, getCommonNamespace(kube, namespace), metav1.ListOptions{})
if err != nil {
return err
}
Expand Down
6 changes: 3 additions & 3 deletions cmd/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ var imageCmd = &cobra.Command{
return nil, cobra.ShellCompDirectiveError
}

return getCommonObjects(viper.GetString("namespace"), lister), cobra.ShellCompDirectiveNoFileComp
return getCommonObjects(cmd.Context(), viper.GetString("namespace"), lister), cobra.ShellCompDirectiveNoFileComp
}

return nil, cobra.ShellCompDirectiveNoFileComp
Expand All @@ -46,15 +46,15 @@ var imageCmd = &cobra.Command{
resourceType := args[0]
resourceName := args[1]

ctx, cancel := context.WithCancel(context.Background())
ctx, cancel := context.WithCancel(cmd.Context())
defer cancel()

go func() {
waitForEnd(syscall.SIGINT, syscall.SIGTERM)
cancel()
}()

clients.Execute(func(kube client.Kube) error {
clients.Execute(ctx, func(ctx context.Context, kube client.Kube) error {
podTemplate, err := resource.PodTemplateGetter(ctx, kube, resourceType, resourceName)
if err != nil {
return err
Expand Down
6 changes: 3 additions & 3 deletions cmd/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ var logCmd = &cobra.Command{
return nil, cobra.ShellCompDirectiveError
}

return getCommonObjects(viper.GetString("namespace"), lister), cobra.ShellCompDirectiveNoFileComp
return getCommonObjects(cmd.Context(), viper.GetString("namespace"), lister), cobra.ShellCompDirectiveNoFileComp
}

return nil, cobra.ShellCompDirectiveNoFileComp
Expand All @@ -78,7 +78,7 @@ var logCmd = &cobra.Command{
return errors.New("either labels or `TYPE NAME` args must be specified")
}

ctx, cancel := context.WithCancel(context.Background())
ctx, cancel := context.WithCancel(cmd.Context())
defer cancel()

go func() {
Expand Down Expand Up @@ -120,7 +120,7 @@ var logCmd = &cobra.Command{
resourceName = args[1]
}

clients.Execute(func(kube client.Kube) error {
clients.Execute(ctx, func(ctx context.Context, kube client.Kube) error {
podWatcher, err := resource.WatchPods(ctx, kube, resourceType, resourceName, labelsSelector, dryRun)
if err != nil {
return fmt.Errorf("watch pods: %w", err)
Expand Down
6 changes: 3 additions & 3 deletions cmd/port_forward.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ var portForwardCmd = &cobra.Command{
return nil, cobra.ShellCompDirectiveError
}

return getCommonObjects(viper.GetString("namespace"), lister), cobra.ShellCompDirectiveNoFileComp
return getCommonObjects(cmd.Context(), viper.GetString("namespace"), lister), cobra.ShellCompDirectiveNoFileComp
}

return nil, cobra.ShellCompDirectiveNoFileComp
Expand All @@ -76,7 +76,7 @@ var portForwardCmd = &cobra.Command{
remotePort = ports[0]
}

ctx, cancel := context.WithCancel(context.Background())
ctx, cancel := context.WithCancel(cmd.Context())
defer cancel()

output.Std("", "Listening tcp on %d", localPort)
Expand All @@ -92,7 +92,7 @@ var portForwardCmd = &cobra.Command{
cancel()
}()

clients.Execute(func(kube client.Kube) error {
clients.Execute(ctx, func(ctx context.Context, kube client.Kube) error {
remotePort := remotePort

if resource.IsService(resourceType) {
Expand Down
6 changes: 3 additions & 3 deletions cmd/restart.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ var restartCmd = &cobra.Command{
return nil, cobra.ShellCompDirectiveError
}

return getCommonObjects(viper.GetString("namespace"), lister), cobra.ShellCompDirectiveNoFileComp
return getCommonObjects(cmd.Context(), viper.GetString("namespace"), lister), cobra.ShellCompDirectiveNoFileComp
}

return nil, cobra.ShellCompDirectiveNoFileComp
Expand All @@ -60,7 +60,7 @@ var restartCmd = &cobra.Command{
resourceType := args[0]
resourceName := args[1]

ctx, cancel := context.WithCancel(context.Background())
ctx, cancel := context.WithCancel(cmd.Context())
defer cancel()

var patch restartPatch
Expand All @@ -74,7 +74,7 @@ var restartCmd = &cobra.Command{
return
}

clients.Execute(func(kube client.Kube) error {
clients.Execute(ctx, func(ctx context.Context, kube client.Kube) error {
switch resourceType {
case "ds", "daemonset", "daemonsets":
_, err := kube.AppsV1().DaemonSets(kube.Namespace).Patch(ctx, resourceName, types.MergePatchType, payload, v1.PatchOptions{})
Expand Down
7 changes: 4 additions & 3 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cmd

import (
"context"
"fmt"
"path/filepath"
"strings"
Expand Down Expand Up @@ -37,7 +38,7 @@ var rootCmd = &cobra.Command{
<-output.Done()
},
Run: func(cmd *cobra.Command, args []string) {
clients.Execute(func(kube client.Kube) error {
clients.Execute(cmd.Context(), func(ctx context.Context, kube client.Kube) error {
info, err := kube.Discovery().ServerVersion()
if err != nil {
return fmt.Errorf("get server version: %w", err)
Expand Down Expand Up @@ -172,7 +173,7 @@ func completeContext(_ *cobra.Command, _ []string, _ string) ([]string, cobra.Sh
return completeContexts, cobra.ShellCompDirectiveNoFileComp
}

func completeNamespace(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) {
func completeNamespace(cmd *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) {
lister, err := resource.ListerFor("namespace")
if err != nil {
return nil, cobra.ShellCompDirectiveError
Expand All @@ -183,7 +184,7 @@ func completeNamespace(_ *cobra.Command, _ []string, _ string) ([]string, cobra.
return nil, cobra.ShellCompDirectiveError
}

return getCommonObjects("", lister), cobra.ShellCompDirectiveDefault
return getCommonObjects(cmd.Context(), "", lister), cobra.ShellCompDirectiveDefault
}

func contains(arr []string, value string) bool {
Expand Down
6 changes: 3 additions & 3 deletions cmd/watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ var watchCmd = &cobra.Command{
Use: "watch",
Short: "Get all pods in the namespace",
Run: func(cmd *cobra.Command, args []string) {
ctx, cancel := context.WithCancel(context.Background())
ctx, cancel := context.WithCancel(cmd.Context())
defer cancel()

go func() {
Expand All @@ -50,7 +50,7 @@ var watchCmd = &cobra.Command{
watchTable := initWatchTable()
initialsPodsHash := displayInitialPods(ctx, watchTable)

clients.Execute(func(kube client.Kube) error {
clients.Execute(ctx, func(ctx context.Context, kube client.Kube) error {
watcher, err := resource.WatchPods(ctx, kube, "namespace", kube.Namespace, labelsSelector, false)
if err != nil {
return fmt.Errorf("watch pods: %w", err)
Expand Down Expand Up @@ -128,7 +128,7 @@ func displayInitialPods(ctx context.Context, watchTable *table.Table) map[string
}
}()

clients.Execute(func(kube client.Kube) error {
clients.Execute(ctx, func(ctx context.Context, kube client.Kube) error {
watcher, err := resource.WatchPods(ctx, kube, "namespace", kube.Namespace, labelsSelector, true)
if err != nil {
return fmt.Errorf("watch pods: %w", err)
Expand Down
8 changes: 5 additions & 3 deletions pkg/client/client.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package client

import (
"context"

"github.com/ViBiOh/kmux/pkg/concurrent"
"github.com/ViBiOh/kmux/pkg/output"
"k8s.io/client-go/kubernetes"
Expand All @@ -25,18 +27,18 @@ func New(name, namespace string, config *rest.Config, clientset *kubernetes.Clie
}
}

type Action func(Kube) error
type Action func(context.Context, Kube) error

type Array []Kube

func (a Array) Execute(action Action) {
func (a Array) Execute(ctx context.Context, action Action) {
parallel := concurrent.NewSimple()

for _, client := range a {
client := client

parallel.Go(func() {
if err := action(client); err != nil {
if err := action(ctx, client); err != nil {
client.Err("%s", err)
}
})
Expand Down

0 comments on commit 46608f3

Please sign in to comment.