forked from longhorn/longhorn-manager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdaemon.go
327 lines (285 loc) · 10.3 KB
/
daemon.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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
package app
import (
"context"
"fmt"
"net/http"
_ "net/http/pprof" // for runtime profiling
"os"
"github.com/gorilla/handlers"
"github.com/pkg/errors"
"github.com/rancher/wrangler/v3/pkg/signals"
"github.com/sirupsen/logrus"
"github.com/urfave/cli"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"github.com/longhorn/go-iscsi-helper/iscsi"
iscsiutil "github.com/longhorn/go-iscsi-helper/util"
lhns "github.com/longhorn/go-common-libs/ns"
lhtypes "github.com/longhorn/go-common-libs/types"
"github.com/longhorn/longhorn-manager/api"
"github.com/longhorn/longhorn-manager/controller"
"github.com/longhorn/longhorn-manager/datastore"
"github.com/longhorn/longhorn-manager/manager"
"github.com/longhorn/longhorn-manager/meta"
"github.com/longhorn/longhorn-manager/types"
"github.com/longhorn/longhorn-manager/upgrade"
"github.com/longhorn/longhorn-manager/util"
"github.com/longhorn/longhorn-manager/util/client"
"github.com/longhorn/longhorn-manager/webhook"
metricscollector "github.com/longhorn/longhorn-manager/metrics_collector"
recoverybackend "github.com/longhorn/longhorn-manager/recovery_backend"
)
const (
FlagEngineImage = "engine-image"
FlagInstanceManagerImage = "instance-manager-image"
FlagShareManagerImage = "share-manager-image"
FlagBackingImageManagerImage = "backing-image-manager-image"
FlagManagerImage = "manager-image"
FlagSupportBundleManagerImage = "support-bundle-manager-image"
FlagServiceAccount = "service-account"
FlagKubeConfig = "kube-config"
FlagUpgradeVersionCheck = "upgrade-version-check"
)
func DaemonCmd() cli.Command {
return cli.Command{
Name: "daemon",
Flags: []cli.Flag{
cli.StringFlag{
Name: FlagEngineImage,
Usage: "Specify Longhorn engine image",
},
cli.StringFlag{
Name: FlagInstanceManagerImage,
Usage: "Specify Longhorn instance manager image",
},
cli.StringFlag{
Name: FlagShareManagerImage,
Usage: "Specify Longhorn share manager image",
},
cli.StringFlag{
Name: FlagBackingImageManagerImage,
Usage: "Specify Longhorn backing image manager image",
},
cli.StringFlag{
Name: FlagSupportBundleManagerImage,
Usage: "Specify Longhorn support bundle manager image",
},
cli.StringFlag{
Name: FlagManagerImage,
Usage: "Specify Longhorn manager image",
},
cli.StringFlag{
Name: FlagServiceAccount,
Usage: "Specify service account for manager",
},
cli.StringFlag{
Name: FlagKubeConfig,
Usage: "Specify path to kube config (optional)",
},
cli.BoolFlag{
Name: FlagUpgradeVersionCheck,
Usage: "Enforce version checking for upgrades. If disabled, there will be no requirement for the necessary upgrade source version",
},
},
Action: func(c *cli.Context) {
if err := startManager(c); err != nil {
logrus.Fatalf("Error starting manager: %v", err)
}
},
}
}
func startManager(c *cli.Context) error {
var (
err error
)
engineImage := c.String(FlagEngineImage)
if engineImage == "" {
return fmt.Errorf("require %v", FlagEngineImage)
}
instanceManagerImage := c.String(FlagInstanceManagerImage)
if instanceManagerImage == "" {
return fmt.Errorf("require %v", FlagInstanceManagerImage)
}
shareManagerImage := c.String(FlagShareManagerImage)
if shareManagerImage == "" {
return fmt.Errorf("require %v", FlagShareManagerImage)
}
backingImageManagerImage := c.String(FlagBackingImageManagerImage)
if backingImageManagerImage == "" {
return fmt.Errorf("require %v", FlagBackingImageManagerImage)
}
supportBundleManagerImage := c.String(FlagSupportBundleManagerImage)
if supportBundleManagerImage == "" {
return fmt.Errorf("require %v", FlagSupportBundleManagerImage)
}
managerImage := c.String(FlagManagerImage)
if managerImage == "" {
return fmt.Errorf("require %v", FlagManagerImage)
}
serviceAccount := c.String(FlagServiceAccount)
if serviceAccount == "" {
return fmt.Errorf("require %v", FlagServiceAccount)
}
kubeconfigPath := c.String(FlagKubeConfig)
if err := environmentCheck(); err != nil {
return errors.Wrap(err, "failed to check environment, please make sure you have iscsiadm/open-iscsi installed on the host")
}
currentNodeID, err := util.GetRequiredEnv(types.EnvNodeName)
if err != nil {
return fmt.Errorf("failed to detect the node name")
}
currentIP, err := util.GetRequiredEnv(types.EnvPodIP)
if err != nil {
return fmt.Errorf("failed to detect the node IP")
}
podName, err := util.GetRequiredEnv(types.EnvPodName)
if err != nil {
return fmt.Errorf("failed to detect the manager pod name")
}
podNamespace, err := util.GetRequiredEnv(types.EnvPodNamespace)
if err != nil {
return fmt.Errorf("failed to detect the manager pod namespace")
}
ctx := signals.SetupSignalContext()
logger := logrus.StandardLogger().WithField("node", currentNodeID)
// Conversion webhook needs to be started first since we use its port 9501 as readiness port.
// longhorn-manager pod becomes ready only when conversion webhook is running.
// The services in the longhorn-manager can then start to receive the requests.
// Conversion webhook does not use datastore, since it is a prerequisite for
// datastore operation.
clientsWithoutDatastore, err := client.NewClients(kubeconfigPath, false, ctx.Done())
if err != nil {
return err
}
if err := webhook.StartWebhook(ctx, types.WebhookTypeConversion, clientsWithoutDatastore); err != nil {
return err
}
// This adds the label for the conversion webhook's selector. We do it the hard way without datastore to avoid chicken-and-egg.
pod, err := clientsWithoutDatastore.Clients.K8s.CoreV1().Pods(podNamespace).Get(context.Background(), podName, v1.GetOptions{})
if err != nil {
return err
}
labels := types.GetConversionWebhookLabel()
for key, value := range labels {
pod.Labels[key] = value
}
_, err = clientsWithoutDatastore.Clients.K8s.CoreV1().Pods(podNamespace).Update(context.Background(), pod, v1.UpdateOptions{})
if err != nil {
return err
}
if err := webhook.CheckWebhookServiceAvailability(types.WebhookTypeConversion); err != nil {
return err
}
clients, err := client.NewClients(kubeconfigPath, true, ctx.Done())
if err != nil {
return err
}
if err := webhook.StartWebhook(ctx, types.WebhookTypeAdmission, clients); err != nil {
return err
}
if err := clients.Datastore.AddLabelToManagerPod(currentNodeID, types.GetAdmissionWebhookLabel()); err != nil {
return err
}
if err := webhook.CheckWebhookServiceAvailability(types.WebhookTypeAdmission); err != nil {
return err
}
if err := recoverybackend.StartRecoveryBackend(clients); err != nil {
return err
}
if err := clients.Datastore.AddLabelToManagerPod(currentNodeID, types.GetRecoveryBackendLabel()); err != nil {
return err
}
if err := upgrade.Upgrade(kubeconfigPath, currentNodeID, managerImage, c.Bool(FlagUpgradeVersionCheck)); err != nil {
return err
}
proxyConnCounter := util.NewAtomicCounter()
wsc, err := controller.StartControllers(logger, clients,
currentNodeID, serviceAccount, managerImage, backingImageManagerImage, shareManagerImage,
kubeconfigPath, meta.Version, proxyConnCounter)
if err != nil {
return err
}
m := manager.NewVolumeManager(currentNodeID, clients.Datastore, proxyConnCounter)
metricscollector.InitMetricsCollectorSystem(logger, currentNodeID, clients.Datastore, kubeconfigPath, proxyConnCounter)
defaultImageSettings := map[types.SettingName]string{
types.SettingNameDefaultEngineImage: engineImage,
types.SettingNameDefaultInstanceManagerImage: instanceManagerImage,
types.SettingNameDefaultBackingImageManagerImage: backingImageManagerImage,
types.SettingNameSupportBundleManagerImage: supportBundleManagerImage,
}
if err := clients.Datastore.UpdateCustomizedSettings(defaultImageSettings); err != nil {
return err
}
if err := updateRegistrySecretName(m); err != nil {
return err
}
if err := initDaemonNode(clients.Datastore, currentNodeID); err != nil {
return err
}
if err := m.DeployEngineImage(engineImage); err != nil {
return err
}
server := api.NewServer(m, wsc)
router := http.Handler(api.NewRouter(server))
router = util.FilteredLoggingHandler(os.Stdout, router)
router = handlers.ProxyHeaders(router)
listen := types.GetAPIServerAddressFromIP(currentIP)
logger.Infof("Listening on %s", listen)
go func() {
if err := http.ListenAndServe(listen, router); err != nil {
logrus.Fatalf("Error longhorn backend server failed: %v", err)
}
}()
go func() {
debugAddress := "127.0.0.1:6060"
debugHandler := http.DefaultServeMux
logger.Infof("Debug Server listening on %s", debugAddress)
if err := http.ListenAndServe(debugAddress, debugHandler); err != nil && !errors.Is(err, http.ErrServerClosed) {
logger.WithError(err).Error("Failed to start debug server")
}
}()
<-ctx.Done()
return nil
}
func environmentCheck() error {
// Here we only check if the necessary tool the iscsiadm is installed when Longhorn starts up.
// Others tools and settings like kernel versions, multipathd, nfs client, etc. are checked in the node controller (every 30 sec).
namespaces := []lhtypes.Namespace{lhtypes.NamespaceMnt, lhtypes.NamespaceNet}
nsexec, err := lhns.NewNamespaceExecutor(iscsiutil.ISCSIdProcess, lhtypes.HostProcDirectory, namespaces)
if err != nil {
return err
}
return iscsi.CheckForInitiatorExistence(nsexec)
}
func updateRegistrySecretName(m *manager.VolumeManager) error {
settingRegistrySecret, err := m.GetSetting(types.SettingNameRegistrySecret)
if err != nil {
return err
}
managerDaemonSet, err := m.GetDaemonSetRO(types.LonghornManagerDaemonSetName)
if err != nil {
return err
}
registrySecretName := ""
if len(managerDaemonSet.Spec.Template.Spec.ImagePullSecrets) > 0 {
registrySecretName = managerDaemonSet.Spec.Template.Spec.ImagePullSecrets[0].Name
}
if settingRegistrySecret.Value != registrySecretName {
settingRegistrySecret.Value = registrySecretName
if _, err := m.CreateOrUpdateSetting(settingRegistrySecret); err != nil {
return err
}
}
return nil
}
func initDaemonNode(ds *datastore.DataStore, nodeName string) error {
if _, err := ds.GetNode(nodeName); err != nil {
// init default disk on node when starting longhorn-manager
if datastore.ErrorIsNotFound(err) {
if _, err = ds.CreateDefaultNode(nodeName); err != nil {
return err
}
}
return err
}
return nil
}