forked from openshift/microshift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.go
294 lines (250 loc) · 8.99 KB
/
run.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 cmd
import (
"context"
"fmt"
"os"
"os/signal"
"path/filepath"
"strconv"
"strings"
"syscall"
"time"
"github.com/coreos/go-systemd/daemon"
"github.com/openshift/microshift/pkg/admin/data"
"github.com/openshift/microshift/pkg/admin/prerun"
"github.com/openshift/microshift/pkg/config"
"github.com/openshift/microshift/pkg/controllers"
"github.com/openshift/microshift/pkg/kustomize"
"github.com/openshift/microshift/pkg/loadbalancerservice"
"github.com/openshift/microshift/pkg/mdns"
"github.com/openshift/microshift/pkg/node"
"github.com/openshift/microshift/pkg/release"
"github.com/openshift/microshift/pkg/servicemanager"
"github.com/openshift/microshift/pkg/sysconfwatch"
"github.com/openshift/microshift/pkg/util"
"github.com/openshift/microshift/pkg/util/cryptomaterial/certchains"
"github.com/openshift/microshift/pkg/version"
"github.com/spf13/cobra"
logsAPIV1 "k8s.io/component-base/logs/api/v1"
"k8s.io/klog/v2"
"sigs.k8s.io/yaml"
)
const (
gracefulShutdownTimeout = 15
)
var (
preRunFailedLogPath = util.LogFilePath(filepath.Join(config.BackupsDir, "prerun_failed.log"))
cleanUpFileLogPaths = []util.LogFilePath{
preRunFailedLogPath,
}
)
func NewRunMicroshiftCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "run",
Short: "Run MicroShift",
}
var multinode bool
flags := cmd.Flags()
flags.BoolVar(&multinode, "multinode", false, "enable multinode mode")
err := flags.MarkHidden("multinode")
if err != nil {
panic(err)
}
cmd.RunE = func(cmd *cobra.Command, args []string) error {
versionInfo := version.Get()
klog.InfoS("Version", "microshift", versionInfo.String(), "base", release.Base)
cfg, err := config.ActiveConfig()
if err != nil {
return err
}
// `v` is a flag registered in klog's init()
if vFlag := flags.Lookup("v"); vFlag != nil {
verbosity := strconv.Itoa(cfg.GetVerbosity())
if err := vFlag.Value.Set(verbosity); err != nil {
klog.Errorf("Failed to set log verbosity: %v", err)
}
}
cfg = config.ConfigMultiNode(cfg, multinode)
for _, w := range cfg.Warnings {
klog.Warningf("Configuration warning: %s", w)
}
// Things to very badly if the node's name has changed
// since the last time the server started.
err = cfg.EnsureNodeNameHasNotChanged()
if err != nil {
return err
}
return RunMicroshift(cfg)
}
return cmd
}
func cleanUpPreviousLogFiles() {
for _, p := range cleanUpFileLogPaths {
if errLog := p.Remove(); errLog != nil {
klog.ErrorS(errLog, "Failed to remove log file", "path", p)
}
}
}
func writeLogFileError(l util.LogFilePath, err error) {
if errLog := l.Write([]byte(err.Error())); errLog != nil {
klog.ErrorS(errLog, "Failed to write log file", "path", l)
}
}
func logConfig(cfg *config.Config) {
marshalled, err := yaml.Marshal(cfg)
if err != nil {
klog.Fatal(err)
}
klog.Info("Effective configuration:")
for _, line := range strings.Split(string(marshalled), "\n") {
klog.Info(line)
}
}
func prerunDataManagement() error {
dataManager, err := data.NewManager(config.BackupsDir)
if err != nil {
return fmt.Errorf("failed to create data manager: %w", err)
}
return prerun.DataManagement(dataManager)
}
func RunMicroshift(cfg *config.Config) error {
// fail early if we don't have enough privileges
if os.Geteuid() > 0 {
klog.Fatalf("MicroShift must be run privileged")
}
klog.InfoS("MICROSHIFT STARTING")
microshiftStart := time.Now()
// Tell the logging code that it's OK to receive reconfiguration
// instructions unless those instructions are different. This
// overrides the default behavior of erroring out if any
// instructions are received a second time. That default behavior
// causes issues with embedded components that all use the same
// logging code and are not expecting to be compiled together into
// the same process, as they are in MicroShift. See comments in
// k8s.io/component-base/logs/api/v1/options.go for details.
logsAPIV1.ReapplyHandling = logsAPIV1.ReapplyHandlingIgnoreUnchanged
cleanUpPreviousLogFiles()
if err := prerunDataManagement(); err != nil {
writeLogFileError(preRunFailedLogPath, err)
return err
}
logConfig(cfg)
// TO-DO: When multi-node is ready, we need to add the controller host-name/mDNS hostname
// or VIP to this list on start
// see https://github.com/openshift/microshift/pull/471
if err := util.AddToNoProxyEnv(
cfg.Node.NodeIP,
cfg.Node.HostnameOverride,
cfg.Network.ClusterNetwork[0],
cfg.Network.ServiceNetwork[0],
".svc",
".cluster.local",
"."+cfg.DNS.BaseDomain); err != nil {
klog.Fatal(err)
}
if err := util.MakeDir(config.DataDir); err != nil {
return fmt.Errorf("failed to create dir %q: %w", config.DataDir, err)
}
if err := prerun.VersionMetadataManagement(); err != nil {
writeLogFileError(preRunFailedLogPath, err)
return err
}
// TODO: change to only initialize what is strictly necessary for the selected role(s)
certChains, err := initCerts(cfg)
if err != nil {
klog.Fatalf("failed to retrieve the necessary certificates: %v", err)
}
// create kubeconfig for kube-scheduler, kubelet,controller-manager
if err := initKubeconfigs(cfg, certChains); err != nil {
klog.Fatalf("failed to create the necessary kubeconfigs for internal components: %v", err)
}
// Establish the context we will use to control execution
runCtx, runCancel := context.WithCancel(context.Background())
m := servicemanager.NewServiceManager()
util.Must(m.AddService(node.NewNetworkConfiguration(cfg)))
util.Must(m.AddService(controllers.NewEtcd(cfg)))
util.Must(m.AddService(sysconfwatch.NewSysConfWatchController(cfg)))
util.Must(m.AddService(controllers.NewKubeAPIServer(cfg)))
util.Must(m.AddService(controllers.NewKubeScheduler(cfg)))
util.Must(m.AddService(controllers.NewKubeControllerManager(runCtx, cfg)))
util.Must(m.AddService(controllers.NewOpenShiftCRDManager(cfg)))
util.Must(m.AddService(controllers.NewRouteControllerManager(cfg)))
util.Must(m.AddService(controllers.NewOpenShiftDefaultSCCManager(cfg)))
util.Must(m.AddService(mdns.NewMicroShiftmDNSController(cfg)))
util.Must(m.AddService(controllers.NewInfrastructureServices(cfg)))
util.Must(m.AddService(controllers.NewClusterPolicyController(cfg)))
util.Must(m.AddService(controllers.NewVersionManager(cfg)))
util.Must(m.AddService(kustomize.NewKustomizer(cfg)))
util.Must(m.AddService(node.NewKubeletServer(cfg)))
util.Must(m.AddService(loadbalancerservice.NewLoadbalancerServiceController(cfg)))
util.Must(m.AddService(controllers.NewKubeStorageVersionMigrator(cfg)))
util.Must(m.AddService(controllers.NewClusterID(cfg)))
// Storing and clearing the env, so other components don't send the READY=1 until MicroShift is fully ready
notifySocket := os.Getenv("NOTIFY_SOCKET")
os.Unsetenv("NOTIFY_SOCKET")
klog.InfoS("MICROSHIFT STARTING SERVICES", "since-start", time.Since(microshiftStart))
_, rotationDate, err := certchains.WhenToRotateAtEarliest(certChains)
if err != nil {
klog.Fatalf("failed to determine when to rotate certificates: %v", err)
}
// Establish a deadline for restarting to rotate the certificates.
certCtx, certCancel := context.WithDeadline(context.Background(), rotationDate)
// Watch for the certificate deadline context to be done, log a
// message, and cancel the run context to propagate the shutdown.
go func() {
select {
case <-certCtx.Done():
klog.Info("Stopping services for certificate rotation")
runCancel()
return
case <-runCtx.Done():
klog.Info("Certificate watcher exiting")
certCancel()
return
}
}()
// Start everything up
ready, stopped := make(chan struct{}), make(chan struct{})
go func() {
klog.Infof("Started %s", m.Name())
if err := m.Run(runCtx, ready, stopped); err != nil {
klog.Errorf("Stopped %s: %v", m.Name(), err)
} else {
klog.Infof("%s completed", m.Name())
}
}()
// Connect signal handler
sigTerm := make(chan os.Signal, 1)
signal.Notify(sigTerm, os.Interrupt, syscall.SIGTERM)
select {
case <-ready:
klog.InfoS("MICROSHIFT READY", "since-start", time.Since(microshiftStart))
os.Setenv("NOTIFY_SOCKET", notifySocket)
if supported, err := daemon.SdNotify(false, daemon.SdNotifyReady); err != nil {
klog.Warningf("error sending sd_notify readiness message: %v", err)
} else if supported {
klog.Info("sent sd_notify readiness message")
} else {
klog.Info("service does not support sd_notify readiness messages")
}
// Watch for SIGTERM to exit, now that we are ready.
<-sigTerm
klog.Info("Interrupt received")
case <-sigTerm:
// A signal that comes in before we are ready is handled here.
klog.Info("Interrupt received")
case <-runCtx.Done():
// We might end up here if the certificate rotation is
// triggered and we exit on our own, instead of via a signal.
}
klog.Info("MICROSHIFT STOPPING")
microshiftStop := time.Now()
runCancel()
select {
case <-stopped:
case <-time.After(time.Duration(gracefulShutdownTimeout) * time.Second):
klog.InfoS("MICROSHIFT STOP TIMED OUT", "since-stop", time.Since(microshiftStop))
}
klog.InfoS("MICROSHIFT STOPPED", "since-stop", time.Since(microshiftStop))
return nil
}