...
1
16
17 package main
18
19 import (
20 "context"
21 "fmt"
22 "os"
23 "strings"
24
25 "github.com/spf13/cobra"
26
27 "k8s.io/apimachinery/pkg/util/runtime"
28 "k8s.io/component-base/cli"
29 "k8s.io/component-base/featuregate"
30 "k8s.io/component-base/logs"
31 logsapi "k8s.io/component-base/logs/api/v1"
32 "k8s.io/component-base/logs/example"
33 "k8s.io/klog/v2"
34
35 _ "k8s.io/component-base/logs/json/register"
36 )
37
38 var featureGate = featuregate.NewFeatureGate()
39
40 func main() {
41 runtime.Must(logsapi.AddFeatureGates(featureGate))
42 command := NewLoggerCommand()
43 code := cli.Run(command)
44 os.Exit(code)
45 }
46
47 func NewLoggerCommand() *cobra.Command {
48 c := logsapi.NewLoggingConfiguration()
49 cmd := &cobra.Command{
50 Run: func(cmd *cobra.Command, args []string) {
51 logs.InitLogs()
52 if err := logsapi.ValidateAndApply(c, featureGate); err != nil {
53 fmt.Fprintf(os.Stderr, "%v\n", err)
54 os.Exit(1)
55 }
56 if len(args) > 0 {
57 fmt.Fprintf(os.Stderr, "Unexpected additional command line arguments:\n %s\n", strings.Join(args, "\n "))
58 os.Exit(1)
59 }
60
61
62 logger := klog.LoggerWithValues(klog.LoggerWithName(klog.Background(), "example"), "foo", "bar")
63 ctx := klog.NewContext(context.Background(), logger)
64
65
66 example.Run(ctx)
67 },
68 }
69 logsapi.AddFeatureGates(featureGate)
70 featureGate.AddFlag(cmd.Flags())
71 logsapi.AddFlags(c, cmd.Flags())
72 return cmd
73 }
74
View as plain text