forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmd.go
294 lines (250 loc) · 8.11 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
291
292
293
294
package openshift_sdn
import (
"errors"
"fmt"
"io"
"net/url"
"os"
"path/filepath"
"time"
"github.com/fsnotify/fsnotify"
"github.com/spf13/cobra"
"k8s.io/klog"
kerrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/client-go/tools/clientcmd"
kubeproxyconfig "k8s.io/kubernetes/pkg/proxy/apis/config"
"k8s.io/kubernetes/pkg/util/interrupt"
"github.com/openshift/library-go/pkg/serviceability"
configapi "github.com/openshift/origin/pkg/cmd/server/apis/config"
_ "github.com/openshift/origin/pkg/cmd/server/apis/config/install"
configapilatest "github.com/openshift/origin/pkg/cmd/server/apis/config/latest"
networkvalidation "github.com/openshift/origin/pkg/cmd/server/apis/config/validation/network"
sdnnode "github.com/openshift/origin/pkg/network/node"
sdnproxy "github.com/openshift/origin/pkg/network/proxy"
"github.com/openshift/origin/pkg/version"
)
// OpenShiftSDN stores the variables needed to initialize the real networking
// processess from the command line.
type OpenShiftSDN struct {
ConfigFilePath string
KubeConfigFilePath string
URLOnlyKubeConfigFilePath string
NodeConfig *configapi.NodeConfig
ProxyConfig *kubeproxyconfig.KubeProxyConfiguration
informers *informers
OsdnNode *sdnnode.OsdnNode
OsdnProxy *sdnproxy.OsdnProxy
}
var networkLong = `
Start OpenShift SDN node components. This includes the service proxy.
This will also read the node name from the environment variable K8S_NODE_NAME.`
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: Gracefully shutting down ...\n")
close(ch)
}).Run(func() error {
sdn.Run(c, errout, ch)
return nil
})
},
}
flags := cmd.Flags()
flags.StringVar(&sdn.ConfigFilePath, "config", "", "Location of the node configuration file to run from (required)")
flags.StringVar(&sdn.KubeConfigFilePath, "kubeconfig", "", "Path to the kubeconfig file to use for requests to the Kubernetes API. Optional. When omitted, will use the in-cluster config")
flags.StringVar(&sdn.URLOnlyKubeConfigFilePath, "url-only-kubeconfig", "", "Path to a kubeconfig file to use, but only to determine the URL to the apiserver. The in-cluster credentials will be used. Cannot use with --kubeconfig.")
return cmd
}
// Run starts the network process. Does not return.
func (sdn *OpenShiftSDN) Run(c *cobra.Command, errout io.Writer, stopCh chan struct{}) {
err := injectKubeAPIEnv(sdn.URLOnlyKubeConfigFilePath)
if err != nil {
klog.Fatal(err)
}
// Parse config file, build config objects
err = sdn.ValidateAndParse()
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.ConfigFilePath, 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
os.Exit(1)
}
// ValidateAndParse validates the command line options, parses the node
// configuration, and builds the upstream proxy configuration.
func (sdn *OpenShiftSDN) ValidateAndParse() error {
if len(sdn.ConfigFilePath) == 0 {
return errors.New("--config is required")
}
if len(sdn.KubeConfigFilePath) > 0 && len(sdn.URLOnlyKubeConfigFilePath) > 0 {
return errors.New("cannot pass --kubeconfig and --url-only-kubeconfig")
}
klog.V(2).Infof("Reading node configuration from %s", sdn.ConfigFilePath)
var err error
sdn.NodeConfig, err = configapilatest.ReadAndResolveNodeConfig(sdn.ConfigFilePath)
if err != nil {
return err
}
if len(sdn.KubeConfigFilePath) > 0 {
sdn.NodeConfig.MasterKubeConfig = sdn.KubeConfigFilePath
}
// Get the nodename from the environment, if available
if len(sdn.NodeConfig.NodeName) == 0 {
sdn.NodeConfig.NodeName = os.Getenv("K8S_NODE_NAME")
}
// Validate the node config
validationResults := networkvalidation.ValidateInClusterNetworkNodeConfig(sdn.NodeConfig, nil)
if len(validationResults.Warnings) != 0 {
for _, warning := range validationResults.Warnings {
klog.Warningf("Warning: %v, node start will continue.", warning)
}
}
if len(validationResults.Errors) != 0 {
klog.V(4).Infof("Configuration is invalid: %#v", sdn.NodeConfig)
return kerrors.NewInvalid(configapi.Kind("NodeConfig"), sdn.ConfigFilePath, validationResults.Errors)
}
sdn.ProxyConfig, err = ProxyConfigFromNodeConfig(*sdn.NodeConfig)
if err != nil {
klog.V(4).Infof("Unable to build proxy config: %v", err)
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)
}
// 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()
err := sdn.runSDN()
if err != nil {
return err
}
sdn.runProxy()
sdn.informers.start(stopCh)
return nil
}
// injectKubeAPIEnv consumes the url-only-kubeconfig and re-injects it as
// environment variables. We need to do this because we cannot use the
// apiserver service ip (since we set it up!), but we want to use the in-cluster
// configuration. So, take the server URL from the kubelet kubeconfig.
func injectKubeAPIEnv(kcPath string) error {
if kcPath != "" {
kubeconfig, err := clientcmd.LoadFromFile(kcPath)
if err != nil {
return err
}
clusterName := kubeconfig.Contexts[kubeconfig.CurrentContext].Cluster
apiURL := kubeconfig.Clusters[clusterName].Server
url, err := url.Parse(apiURL)
if err != nil {
return err
}
// The kubernetes in-cluster functions don't let you override the apiserver
// directly; gotta "pass" it via environment vars.
klog.V(2).Infof("Overriding kubernetes api to %s", apiURL)
os.Setenv("KUBERNETES_SERVICE_HOST", url.Hostname())
os.Setenv("KUBERNETES_SERVICE_PORT", url.Port())
}
return nil
}
// 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
}