-
Notifications
You must be signed in to change notification settings - Fork 28
/
dwd.go
129 lines (115 loc) · 3.28 KB
/
dwd.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// SPDX-FileCopyrightText: 2024 SAP SE or an SAP affiliate company and Gardener contributors
//
// SPDX-License-Identifier: Apache-2.0
package main
import (
"flag"
"fmt"
"os"
"github.com/go-logr/logr"
"github.com/gardener/dependency-watchdog/cmd"
"go.uber.org/zap/zapcore"
// Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.)
// to ensure that exec-entrypoint and run can make use of them.
_ "k8s.io/client-go/plugin/pkg/client/auth"
"k8s.io/apimachinery/pkg/runtime"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
// +kubebuilder:scaffold:imports
)
var (
scheme = runtime.NewScheme()
logger logr.Logger
)
func init() {
utilruntime.Must(clientgoscheme.AddToScheme(scheme))
// +kubebuilder:scaffold:scheme
}
func main() {
args := os.Args[1:]
checkArgs(args)
ctx := ctrl.SetupSignalHandler()
opts := zap.Options{
Development: true,
Level: zapcore.DebugLevel,
TimeEncoder: zapcore.RFC3339TimeEncoder,
}
opts.BindFlags(flag.CommandLine)
_, command, err := parseCommand(args)
if err != nil {
os.Exit(2)
}
// initializing global logger
ctrl.SetLogger(zap.New(zap.UseFlagOptions(&opts)))
// creating root logger from global logger
logger = ctrl.Log.WithName("dwd")
mgr, err := command.Run(logger)
if err != nil {
logger.Error(err, fmt.Sprintf("failed to run command %s", command.Name))
os.Exit(1)
}
// starting manager
logger.Info("Starting manager")
if err = mgr.Start(ctx); err != nil {
logger.Error(err, "Failed to run the manager")
os.Exit(1)
}
}
func checkArgs(args []string) {
switch {
case len(args) < 1, args[0] == "-h", args[0] == "--help":
cmd.PrintCliUsage(os.Stdout)
os.Exit(0)
case args[0] == "help":
if len(args) == 1 {
_, err := fmt.Fprintf(os.Stderr, "Incorrect usage. To get the CLI usage help use `-h | --help`. To get a command help use `dwd help <command-name>")
if err != nil {
return
}
os.Exit(2)
}
requestedCommand := args[1]
if _, err := getCommand(requestedCommand); err != nil {
os.Exit(2)
}
cmd.PrintHelp(requestedCommand, os.Stdout)
os.Exit(0)
}
}
func getCommand(cmdName string) (*cmd.Command, error) {
supportedCmdNames := make([]string, len(cmd.Commands))
for _, cmnd := range cmd.Commands {
supportedCmdNames = append(supportedCmdNames, cmnd.Name)
if cmdName == cmnd.Name {
return cmnd, nil
}
}
return nil, fmt.Errorf("unknown command %s. Supported commands are: %v", cmdName, supportedCmdNames)
}
func parseCommand(args []string) ([]string, *cmd.Command, error) {
requestedCmdName := args[0]
command, err := getCommand(requestedCmdName)
if err != nil {
_, err := fmt.Fprintf(os.Stderr, "unexpected error when fetching matching command %s. This should have been checked earlier. Error: %v", requestedCmdName, err)
if err != nil {
return nil, nil, err
}
return nil, nil, err
}
fs := flag.CommandLine
fs.Usage = func() {}
if command.AddFlags != nil {
command.AddFlags(fs)
}
if err := fs.Parse(args[1:]); err != nil {
if err == flag.ErrHelp {
cmd.PrintHelp(requestedCmdName, os.Stdout)
return nil, nil, err
}
cmd.PrintHelp(requestedCmdName, os.Stderr)
return nil, nil, err
}
return fs.Args(), command, nil
}