-
Notifications
You must be signed in to change notification settings - Fork 90
/
cmd.go
290 lines (248 loc) · 7.56 KB
/
cmd.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
package openshift_sdn_node
import (
"fmt"
"github.com/spf13/pflag"
"io"
"os"
"path/filepath"
"time"
"github.com/fsnotify/fsnotify"
"github.com/spf13/cobra"
"k8s.io/klog/v2"
kerrors "k8s.io/apimachinery/pkg/api/errors"
utilwait "k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/tools/record"
kubeproxyconfig "k8s.io/kubernetes/pkg/proxy/apis/config"
"k8s.io/kubernetes/pkg/util/interrupt"
"k8s.io/kubernetes/pkg/util/iptables"
kexec "k8s.io/utils/exec"
"sigs.k8s.io/yaml"
"github.com/openshift/library-go/pkg/serviceability"
sdnnode "github.com/openshift/sdn/pkg/network/node"
sdnproxy "github.com/openshift/sdn/pkg/network/proxy"
"github.com/openshift/sdn/pkg/version"
)
// openShiftSDN stores the variables needed to initialize the real networking
// processess from the command line.
type openShiftSDN struct {
nodeName string
nodeIP string
platformType string
proxyConfigFilePath string
proxyConfig *kubeproxyconfig.KubeProxyConfiguration
mtuOverrideFilePath string
overrideMTU uint32
routableMTU uint32
informers *sdnInformers
osdnNode *sdnnode.OsdnNode
sdnRecorder record.EventRecorder
osdnProxy *sdnproxy.OsdnProxy
ipt iptables.Interface
}
var networkLong = `
Start OpenShift SDN node components. This includes the service proxy.
`
func NewOpenShiftSDNCommand(basename string, errout io.Writer) *cobra.Command {
sdn := &openShiftSDN{}
cmd := &cobra.Command{
Use: basename,
Short: "Start OpenShiftSDN",
Long: networkLong,
Run: func(c *cobra.Command, _ []string) {
ch := make(chan struct{})
interrupt.New(func(s os.Signal) {
fmt.Fprintf(errout, "interrupt: Signal %s received. Gracefully shutting down ...\n", s.String())
close(ch)
}).Run(func() error {
sdn.run(c, errout, ch)
return nil
})
},
}
flags := cmd.Flags()
flags.StringVar(&sdn.nodeName, "node-name", "", "Kubernetes node name")
cmd.MarkFlagRequired("node-name")
flags.StringVar(&sdn.nodeIP, "node-ip", "", "Kubernetes node IP")
cmd.MarkFlagRequired("node-ip")
flags.StringVar(&sdn.proxyConfigFilePath, "proxy-config", "", "Location of the kube-proxy configuration file")
cmd.MarkFlagRequired("proxy-config")
flags.StringVar(&sdn.platformType, "platform-type", "", "The cloud provider platform type openshift-sdn is deployed on")
flags.StringVar(&sdn.mtuOverrideFilePath, "mtu-override", "", "Location of an MTU-override configuration file")
return cmd
}
// run starts the network process. Does not return.
func (sdn *openShiftSDN) run(c *cobra.Command, errout io.Writer, stopCh chan struct{}) {
// Parse config file, build config objects
err := sdn.validateAndParse(c.Flags())
if err != nil {
if kerrors.IsInvalid(err) {
if details := err.(*kerrors.StatusError).ErrStatus.Details; details != nil {
fmt.Fprintf(errout, "Invalid %s %s\n", details.Kind, details.Name)
for _, cause := range details.Causes {
fmt.Fprintf(errout, " %s: %s\n", cause.Field, cause.Message)
}
os.Exit(255)
}
}
klog.Fatal(err)
}
// Set up a watch on our config file; if it changes, we should exit -
// (we don't have the ability to dynamically reload config changes).
if err := watchForChanges(sdn.proxyConfigFilePath, stopCh); err != nil {
klog.Fatalf("unable to setup configuration watch: %v", err)
}
// Build underlying network objects
err = sdn.init()
if err != nil {
klog.Fatalf("Failed to initialize sdn: %v", err)
}
err = sdn.start(stopCh)
if err != nil {
klog.Fatalf("Failed to start sdn: %v", err)
}
<-stopCh
time.Sleep(500 * time.Millisecond) // gracefully shut down
}
// validateAndParse validates the command line options, parses the node
// configuration, and builds the upstream proxy configuration.
func (sdn *openShiftSDN) validateAndParse(fs *pflag.FlagSet) error {
klog.V(2).Infof("Reading proxy configuration from %s", sdn.proxyConfigFilePath)
var err error
sdn.proxyConfig, err = readProxyConfig(sdn.proxyConfigFilePath, fs)
if err != nil {
return err
}
if sdn.mtuOverrideFilePath != "" {
klog.V(2).Infof("Reading MTU override from %s", sdn.mtuOverrideFilePath)
sdn.overrideMTU, sdn.routableMTU, err = readMTUOverride(sdn.mtuOverrideFilePath)
if err != nil {
return err
}
}
return nil
}
// init builds the underlying structs for the network processes.
func (sdn *openShiftSDN) init() error {
// Build the informers
var err error
err = sdn.buildInformers()
if err != nil {
return fmt.Errorf("failed to build informers: %v", err)
}
sdn.ipt = iptables.New(kexec.New(), iptables.ProtocolIPv4)
// Configure SDN
err = sdn.initSDN()
if err != nil {
return fmt.Errorf("failed to initialize SDN: %v", err)
}
// Configure the proxy
err = sdn.initProxy()
if err != nil {
return fmt.Errorf("failed to initialize proxy: %v", err)
}
return nil
}
// Start starts the network, proxy, and informers, then returns.
func (sdn *openShiftSDN) start(stopCh <-chan struct{}) error {
klog.Infof("Starting node networking (%s)", version.Get().String())
serviceability.StartProfiler()
if err := sdn.runSDN(); err != nil {
return err
}
proxyInitChan := make(chan bool)
sdn.runProxy(proxyInitChan)
sdn.informers.start(stopCh)
klog.V(2).Infof("openshift-sdn network plugin waiting for proxy startup to complete")
<-proxyInitChan
klog.V(2).Infof("openshift-sdn network plugin registering startup")
if err := sdn.writeConfigFile(); err != nil {
klog.Fatal(err)
}
klog.V(2).Infof("openshift-sdn network plugin ready")
go sdn.ipt.Monitor(iptables.Chain("OPENSHIFT-SDN-CANARY"),
[]iptables.Table{iptables.TableMangle, iptables.TableNAT, iptables.TableFilter},
sdn.reloadIPTables,
sdn.proxyConfig.IPTables.SyncPeriod.Duration,
utilwait.NeverStop)
return nil
}
// reloadIPTables reloads node and proxy iptables rules after a flush
func (sdn *openShiftSDN) reloadIPTables() {
if err := sdn.osdnNode.ReloadIPTables(); err != nil {
klog.Errorf("Reloading openshift node iptables rules failed: %v", err)
}
if sdn.osdnProxy != nil {
if err := sdn.osdnProxy.ReloadIPTables(); err != nil {
klog.Errorf("Reloading openshift proxy iptables rules failed: %v", err)
}
}
}
// watchForChanges closes stopCh if the configuration file changed.
func watchForChanges(configPath string, stopCh chan struct{}) error {
configPath, err := filepath.Abs(configPath)
if err != nil {
return err
}
watcher, err := fsnotify.NewWatcher()
if err != nil {
return err
}
// Watch all symlinks for changes
p := configPath
maxdepth := 100
for depth := 0; depth < maxdepth; depth++ {
if err := watcher.Add(p); err != nil {
return err
}
klog.V(2).Infof("Watching config file %s for changes", p)
stat, err := os.Lstat(p)
if err != nil {
return err
}
// configmaps are usually symlinks
if stat.Mode()&os.ModeSymlink > 0 {
p, err = filepath.EvalSymlinks(p)
if err != nil {
return err
}
} else {
break
}
}
go func() {
for {
select {
case <-stopCh:
case event, ok := <-watcher.Events:
if !ok {
return
}
klog.V(2).Infof("Configuration file %s changed, exiting...", event.Name)
close(stopCh)
return
case err, ok := <-watcher.Errors:
if !ok {
return
}
klog.V(4).Infof("fsnotify error %v", err)
}
}
}()
return nil
}
// readMTUOverride reads the --mtu-override file, if any
func readMTUOverride(file string) (uint32, uint32, error) {
bytes, err := os.ReadFile(file)
if err != nil {
return 0, 0, err
}
conf := struct {
OverlayMTU uint32 `json:"mtu"`
RoutableMTU uint32 `json:"routable-mtu"`
}{}
err = yaml.Unmarshal(bytes, &conf)
if err != nil {
return 0, 0, err
}
return conf.OverlayMTU, conf.RoutableMTU, err
}