Tweak CLI interface
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
This commit is contained in:
parent
7f10c6a2c2
commit
2ea2ebe836
|
@ -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
|
// Watcher watches a deployment and tails the logs for its currently active
|
||||||
// pods.
|
// pods.
|
||||||
type Watcher struct {
|
type Watcher struct {
|
||||||
deployName string
|
deployName string
|
||||||
container string
|
container string
|
||||||
allowNonExistentDeployment bool
|
strictExist bool
|
||||||
clientset KubernetesClient
|
clientset KubernetesClient
|
||||||
deployment *appsv1.Deployment
|
deployment *appsv1.Deployment
|
||||||
podWatcher PodWatcherInterface
|
podWatcher PodWatcherInterface
|
||||||
podWatcherFunc PodWatcherFunc
|
podWatcherFunc PodWatcherFunc
|
||||||
errChan chan error
|
errChan chan error
|
||||||
dst *concurrentWriter
|
dst *concurrentWriter
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewWatcher creates a new Watcher.
|
// 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{
|
return &Watcher{
|
||||||
deployName: deployName,
|
deployName: deployName,
|
||||||
container: container,
|
container: container,
|
||||||
allowNonExistentDeployment: allowNonExistentDeployment,
|
strictExist: strictExist,
|
||||||
clientset: clientset,
|
clientset: clientset,
|
||||||
podWatcherFunc: podWatcherFunc,
|
podWatcherFunc: podWatcherFunc,
|
||||||
errChan: make(chan error),
|
errChan: make(chan error),
|
||||||
dst: &concurrentWriter{w: dst},
|
dst: &concurrentWriter{w: dst},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -36,7 +36,7 @@ func TestWatcherAllowNonExistent(t *testing.T) {
|
||||||
|
|
||||||
var buf bytes.Buffer
|
var buf bytes.Buffer
|
||||||
client := logs.KubernetesClient{Interface: clientset}
|
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())
|
err := watcher.Watch(context.Background())
|
||||||
assert.EqualError(t, err, `deployments.apps "mydeployment" not found`)
|
assert.EqualError(t, err, `deployments.apps "mydeployment" not found`)
|
||||||
|
@ -52,7 +52,7 @@ func TestWatcherPodWatcherError(t *testing.T) {
|
||||||
var buf bytes.Buffer
|
var buf bytes.Buffer
|
||||||
client := logs.KubernetesClient{Interface: clientset}
|
client := logs.KubernetesClient{Interface: clientset}
|
||||||
wantErr := errors.New("foo")
|
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() {
|
go func() {
|
||||||
defer deploymentsWatcher.Stop()
|
defer deploymentsWatcher.Stop()
|
||||||
|
@ -105,7 +105,7 @@ func TestWatcherWithPodWatcher(t *testing.T) {
|
||||||
|
|
||||||
var buf bytes.Buffer
|
var buf bytes.Buffer
|
||||||
client := logs.KubernetesClient{Interface: clientset}
|
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)
|
err := watcher.Watch(ctx)
|
||||||
require.EqualError(t, err, context.Canceled.Error())
|
require.EqualError(t, err, context.Canceled.Error())
|
||||||
|
|
12
main.go
12
main.go
|
@ -15,10 +15,10 @@ import (
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
var (
|
var (
|
||||||
kubeconfig *string
|
kubeconfig *string
|
||||||
deployName *string
|
deployName *string
|
||||||
container *string
|
container *string
|
||||||
allowNonExistentDeployment *bool
|
strictExist *bool
|
||||||
)
|
)
|
||||||
if home := homedir.HomeDir(); home != "" {
|
if home := homedir.HomeDir(); home != "" {
|
||||||
kubeconfig = flag.String("kubeconfig", filepath.Join(home, ".kube", "config"), "(optional) absolute path to the kubeconfig file")
|
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")
|
deployName = flag.String("deployment", "", "name of a deployment to monitor")
|
||||||
container = flag.String("container", "", "name of a specific container")
|
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()
|
flag.Parse()
|
||||||
|
|
||||||
config, err := clientcmd.BuildConfigFromFlags("", *kubeconfig)
|
config, err := clientcmd.BuildConfigFromFlags("", *kubeconfig)
|
||||||
|
@ -44,7 +44,7 @@ func main() {
|
||||||
watcher := logs.NewWatcher(
|
watcher := logs.NewWatcher(
|
||||||
*deployName,
|
*deployName,
|
||||||
*container,
|
*container,
|
||||||
*allowNonExistentDeployment,
|
*strictExist,
|
||||||
logs.KubernetesClient{Interface: clientset},
|
logs.KubernetesClient{Interface: clientset},
|
||||||
logs.NewPodWatcher,
|
logs.NewPodWatcher,
|
||||||
os.Stdout,
|
os.Stdout,
|
||||||
|
|
Loading…
Reference in New Issue