-
Notifications
You must be signed in to change notification settings - Fork 335
/
config.go
875 lines (783 loc) · 28.9 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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
package config
import (
"fmt"
"net"
"net/url"
"os"
"path/filepath"
"reflect"
"runtime"
"strings"
"github.com/sirupsen/logrus"
"github.com/urfave/cli"
gcfg "gopkg.in/gcfg.v1"
kexec "k8s.io/utils/exec"
)
// The following are global config parameters that other modules may access directly
var (
// ovn-kubernetes version, to be changed with every release
Version = "0.3.0"
// Default holds parsed config file parameters and command-line overrides
Default = DefaultConfig{
MTU: 1400,
ConntrackZone: 64000,
EncapType: "geneve",
EncapIP: "",
}
// Logging holds logging-related parsed config file parameters and command-line overrides
Logging = LoggingConfig{
File: "", // do not log to a file by default
Level: 4,
}
// CNI holds CNI-related parsed config file parameters and command-line overrides
CNI = CNIConfig{
ConfDir: "/etc/cni/net.d",
Plugin: "ovn-k8s-cni-overlay",
WinHNSNetworkID: "",
}
// Kubernetes holds Kubernetes-related parsed config file parameters and command-line overrides
Kubernetes = KubernetesConfig{
APIServer: "http://localhost:8080",
}
// OvnNorth holds northbound OVN database client and server authentication and location details
OvnNorth OvnAuthConfig
// OvnSouth holds southbound OVN database client and server authentication and location details
OvnSouth OvnAuthConfig
)
// DefaultConfig holds parsed config file parameters and command-line overrides
type DefaultConfig struct {
// MTU value used for the overlay networks.
MTU int `gcfg:"mtu"`
// ConntrackZone affects only the gateway nodes, This value is used to track connections
// that are initiated from the pods so that the reverse connections go back to the pods.
// This represents the conntrack zone used for the conntrack flow rules.
ConntrackZone int `gcfg:"conntrack-zone"`
// EncapType value defines the encapsulation protocol to use to transmit packets between
// hypervisors. By default the value is 'geneve'
EncapType string `gcfg:"encap-type"`
// The IP address of the encapsulation endpoint. If not specified, the IP address the
// NodeName resolves to will be used
EncapIP string `gcfg:"encap-ip"`
}
// LoggingConfig holds logging-related parsed config file parameters and command-line overrides
type LoggingConfig struct {
// File is the path of the file to log to
File string `gcfg:"logfile"`
// Level is the logging verbosity level
Level int `gcfg:"loglevel"`
}
// CNIConfig holds CNI-related parsed config file parameters and command-line overrides
type CNIConfig struct {
// ConfDir specifies the CNI config directory in which to write the overlay CNI config file
ConfDir string `gcfg:"conf-dir"`
// Plugin specifies the name of the CNI plugin
Plugin string `gcfg:"plugin"`
// Windows ONLY, specifies the ID of the HNS Network to which the containers will be attached
WinHNSNetworkID string `gcfg:"win-hnsnetwork-id"`
}
// KubernetesConfig holds Kubernetes-related parsed config file parameters and command-line overrides
type KubernetesConfig struct {
Kubeconfig string `gcfg:"kubeconfig"`
CACert string `gcfg:"cacert"`
APIServer string `gcfg:"apiserver"`
Token string `gcfg:"token"`
}
// OvnAuthConfig holds client and server authentication and location details for
// an OVN database (either northbound or southbound)
type OvnAuthConfig struct {
ClientAuth *OvnDBAuth
ServerAuth *OvnDBAuth
}
// Holds values read from the config file or command-line that are then
// synthesized into OvnDBAuth structures in an OvnAuthConfig object
type rawOvnAuthConfig struct {
Address string `gcfg:"address"`
ClientPrivKey string `gcfg:"client-privkey"`
ClientCert string `gcfg:"client-cert"`
ClientCACert string `gcfg:"client-cacert"`
ServerPrivKey string `gcfg:"server-privkey"`
ServerCert string `gcfg:"server-cert"`
ServerCACert string `gcfg:"server-cacert"`
}
// OvnDBScheme describes the OVN database connection transport method
type OvnDBScheme string
const (
// OvnDBSchemeSSL specifies SSL as the OVN database transport method
OvnDBSchemeSSL OvnDBScheme = "ssl"
// OvnDBSchemeTCP specifies TCP as the OVN database transport method
OvnDBSchemeTCP OvnDBScheme = "tcp"
// OvnDBSchemeUnix specifies Unix domains sockets as the OVN database transport method
OvnDBSchemeUnix OvnDBScheme = "unix"
)
// Config is used to read the structured config file and to cache config in testcases
type config struct {
Default DefaultConfig
Logging LoggingConfig
CNI CNIConfig
Kubernetes KubernetesConfig
OvnNorth rawOvnAuthConfig
OvnSouth rawOvnAuthConfig
}
var (
savedDefault DefaultConfig
savedLogging LoggingConfig
savedCNI CNIConfig
savedKubernetes KubernetesConfig
savedOvnNorth OvnAuthConfig
savedOvnSouth OvnAuthConfig
)
func init() {
// Cache original default config values so they can be restored by testcases
savedDefault = Default
savedLogging = Logging
savedCNI = CNI
savedKubernetes = Kubernetes
savedOvnNorth = OvnNorth
savedOvnSouth = OvnSouth
}
// RestoreDefaultConfig restores default config values. Used by testcases to
// provide a pristine environment between tests.
func RestoreDefaultConfig() {
Default = savedDefault
Logging = savedLogging
CNI = savedCNI
Kubernetes = savedKubernetes
OvnNorth = savedOvnNorth
OvnSouth = savedOvnSouth
}
// copy members of struct 'src' into the corresponding field in struct 'dst'
// if the field in 'src' is a non-zero int or a non-zero-length string. This
// function should be called with pointers to structs.
func overrideFields(dst, src interface{}) {
dstStruct := reflect.ValueOf(dst).Elem()
srcStruct := reflect.ValueOf(src).Elem()
if dstStruct.Kind() != srcStruct.Kind() || dstStruct.Kind() != reflect.Struct {
panic("mismatched value types")
}
if dstStruct.NumField() != srcStruct.NumField() {
panic("mismatched struct types")
}
for i := 0; i < dstStruct.NumField(); i++ {
dstField := dstStruct.Field(i)
srcField := srcStruct.Field(i)
if dstField.Kind() != srcField.Kind() {
panic("mismatched struct fields")
}
switch srcField.Kind() {
case reflect.String:
if srcField.String() != "" {
dstField.Set(srcField)
}
case reflect.Int:
if srcField.Int() != 0 {
dstField.Set(srcField)
}
default:
panic(fmt.Sprintf("unhandled struct field type: %v", srcField.Kind()))
}
}
}
var cliConfig config
// Flags are general command-line flags. Apps should add these flags to their
// own urfave/cli flags and call InitConfig() early in the application.
var Flags = []cli.Flag{
cli.StringFlag{
Name: "config-file",
Usage: "configuration file path (default: /etc/openvswitch/ovn_k8s.conf)",
},
// Generic options
cli.IntFlag{
Name: "mtu",
Usage: "MTU value used for the overlay networks (default: 1400)",
Destination: &cliConfig.Default.MTU,
},
cli.IntFlag{
Name: "conntrack-zone",
Usage: "For gateway nodes, the conntrack zone used for conntrack flow rules (default: 64000)",
Destination: &cliConfig.Default.ConntrackZone,
},
cli.StringFlag{
Name: "encap-type",
Usage: "The encapsulation protocol to use to transmit packets between hypervisors (default: geneve)",
Destination: &cliConfig.Default.EncapType,
},
cli.StringFlag{
Name: "encap-ip",
Usage: "The IP address of the encapsulation endpoint (default: Node IP address resolved from Node hostname)",
Destination: &cliConfig.Default.EncapIP,
},
// Logging options
cli.IntFlag{
Name: "loglevel",
Usage: "log verbosity and level: 5=debug, 4=info, 3=warn, 2=error, 1=fatal (default: 4)",
Destination: &cliConfig.Logging.Level,
},
cli.StringFlag{
Name: "logfile",
Usage: "path of a file to direct log output to",
Destination: &cliConfig.Logging.File,
},
// CNI options
cli.StringFlag{
Name: "cni-conf-dir",
Usage: "the CNI config directory in which to write the overlay CNI config file (default: /etc/cni/net.d)",
Destination: &cliConfig.CNI.ConfDir,
},
cli.StringFlag{
Name: "cni-plugin",
Usage: "the name of the CNI plugin (default: ovn-k8s-cni-overlay)",
Destination: &cliConfig.CNI.Plugin,
},
cli.StringFlag{
Name: "win-hnsnetwork-id",
Usage: "the ID of the HNS network to which containers will be attached (default: not set)",
Destination: &cliConfig.CNI.WinHNSNetworkID,
},
// Kubernetes-related options
cli.StringFlag{
Name: "k8s-kubeconfig",
Usage: "absolute path to the Kubernetes kubeconfig file (not required if the --k8s-apiserver, --k8s-ca-cert, and --k8s-token are given)",
Destination: &cliConfig.Kubernetes.Kubeconfig,
},
cli.StringFlag{
Name: "k8s-apiserver",
Usage: "URL of the Kubernetes API server (not required if --k8s-kubeconfig is given) (default: http://localhost:8443)",
Destination: &cliConfig.Kubernetes.APIServer,
},
cli.StringFlag{
Name: "k8s-cacert",
Usage: "the absolute path to the Kubernetes API CA certificate (not required if --k8s-kubeconfig is given)",
Destination: &cliConfig.Kubernetes.CACert,
},
cli.StringFlag{
Name: "k8s-token",
Usage: "the Kubernetes API authentication token (not required if --k8s-kubeconfig is given)",
Destination: &cliConfig.Kubernetes.Token,
},
// OVN northbound database options
cli.StringFlag{
Name: "nb-address",
Usage: "IP address and port of the OVN northbound API " +
"(eg, ssl://1.2.3.4:6641,ssl://1.2.3.5:6642). Leave empty to " +
"use a local unix socket.",
Destination: &cliConfig.OvnNorth.Address,
},
cli.StringFlag{
Name: "nb-server-privkey",
Usage: "Private key that the OVN northbound API should use for securing the API. Leave empty to use local unix socket. (default: /etc/openvswitch/ovnnb-privkey.pem)",
Destination: &cliConfig.OvnNorth.ServerPrivKey,
},
cli.StringFlag{
Name: "nb-server-cert",
Usage: "Server certificate that the OVN northbound API should use for securing the API. Leave empty to use local unix socket. (default: /etc/openvswitch/ovnnb-cert.pem)",
Destination: &cliConfig.OvnNorth.ServerCert,
},
cli.StringFlag{
Name: "nb-server-cacert",
Usage: "CA certificate that the OVN northbound API should use for securing the API. Leave empty to use local unix socket. (default: /etc/openvswitch/ovnnb-ca.cert)",
Destination: &cliConfig.OvnNorth.ServerCACert,
},
cli.StringFlag{
Name: "nb-client-privkey",
Usage: "Private key that the client should use for talking to the OVN database. Leave empty to use local unix socket. (default: /etc/openvswitch/ovnnb-privkey.pem)",
Destination: &cliConfig.OvnNorth.ClientPrivKey,
},
cli.StringFlag{
Name: "nb-client-cert",
Usage: "Client certificate that the client should use for talking to the OVN database. Leave empty to use local unix socket. (default: /etc/openvswitch/ovnnb-cert.pem)",
Destination: &cliConfig.OvnNorth.ClientCert,
},
cli.StringFlag{
Name: "nb-client-cacert",
Usage: "CA certificate that the client should use for talking to the OVN database. Leave empty to use local unix socket. (default: /etc/openvswitch/ovnnb-ca.cert)",
Destination: &cliConfig.OvnNorth.ClientCACert,
},
// OVN southbound database options
cli.StringFlag{
Name: "sb-address",
Usage: "IP address and port of the OVN southbound API " +
"(eg, ssl://1.2.3.4:6642,ssl://1.2.3.5:6642). " +
"Leave empty to use a local unix socket.",
Destination: &cliConfig.OvnSouth.Address,
},
cli.StringFlag{
Name: "sb-server-privkey",
Usage: "Private key that the OVN southbound API should use for securing the API. Leave empty to use local unix socket. (default: /etc/openvswitch/ovnsb-privkey.pem)",
Destination: &cliConfig.OvnSouth.ServerPrivKey,
},
cli.StringFlag{
Name: "sb-server-cert",
Usage: "Server certificate that the OVN southbound API should use for securing the API. Leave empty to use local unix socket. (default: /etc/openvswitch/ovnsb-cert.pem)",
Destination: &cliConfig.OvnSouth.ServerCert,
},
cli.StringFlag{
Name: "sb-server-cacert",
Usage: "CA certificate that the OVN southbound API should use for securing the API. Leave empty to use local unix socket. (default: /etc/openvswitch/ovnsb-ca.cert)",
Destination: &cliConfig.OvnSouth.ServerCACert,
},
cli.StringFlag{
Name: "sb-client-privkey",
Usage: "Private key that the client should use for talking to the OVN database. Leave empty to use local unix socket. (default: /etc/openvswitch/ovnsb-privkey.pem)",
Destination: &cliConfig.OvnSouth.ClientPrivKey,
},
cli.StringFlag{
Name: "sb-client-cert",
Usage: "Client certificate that the client should use for talking to the OVN database. Leave empty to use local unix socket. (default: /etc/openvswitch/ovnsb-cert.pem)",
Destination: &cliConfig.OvnSouth.ClientCert,
},
cli.StringFlag{
Name: "sb-client-cacert",
Usage: "CA certificate that the client should use for talking to the OVN database. Leave empty to use local unix socket. (default: /etc/openvswitch/ovnsb-ca.cert)",
Destination: &cliConfig.OvnSouth.ClientCACert,
},
}
// Defaults are a set of flags to indicate which options should be read from
// ovs-vsctl and used as default values if option is not found via the config
// file or command-line
type Defaults struct {
OvnNorthAddress bool
K8sAPIServer bool
K8sToken bool
K8sCert bool
}
const (
ovsVsctlCommand = "ovs-vsctl"
)
// Can't use pkg/ovs or pkg/util here because those package import this one
func rawExec(exec kexec.Interface, cmd string, args ...string) (string, error) {
cmdPath, err := exec.LookPath(cmd)
if err != nil {
return "", err
}
logrus.Debugf("exec: %s %s", cmdPath, strings.Join(args, " "))
out, err := exec.Command(cmdPath, args...).CombinedOutput()
if err != nil {
logrus.Debugf("exec: %s %s => %v", cmdPath, strings.Join(args, " "), err)
return "", err
}
return strings.TrimSpace(string(out)), nil
}
// Can't use pkg/ovs or pkg/util here because those package import this one
func runOVSVsctl(exec kexec.Interface, args ...string) (string, error) {
newArgs := append([]string{"--timeout=15"}, args...)
out, err := rawExec(exec, ovsVsctlCommand, newArgs...)
if err != nil {
return "", err
}
return strings.Trim(strings.TrimSpace(out), "\""), nil
}
func getOVSExternalID(exec kexec.Interface, name string) string {
out, err := runOVSVsctl(exec,
"--if-exists",
"get",
"Open_vSwitch",
".",
"external_ids:"+name)
if err != nil {
logrus.Debugf("failed to get OVS external_id %s: %v\n\t%s", name, err, out)
return ""
}
return out
}
func setOVSExternalID(exec kexec.Interface, key, value string) error {
out, err := runOVSVsctl(exec,
"set",
"Open_vSwitch",
".",
fmt.Sprintf("external_ids:%s=%s", key, value))
if err != nil {
return fmt.Errorf("Error setting OVS external ID '%s=%s': %v\n %q", key, value, err, out)
}
return nil
}
func buildKubernetesConfig(exec kexec.Interface, cli, file *config, defaults *Defaults) error {
// Grab default values from OVS external IDs
if defaults.K8sAPIServer {
Kubernetes.APIServer = getOVSExternalID(exec, "k8s-api-server")
}
if defaults.K8sToken {
Kubernetes.Token = getOVSExternalID(exec, "k8s-api-token")
}
if defaults.K8sCert {
Kubernetes.CACert = getOVSExternalID(exec, "k8s-ca-certificate")
}
// Copy config file values over default values
overrideFields(&Kubernetes, &file.Kubernetes)
// And CLI overrides over config file and default values
overrideFields(&Kubernetes, &cli.Kubernetes)
if Kubernetes.Kubeconfig != "" && !pathExists(Kubernetes.Kubeconfig) {
return fmt.Errorf("kubernetes kubeconfig file %q not found", Kubernetes.Kubeconfig)
}
if Kubernetes.CACert != "" && !pathExists(Kubernetes.CACert) {
return fmt.Errorf("kubernetes CA certificate file %q not found", Kubernetes.CACert)
}
url, err := url.Parse(Kubernetes.APIServer)
if err != nil {
return fmt.Errorf("kubernetes API server address %q invalid: %v", Kubernetes.APIServer, err)
} else if url.Scheme != "https" && url.Scheme != "http" {
return fmt.Errorf("kubernetes API server URL scheme %q invalid", url.Scheme)
}
return nil
}
func buildOvnAuth(exec kexec.Interface, direction, externalID string, cliAuth, confAuth *rawOvnAuthConfig, readAddress bool) (OvnAuthConfig, error) {
ctlCmd := "ovn-" + direction + "ctl"
// Determine final address so we know how to set cert/key defaults
address := cliAuth.Address
if address == "" {
address = confAuth.Address
}
if address == "" && readAddress {
address = getOVSExternalID(exec, "ovn-"+direction)
}
auth := &rawOvnAuthConfig{Address: address}
if strings.HasPrefix(address, "ssl") {
// Set up default SSL cert/key paths
auth.ClientCACert = "/etc/openvswitch/ovn" + direction + "-ca.cert"
auth.ServerCACert = auth.ClientCACert
auth.ClientPrivKey = "/etc/openvswitch/ovn" + direction + "-privkey.pem"
auth.ServerPrivKey = auth.ClientPrivKey
auth.ClientCert = "/etc/openvswitch/ovn" + direction + "-cert.pem"
auth.ServerCert = auth.ClientCert
}
overrideFields(auth, confAuth)
overrideFields(auth, cliAuth)
clientAuth, err := newOvnDBAuth(exec, ctlCmd, externalID, auth.Address, auth.ClientPrivKey, auth.ClientCert, auth.ClientCACert, false)
if err != nil {
return OvnAuthConfig{}, err
}
serverAuth, err := newOvnDBAuth(exec, ctlCmd, externalID, auth.Address, auth.ServerPrivKey, auth.ServerCert, auth.ServerCACert, true)
if err != nil {
return OvnAuthConfig{}, err
}
return OvnAuthConfig{
ClientAuth: clientAuth,
ServerAuth: serverAuth,
}, nil
}
// getConfigFilePath returns config file path and 'true' if the config file is
// the fallback path (eg not given by the user), 'false' if given explicitly
// by the user
func getConfigFilePath(ctx *cli.Context) (string, bool) {
configFile := ctx.String("config-file")
if configFile != "" {
return configFile, false
}
// Linux default
if runtime.GOOS != "windows" {
return filepath.Join("/etc", "openvswitch", "ovn_k8s.conf"), true
}
// Windows default
return filepath.Join(os.Getenv("OVS_SYSCONFDIR"), "ovn_k8s.conf"), true
}
// InitConfig reads the config file and common command-line options and
// constructs the global config object from them. It returns the config file
// path (if explicitly specified) or an error
func InitConfig(ctx *cli.Context, exec kexec.Interface, defaults *Defaults) (string, error) {
return InitConfigWithPath(ctx, exec, "", defaults)
}
// InitConfigWithPath reads the given config file (or if empty, reads the config file
// specified by command-line arguments, or empty, the default config file) and
// common command-line options and constructs the global config object from
// them. It returns the config file path (if explicitly specified) or an error
func InitConfigWithPath(ctx *cli.Context, exec kexec.Interface, configFile string, defaults *Defaults) (string, error) {
var cfg config
var retConfigFile string
var configFileIsDefault bool
// If no specific config file was given, try to find one from command-line
// arguments, or the platform-specific default config file path
if configFile == "" {
configFile, configFileIsDefault = getConfigFilePath(ctx)
}
logrus.SetOutput(os.Stderr)
if !configFileIsDefault {
// Only return explicitly specified config file
retConfigFile = configFile
}
f, err := os.Open(configFile)
// Failure to find a default config file is not a hard error
if err != nil && !configFileIsDefault {
return "", fmt.Errorf("failed to open config file %s: %v", configFile, err)
}
if f != nil {
defer f.Close()
// Parse ovn-k8s config file.
if err = gcfg.ReadInto(&cfg, f); err != nil {
return "", fmt.Errorf("failed to parse config file %s: %v", f.Name(), err)
}
logrus.Infof("Parsed config file %s", f.Name())
logrus.Infof("Parsed config: %+v", cfg)
}
if defaults == nil {
defaults = &Defaults{}
}
// Build config that needs no special processing
overrideFields(&Default, &cfg.Default)
overrideFields(&Default, &cliConfig.Default)
overrideFields(&CNI, &cfg.CNI)
overrideFields(&CNI, &cliConfig.CNI)
// Logging setup
overrideFields(&Logging, &cfg.Logging)
overrideFields(&Logging, &cliConfig.Logging)
logrus.SetLevel(logrus.Level(Logging.Level))
if Logging.File != "" {
var file *os.File
file, err = os.OpenFile(Logging.File, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0660)
if err != nil {
logrus.Errorf("failed to open logfile %s (%v). Ignoring..", Logging.File, err)
} else {
logrus.SetOutput(file)
}
}
if err = buildKubernetesConfig(exec, &cliConfig, &cfg, defaults); err != nil {
return "", err
}
OvnNorth, err = buildOvnAuth(exec, "nb", "ovn-nb", &cliConfig.OvnNorth, &cfg.OvnNorth, defaults.OvnNorthAddress)
if err != nil {
return "", err
}
OvnSouth, err = buildOvnAuth(exec, "sb", "ovn-remote", &cliConfig.OvnSouth, &cfg.OvnSouth, false)
if err != nil {
return "", err
}
logrus.Debugf("Default config: %+v", Default)
logrus.Debugf("Logging config: %+v", Logging)
logrus.Debugf("CNI config: %+v", CNI)
logrus.Debugf("Kubernetes config: %+v", Kubernetes)
logrus.Debugf("OVN North config: %+v", OvnNorth)
logrus.Debugf("OVN South config: %+v", OvnSouth)
return retConfigFile, nil
}
// OvnDBAuth describes an OVN database location and authentication method
type OvnDBAuth struct {
OvnAddressForClient string // e.g: "ssl:192.168.1.2:6641,ssl:192.168.1.2:6642"
OvnAddressForServer string // e.g: "pssl:6641"
PrivKey string
Cert string
CACert string
Scheme OvnDBScheme
server bool
ctlCmd string // e.g: ovn-nbctl
externalID string // ovn-nb or ovn-remote
exec kexec.Interface
}
func pathExists(path string) bool {
_, err := os.Stat(path)
if err != nil && os.IsNotExist(err) {
return false
}
return true
}
// newOvnDBAuth returns an OvnDBAuth object describing the connection to an
// OVN database, given a connection description string and authentication
// details
func newOvnDBAuth(exec kexec.Interface, ctlCmd, externalID, urlString, privkey, cert, cacert string, server bool) (*OvnDBAuth, error) {
if urlString == "" {
if privkey != "" || cert != "" || cacert != "" {
return nil, fmt.Errorf("certificate or key given; perhaps you mean to use the 'ssl' scheme?")
}
return &OvnDBAuth{
server: server,
Scheme: OvnDBSchemeUnix,
ctlCmd: ctlCmd,
externalID: externalID,
exec: exec,
}, nil
}
auth := &OvnDBAuth{
server: server,
ctlCmd: ctlCmd,
externalID: externalID,
exec: exec,
}
// urlString can be of form "ssl:1.2.3.4:6641,ssl:1.2.3.5:6641" or
// "ssl://1.2.3.4:6641,ssl://1.2.3.5:6641"
scheme := ""
urlString = strings.Replace(urlString, "//", "", -1)
ovnAddresses := strings.Split(urlString, ",")
for _, ovnAddress := range ovnAddresses {
splits := strings.Split(ovnAddress, ":")
if len(splits) != 3 {
return nil, fmt.Errorf("Failed to parse OVN address %s", urlString)
}
hostPort := splits[1] + ":" + splits[2]
if scheme == "" {
scheme = splits[0]
} else if scheme != splits[0] {
return nil, fmt.Errorf("Invalid protocols in OVN address %s",
urlString)
}
host, port, err := net.SplitHostPort(hostPort)
if err != nil {
return nil, fmt.Errorf("failed to parse OVN DB host/port %q: %v",
hostPort, err)
}
ip := net.ParseIP(host)
if ip == nil {
return nil, fmt.Errorf("OVN DB host %q must be an IP address, "+
"not a DNS name", hostPort)
}
if server && auth.OvnAddressForServer == "" {
auth.OvnAddressForServer = fmt.Sprintf("p%s:%s", scheme, port)
}
if !server {
if auth.OvnAddressForClient == "" {
auth.OvnAddressForClient = fmt.Sprintf("%s:%s:%s",
scheme, host, port)
} else {
auth.OvnAddressForClient = fmt.Sprintf("%s,%s:%s:%s",
auth.OvnAddressForClient, scheme, host, port)
}
}
}
switch {
case scheme == "ssl":
if privkey == "" || cert == "" || cacert == "" {
return nil, fmt.Errorf("must specify private key, certificate, and CA certificate for 'ssl' scheme")
}
auth.Scheme = OvnDBSchemeSSL
auth.PrivKey = privkey
auth.Cert = cert
auth.CACert = cacert
case scheme == "tcp":
if privkey != "" || cert != "" || cacert != "" {
return nil, fmt.Errorf("certificate or key given; perhaps you mean to use the 'ssl' scheme?")
}
auth.Scheme = OvnDBSchemeTCP
default:
return nil, fmt.Errorf("unknown OVN DB scheme %q", scheme)
}
return auth, nil
}
func (a *OvnDBAuth) ensureCACert() error {
if pathExists(a.CACert) {
// CA file exists, nothing to do
return nil
}
if a.server {
// Server requires CA certificate file
return fmt.Errorf("CA certificate file %s not found", a.CACert)
}
// Client can bootstrap the CA from the OVN API. Use nbctl for both
// SB and NB since ovn-sbctl only supports --bootstrap-ca-cert from
// 2.9.90+.
// FIXME: change back to a.ctlCmd when sbctl supports --bootstrap-ca-cert
// https://github.com/openvswitch/ovs/pull/226
args := []string{
"--db=" + a.GetURL(),
"--timeout=5",
}
if a.Scheme == OvnDBSchemeSSL {
args = append(args, "--private-key="+a.PrivKey)
args = append(args, "--certificate="+a.Cert)
args = append(args, "--bootstrap-ca-cert="+a.CACert)
}
args = append(args, "list", "nb_global")
_, _ = rawExec(a.exec, "ovn-nbctl", args...)
if _, err := os.Stat(a.CACert); os.IsNotExist(err) {
logrus.Warnf("bootstrapping %s CA certificate failed", a.CACert)
}
return nil
}
// GetURL returns a URL suitable for passing to ovn-northd which describes the
// transport mechanism for connection to the database
func (a *OvnDBAuth) GetURL() string {
// FIXME: support specific IP Addresses or non-default Unix socket paths
if a.server {
return a.OvnAddressForServer
}
return a.OvnAddressForClient
}
// SetDBAuth sets the authentication configuration and connection method
// for the OVN northbound or southbound database server or client
func (a *OvnDBAuth) SetDBAuth() error {
if a.Scheme == OvnDBSchemeUnix {
// Nothing to do
return nil
} else if a.Scheme == OvnDBSchemeSSL {
// Both server and client SSL schemes require privkey and cert
if !pathExists(a.PrivKey) {
return fmt.Errorf("private key file %s not found", a.PrivKey)
}
if !pathExists(a.Cert) {
return fmt.Errorf("certificate file %s not found", a.Cert)
}
}
if a.server {
// Set the database connection method
out, err := rawExec(a.exec, a.ctlCmd, "set-connection", a.GetURL())
if err != nil {
return fmt.Errorf("error setting %s API connection: %v\n %q", a.ctlCmd, err, out)
}
if a.Scheme == OvnDBSchemeSSL {
// Server auth requires CA certificate to exist
if !pathExists(a.CACert) {
return fmt.Errorf("server CA certificate file %s not found", a.CACert)
}
// Tell the database what SSL keys and certs to use, but before that delete
// any SSL configuration. Otherwise, ovn-{nbctl|sbctl} set-ssl command will hang
out, err = rawExec(a.exec, a.ctlCmd, "del-ssl")
if err != nil {
return fmt.Errorf("error deleting %s SSL configuration: %v\n %q", a.ctlCmd, err, out)
}
out, err = rawExec(a.exec, a.ctlCmd, "set-ssl", a.PrivKey, a.Cert, a.CACert)
if err != nil {
return fmt.Errorf("error setting %s SSL API certificates: %v\n %q", a.ctlCmd, err, out)
}
}
} else {
if a.Scheme == OvnDBSchemeSSL {
// Client can bootstrap the CA cert from the DB
if err := a.ensureCACert(); err != nil {
return err
}
// Tell Southbound DB clients (like ovn-controller)
// which certificates to use to talk to the DB.
// Must happen *before* setting the "ovn-remote"
// external-id.
if a.ctlCmd == "ovn-sbctl" {
out, err := runOVSVsctl(a.exec, "del-ssl")
if err != nil {
return fmt.Errorf("error deleting ovs-vsctl SSL "+
"configuration: %q (%v)", out, err)
}
out, err = runOVSVsctl(a.exec, "set-ssl", a.PrivKey, a.Cert, a.CACert)
if err != nil {
return fmt.Errorf("error setting client southbound DB SSL options: %v\n %q", err, out)
}
}
}
if err := setOVSExternalID(a.exec, a.externalID, "\""+a.GetURL()+"\""); err != nil {
return err
}
}
return nil
}
func (a *OvnDBAuth) updateIP(newIP string) error {
if a.OvnAddressForClient != "" {
s := strings.Split(a.OvnAddressForClient, ":")
if len(s) != 3 {
return fmt.Errorf("failed to parse OvnDBAuth "+
"a.OvnAddressForClient: %q", a.OvnAddressForClient)
}
a.OvnAddressForClient = s[0] + ":" + newIP + s[2]
}
return nil
}
// UpdateOvnNodeAuth updates the host and URL in ClientAuth and ServerAuth
// for both OvnNorth and OvnSouth. It updates them with the new masterIP.
func UpdateOvnNodeAuth(masterIP string) error {
logrus.Debugf("Update OVN node auth with new master ip: %s", masterIP)
if err := OvnNorth.ClientAuth.updateIP(masterIP); err != nil {
return fmt.Errorf("failed to update OvnNorth ClientAuth URL: %v", err)
}
if err := OvnNorth.ServerAuth.updateIP(masterIP); err != nil {
return fmt.Errorf("failed to update OvnNorth ServerAuth URL: %v", err)
}
if err := OvnSouth.ClientAuth.updateIP(masterIP); err != nil {
return fmt.Errorf("failed to update OvnSouth ClientAuth URL: %v", err)
}
if err := OvnSouth.ServerAuth.updateIP(masterIP); err != nil {
return fmt.Errorf("failed to update OvnSouth ServerAuth URL: %v", err)
}
return nil
}