-
Notifications
You must be signed in to change notification settings - Fork 434
/
config.go
343 lines (312 loc) · 11.6 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
package daemon
import (
"context"
"errors"
"flag"
"fmt"
"net"
"os"
"os/exec"
"regexp"
"runtime"
"strings"
"github.com/sirupsen/logrus"
"github.com/spf13/pflag"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/klog/v2"
kubeovnv1 "github.com/kubeovn/kube-ovn/pkg/apis/kubeovn/v1"
clientset "github.com/kubeovn/kube-ovn/pkg/client/clientset/versioned"
"github.com/kubeovn/kube-ovn/pkg/util"
)
// Configuration is the daemon conf
type Configuration struct {
Iface string
DPDKTunnelIface string
MTU int
MSS int
EnableMirror bool
MirrorNic string
BindSocket string
OvsSocket string
KubeConfigFile string
KubeClient kubernetes.Interface
KubeOvnClient clientset.Interface
NodeName string
ServiceClusterIPRange string
NodeLocalDnsIP string
EncapChecksum bool
EnablePprof bool
PprofPort int
NetworkType string
CniConfDir string
CniConfFile string
CniConfName string
DefaultProviderName string
DefaultInterfaceName string
ExternalGatewayConfigNS string
}
// ParseFlags will parse cmd args then init kubeClient and configuration
// TODO: validate configuration
func ParseFlags() *Configuration {
var (
argNodeName = pflag.String("node-name", "", "Name of the node on which the daemon is running on.")
argIface = pflag.String("iface", "", "The iface used to inter-host pod communication, can be a nic name or a group of regex separated by comma (default the default route iface)")
argDPDKTunnelIface = pflag.String("dpdk-tunnel-iface", "br-phy", "Specifies the name of the dpdk tunnel iface.")
argMTU = pflag.Int("mtu", 0, "The MTU used by pod iface in overlay networks (default iface MTU - 100)")
argEnableMirror = pflag.Bool("enable-mirror", false, "Enable traffic mirror (default false)")
argMirrorNic = pflag.String("mirror-iface", "mirror0", "The mirror nic name that will be created by kube-ovn")
argBindSocket = pflag.String("bind-socket", defaultBindSocket, "The socket daemon bind to.")
argOvsSocket = pflag.String("ovs-socket", "", "The socket to local ovs-server")
argKubeConfigFile = pflag.String("kubeconfig", "", "Path to kubeconfig file with authorization and master location information. If not set use the inCluster token.")
argServiceClusterIPRange = pflag.String("service-cluster-ip-range", "10.96.0.0/12", "The kubernetes service cluster ip range")
argNodeLocalDnsIP = pflag.String("node-local-dns-ip", "", "If use nodelocaldns the local dns server ip should be set here.")
argEncapChecksum = pflag.Bool("encap-checksum", true, "Enable checksum")
argEnablePprof = pflag.Bool("enable-pprof", false, "Enable pprof")
argPprofPort = pflag.Int("pprof-port", 10665, "The port to get profiling data")
argsNetworkType = pflag.String("network-type", util.NetworkTypeGeneve, "Tunnel encapsulation protocol in overlay networks")
argCniConfDir = pflag.String("cni-conf-dir", "/etc/cni/net.d", "Path of the CNI config directory.")
argCniConfFile = pflag.String("cni-conf-file", "/kube-ovn/01-kube-ovn.conflist", "Path of the CNI config file.")
argsCniConfName = pflag.String("cni-conf-name", "01-kube-ovn.conflist", "Specify the name of kube ovn conflist name in dir /etc/cni/net.d/, default: 01-kube-ovn.conflist")
argsDefaultProviderName = pflag.String("default-provider-name", "provider", "The vlan or vxlan type default provider interface name")
argsDefaultInterfaceName = pflag.String("default-interface-name", "", "The default host interface name in the vlan/vxlan type")
argExternalGatewayConfigNS = pflag.String("external-gateway-config-ns", "kube-system", "The namespace of configmap external-gateway-config, default: kube-system")
)
// mute info log for ipset lib
logrus.SetLevel(logrus.WarnLevel)
klogFlags := flag.NewFlagSet("klog", flag.ExitOnError)
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.Fatalf("failed to set flag, %v", err)
}
}
})
pflag.CommandLine.AddGoFlagSet(klogFlags)
pflag.CommandLine.AddGoFlagSet(flag.CommandLine)
pflag.Parse()
config := &Configuration{
Iface: *argIface,
DPDKTunnelIface: *argDPDKTunnelIface,
MTU: *argMTU,
EnableMirror: *argEnableMirror,
MirrorNic: *argMirrorNic,
BindSocket: *argBindSocket,
OvsSocket: *argOvsSocket,
KubeConfigFile: *argKubeConfigFile,
EnablePprof: *argEnablePprof,
PprofPort: *argPprofPort,
NodeName: strings.ToLower(*argNodeName),
ServiceClusterIPRange: *argServiceClusterIPRange,
NodeLocalDnsIP: *argNodeLocalDnsIP,
EncapChecksum: *argEncapChecksum,
NetworkType: *argsNetworkType,
CniConfDir: *argCniConfDir,
CniConfFile: *argCniConfFile,
CniConfName: *argsCniConfName,
DefaultProviderName: *argsDefaultProviderName,
DefaultInterfaceName: *argsDefaultInterfaceName,
ExternalGatewayConfigNS: *argExternalGatewayConfigNS,
}
return config
}
func (config *Configuration) Init(nicBridgeMappings map[string]string) error {
if config.NodeName == "" {
klog.Info("node name not specified in command line parameters, fall back to the environment variable")
if config.NodeName = strings.ToLower(os.Getenv(util.HostnameEnv)); config.NodeName == "" {
klog.Info("node name not specified in environment variables, fall back to the hostname")
hostname, err := os.Hostname()
if err != nil {
return fmt.Errorf("failed to get hostname: %v", err)
}
config.NodeName = strings.ToLower(hostname)
}
}
if err := config.initKubeClient(); err != nil {
return err
}
if err := config.initNicConfig(nicBridgeMappings); err != nil {
return err
}
klog.Infof("daemon config: %v", config)
return nil
}
func (config *Configuration) initNicConfig(nicBridgeMappings map[string]string) error {
// Support to specify node network card separately
node, err := config.KubeClient.CoreV1().Nodes().Get(context.Background(), config.NodeName, metav1.GetOptions{})
if err != nil {
klog.Errorf("Failed to find node info, err: %v", err)
return err
}
if nodeTunnelName := node.GetAnnotations()[util.TunnelInterfaceAnnotation]; nodeTunnelName != "" {
config.Iface = nodeTunnelName
klog.Infof("Find node tunnel interface name: %v", nodeTunnelName)
}
isDPDKNode := node.GetLabels()[util.OvsDpTypeLabel] == "userspace"
if isDPDKNode {
config.Iface = config.DPDKTunnelIface
}
var mtu int
var encapIP string
if config.Iface == "" {
encapIP = config.getEncapIP(node)
if config.Iface, mtu, err = getIfaceByIP(encapIP); err != nil {
klog.Errorf("failed to get interface by IP %s: %v", encapIP, err)
return err
}
} else {
tunnelNic := config.Iface
if brName := nicBridgeMappings[tunnelNic]; brName != "" {
klog.Infof("nic %s has been bridged to %s, use %s as the tunnel interface instead", tunnelNic, brName, brName)
tunnelNic = brName
}
iface, err := findInterface(tunnelNic)
if err != nil {
klog.Errorf("failed to find iface %s, %v", tunnelNic, err)
return err
}
addrs, err := iface.Addrs()
if err != nil {
return fmt.Errorf("failed to get iface addr. %v", err)
}
for _, addr := range addrs {
ipStr := strings.Split(addr.String(), "/")[0]
if ip := net.ParseIP(ipStr); ip == nil || ip.IsLinkLocalUnicast() {
continue
}
encapIP = ipStr
break
}
if len(encapIP) == 0 {
return fmt.Errorf("iface %s has no valid IP address", tunnelNic)
}
mtu = iface.MTU
}
encapIsIPv6 := util.CheckProtocol(encapIP) == kubeovnv1.ProtocolIPv6
if encapIsIPv6 && runtime.GOOS == "windows" {
// OVS windows datapath does not IPv6 tunnel in version v2.17
err = errors.New("IPv6 tunnel is not supported on Windows currently")
klog.Error(err)
return err
}
if config.MTU == 0 {
switch config.NetworkType {
case util.NetworkTypeGeneve, util.NetworkTypeVlan:
config.MTU = mtu - util.GeneveHeaderLength
case util.NetworkTypeVxlan:
config.MTU = mtu - util.VxlanHeaderLength
case util.NetworkTypeStt:
config.MTU = mtu - util.SttHeaderLength
default:
return fmt.Errorf("invalid network type: %s", config.NetworkType)
}
if encapIsIPv6 {
// IPv6 header size is 40
config.MTU -= 20
}
}
config.MSS = config.MTU - util.TcpIpHeaderLength
if !config.EncapChecksum {
if err := disableChecksum(); err != nil {
klog.Errorf("failed to set checksum offload, %v", err)
}
}
if err = config.initRuntimeConfig(node); err != nil {
klog.Error(err)
return err
}
return setEncapIP(encapIP)
}
func (config *Configuration) getEncapIP(node *corev1.Node) string {
if podIP := os.Getenv(util.POD_IP); podIP != "" {
return podIP
}
klog.Info("environment variable POD_IP not found, fall back to node address")
ipv4, ipv6 := util.GetNodeInternalIP(*node)
if ipv4 != "" {
return ipv4
}
return ipv6
}
func findInterface(ifaceStr string) (*net.Interface, error) {
iface, err := net.InterfaceByName(ifaceStr)
if err == nil && iface != nil {
return iface, nil
}
ifaceRegex, err := regexp.Compile("(" + strings.Join(strings.Split(ifaceStr, ","), ")|(") + ")")
if err != nil {
klog.Errorf("Invalid interface regex %s", ifaceStr)
return nil, err
}
ifaces, err := net.Interfaces()
if err != nil {
klog.Errorf("failed to list interfaces, %v", err)
return nil, err
}
for _, iface := range ifaces {
if ifaceRegex.MatchString(iface.Name) {
klog.Infof("use %s as tunnel interface", iface.Name)
return &iface, nil
}
}
klog.Errorf("network interface %s not exist", ifaceStr)
return nil, fmt.Errorf("network interface %s not exist", ifaceStr)
}
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 = util.ContentType
cfg.AcceptContentTypes = util.AcceptContentTypes
kubeClient, err := kubernetes.NewForConfig(cfg)
if err != nil {
klog.Errorf("init kubernetes client failed %v", err)
return err
}
config.KubeClient = kubeClient
return nil
}
func setEncapIP(ip string) error {
raw, err := exec.Command(
"ovs-vsctl", "set", "open", ".", fmt.Sprintf("external-ids:ovn-encap-ip=%s", ip)).CombinedOutput()
if err != nil {
return fmt.Errorf("failed to set ovn-encap-ip, %s", string(raw))
}
return nil
}
func disableChecksum() error {
raw, err := exec.Command(
"ovs-vsctl", "set", "open", ".", "external-ids:ovn-encap-csum=false").CombinedOutput()
if err != nil {
return fmt.Errorf("failed to set ovn-encap-csum, %s", string(raw))
}
return nil
}