From 32a0fea01c4b6ed6571ec68b1b6bf0b2f862eec9 Mon Sep 17 00:00:00 2001 From: Simon Emms Date: Fri, 20 Dec 2024 17:15:51 +0000 Subject: [PATCH] feat: add labels to the volume identify the k8s usage --- internal/api/volume.go | 10 ++++++++++ internal/csi/volume.go | 1 + internal/driver/controller.go | 4 +++- 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/internal/api/volume.go b/internal/api/volume.go index 128cecf4..fbdc542f 100644 --- a/internal/api/volume.go +++ b/internal/api/volume.go @@ -227,6 +227,16 @@ func (s *VolumeService) Attach(ctx context.Context, volume *csi.Volume, server * return volumes.ErrAttached } + if _, _, err := s.client.Volume.Update(ctx, hcloudVolume, hcloud.VolumeUpdateOpts{Labels: volume.Labels}); err != nil { + s.logger.Info( + "failed to update labels on volume", + "volume-id", volume.ID, + "server-id", server.ID, + "err", err, + ) + return err + } + action, _, err := s.client.Volume.Attach(ctx, hcloudVolume, hcloudServer) if err != nil { s.logger.Info( diff --git a/internal/csi/volume.go b/internal/csi/volume.go index 84862a48..de2d6de1 100644 --- a/internal/csi/volume.go +++ b/internal/csi/volume.go @@ -5,6 +5,7 @@ type Volume struct { ID int64 Name string Size int // GB + Labels map[string]string Location string LinuxDevice string Server *Server diff --git a/internal/driver/controller.go b/internal/driver/controller.go index 2591a920..0297e19d 100644 --- a/internal/driver/controller.go +++ b/internal/driver/controller.go @@ -166,7 +166,9 @@ func (s *ControllerService) ControllerPublishVolume(ctx context.Context, req *pr return nil, status.Error(codes.InvalidArgument, "readonly volumes are not supported") } - volume := &csi.Volume{ID: volumeID} + volume := &csi.Volume{ID: volumeID, Labels: map[string]string{ + "hello": "world", + }} server := &csi.Server{ID: serverID} if err := s.volumeService.Attach(ctx, volume, server); err != nil {