forked from coreos/container-linux-config-transpiler
/
flannel.go
233 lines (210 loc) · 7.31 KB
/
flannel.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
package types
import (
"encoding/json"
"errors"
"fmt"
"github.com/coreos/go-semver/semver"
ignTypes "github.com/coreos/ignition/config/v2_0/types"
"github.com/coreos/ignition/config/validate"
"github.com/coreos/ignition/config/validate/report"
)
var (
ErrFlannelTooOld = errors.New("invalid flannel version (too old)")
ErrFlannelMinorTooNew = errors.New("flannel minor version too new. Only options available in the previous minor version will be supported")
ErrNetConfigInvalidJSON = errors.New("flannel network config doesn't appear to be valid JSON")
ErrNetConfigProvidedAndKubeMgrSet = errors.New("flannel network config cannot be provided if kube_subnet_mgr is set")
OldestFlannelVersion = *semver.New("0.5.0")
FlannelDefaultVersion = *semver.New("0.6.0")
)
type Flannel struct {
Version *FlannelVersion `yaml:"version"`
NetworkConfig NetworkConfig `yaml:"network_config"`
Options
}
type flannelCommon Flannel
type FlannelVersion semver.Version
type NetworkConfig string
func (nc NetworkConfig) Validate() report.Report {
if nc == "" {
return report.Report{}
}
tmp := make(map[string]interface{})
err := json.Unmarshal([]byte(nc), &tmp)
if err != nil {
return report.ReportFromError(ErrNetConfigInvalidJSON, report.EntryError)
}
return report.Report{}
}
func (v *FlannelVersion) UnmarshalYAML(unmarshal func(interface{}) error) error {
t := semver.Version(*v)
if err := unmarshal(&t); err != nil {
return err
}
*v = FlannelVersion(t)
return nil
}
func (fv FlannelVersion) Validate() report.Report {
v := semver.Version(fv)
switch {
case v.LessThan(OldestFlannelVersion):
return report.ReportFromError(ErrFlannelTooOld, report.EntryError)
case v.Major == 0 && fv.Minor > 7:
return report.ReportFromError(ErrFlannelMinorTooNew, report.EntryWarning)
}
return report.Report{}
}
func (fv FlannelVersion) String() string {
return semver.Version(fv).String()
}
func (f *Flannel) Validate() report.Report {
switch o := f.Options.(type) {
case Flannel0_7:
if o.KubeSubnetMgr && f.NetworkConfig != "" {
return report.ReportFromError(ErrNetConfigProvidedAndKubeMgrSet, report.EntryError)
}
}
return report.Report{}
}
func (flannel *Flannel) UnmarshalYAML(unmarshal func(interface{}) error) error {
t := flannelCommon(*flannel)
if err := unmarshal(&t); err != nil {
return err
}
*flannel = Flannel(t)
var v semver.Version
if flannel.Version == nil {
v = FlannelDefaultVersion
} else {
v = semver.Version(*flannel.Version)
}
if v.Major == 0 && v.Minor >= 7 {
o := Flannel0_7{}
if err := unmarshal(&o); err != nil {
return err
}
flannel.Options = o
} else if v.Major == 0 && v.Minor == 6 {
o := Flannel0_6{}
if err := unmarshal(&o); err != nil {
return err
}
flannel.Options = o
} else if v.Major == 0 && v.Minor == 5 {
o := Flannel0_5{}
if err := unmarshal(&o); err != nil {
return err
}
flannel.Options = o
}
return nil
}
func init() {
register2_0(func(in Config, ast validate.AstNode, out ignTypes.Config, platform string) (ignTypes.Config, report.Report, validate.AstNode) {
if in.Flannel != nil {
contents, err := flannelContents(*in.Flannel, platform)
if err != nil {
return ignTypes.Config{}, report.ReportFromError(err, report.EntryError), ast
}
out.Systemd.Units = append(out.Systemd.Units, ignTypes.SystemdUnit{
Name: "flanneld.service",
Enable: true,
DropIns: []ignTypes.SystemdUnitDropIn{{
Name: "20-clct-flannel.conf",
Contents: contents,
}},
})
}
return out, report.Report{}, ast
})
}
// flannelContents creates the string containing the systemd drop in for flannel
func flannelContents(flannel Flannel, platform string) (string, error) {
args := getCliArgs(flannel.Options)
var vars []string
if flannel.Version != nil {
vars = []string{fmt.Sprintf("FLANNEL_IMAGE_TAG=v%s", flannel.Version)}
}
unit, err := assembleUnit("/usr/lib/coreos/flannel-wrapper $FLANNEL_OPTS", args, vars, platform)
if err != nil {
return "", err
}
if flannel.NetworkConfig != "" {
pre := "ExecStartPre=/usr/bin/etcdctl"
var endpoints string
var etcdCAFile string
var etcdCertFile string
var etcdKeyFile string
switch o := flannel.Options.(type) {
case Flannel0_7:
endpoints = o.EtcdEndpoints
etcdCAFile = o.EtcdCAFile
etcdCertFile = o.EtcdCertFile
etcdKeyFile = o.EtcdKeyFile
case Flannel0_6:
endpoints = o.EtcdEndpoints
etcdCAFile = o.EtcdCAFile
etcdCertFile = o.EtcdCertFile
etcdKeyFile = o.EtcdKeyFile
case Flannel0_5:
endpoints = o.EtcdEndpoints
etcdCAFile = o.EtcdCAFile
etcdCertFile = o.EtcdCertFile
etcdKeyFile = o.EtcdKeyFile
}
if endpoints != "" {
pre += fmt.Sprintf(" --endpoints=%q", endpoints)
}
if etcdCAFile != "" {
pre += fmt.Sprintf(" --ca-file=%q", etcdCAFile)
}
if etcdCertFile != "" {
pre += fmt.Sprintf(" --cert-file=%q", etcdCertFile)
}
if etcdKeyFile != "" {
pre += fmt.Sprintf(" --key-file=%q", etcdKeyFile)
}
pre += fmt.Sprintf(" set /coreos.com/network/config %q", flannel.NetworkConfig)
unit.Service.Add(pre)
}
return unit.String(), nil
}
// Flannel0_7 represents flannel options for version 0.7.x. Don't embed Flannel0_6 because
// the yaml parser doesn't handle embedded structs
type Flannel0_7 struct {
EtcdUsername string `yaml:"etcd_username" cli:"etcd-username"`
EtcdPassword string `yaml:"etcd_password" cli:"etcd-password"`
EtcdEndpoints string `yaml:"etcd_endpoints" cli:"etcd-endpoints"`
EtcdCAFile string `yaml:"etcd_cafile" cli:"etcd-cafile"`
EtcdCertFile string `yaml:"etcd_certfile" cli:"etcd-certfile"`
EtcdKeyFile string `yaml:"etcd_keyfile" cli:"etcd-keyfile"`
EtcdPrefix string `yaml:"etcd_prefix" cli:"etcd-prefix"`
IPMasq string `yaml:"ip_masq" cli:"ip-masq"`
SubnetFile string `yaml:"subnet_file" cli:"subnet-file"`
Iface string `yaml:"interface" cli:"iface"`
PublicIP string `yaml:"public_ip" cli:"public-ip"`
KubeSubnetMgr bool `yaml:"kube_subnet_mgr" cli:"kube-subnet-mgr"`
}
type Flannel0_6 struct {
EtcdUsername string `yaml:"etcd_username" cli:"etcd-username"`
EtcdPassword string `yaml:"etcd_password" cli:"etcd-password"`
EtcdEndpoints string `yaml:"etcd_endpoints" cli:"etcd-endpoints"`
EtcdCAFile string `yaml:"etcd_cafile" cli:"etcd-cafile"`
EtcdCertFile string `yaml:"etcd_certfile" cli:"etcd-certfile"`
EtcdKeyFile string `yaml:"etcd_keyfile" cli:"etcd-keyfile"`
EtcdPrefix string `yaml:"etcd_prefix" cli:"etcd-prefix"`
IPMasq string `yaml:"ip_masq" cli:"ip-masq"`
SubnetFile string `yaml:"subnet_file" cli:"subnet-file"`
Iface string `yaml:"interface" cli:"iface"`
PublicIP string `yaml:"public_ip" cli:"public-ip"`
}
type Flannel0_5 struct {
EtcdEndpoints string `yaml:"etcd_endpoints" cli:"etcd-endpoints"`
EtcdCAFile string `yaml:"etcd_cafile" cli:"etcd-cafile"`
EtcdCertFile string `yaml:"etcd_certfile" cli:"etcd-certfile"`
EtcdKeyFile string `yaml:"etcd_keyfile" cli:"etcd-keyfile"`
EtcdPrefix string `yaml:"etcd_prefix" cli:"etcd-prefix"`
IPMasq string `yaml:"ip_masq" cli:"ip-masq"`
SubnetFile string `yaml:"subnet_file" cli:"subnet-file"`
Iface string `yaml:"interface" cli:"iface"`
PublicIP string `yaml:"public_ip" cli:"public-ip"`
}