/
config.go
160 lines (133 loc) · 3.7 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
// Copyright 2016-2018 Authors of Cilium
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package workloads
import (
"fmt"
"net"
"sync"
"github.com/cilium/cilium/api/v1/models"
)
// setOpts validates the specified options against the selected workload runtime
// and then modifies the configuration
func setOpts(opts map[string]string, supportedOpts workloadRuntimeOpts) error {
errors := 0
for key, val := range opts {
opt, ok := supportedOpts[key]
if !ok {
errors++
log.Errorf("unknown workload runtime configuration key %q", key)
continue
}
if opt.validate != nil {
if err := opt.validate(val); err != nil {
log.Errorf("invalid value for key %s: %s", key, err)
errors++
}
}
}
// if errors have occurred, print the supported configuration keys to
// the log
if errors > 0 {
log.Error("Supported configuration keys:")
for key, val := range supportedOpts {
log.Errorf(" %-12s %s", key, val.description)
}
return fmt.Errorf("invalid workload runtime configuration, see log for details")
}
// modify the configuration atomically after verification
for key, val := range opts {
supportedOpts[key].value = val
}
return nil
}
func getOpts(opts workloadRuntimeOpts) map[string]string {
result := map[string]string{}
for key, opt := range opts {
result[key] = opt.value
}
return result
}
var (
setupOnce sync.Once
allocator allocatorInterface
)
func setupWorkload(workloadRuntime WorkloadRuntimeType, opts map[string]string) error {
workloadMod := getWorkload(workloadRuntime)
if workloadMod == nil {
return fmt.Errorf("unknown workloadRuntime runtime type %s", workloadRuntime)
}
if err := workloadMod.setConfig(opts); err != nil {
return err
}
return initClient(workloadMod)
}
type allocatorInterface interface {
BlacklistIP(ip net.IP, owner string)
}
// Setup sets up the workload runtime specified in workloadRuntime and configures it
// with the options provided in opts
func Setup(a allocatorInterface, workloadRuntimes []string, opts map[WorkloadRuntimeType]map[string]string) error {
return setup(a, workloadRuntimes, opts, false)
}
func setup(a allocatorInterface, workloadRuntimes []string, opts map[WorkloadRuntimeType]map[string]string, bypassStatusCheck bool) error {
var (
st *models.Status
err error
)
if bypassStatusCheck {
st = &models.Status{State: models.StatusStateOk}
}
setupOnce.Do(
func() {
allocator = a
var crt WorkloadRuntimeType
for _, runtime := range workloadRuntimes {
crt, err = parseRuntimeType(runtime)
if err != nil {
return
}
switch crt {
case None:
unregisterWorkloads()
err = nil
return
}
}
for _, runtime := range workloadRuntimes {
crt, err = parseRuntimeType(runtime)
if err != nil {
err = nil
}
switch crt {
case Auto:
var setupErr error
for _, rt := range []WorkloadRuntimeType{Docker, ContainerD, CRIO} {
setupErr = setupWorkload(rt, opts[rt])
if !bypassStatusCheck {
st = Status()
}
if setupErr == nil && st.State == models.StatusStateOk {
return
}
}
err = setupErr
return
default:
err = setupWorkload(crt, opts[crt])
return
}
}
})
return err
}