-
Notifications
You must be signed in to change notification settings - Fork 434
/
config.go
127 lines (106 loc) · 3.46 KB
/
config.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
package ovn_ic_controller
import (
"flag"
"fmt"
"os"
"github.com/spf13/pflag"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/klog/v2"
clientset "github.com/kubeovn/kube-ovn/pkg/client/clientset/versioned"
"github.com/kubeovn/kube-ovn/pkg/util"
)
// Configuration is the controller conf
type Configuration struct {
KubeConfigFile string
KubeClient kubernetes.Interface
KubeOvnClient clientset.Interface
PodNamespace string
OvnNbAddr string
OvnSbAddr string
OvnTimeout int
NodeSwitch string
ClusterRouter string
NodeSwitchCIDR string
}
func ParseFlags() (*Configuration, error) {
var (
argKubeConfigFile = pflag.String("kubeconfig", "", "Path to kubeconfig file with authorization and master location information. If not set use the inCluster token.")
argOvnNbAddr = pflag.String("ovn-nb-addr", "", "ovn-nb address")
argOvnSbAddr = pflag.String("ovn-sb-addr", "", "ovn-sb address")
argOvnTimeout = pflag.Int("ovn-timeout", 60, "")
argClusterRouter = pflag.String("cluster-router", util.DefaultVpc, "The router name for cluster router")
argNodeSwitch = pflag.String("node-switch", "join", "The name of node gateway switch which help node to access pod network")
argNodeSwitchCIDR = pflag.String("node-switch-cidr", "100.64.0.0/16", "The cidr for node switch")
)
klogFlags := flag.NewFlagSet("klog", flag.ContinueOnError)
klog.InitFlags(klogFlags)
// Sync the glog and klog flags.
pflag.CommandLine.VisitAll(func(f1 *pflag.Flag) {
f2 := klogFlags.Lookup(f1.Name)
if f2 != nil {
value := f1.Value.String()
if err := f2.Value.Set(value); err != nil {
klog.Errorf("failed to set flag %v", err)
}
}
})
// change the behavior of cmdline
// not exit. not good
pflag.CommandLine.Init(os.Args[0], pflag.ContinueOnError)
pflag.CommandLine.AddGoFlagSet(klogFlags)
pflag.CommandLine.AddGoFlagSet(flag.CommandLine)
if err := pflag.CommandLine.Parse(os.Args[1:]); err != nil {
return nil, err
}
config := &Configuration{
KubeConfigFile: *argKubeConfigFile,
PodNamespace: os.Getenv("POD_NAMESPACE"),
OvnNbAddr: *argOvnNbAddr,
OvnSbAddr: *argOvnSbAddr,
OvnTimeout: *argOvnTimeout,
ClusterRouter: *argClusterRouter,
NodeSwitch: *argNodeSwitch,
NodeSwitchCIDR: *argNodeSwitchCIDR,
}
if err := config.initKubeClient(); err != nil {
return nil, fmt.Errorf("failed to init kube client, %v", err)
}
return config, nil
}
func (config *Configuration) initKubeClient() error {
var cfg *rest.Config
var err error
if config.KubeConfigFile == "" {
klog.Infof("no --kubeconfig, use in-cluster kubernetes config")
cfg, err = rest.InClusterConfig()
if err != nil {
klog.Errorf("use in cluster config failed %v", err)
return err
}
} else {
cfg, err = clientcmd.BuildConfigFromFlags("", config.KubeConfigFile)
if err != nil {
klog.Errorf("use --kubeconfig %s failed %v", config.KubeConfigFile, err)
return err
}
}
cfg.QPS = 1000
cfg.Burst = 2000
kubeOvnClient, err := clientset.NewForConfig(cfg)
if err != nil {
klog.Errorf("init kubeovn client failed %v", err)
return err
}
config.KubeOvnClient = kubeOvnClient
cfg.ContentType = "application/vnd.kubernetes.protobuf"
cfg.AcceptContentTypes = "application/vnd.kubernetes.protobuf,application/json"
kubeClient, err := kubernetes.NewForConfig(cfg)
if err != nil {
klog.Errorf("init kubernetes client failed %v", err)
return err
}
config.KubeClient = kubeClient
return nil
}