diff --git a/Makefile b/Makefile index 3d956f9..f8f70d9 100644 --- a/Makefile +++ b/Makefile @@ -2,15 +2,15 @@ GO = go BIN = $(CURDIR)/.bin LINT_CONFIG = $(CURDIR)/.golangci.yaml -LDFLAGS_VERSION := -X main.version=$(VERSION) -X main.gitCommit=$(COMMIT) -X main.gitBranch=$(BRANCH) -X \"main.buildDate=$(DATE)\" MODULE = $(shell $(GO) list -m) DATE ?= $(shell date +%FT%T%z) -VERSION ?= $(shell git describe --tags --always --dirty --match="v*" 2> /dev/null || \ +VERSION ?= $(shell git describe --tags --always --dirty --match="[0-9]*.[0-9]*.[0-9]*" 2> /dev/null || \ cat $(CURDIR)/.version 2> /dev/null || echo v0) COMMIT ?= $(shell git rev-parse --short HEAD 2>/dev/null) BRANCH ?= $(shell git rev-parse --abbrev-ref HEAD 2>/dev/null) +LDFLAGS_VERSION := -X main.version=$(VERSION) -X main.gitCommit=$(COMMIT) -X main.gitBranch=$(BRANCH) -X \"main.buildDate=$(DATE)\" # platforms and architectures for release; default to MacOS (darwin) and arm64 (M1/M2) TARGETOS := $(or $(TARGETOS), darwin) diff --git a/cmd/main.go b/cmd/main.go index 86c7a3d..1deabfb 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -75,16 +75,47 @@ func run(ctx context.Context, log *logrus.Entry, cfg config.Config) error { return nil } -func mainCmd(c *cli.Context) error { - ctx := signals.SetupSignalHandler() - cfg := config.LoadConfig(c) - +func prepareLogler(cfg config.Config, c *cli.Context) *logrus.Entry { logger := logrus.New() + + // set debug log level + switch level := c.String("log-level"); level { + case "debug", "DEBUG": + logger.SetLevel(logrus.DebugLevel) + case "info", "INFO": + logger.SetLevel(logrus.InfoLevel) + case "warning", "WARNING": + logger.SetLevel(logrus.WarnLevel) + case "error", "ERROR": + logger.SetLevel(logrus.ErrorLevel) + case "fatal", "FATAL": + logger.SetLevel(logrus.FatalLevel) + case "panic", "PANIC": + logger.SetLevel(logrus.PanicLevel) + default: + logger.SetLevel(logrus.WarnLevel) + } + + logger.SetFormatter(&logrus.TextFormatter{ + FullTimestamp: true, + }) + if c.Bool("json") { + logger.SetFormatter(&logrus.JSONFormatter{}) + } + log := logger.WithFields(logrus.Fields{ "cluster": cfg.ClusterName, "version": version, }) + return log +} + +func runCmd(c *cli.Context) error { + ctx := signals.SetupSignalHandler() + cfg := config.LoadConfig(c) + log := prepareLogler(cfg, c) + if err := run(ctx, log, cfg); err != nil { log.Fatalf("eks-lens agent failed: %v", err) } @@ -94,28 +125,50 @@ func mainCmd(c *cli.Context) error { func main() { app := &cli.App{ - Flags: []cli.Flag{ - &cli.StringFlag{ - Name: "custer-name", - Usage: "EKS cluster name", - Required: true, - EnvVars: []string{"CLUSTER_NAME"}, - }, - &cli.StringFlag{ - Name: "kubeconfig", - Usage: "Path to kubeconfig file", - EnvVars: []string{"KUBECONFIG"}, - }, - &cli.StringFlag{ - Name: "stream-name", - Usage: "Amazon Kinesis Data Stream name", - Required: true, - EnvVars: []string{"STREAM_NAME"}, + Commands: []*cli.Command{ + { + Name: "run", + Usage: "run eks-lens agent", + Flags: []cli.Flag{ + &cli.StringFlag{ + Name: "cluster-name", + Usage: "EKS cluster name", + Required: true, + EnvVars: []string{"CLUSTER_NAME"}, + Category: "Configuration", + }, + &cli.StringFlag{ + Name: "kubeconfig", + Usage: "Path to kubeconfig file", + EnvVars: []string{"KUBECONFIG"}, + Category: "Configuration", + }, + &cli.StringFlag{ + Name: "stream-name", + Usage: "Amazon Kinesis Data Stream name", + Required: true, + EnvVars: []string{"STREAM_NAME"}, + Category: "Configuration", + }, + &cli.StringFlag{ + Name: "log-level", + Usage: "set log level (debug, info, warning(*), error, fatal, panic)", + Value: "warning", + EnvVars: []string{"LOG_LEVEL"}, + Category: "Logging", + }, + &cli.BoolFlag{ + Name: "json", + Usage: "produce log in JSON format: Logstash and Splunk friendly", + EnvVars: []string{"LOG_JSON"}, + Category: "Logging", + }, + }, + Action: runCmd, }, }, Name: "eks-lens-agent", Usage: "eks-lens-agent is a data collection agent for EKS Lens", - Action: mainCmd, Version: version, } cli.VersionPrinter = func(c *cli.Context) { diff --git a/deploy/deployment.yaml b/deploy/deployment.yaml index 233f067..41b9b65 100644 --- a/deploy/deployment.yaml +++ b/deploy/deployment.yaml @@ -19,10 +19,14 @@ spec: containers: - name: eks-lens-agent image: ghcr.io/doitintl/eks-lens-agent:latest + args: + - run env: - name: CLUSTER_NAME value: gaia-cost - name: STREAM_NAME value: eks-lens + - name: LOG_LEVEL + value: debug imagePullPolicy: Always restartPolicy: Always