-
Notifications
You must be signed in to change notification settings - Fork 36
/
vgmanager.go
337 lines (297 loc) · 11.7 KB
/
vgmanager.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
328
329
330
331
332
333
334
335
336
337
/*
Copyright © 2023 Red Hat, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package vgmanager
import (
"context"
"errors"
"fmt"
"net/http"
"os"
"os/signal"
"syscall"
"time"
"github.com/container-storage-interface/spec/lib/go/csi"
"github.com/fsnotify/fsnotify"
"github.com/go-logr/logr"
"github.com/openshift/lvm-operator/internal/cluster"
"github.com/openshift/lvm-operator/internal/controllers/constants"
"github.com/openshift/lvm-operator/internal/controllers/lvmcluster/resource"
"github.com/openshift/lvm-operator/internal/controllers/vgmanager"
"github.com/openshift/lvm-operator/internal/controllers/vgmanager/dmsetup"
"github.com/openshift/lvm-operator/internal/controllers/vgmanager/filter"
"github.com/openshift/lvm-operator/internal/controllers/vgmanager/lsblk"
"github.com/openshift/lvm-operator/internal/controllers/vgmanager/lvm"
"github.com/openshift/lvm-operator/internal/controllers/vgmanager/lvmd"
"github.com/openshift/lvm-operator/internal/controllers/vgmanager/wipefs"
internalCSI "github.com/openshift/lvm-operator/internal/csi"
"github.com/openshift/lvm-operator/internal/migration/tagging"
"github.com/spf13/cobra"
"github.com/topolvm/topolvm"
"github.com/topolvm/topolvm/pkg/controller"
"github.com/topolvm/topolvm/pkg/driver"
topoLVMD "github.com/topolvm/topolvm/pkg/lvmd"
"github.com/topolvm/topolvm/pkg/runners"
"google.golang.org/grpc"
"k8s.io/utils/ptr"
"sigs.k8s.io/controller-runtime/pkg/manager"
"k8s.io/apimachinery/pkg/runtime"
// Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.)
// to ensure that exec-entrypoint and run can make use of them.
_ "k8s.io/client-go/plugin/pkg/client/auth"
registerapi "k8s.io/kubelet/pkg/apis/pluginregistration/v1"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/cache"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/healthz"
"sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/metrics/filters"
metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"
"sigs.k8s.io/controller-runtime/pkg/webhook"
"sigs.k8s.io/yaml"
)
const (
DefaultDiagnosticsAddr = ":8443"
DefaultHealthProbeAddr = ":8081"
)
var ErrConfigModified = errors.New("lvmd config file is modified")
type Options struct {
Scheme *runtime.Scheme
SetupLog logr.Logger
diagnosticsAddr string
healthProbeAddr string
}
// NewCmd creates a new CLI command
func NewCmd(opts *Options) *cobra.Command {
cmd := &cobra.Command{
Use: "vgmanager",
Short: "Commands for running vgmanager",
Long: `Controller reconciling LVMVolumeGroup on individual nodes`,
SilenceErrors: false,
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error {
return run(cmd, args, opts)
},
}
cmd.Flags().StringVar(
&opts.diagnosticsAddr, "diagnosticsAddr", DefaultDiagnosticsAddr, "The address the diagnostics endpoint binds to.",
)
cmd.Flags().StringVar(
&opts.healthProbeAddr, "health-probe-bind-address", DefaultHealthProbeAddr, "The address the probe endpoint binds to.",
)
return cmd
}
func run(cmd *cobra.Command, _ []string, opts *Options) error {
ctx, cancelWithCause := context.WithCancelCause(cmd.Context())
defer cancelWithCause(nil)
ctx, cancel := signal.NotifyContext(ctx, os.Interrupt, syscall.SIGTERM)
defer cancel()
lvm := lvm.NewDefaultHostLVM()
nodeName := os.Getenv("NODE_NAME")
operatorNamespace, err := cluster.GetOperatorNamespace()
if err != nil {
return fmt.Errorf("unable to get operatorNamespace: %w", err)
}
setupClient, err := client.New(ctrl.GetConfigOrDie(), client.Options{Scheme: opts.Scheme})
if err != nil {
return fmt.Errorf("unable to initialize setup client for pre-manager startup checks: %w", err)
}
if err := tagging.AddTagToVGs(ctx, setupClient, lvm, nodeName, operatorNamespace); err != nil {
opts.SetupLog.Error(err, "failed to tag existing volume groups")
}
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
Scheme: opts.Scheme,
Metrics: metricsserver.Options{
BindAddress: opts.diagnosticsAddr,
SecureServing: true,
FilterProvider: filters.WithAuthenticationAndAuthorization,
},
WebhookServer: &webhook.DefaultServer{Options: webhook.Options{
Port: 9443,
}},
HealthProbeBindAddress: opts.healthProbeAddr,
LeaderElection: false,
Cache: cache.Options{
DefaultNamespaces: map[string]cache.Config{
operatorNamespace: {},
},
},
GracefulShutdownTimeout: ptr.To(time.Duration(-1)),
})
if err != nil {
return fmt.Errorf("unable to start manager: %w", err)
}
lvmdConfig := &lvmd.Config{}
if err := loadConfFile(ctx, lvmdConfig, lvmd.DefaultFileConfigPath); err != nil {
opts.SetupLog.Error(err, "lvmd config could not be loaded, starting without topolvm components and attempting bootstrap")
} else {
topoLVMD.Containerized(true)
lvclnt, vgclnt := topoLVMD.NewEmbeddedServiceClients(ctx, lvmdConfig.DeviceClasses, lvmdConfig.LvcreateOptionClasses)
if err := controller.SetupLogicalVolumeReconcilerWithServices(mgr, mgr.GetClient(), nodeName, vgclnt, lvclnt); err != nil {
return fmt.Errorf("unable to create LogicalVolumeReconciler: %w", err)
}
if err := mgr.Add(runners.NewMetricsExporter(vgclnt, mgr.GetClient(), nodeName)); err != nil { // adjusted signature
return fmt.Errorf("could not add topolvm metrics: %w", err)
}
if err := os.MkdirAll(topolvm.DeviceDirectory, 0755); err != nil {
return err
}
grpcServer := grpc.NewServer(grpc.UnaryInterceptor(ErrorLoggingInterceptor),
grpc.SharedWriteBuffer(true),
grpc.MaxConcurrentStreams(1),
grpc.NumStreamWorkers(1))
identityServer := driver.NewIdentityServer(func() (bool, error) {
return true, nil
})
csi.RegisterIdentityServer(grpcServer, identityServer)
registrationServer := internalCSI.NewRegistrationServer(
constants.TopolvmCSIDriverName, registrationPath(), []string{"1.0.0"})
registerapi.RegisterRegistrationServer(grpcServer, registrationServer)
if err = mgr.Add(runners.NewGRPCRunner(grpcServer, pluginRegistrationSocketPath(), false)); err != nil {
return fmt.Errorf("could not add grpc runner for registration server: %w", err)
}
nodeServer, err := driver.NewNodeServer(nodeName, vgclnt, lvclnt, mgr) // adjusted signature
if err != nil {
return fmt.Errorf("could not setup topolvm node server: %w", err)
}
csi.RegisterNodeServer(grpcServer, nodeServer)
err = mgr.Add(runners.NewGRPCRunner(grpcServer, constants.DefaultCSISocket, false))
if err != nil {
return fmt.Errorf("could not add grpc runner for node server: %w", err)
}
}
if err = (&vgmanager.Reconciler{
Client: mgr.GetClient(),
EventRecorder: mgr.GetEventRecorderFor(vgmanager.ControllerName),
LVMD: lvmd.DefaultConfigurator(),
Scheme: mgr.GetScheme(),
LSBLK: lsblk.NewDefaultHostLSBLK(),
Wipefs: wipefs.NewDefaultHostWipefs(),
Dmsetup: dmsetup.NewDefaultHostDmsetup(),
LVM: lvm,
NodeName: nodeName,
Namespace: operatorNamespace,
Filters: filter.DefaultFilters,
}).SetupWithManager(mgr); err != nil {
return fmt.Errorf("unable to create controller VGManager: %w", err)
}
if err := mgr.AddReadyzCheck("readyz", readyCheck(mgr)); err != nil {
return fmt.Errorf("unable to set up ready check: %w", err)
}
if err := mgr.AddHealthzCheck("healthz", healthz.Ping); err != nil {
return fmt.Errorf("unable to set up health check: %w", err)
}
// Create new watcher.
watcher, err := fsnotify.NewWatcher()
if err != nil {
return fmt.Errorf("unable to set up file watcher: %w", err)
}
defer watcher.Close()
// Start listening for events on TopoLVM files.
go watchTopoLVMAndNotify(opts, cancelWithCause, watcher)
opts.SetupLog.Info("starting manager")
if err := mgr.Start(ctx); err != nil {
return fmt.Errorf("problem running manager: %w", err)
}
if errors.Is(context.Cause(ctx), ErrConfigModified) {
opts.SetupLog.Info("restarting controller due to modified configuration")
return run(cmd, nil, opts)
} else if err := ctx.Err(); err != nil {
opts.SetupLog.Error(err, "exiting abnormally")
os.Exit(1)
}
return nil
}
// watchTopoLVMAndNotify watches for changes to the lvmd config file and cancels the context if it changes.
// This is used to restart the manager when the lvmd config file changes.
// This is a blocking function and should be run in a goroutine.
// If the directory does not exist, it will be created to make it possible to watch for changes.
// If the watch determines that the lvmd config file has been modified, it will cancel the context with the ErrConfigModified error.
func watchTopoLVMAndNotify(opts *Options, cancelWithCause context.CancelCauseFunc, watcher *fsnotify.Watcher) {
// check if file exists, otherwise the watcher errors
fi, err := os.Stat(lvmd.DefaultFileConfigDir)
if err != nil {
if os.IsNotExist(err) {
if err := os.MkdirAll(lvmd.DefaultFileConfigDir, 0755); err != nil {
opts.SetupLog.Error(err, "unable to create lvmd config directory when it did not exist before")
}
} else {
opts.SetupLog.Error(err, "unable to check if lvmd config directory exists")
cancelWithCause(err)
}
} else if !fi.IsDir() {
opts.SetupLog.Error(err, "expected lvmd config directory is not a directory")
cancelWithCause(err)
}
err = watcher.Add(lvmd.DefaultFileConfigDir)
if err != nil {
opts.SetupLog.Error(err, "unable to add file path to watcher")
}
for {
select {
case event, ok := <-watcher.Events:
if !ok {
return
}
if event.Name != lvmd.DefaultFileConfigPath {
continue
}
if event.Has(fsnotify.Write) || event.Has(fsnotify.Remove) || event.Has(fsnotify.Chmod) {
opts.SetupLog.Info("lvmd config file is modified", "eventName", event.Name)
cancelWithCause(fmt.Errorf("%w: %s", ErrConfigModified, event.Name))
}
case err, ok := <-watcher.Errors:
if !ok {
return
}
opts.SetupLog.Error(err, "file watcher error")
}
}
}
func loadConfFile(ctx context.Context, config *lvmd.Config, cfgFilePath string) error {
b, err := os.ReadFile(cfgFilePath)
if err != nil {
return err
}
err = yaml.Unmarshal(b, config)
if err != nil {
return err
}
log.FromContext(ctx).Info("configuration file loaded",
"device_classes", config.DeviceClasses,
"file_name", cfgFilePath,
)
return nil
}
func ErrorLoggingInterceptor(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error) {
resp, err = handler(ctx, req)
if err != nil {
ctrl.Log.Error(err, "error on grpc call", "method", info.FullMethod)
}
return resp, err
}
func registrationPath() string {
return fmt.Sprintf("%splugins/%s/node/csi-topolvm.sock", resource.GetAbsoluteKubeletPath(constants.CSIKubeletRootDir), constants.TopolvmCSIDriverName)
}
func pluginRegistrationSocketPath() string {
return fmt.Sprintf("%s/%s-reg.sock", constants.DefaultPluginRegistrationPath, constants.TopolvmCSIDriverName)
}
// readyCheck returns a healthz.Checker that verifies the operator is ready
func readyCheck(mgr manager.Manager) healthz.Checker {
return func(req *http.Request) error {
// Perform various checks here to determine if the operator is ready
if !mgr.GetCache().WaitForCacheSync(req.Context()) {
return fmt.Errorf("informer cache not synced and thus not ready")
}
return nil
}
}