Skip to content

Commit

Permalink
feat: adds support for multiple -n/--namespace arguments.
Browse files Browse the repository at this point in the history
  • Loading branch information
atombender committed Oct 3, 2022
1 parent cf9b535 commit 6165271
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 52 deletions.
89 changes: 45 additions & 44 deletions controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
)

type ControllerOptions struct {
Namespace string
Namespaces []string
InclusionMatcher Matcher
ExclusionMatcher Matcher
SinceStart bool
Expand Down Expand Up @@ -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) {
Expand Down
16 changes: 8 additions & 8 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func main() {
contextName string
kubeconfigPath string
labelSelectorExpr string
namespace string
namespaces []string
allNamespaces bool
quiet bool
timestamps bool
Expand All @@ -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.")
Expand Down Expand Up @@ -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}
}
}

Expand All @@ -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
Expand Down Expand Up @@ -224,7 +224,7 @@ func main() {

var stdoutMutex sync.Mutex
controller := NewController(clientset, ControllerOptions{
Namespace: namespace,
Namespaces: namespaces,
InclusionMatcher: inclusionMatcher,
ExclusionMatcher: exclusionMatcher,
SinceStart: sinceStart,
Expand Down

0 comments on commit 6165271

Please sign in to comment.