Tweak CLI interface
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Rob Watson 2022-06-01 22:04:20 +02:00
parent 7f10c6a2c2
commit 2ea2ebe836
3 changed files with 26 additions and 26 deletions

View File

@ -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},
}
}

View File

@ -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())

12
main.go
View File

@ -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,