Lorenz Brun | 0db90ba | 2020-04-06 14:04:52 +0200 | [diff] [blame] | 1 | // Copyright 2020 The Monogon Project Authors. |
| 2 | // |
| 3 | // SPDX-License-Identifier: Apache-2.0 |
| 4 | // |
| 5 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | // you may not use this file except in compliance with the License. |
| 7 | // You may obtain a copy of the License at |
| 8 | // |
| 9 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | // |
| 11 | // Unless required by applicable law or agreed to in writing, software |
| 12 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | // See the License for the specific language governing permissions and |
| 15 | // limitations under the License. |
| 16 | |
| 17 | package kubernetes |
| 18 | |
| 19 | import ( |
| 20 | "context" |
| 21 | "fmt" |
| 22 | "net" |
| 23 | "os" |
| 24 | "path/filepath" |
| 25 | "regexp" |
| 26 | |
Lorenz Brun | 0db90ba | 2020-04-06 14:04:52 +0200 | [diff] [blame] | 27 | "github.com/container-storage-interface/spec/lib/go/csi" |
| 28 | "github.com/golang/protobuf/ptypes/wrappers" |
Lorenz Brun | 0db90ba | 2020-04-06 14:04:52 +0200 | [diff] [blame] | 29 | "golang.org/x/sys/unix" |
| 30 | "google.golang.org/grpc" |
| 31 | "google.golang.org/grpc/codes" |
| 32 | "google.golang.org/grpc/status" |
| 33 | pluginregistration "k8s.io/kubelet/pkg/apis/pluginregistration/v1" |
| 34 | |
Serge Bazanski | 77cb6c5 | 2020-12-19 00:09:22 +0100 | [diff] [blame] | 35 | "git.monogon.dev/source/nexantic.git/metropolis/node/core/localstorage" |
Serge Bazanski | 549b72b | 2021-01-07 14:54:19 +0100 | [diff] [blame] | 36 | "git.monogon.dev/source/nexantic.git/metropolis/pkg/logtree" |
| 37 | "git.monogon.dev/source/nexantic.git/metropolis/pkg/fsquota" |
| 38 | "git.monogon.dev/source/nexantic.git/metropolis/pkg/supervisor" |
Lorenz Brun | 0db90ba | 2020-04-06 14:04:52 +0200 | [diff] [blame] | 39 | ) |
| 40 | |
| 41 | // Derived from K8s spec for acceptable names, but shortened to 130 characters to avoid issues with |
| 42 | // maximum path length. We don't provision longer names so this applies only if you manually create |
| 43 | // a volume with a name of more than 130 characters. |
Lorenz Brun | b15abad | 2020-04-16 11:17:12 +0200 | [diff] [blame] | 44 | var acceptableNames = regexp.MustCompile("^[a-z][a-bz0-9-.]{0,128}[a-z0-9]$") |
Lorenz Brun | 0db90ba | 2020-04-06 14:04:52 +0200 | [diff] [blame] | 45 | |
| 46 | const volumeDir = "volumes" |
| 47 | |
Serge Bazanski | c2c7ad9 | 2020-07-13 17:20:09 +0200 | [diff] [blame] | 48 | type csiPluginServer struct { |
| 49 | KubeletDirectory *localstorage.DataKubernetesKubeletDirectory |
| 50 | VolumesDirectory *localstorage.DataVolumesDirectory |
Lorenz Brun | 0db90ba | 2020-04-06 14:04:52 +0200 | [diff] [blame] | 51 | |
Serge Bazanski | c735967 | 2020-10-30 16:38:57 +0100 | [diff] [blame] | 52 | logger logtree.LeveledLogger |
Lorenz Brun | 0db90ba | 2020-04-06 14:04:52 +0200 | [diff] [blame] | 53 | } |
| 54 | |
Serge Bazanski | c2c7ad9 | 2020-07-13 17:20:09 +0200 | [diff] [blame] | 55 | func (s *csiPluginServer) Run(ctx context.Context) error { |
| 56 | s.logger = supervisor.Logger(ctx) |
Lorenz Brun | 0db90ba | 2020-04-06 14:04:52 +0200 | [diff] [blame] | 57 | |
Serge Bazanski | c2c7ad9 | 2020-07-13 17:20:09 +0200 | [diff] [blame] | 58 | pluginListener, err := net.ListenUnix("unix", &net.UnixAddr{Name: s.KubeletDirectory.Plugins.VFS.FullPath(), Net: "unix"}) |
| 59 | if err != nil { |
| 60 | return fmt.Errorf("failed to listen on CSI socket: %w", err) |
Lorenz Brun | 0db90ba | 2020-04-06 14:04:52 +0200 | [diff] [blame] | 61 | } |
Serge Bazanski | c2c7ad9 | 2020-07-13 17:20:09 +0200 | [diff] [blame] | 62 | pluginListener.SetUnlinkOnClose(true) |
| 63 | |
| 64 | pluginServer := grpc.NewServer() |
| 65 | csi.RegisterIdentityServer(pluginServer, s) |
| 66 | csi.RegisterNodeServer(pluginServer, s) |
| 67 | // Enable graceful shutdown since we don't have long-running RPCs and most of them shouldn't and can't be |
| 68 | // cancelled anyways. |
| 69 | if err := supervisor.Run(ctx, "csi-node", supervisor.GRPCServer(pluginServer, pluginListener, true)); err != nil { |
| 70 | return err |
| 71 | } |
| 72 | |
| 73 | registrationListener, err := net.ListenUnix("unix", &net.UnixAddr{Name: s.KubeletDirectory.PluginsRegistry.VFSReg.FullPath(), Net: "unix"}) |
| 74 | if err != nil { |
| 75 | return fmt.Errorf("failed to listen on CSI registration socket: %w", err) |
| 76 | } |
| 77 | registrationListener.SetUnlinkOnClose(true) |
| 78 | |
| 79 | registrationServer := grpc.NewServer() |
| 80 | pluginregistration.RegisterRegistrationServer(registrationServer, s) |
| 81 | if err := supervisor.Run(ctx, "registration", supervisor.GRPCServer(registrationServer, registrationListener, true)); err != nil { |
| 82 | return err |
| 83 | } |
| 84 | supervisor.Signal(ctx, supervisor.SignalHealthy) |
| 85 | supervisor.Signal(ctx, supervisor.SignalDone) |
| 86 | return nil |
Lorenz Brun | 0db90ba | 2020-04-06 14:04:52 +0200 | [diff] [blame] | 87 | } |
| 88 | |
Serge Bazanski | c2c7ad9 | 2020-07-13 17:20:09 +0200 | [diff] [blame] | 89 | func (*csiPluginServer) NodeStageVolume(ctx context.Context, req *csi.NodeStageVolumeRequest) (*csi.NodeStageVolumeResponse, error) { |
Lorenz Brun | 0db90ba | 2020-04-06 14:04:52 +0200 | [diff] [blame] | 90 | return nil, status.Errorf(codes.Unimplemented, "method NodeStageVolume not supported") |
| 91 | } |
Serge Bazanski | c2c7ad9 | 2020-07-13 17:20:09 +0200 | [diff] [blame] | 92 | |
| 93 | func (*csiPluginServer) NodeUnstageVolume(ctx context.Context, req *csi.NodeUnstageVolumeRequest) (*csi.NodeUnstageVolumeResponse, error) { |
Lorenz Brun | 0db90ba | 2020-04-06 14:04:52 +0200 | [diff] [blame] | 94 | return nil, status.Errorf(codes.Unimplemented, "method NodeUnstageVolume not supported") |
| 95 | } |
| 96 | |
Serge Bazanski | c2c7ad9 | 2020-07-13 17:20:09 +0200 | [diff] [blame] | 97 | func (s *csiPluginServer) NodePublishVolume(ctx context.Context, req *csi.NodePublishVolumeRequest) (*csi.NodePublishVolumeResponse, error) { |
Lorenz Brun | 0db90ba | 2020-04-06 14:04:52 +0200 | [diff] [blame] | 98 | if !acceptableNames.MatchString(req.VolumeId) { |
| 99 | return nil, status.Error(codes.InvalidArgument, "invalid characters in volume id") |
| 100 | } |
Serge Bazanski | c2c7ad9 | 2020-07-13 17:20:09 +0200 | [diff] [blame] | 101 | |
| 102 | // TODO(q3k): move this logic to localstorage? |
| 103 | volumePath := filepath.Join(s.VolumesDirectory.FullPath(), req.VolumeId) |
| 104 | |
Lorenz Brun | 0db90ba | 2020-04-06 14:04:52 +0200 | [diff] [blame] | 105 | switch req.VolumeCapability.AccessMode.Mode { |
| 106 | case csi.VolumeCapability_AccessMode_SINGLE_NODE_WRITER: |
| 107 | case csi.VolumeCapability_AccessMode_SINGLE_NODE_READER_ONLY: |
| 108 | default: |
| 109 | return nil, status.Error(codes.InvalidArgument, "unsupported access mode") |
| 110 | } |
| 111 | switch req.VolumeCapability.AccessType.(type) { |
| 112 | case *csi.VolumeCapability_Mount: |
| 113 | default: |
| 114 | return nil, status.Error(codes.InvalidArgument, "unsupported access type") |
| 115 | } |
| 116 | |
Serge Bazanski | c2c7ad9 | 2020-07-13 17:20:09 +0200 | [diff] [blame] | 117 | err := unix.Mount(volumePath, req.TargetPath, "", unix.MS_BIND, "") |
| 118 | switch { |
| 119 | case err == unix.ENOENT: |
Lorenz Brun | 0db90ba | 2020-04-06 14:04:52 +0200 | [diff] [blame] | 120 | return nil, status.Error(codes.NotFound, "volume not found") |
Serge Bazanski | c2c7ad9 | 2020-07-13 17:20:09 +0200 | [diff] [blame] | 121 | case err != nil: |
Lorenz Brun | 0db90ba | 2020-04-06 14:04:52 +0200 | [diff] [blame] | 122 | return nil, status.Errorf(codes.Unavailable, "failed to bind-mount volume: %v", err) |
| 123 | } |
Serge Bazanski | c2c7ad9 | 2020-07-13 17:20:09 +0200 | [diff] [blame] | 124 | |
Lorenz Brun | 0db90ba | 2020-04-06 14:04:52 +0200 | [diff] [blame] | 125 | if req.Readonly { |
| 126 | err := unix.Mount(volumePath, req.TargetPath, "", unix.MS_BIND|unix.MS_REMOUNT|unix.MS_RDONLY, "") |
| 127 | if err != nil { |
| 128 | _ = unix.Unmount(req.TargetPath, 0) // Best-effort |
| 129 | return nil, status.Errorf(codes.Unavailable, "failed to remount volume: %v", err) |
| 130 | } |
| 131 | } |
| 132 | return &csi.NodePublishVolumeResponse{}, nil |
| 133 | } |
Serge Bazanski | c2c7ad9 | 2020-07-13 17:20:09 +0200 | [diff] [blame] | 134 | |
| 135 | func (*csiPluginServer) NodeUnpublishVolume(ctx context.Context, req *csi.NodeUnpublishVolumeRequest) (*csi.NodeUnpublishVolumeResponse, error) { |
Lorenz Brun | 0db90ba | 2020-04-06 14:04:52 +0200 | [diff] [blame] | 136 | if err := unix.Unmount(req.TargetPath, 0); err != nil { |
| 137 | return nil, status.Errorf(codes.Unavailable, "failed to unmount volume: %v", err) |
| 138 | } |
| 139 | return &csi.NodeUnpublishVolumeResponse{}, nil |
| 140 | } |
Serge Bazanski | c2c7ad9 | 2020-07-13 17:20:09 +0200 | [diff] [blame] | 141 | |
| 142 | func (*csiPluginServer) NodeGetVolumeStats(ctx context.Context, req *csi.NodeGetVolumeStatsRequest) (*csi.NodeGetVolumeStatsResponse, error) { |
Lorenz Brun | 0db90ba | 2020-04-06 14:04:52 +0200 | [diff] [blame] | 143 | quota, err := fsquota.GetQuota(req.VolumePath) |
| 144 | if os.IsNotExist(err) { |
| 145 | return nil, status.Error(codes.NotFound, "volume does not exist at this path") |
| 146 | } else if err != nil { |
| 147 | return nil, status.Errorf(codes.Unavailable, "failed to get quota: %v", err) |
| 148 | } |
| 149 | |
| 150 | return &csi.NodeGetVolumeStatsResponse{ |
| 151 | Usage: []*csi.VolumeUsage{ |
| 152 | { |
| 153 | Total: int64(quota.Bytes), |
| 154 | Unit: csi.VolumeUsage_BYTES, |
| 155 | Used: int64(quota.BytesUsed), |
| 156 | Available: int64(quota.Bytes - quota.BytesUsed), |
| 157 | }, |
| 158 | { |
| 159 | Total: int64(quota.Inodes), |
| 160 | Unit: csi.VolumeUsage_INODES, |
| 161 | Used: int64(quota.InodesUsed), |
| 162 | Available: int64(quota.Inodes - quota.InodesUsed), |
| 163 | }, |
| 164 | }, |
| 165 | }, nil |
| 166 | } |
Serge Bazanski | c2c7ad9 | 2020-07-13 17:20:09 +0200 | [diff] [blame] | 167 | |
| 168 | func (*csiPluginServer) NodeExpandVolume(ctx context.Context, req *csi.NodeExpandVolumeRequest) (*csi.NodeExpandVolumeResponse, error) { |
Lorenz Brun | 0db90ba | 2020-04-06 14:04:52 +0200 | [diff] [blame] | 169 | if req.CapacityRange.LimitBytes <= 0 { |
| 170 | return nil, status.Error(codes.InvalidArgument, "invalid expanded volume size: at or below zero bytes") |
| 171 | } |
| 172 | if err := fsquota.SetQuota(req.VolumePath, uint64(req.CapacityRange.LimitBytes), 0); err != nil { |
| 173 | return nil, status.Errorf(codes.Unavailable, "failed to update quota: %v", err) |
| 174 | } |
| 175 | return &csi.NodeExpandVolumeResponse{CapacityBytes: req.CapacityRange.LimitBytes}, nil |
| 176 | } |
| 177 | |
| 178 | func rpcCapability(cap csi.NodeServiceCapability_RPC_Type) *csi.NodeServiceCapability { |
| 179 | return &csi.NodeServiceCapability{ |
| 180 | Type: &csi.NodeServiceCapability_Rpc{ |
| 181 | Rpc: &csi.NodeServiceCapability_RPC{Type: cap}, |
| 182 | }, |
| 183 | } |
| 184 | } |
Serge Bazanski | c2c7ad9 | 2020-07-13 17:20:09 +0200 | [diff] [blame] | 185 | |
| 186 | func (*csiPluginServer) NodeGetCapabilities(ctx context.Context, req *csi.NodeGetCapabilitiesRequest) (*csi.NodeGetCapabilitiesResponse, error) { |
Lorenz Brun | 0db90ba | 2020-04-06 14:04:52 +0200 | [diff] [blame] | 187 | return &csi.NodeGetCapabilitiesResponse{ |
| 188 | Capabilities: []*csi.NodeServiceCapability{ |
| 189 | rpcCapability(csi.NodeServiceCapability_RPC_EXPAND_VOLUME), |
| 190 | rpcCapability(csi.NodeServiceCapability_RPC_GET_VOLUME_STATS), |
| 191 | }, |
| 192 | }, nil |
| 193 | } |
Serge Bazanski | c2c7ad9 | 2020-07-13 17:20:09 +0200 | [diff] [blame] | 194 | |
| 195 | func (*csiPluginServer) NodeGetInfo(ctx context.Context, req *csi.NodeGetInfoRequest) (*csi.NodeGetInfoResponse, error) { |
Lorenz Brun | 0db90ba | 2020-04-06 14:04:52 +0200 | [diff] [blame] | 196 | hostname, err := os.Hostname() |
| 197 | if err != nil { |
| 198 | return nil, status.Errorf(codes.Unavailable, "failed to get node identity: %v", err) |
| 199 | } |
| 200 | return &csi.NodeGetInfoResponse{ |
| 201 | NodeId: hostname, |
| 202 | }, nil |
| 203 | } |
| 204 | |
| 205 | // CSI Identity endpoints |
Serge Bazanski | c2c7ad9 | 2020-07-13 17:20:09 +0200 | [diff] [blame] | 206 | func (*csiPluginServer) GetPluginInfo(ctx context.Context, req *csi.GetPluginInfoRequest) (*csi.GetPluginInfoResponse, error) { |
Lorenz Brun | 0db90ba | 2020-04-06 14:04:52 +0200 | [diff] [blame] | 207 | return &csi.GetPluginInfoResponse{ |
Serge Bazanski | 662b5b3 | 2020-12-21 13:49:00 +0100 | [diff] [blame] | 208 | Name: "dev.monogon.metropolis.vfs", |
Lorenz Brun | 0db90ba | 2020-04-06 14:04:52 +0200 | [diff] [blame] | 209 | VendorVersion: "0.0.1", // TODO(lorenz): Maybe stamp? |
| 210 | }, nil |
| 211 | } |
Serge Bazanski | c2c7ad9 | 2020-07-13 17:20:09 +0200 | [diff] [blame] | 212 | |
| 213 | func (*csiPluginServer) GetPluginCapabilities(ctx context.Context, req *csi.GetPluginCapabilitiesRequest) (*csi.GetPluginCapabilitiesResponse, error) { |
Lorenz Brun | 0db90ba | 2020-04-06 14:04:52 +0200 | [diff] [blame] | 214 | return &csi.GetPluginCapabilitiesResponse{ |
| 215 | Capabilities: []*csi.PluginCapability{ |
| 216 | { |
| 217 | Type: &csi.PluginCapability_VolumeExpansion_{ |
| 218 | VolumeExpansion: &csi.PluginCapability_VolumeExpansion{ |
| 219 | Type: csi.PluginCapability_VolumeExpansion_ONLINE, |
| 220 | }, |
| 221 | }, |
| 222 | }, |
| 223 | }, |
| 224 | }, nil |
| 225 | } |
| 226 | |
Serge Bazanski | c2c7ad9 | 2020-07-13 17:20:09 +0200 | [diff] [blame] | 227 | func (s *csiPluginServer) Probe(ctx context.Context, req *csi.ProbeRequest) (*csi.ProbeResponse, error) { |
| 228 | return &csi.ProbeResponse{Ready: &wrappers.BoolValue{Value: true}}, nil |
Lorenz Brun | 0db90ba | 2020-04-06 14:04:52 +0200 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | // Registration endpoints |
Serge Bazanski | c2c7ad9 | 2020-07-13 17:20:09 +0200 | [diff] [blame] | 232 | func (s *csiPluginServer) GetInfo(ctx context.Context, req *pluginregistration.InfoRequest) (*pluginregistration.PluginInfo, error) { |
Lorenz Brun | 0db90ba | 2020-04-06 14:04:52 +0200 | [diff] [blame] | 233 | return &pluginregistration.PluginInfo{ |
| 234 | Type: "CSIPlugin", |
Serge Bazanski | 662b5b3 | 2020-12-21 13:49:00 +0100 | [diff] [blame] | 235 | Name: "dev.monogon.metropolis.vfs", |
Serge Bazanski | c2c7ad9 | 2020-07-13 17:20:09 +0200 | [diff] [blame] | 236 | Endpoint: s.KubeletDirectory.Plugins.VFS.FullPath(), |
Lorenz Brun | 0db90ba | 2020-04-06 14:04:52 +0200 | [diff] [blame] | 237 | SupportedVersions: []string{"1.2"}, // Keep in sync with container-storage-interface/spec package version |
| 238 | }, nil |
| 239 | } |
| 240 | |
Serge Bazanski | c2c7ad9 | 2020-07-13 17:20:09 +0200 | [diff] [blame] | 241 | func (s *csiPluginServer) NotifyRegistrationStatus(ctx context.Context, req *pluginregistration.RegistrationStatus) (*pluginregistration.RegistrationStatusResponse, error) { |
Lorenz Brun | 0db90ba | 2020-04-06 14:04:52 +0200 | [diff] [blame] | 242 | if req.Error != "" { |
Serge Bazanski | c735967 | 2020-10-30 16:38:57 +0100 | [diff] [blame] | 243 | s.logger.Warningf("Kubelet failed registering CSI plugin: %v", req.Error) |
Lorenz Brun | 0db90ba | 2020-04-06 14:04:52 +0200 | [diff] [blame] | 244 | } |
| 245 | return &pluginregistration.RegistrationStatusResponse{}, nil |
| 246 | } |