Skip to content

Commit

Permalink
cmd/kail: add --current-ns flag
Browse files Browse the repository at this point in the history
refs #33
  • Loading branch information
boz committed May 16, 2019
1 parent edd9a54 commit 51295f6
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 4 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ Flag | Selection
`--ing NAME` | match pods belonging to services targeted by the given ingress
`-c, --containers CONTAINER-NAME` | restrict which containers logs are shown for
`--ignore LABEL-SELECTOR` | Ignore pods that the selector matches. (default: `kail.ignore=true`)
`--current-ns` | Match pods in the namespace specified in Kubernetes' "current context"

#### Name Selection

Expand Down
41 changes: 37 additions & 4 deletions cmd/kail/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
logutil_logrus "github.com/boz/go-logutil/logrus"
"github.com/boz/kail"
"github.com/boz/kcache/nsname"
"github.com/boz/kcache/util"
"github.com/sirupsen/logrus"
kingpin "gopkg.in/alecthomas/kingpin.v2"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -46,6 +45,10 @@ var (

flagContext = kingpin.Flag("context", "kubernetes context").PlaceHolder("CONTEXT-NAME").String()

flagCurrentNS = kingpin.Flag("current-ns", "use namespace from current context").
Default("false").
Bool()

flagContainers = kingpin.Flag("containers", "containers").Short('c').PlaceHolder("NAME").Strings()

flagDryRun = kingpin.Flag("dry-run", "print matching pods and exit").
Expand Down Expand Up @@ -74,6 +77,10 @@ var (
String()
)

var (
currentNS = ""
)

func main() {

// XXX: hack to make kubectl run work
Expand Down Expand Up @@ -165,14 +172,36 @@ func createLog() logutil.Log {
}

func createKubeClient() (kubernetes.Interface, *rest.Config) {
overrides := &clientcmd.ConfigOverrides{}

config, err := rest.InClusterConfig()
switch {
case err == nil:
cs, err := kubernetes.NewForConfig(config)
kingpin.FatalIfError(err, "Error configuring kubernetes connection")
return cs, config
case config != nil:
kingpin.Fatalf("Error configuring in-cluster config: %v", err)
}

overrides := &clientcmd.ConfigOverrides{}
if flagContext != nil {
overrides.CurrentContext = *flagContext
}

cs, rc, err := util.KubeClient(overrides)
kingpin.FatalIfError(err, "Error configuring kubernetes connection")
cc := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(
clientcmd.NewDefaultClientConfigLoadingRules(), overrides)

if *flagCurrentNS {
ns, _, err := cc.Namespace()
kingpin.FatalIfError(err, "Error determining current namespace")
currentNS = ns
}

rc, err := cc.ClientConfig()
kingpin.FatalIfError(err, "Error determining client config")

cs, err := kubernetes.NewForConfig(rc)
kingpin.FatalIfError(err, "Error building kubernetes config")

_, err = cs.CoreV1().Namespaces().List(metav1.ListOptions{})
kingpin.FatalIfError(err, "Can't connnect to kubernetes")
Expand All @@ -195,6 +224,10 @@ func createDSBuilder() kail.DSBuilder {
dsb = dsb.WithPods(ids...)
}

if *flagCurrentNS && currentNS != "" {
dsb = dsb.WithNamespace(currentNS)
}

if len(*flagNs) > 0 {
dsb = dsb.WithNamespace(*flagNs...)
}
Expand Down

0 comments on commit 51295f6

Please sign in to comment.