From 2ea2ebe836993786576c9cab4fdf38341d75acca Mon Sep 17 00:00:00 2001 From: Rob Watson Date: Wed, 1 Jun 2022 22:04:20 +0200 Subject: [PATCH] Tweak CLI interface --- logs/watcher.go | 34 +++++++++++++++++----------------- logs/watcher_test.go | 6 +++--- main.go | 12 ++++++------ 3 files changed, 26 insertions(+), 26 deletions(-) diff --git a/logs/watcher.go b/logs/watcher.go index 10b2af4..73f8bb0 100644 --- a/logs/watcher.go +++ b/logs/watcher.go @@ -47,27 +47,27 @@ type PodWatcherFunc func(KubernetesClient, string, *metav1.LabelSelector, io.Wri // Watcher watches a deployment and tails the logs for its currently active // pods. type Watcher struct { - deployName string - container string - allowNonExistentDeployment bool - clientset KubernetesClient - deployment *appsv1.Deployment - podWatcher PodWatcherInterface - podWatcherFunc PodWatcherFunc - errChan chan error - dst *concurrentWriter + deployName string + container string + strictExist bool + clientset KubernetesClient + deployment *appsv1.Deployment + podWatcher PodWatcherInterface + podWatcherFunc PodWatcherFunc + errChan chan error + dst *concurrentWriter } // NewWatcher creates a new Watcher. -func NewWatcher(deployName string, container string, allowNonExistentDeployment bool, clientset KubernetesClient, podWatcherFunc PodWatcherFunc, dst io.Writer) *Watcher { +func NewWatcher(deployName string, container string, strictExist bool, clientset KubernetesClient, podWatcherFunc PodWatcherFunc, dst io.Writer) *Watcher { return &Watcher{ - deployName: deployName, - container: container, - allowNonExistentDeployment: allowNonExistentDeployment, - clientset: clientset, - podWatcherFunc: podWatcherFunc, - errChan: make(chan error), - dst: &concurrentWriter{w: dst}, + deployName: deployName, + container: container, + strictExist: strictExist, + clientset: clientset, + podWatcherFunc: podWatcherFunc, + errChan: make(chan error), + dst: &concurrentWriter{w: dst}, } } diff --git a/logs/watcher_test.go b/logs/watcher_test.go index d897471..14852bf 100644 --- a/logs/watcher_test.go +++ b/logs/watcher_test.go @@ -36,7 +36,7 @@ func TestWatcherAllowNonExistent(t *testing.T) { var buf bytes.Buffer client := logs.KubernetesClient{Interface: clientset} - watcher := logs.NewWatcher("mydeployment", "mycontainer", false, client, mockPodwatcherFunc(nil), &buf) + watcher := logs.NewWatcher("mydeployment", "mycontainer", true, client, mockPodwatcherFunc(nil), &buf) err := watcher.Watch(context.Background()) assert.EqualError(t, err, `deployments.apps "mydeployment" not found`) @@ -52,7 +52,7 @@ func TestWatcherPodWatcherError(t *testing.T) { var buf bytes.Buffer client := logs.KubernetesClient{Interface: clientset} wantErr := errors.New("foo") - watcher := logs.NewWatcher("mydeployment", "mycontainer", false, client, mockPodwatcherFunc(wantErr), &buf) + watcher := logs.NewWatcher("mydeployment", "mycontainer", true, client, mockPodwatcherFunc(wantErr), &buf) go func() { defer deploymentsWatcher.Stop() @@ -105,7 +105,7 @@ func TestWatcherWithPodWatcher(t *testing.T) { var buf bytes.Buffer client := logs.KubernetesClient{Interface: clientset} - watcher := logs.NewWatcher("mydeployment", "mycontainer", true, client, logs.NewPodWatcher, &buf) + watcher := logs.NewWatcher("mydeployment", "mycontainer", false, client, logs.NewPodWatcher, &buf) err := watcher.Watch(ctx) require.EqualError(t, err, context.Canceled.Error()) diff --git a/main.go b/main.go index 0033229..700d465 100644 --- a/main.go +++ b/main.go @@ -15,10 +15,10 @@ import ( func main() { var ( - kubeconfig *string - deployName *string - container *string - allowNonExistentDeployment *bool + kubeconfig *string + deployName *string + container *string + strictExist *bool ) if home := homedir.HomeDir(); home != "" { kubeconfig = flag.String("kubeconfig", filepath.Join(home, ".kube", "config"), "(optional) absolute path to the kubeconfig file") @@ -27,7 +27,7 @@ func main() { } deployName = flag.String("deployment", "", "name of a deployment to monitor") container = flag.String("container", "", "name of a specific container") - allowNonExistentDeployment = flag.Bool("allow-nonexistent", true, "allow deployment to be non-existent on launch") + strictExist = flag.Bool("strict-exist", false, "require deployment to exist on launch") flag.Parse() config, err := clientcmd.BuildConfigFromFlags("", *kubeconfig) @@ -44,7 +44,7 @@ func main() { watcher := logs.NewWatcher( *deployName, *container, - *allowNonExistentDeployment, + *strictExist, logs.KubernetesClient{Interface: clientset}, logs.NewPodWatcher, os.Stdout,