kubectl-persistent-logger/main.go

99 lines
2.0 KiB
Go

package main
import (
"context"
"flag"
"fmt"
"io"
"log"
"os"
"git.netflux.io/rob/kubectl-persistent-logger/logs"
corev1 "k8s.io/api/core/v1"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/kubernetes"
clientconfig "sigs.k8s.io/controller-runtime/pkg/client/config"
)
var (
version = "0.1.0"
gitChanges = "unknown"
gitSHA1 = "unknown"
buildTime = "unknown"
)
func main() {
var (
namespace string
container string
strictExist bool
debug bool
printVersion bool
)
flag.StringVar(&namespace, "namespace", corev1.NamespaceDefault, "namespace")
flag.StringVar(&container, "container", "", "name of a specific container")
flag.BoolVar(&strictExist, "strict", false, "require deployment to exist on launch")
flag.BoolVar(&debug, "debug", false, "enable debug logging to stderr")
flag.BoolVar(&printVersion, "version", false, "print version and exit")
flag.Parse()
if printVersion {
fmt.Fprintf(os.Stderr, "kubectl-persistent-logger %s, gitSHA1 = %s, gitChanges = %s, buildTime = %v\n", version, gitSHA1, gitChanges, buildTime)
os.Exit(0)
}
logger := log.New(io.Discard, "", 0)
if debug {
logger = log.New(os.Stderr, "", 0)
}
var params logs.WatcherParams
switch len(flag.Args()) {
case 1:
// TODO: handle type/name style
flag.Usage()
os.Exit(1)
case 2:
kind, err := logs.ParseType(flag.Arg(0))
if err != nil {
log.Fatal(err)
}
params = logs.WatcherParams{
Type: kind,
Name: flag.Arg(1),
Namespace: namespace,
}
default:
flag.Usage()
os.Exit(1)
}
cfg, err := clientconfig.GetConfig()
if err != nil {
log.Fatal(err)
}
clientset, err := kubernetes.NewForConfig(cfg)
if err != nil {
log.Fatal(err)
}
dynamicclient, err := dynamic.NewForConfig(cfg)
if err != nil {
log.Fatal(err)
}
watcher := logs.NewWatcher(
params,
logs.KubernetesClient{Typed: clientset, Untyped: dynamicclient},
logs.NewPodWatcher,
os.Stdout,
logger,
)
if err := watcher.Watch(context.Background()); err != nil {
log.Fatal(err)
}
}