diff --git a/controller.go b/controller.go index 84da0bd..aa8e5d6 100644 --- a/controller.go +++ b/controller.go @@ -16,7 +16,7 @@ import ( ) type ControllerOptions struct { - Namespace string + Namespaces []string InclusionMatcher Matcher ExclusionMatcher Matcher SinceStart bool @@ -63,55 +63,56 @@ func NewController( } func (ctl *Controller) Run(ctx context.Context) error { - podListWatcher := cache.NewListWatchFromClient( - ctl.client.CoreV1().RESTClient(), "pods", ctl.Namespace, fields.Everything()) + stopCh := make(chan struct{}) + defer close(stopCh) - obj, err := podListWatcher.List(metav1.ListOptions{}) - if err != nil { - panic(err) - } - switch t := obj.(type) { - case *v1.PodList: - for _, pod := range t.Items { - ctl.onInitialAdd(&pod) + for _, ns := range ctl.Namespaces { + podListWatcher := cache.NewListWatchFromClient( + ctl.client.CoreV1().RESTClient(), "pods", ns, fields.Everything()) + + obj, err := podListWatcher.List(metav1.ListOptions{}) + if err != nil { + panic(err) } - case *internalversion.List: - for _, item := range t.Items { - if pod, ok := item.(*v1.Pod); ok { - ctl.onInitialAdd(pod) + switch t := obj.(type) { + case *v1.PodList: + for _, pod := range t.Items { + ctl.onInitialAdd(&pod) } + case *internalversion.List: + for _, item := range t.Items { + if pod, ok := item.(*v1.Pod); ok { + ctl.onInitialAdd(pod) + } + } + default: + panic("unable to get pod list") } - default: - panic("unable to get pod list") - } - _, informer := cache.NewIndexerInformer( - podListWatcher, &v1.Pod{}, 0, cache.ResourceEventHandlerFuncs{ - AddFunc: func(obj interface{}) { - if pod, ok := obj.(*v1.Pod); ok { - ctl.onAdd(pod) - } - }, - UpdateFunc: func(old interface{}, new interface{}) { - if pod, ok := new.(*v1.Pod); ok { - ctl.onUpdate(pod) - } - }, - DeleteFunc: func(obj interface{}) { - if pod, ok := obj.(*v1.Pod); ok { - ctl.onDelete(pod) - } - }, - }, cache.Indexers{}) - - stopCh := make(chan struct{}, 1) - go informer.Run(stopCh) - select { - case <-stopCh: - return nil - case <-ctx.Done(): - return ctx.Err() + _, informer := cache.NewIndexerInformer( + podListWatcher, &v1.Pod{}, 0, cache.ResourceEventHandlerFuncs{ + AddFunc: func(obj interface{}) { + if pod, ok := obj.(*v1.Pod); ok { + ctl.onAdd(pod) + } + }, + UpdateFunc: func(old interface{}, new interface{}) { + if pod, ok := new.(*v1.Pod); ok { + ctl.onUpdate(pod) + } + }, + DeleteFunc: func(obj interface{}) { + if pod, ok := obj.(*v1.Pod); ok { + ctl.onDelete(pod) + } + }, + }, cache.Indexers{}) + + go informer.Run(stopCh) } + + <-ctx.Done() + return ctx.Err() } func (ctl *Controller) onInitialAdd(pod *v1.Pod) { diff --git a/main.go b/main.go index a623b77..6b2ec6b 100644 --- a/main.go +++ b/main.go @@ -26,7 +26,7 @@ func main() { contextName string kubeconfigPath string labelSelectorExpr string - namespace string + namespaces []string allNamespaces bool quiet bool timestamps bool @@ -46,7 +46,7 @@ func main() { flags.StringVar(&contextName, "context", "", "Kubernetes context name") flags.StringVar(&kubeconfigPath, "kubeconfig", "", "Path to kubeconfig (only required out-of-cluster)") - flags.StringVarP(&namespace, "namespace", "n", "", "Kubernetes namespace") + flags.StringArrayVarP(&namespaces, "namespace", "n", []string{}, "Kubernetes namespace") flags.StringArrayVarP(&excludePatternStrings, "exclude", "x", []string{}, "Exclude using a regular expression. Pattern can be repeated. Takes priority over"+ " include patterns and labels.") @@ -140,12 +140,12 @@ func main() { } if allNamespaces { - namespace = v1.NamespaceAll - } else if namespace == "" { + namespaces = []string{v1.NamespaceAll} + } else if len(namespaces) == 0 { if rawConfig.Contexts[rawConfig.CurrentContext].Namespace == "" { - namespace = v1.NamespaceDefault + namespaces = []string{v1.NamespaceDefault} } else { - namespace = rawConfig.Contexts[rawConfig.CurrentContext].Namespace + namespaces = []string{rawConfig.Contexts[rawConfig.CurrentContext].Namespace} } } @@ -159,7 +159,7 @@ func main() { } formatPod := func(pod *v1.Pod) string { - if allNamespaces { + if allNamespaces || len(namespaces) > 1 { return fmt.Sprintf("%s/%s", pod.Namespace, pod.Name) } return pod.Name @@ -224,7 +224,7 @@ func main() { var stdoutMutex sync.Mutex controller := NewController(clientset, ControllerOptions{ - Namespace: namespace, + Namespaces: namespaces, InclusionMatcher: inclusionMatcher, ExclusionMatcher: exclusionMatcher, SinceStart: sinceStart,