Skip to content

Commit

Permalink
feat: intercept Kubernetes client log messages and print them.
Browse files Browse the repository at this point in the history
  • Loading branch information
atombender committed Oct 3, 2022
1 parent 6165271 commit 3b291e7
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 21 deletions.
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,18 @@ go 1.17

require (
github.com/fatih/color v1.7.0
github.com/go-logr/logr v1.2.0
github.com/jpillora/backoff v1.0.0
github.com/spf13/pflag v1.0.5
k8s.io/api v0.23.0
k8s.io/apimachinery v0.23.0
k8s.io/client-go v0.23.0
k8s.io/klog/v2 v2.30.0
)

require (
cloud.google.com/go v0.81.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/go-logr/logr v1.2.0 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/go-cmp v0.5.5 // indirect
Expand All @@ -37,7 +38,6 @@ require (
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
k8s.io/klog/v2 v2.30.0 // indirect
k8s.io/kube-openapi v0.0.0-20211115234752-e816edb12b65 // indirect
k8s.io/utils v0.0.0-20210930125809-cb0fa318a74b // indirect
sigs.k8s.io/json v0.0.0-20211020170558-c049b76a60c6 // indirect
Expand Down
23 changes: 4 additions & 19 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,22 @@ import (
"regexp"
"sync"
"text/template"
"time"

"github.com/fatih/color"
"github.com/go-logr/logr"
"github.com/spf13/pflag"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/client-go/kubernetes"
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
_ "k8s.io/client-go/plugin/pkg/client/auth/oidc"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/klog/v2"
)

func main() {
klog.SetLogger(logr.New(&kubeLogger{}))

var (
contextName string
kubeconfigPath string
Expand Down Expand Up @@ -278,24 +281,6 @@ func main() {
}
}

func printInfo(format string, args ...interface{}) {
message := fmt.Sprintf(format, args...)
_, _ = fmt.Fprintf(os.Stderr, colorInfo(fmt.Sprintf("==> %s\n", message)))
}

func printError(format string, args ...interface{}) {
message := fmt.Sprintf(format, args...)
_, _ = fmt.Fprintf(os.Stderr, colorError(fmt.Sprintf("==> %s\n", message)))
}

func formatTimestamp(t *time.Time) string {
s := t.Local().Format("2006-01-02T15:04:05.999")
for len(s) < 23 {
s += "0"
}
return s
}

func fail(format string, args ...interface{}) {
_, _ = fmt.Fprintf(os.Stderr, format, args...)
os.Exit(1)
Expand Down
57 changes: 57 additions & 0 deletions printing.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package main

import (
"fmt"
"os"
"strings"
"time"

"github.com/go-logr/logr"
)

func printInfo(format string, args ...interface{}) {
message := fmt.Sprintf(format, args...)
_, _ = fmt.Fprint(os.Stderr, colorInfo("==> "+message+"\n"))
}

func printError(format string, args ...interface{}) {
message := fmt.Sprintf(format, args...)
_, _ = fmt.Fprint(os.Stderr, colorError("==> "+message+"\n"))
}

func formatTimestamp(t *time.Time) string {
s := t.Local().Format("2006-01-02T15:04:05.999")
for len(s) < 23 {
s += "0"
}
return s
}

type kubeLogger struct {
x string
}

func (l *kubeLogger) Init(info logr.RuntimeInfo) {}
func (l *kubeLogger) Enabled(level int) bool { return true }
func (l *kubeLogger) WithValues(keysAndValues ...interface{}) logr.LogSink { return l }
func (l *kubeLogger) WithName(name string) logr.LogSink { return l }

func (l *kubeLogger) Info(level int, msg string, keysAndValues ...interface{}) {
printInfo(formatKeysAndValues(msg, keysAndValues...))
}

func (l *kubeLogger) Error(err error, msg string, keysAndValues ...interface{}) {
printError(formatKeysAndValues(msg, keysAndValues...))
}

func formatKeysAndValues(msg string, kv ...interface{}) string {
var sb strings.Builder
_, _ = sb.WriteString(strings.TrimSpace(msg))
for i := 0; i < len(kv)-1; i++ {
_, _ = sb.WriteString(" ")
_, _ = sb.WriteString(kv[i].(string))
i++
_, _ = sb.WriteString(fmt.Sprintf("=%v", kv[i]))
}
return strings.ReplaceAll(sb.String(), "%", "%%")
}

0 comments on commit 3b291e7

Please sign in to comment.