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"
|
2022-06-09 16:27:17 +00:00
|
|
|
"k8s.io/client-go/dynamic"
|
2022-05-29 19:04:02 +00:00
|
|
|
"k8s.io/client-go/kubernetes"
|
2022-06-07 19:16:37 +00:00
|
|
|
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-09 16:27:17 +00:00
|
|
|
|
2022-06-10 19:38:38 +00:00
|
|
|
flag.StringVar(&namespace, "namespace", corev1.NamespaceDefault, "namespace")
|
2022-06-09 16:27:17 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2022-06-09 16:27:17 +00:00
|
|
|
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,
|
2022-06-09 16:27:17 +00:00
|
|
|
}
|
|
|
|
default:
|
|
|
|
flag.Usage()
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2022-06-07 19:16:37 +00:00
|
|
|
cfg, err := clientconfig.GetConfig()
|
2022-05-29 19:04:02 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2022-06-07 19:16:37 +00:00
|
|
|
clientset, err := kubernetes.NewForConfig(cfg)
|
2022-05-29 19:04:02 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2022-06-09 16:27:17 +00:00
|
|
|
dynamicclient, err := dynamic.NewForConfig(cfg)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2022-05-31 04:11:44 +00:00
|
|
|
watcher := logs.NewWatcher(
|
2022-06-09 16:27:17 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|