kubectl-persistent-logger/main.go

99 lines
2.0 KiB
Go
Raw Normal View History

2022-05-29 19:04:02 +00:00
package main
import (
"context"
"flag"
2022-06-09 18:56:44 +00:00
"fmt"
2022-06-10 17:04:52 +00:00
"io"
2022-05-29 19:04:02 +00:00
"log"
2022-05-30 10:10:58 +00:00
"os"
2022-05-29 19:04:02 +00:00
"git.netflux.io/rob/kubectl-persistent-logger/logs"
2022-06-10 19:38:38 +00:00
corev1 "k8s.io/api/core/v1"
"k8s.io/client-go/dynamic"
2022-05-29 19:04:02 +00:00
"k8s.io/client-go/kubernetes"
clientconfig "sigs.k8s.io/controller-runtime/pkg/client/config"
2022-05-29 19:04:02 +00:00
)
2022-06-09 18:56:44 +00:00
var (
2022-06-10 19:38:38 +00:00
version = "0.1.0"
2022-06-09 18:56:44 +00:00
gitChanges = "unknown"
gitSHA1 = "unknown"
buildTime = "unknown"
)
2022-05-29 19:04:02 +00:00
func main() {
2022-05-31 04:11:44 +00:00
var (
2022-06-10 19:38:38 +00:00
namespace string
container string
strictExist bool
debug bool
printVersion bool
2022-05-31 04:11:44 +00:00
)
2022-06-10 19:38:38 +00:00
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")
2022-06-10 17:04:52 +00:00
flag.BoolVar(&debug, "debug", false, "enable debug logging to stderr")
2022-06-10 19:38:38 +00:00
flag.BoolVar(&printVersion, "version", false, "print version and exit")
2022-05-29 19:04:02 +00:00
flag.Parse()
2022-06-10 19:38:38 +00:00
if printVersion {
fmt.Fprintf(os.Stderr, "kubectl-persistent-logger %s, gitSHA1 = %s, gitChanges = %s, buildTime = %v\n", version, gitSHA1, gitChanges, buildTime)
2022-06-09 18:56:44 +00:00
os.Exit(0)
}
2022-06-10 17:04:52 +00:00
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),
2022-06-10 19:38:38 +00:00
Namespace: namespace,
}
default:
flag.Usage()
os.Exit(1)
}
cfg, err := clientconfig.GetConfig()
2022-05-29 19:04:02 +00:00
if err != nil {
log.Fatal(err)
}
clientset, err := kubernetes.NewForConfig(cfg)
2022-05-29 19:04:02 +00:00
if err != nil {
log.Fatal(err)
}
dynamicclient, err := dynamic.NewForConfig(cfg)
if err != nil {
log.Fatal(err)
}
2022-05-31 04:11:44 +00:00
watcher := logs.NewWatcher(
params,
logs.KubernetesClient{Typed: clientset, Untyped: dynamicclient},
2022-06-01 17:19:55 +00:00
logs.NewPodWatcher,
2022-05-31 04:11:44 +00:00
os.Stdout,
2022-06-10 17:04:52 +00:00
logger,
2022-05-31 04:11:44 +00:00
)
2022-06-10 19:38:38 +00:00
if err := watcher.Watch(context.Background()); err != nil {
2022-05-29 19:04:02 +00:00
log.Fatal(err)
}
}