/
main.go
84 lines (76 loc) · 1.94 KB
/
main.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
package main
import (
"os"
"strings"
"github.com/pascaldekloe/name"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"golang.org/x/sync/errgroup"
"istio.io/istio/pkg/cmd"
"sigs.k8s.io/controller-runtime/pkg/manager/signals"
)
var (
startHook = true
logLevel = "info"
)
var (
rootCmd = &cobra.Command{
Use: "istiofilter",
SilenceUsage: true,
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
logrus.SetReportCaller(true)
level, _ := logrus.ParseLevel(logLevel)
logrus.SetLevel(level)
return nil
},
RunE: func(c *cobra.Command, args []string) error {
cmd.PrintFlags(c.Flags())
ctx := signals.SetupSignalHandler()
group, ctx := errgroup.WithContext(ctx)
group.Go(func() error {
return startController(ctx, webhookCfg.PrivilegeNamespaces)
})
if startHook {
group.Go(func() error {
return Serve(ctx, webhookCfg)
})
}
err := group.Wait()
return err
},
}
)
var webhookCfg = &Config{}
func init() {
cobra.OnInitialize(func() {
BindEnv(rootCmd)
})
rootCmd.PersistentFlags().StringVar(&logLevel, "logLevel", "info", "")
rootCmd.PersistentFlags().BoolVar(&startHook, "starthook", true, "")
rootCmd.PersistentFlags().StringVarP(
&webhookCfg.KeyFile, "keyFile", "k", "", "")
rootCmd.PersistentFlags().StringVarP(
&webhookCfg.CertFile, "certFile", "c", "", "")
rootCmd.PersistentFlags().IntVarP(
&webhookCfg.Port, "port", "p", 443, "")
rootCmd.PersistentFlags().StringSliceVarP(
&webhookCfg.PrivilegeNamespaces, "privilegeNamespaces", "n", []string{"istio-system"}, "")
}
func main() {
if err := rootCmd.Execute(); err != nil {
logrus.Error(err)
os.Exit(-1)
}
}
func BindEnv(cmd *cobra.Command) {
cmd.Flags().VisitAll(func(flag *pflag.Flag) {
flag.Changed = true
if !flag.Changed {
env, ok := os.LookupEnv(strings.ToUpper(name.Delimit(flag.Name, '_')))
if ok {
_ = cmd.Flags().Set(flag.Name, env)
}
}
})
}