Skip to content

Commit

Permalink
feat: prefer providerID
Browse files Browse the repository at this point in the history
Define the node using the ProviderID magic string first.
Otherwise, use the Proxmox VM list.

Signed-off-by: Serge Logvinov <[email protected]>
  • Loading branch information
sergelogvinov committed Feb 15, 2024
1 parent 5580695 commit 7dcde72
Show file tree
Hide file tree
Showing 9 changed files with 146 additions and 64 deletions.
18 changes: 16 additions & 2 deletions cmd/controller/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,18 @@ import (
"google.golang.org/grpc"

"github.com/sergelogvinov/proxmox-csi-plugin/pkg/csi"
"github.com/sergelogvinov/proxmox-csi-plugin/pkg/tools"

clientkubernetes "k8s.io/client-go/kubernetes"
"k8s.io/klog/v2"
)

var (
showVersion = flag.Bool("version", false, "Print the version and exit.")
csiEndpoint = flag.String("csi-address", "unix:///csi/csi.sock", "CSI Endpoint")

cloudconfig = flag.String("cloud-config", "", "The path to the CSI driver cloud config.")
kubeconfig = flag.String("kubeconfig", "", "Absolute path to the kubeconfig file. Either this or master needs to be set if the provisioner is being run out of cluster.")

version string
)
Expand All @@ -60,6 +64,16 @@ func main() {
klog.Fatalln("cloud-config must be provided")
}

kconfig, _, err := tools.BuildConfig(*kubeconfig, "")
if err != nil {
klog.Fatalf("failed to create kubernetes config: %v", err)
}

clientset, err := clientkubernetes.NewForConfig(kconfig)
if err != nil {
klog.Fatalf("failed to create kubernetes client: %v", err)
}

scheme, addr, err := csi.ParseEndpoint(*csiEndpoint)
if err != nil {
klog.Fatalf("Failed to parse endpoint: %v", err)
Expand All @@ -70,7 +84,7 @@ func main() {
klog.Fatalf("Failed to listen on %s: %v", *csiEndpoint, err)
}

logErr := func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
logErr := func(ctx context.Context, req interface{}, _ *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
resp, rpcerr := handler(ctx, req)
if rpcerr != nil {
klog.Errorf("GRPC error: %v", rpcerr)
Expand All @@ -87,7 +101,7 @@ func main() {

identityService := csi.NewIdentityService()

controllerService, err := csi.NewControllerService(*cloudconfig)
controllerService, err := csi.NewControllerService(clientset, *cloudconfig)
if err != nil {
klog.Fatalf("Failed to create controller service: %v", err)
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/node/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func main() {
klog.Fatalf("Failed to listen on %s: %v", *csiEndpoint, err)
}

logErr := func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
logErr := func(ctx context.Context, req interface{}, _ *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
resp, rpcerr := handler(ctx, req)
if rpcerr != nil {
klog.Errorf("GRPC error: %v", rpcerr)
Expand Down
2 changes: 1 addition & 1 deletion cmd/pvecsi-mutate/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func run() int {
Use: command,
Version: fmt.Sprintf("%s (commit: %s)", version, commit),
Short: "A command-line utility to manipulate PersistentVolume/PersistentVolumeClaim on Proxmox VE",
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
PersistentPreRunE: func(cmd *cobra.Command, _ []string) error {
f := cmd.Flags()
loglvl, _ := f.GetString(flagLogLevel) //nolint: errcheck

Expand Down
14 changes: 7 additions & 7 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,18 @@ require (
github.com/golang/protobuf v1.5.3
github.com/jarcoal/httpmock v1.3.1
github.com/kubernetes-csi/csi-lib-utils v0.17.0
github.com/sergelogvinov/proxmox-cloud-controller-manager v0.3.0
github.com/sergelogvinov/proxmox-cloud-controller-manager v0.3.1-0.20240215135711-7b73b5f8a2b1
github.com/siderolabs/go-blockdevice v0.4.7
github.com/sirupsen/logrus v1.9.0
github.com/sirupsen/logrus v1.9.3
github.com/spf13/cobra v1.8.0
github.com/stretchr/testify v1.8.4
google.golang.org/grpc v1.61.0
k8s.io/api v0.29.1
k8s.io/apimachinery v0.29.1
k8s.io/client-go v0.29.1
google.golang.org/grpc v1.61.1
k8s.io/api v0.29.2
k8s.io/apimachinery v0.29.2
k8s.io/client-go v0.29.2
k8s.io/cloud-provider-openstack v1.29.0
k8s.io/klog/v2 v2.120.1
k8s.io/mount-utils v0.29.1
k8s.io/mount-utils v0.29.2
k8s.io/utils v0.0.0-20240102154912-e7106e64919e
)

Expand Down
28 changes: 14 additions & 14 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -83,16 +83,16 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M=
github.com/rogpeppe/go-internal v1.11.0/go.mod h1:ddIwULY96R17DhadqLgMfk9H9tvdUzkipdSkR5nkCZA=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/sergelogvinov/proxmox-cloud-controller-manager v0.3.0 h1:2TurTnjawIx5j3YIRw0PEBbxM7YpLwlVBXfnnnpYH5s=
github.com/sergelogvinov/proxmox-cloud-controller-manager v0.3.0/go.mod h1:SILDj23jkQGVPtWCWdadd3E6VDJlWnN+P4rgKGrg1j8=
github.com/sergelogvinov/proxmox-cloud-controller-manager v0.3.1-0.20240215135711-7b73b5f8a2b1 h1:h9rKPmPAzJggV5oliX2akNAeQu5JmZsfobgN/eT/M4I=
github.com/sergelogvinov/proxmox-cloud-controller-manager v0.3.1-0.20240215135711-7b73b5f8a2b1/go.mod h1:SILDj23jkQGVPtWCWdadd3E6VDJlWnN+P4rgKGrg1j8=
github.com/siderolabs/go-blockdevice v0.4.7 h1:2bk4WpEEflGxjrNwp57ye24Pr+cYgAiAeNMWiQOuWbQ=
github.com/siderolabs/go-blockdevice v0.4.7/go.mod h1:4PeOuk71pReJj1JQEXDE7kIIQJPVe8a+HZQa+qjxSEA=
github.com/siderolabs/go-cmd v0.1.1 h1:nTouZUSxLeiiEe7hFexSVvaTsY/3O8k1s08BxPRrsps=
github.com/siderolabs/go-cmd v0.1.1/go.mod h1:6hY0JG34LxEEwYE8aH2iIHkHX/ir12VRLqfwAf2yJIY=
github.com/siderolabs/go-retry v0.3.2 h1:FzWslFm4y8RY1wU0gIskm0oZHOpsSibZqlR8N8/k4Eo=
github.com/siderolabs/go-retry v0.3.2/go.mod h1:Ac8HIh0nAYDQm04FGZHNofVAXteyd4xR9oujTRrtvK0=
github.com/sirupsen/logrus v1.9.0 h1:trlNQbNUG3OdDrDil03MCb1H2o9nJ1x4/5LYw7byDE0=
github.com/sirupsen/logrus v1.9.0/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/spf13/cobra v1.8.0 h1:7aJaZx1B85qltLMc546zn58BxxfZdR/W22ej9CFoEf0=
github.com/spf13/cobra v1.8.0/go.mod h1:WXLWApfZ71AjXPya3WOlMsY9yMs7YeiHhFVlvLyhcho=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
Expand Down Expand Up @@ -170,8 +170,8 @@ google.golang.org/appengine v1.6.8 h1:IhEN5q69dyKagZPYMSdIjS2HqprW324FRQZJcGqPAs
google.golang.org/appengine v1.6.8/go.mod h1:1jJ3jBArFh5pcgW8gCtRJnepW8FzD1V44FJffLiz/Ds=
google.golang.org/genproto/googleapis/rpc v0.0.0-20240102182953-50ed04b92917 h1:6G8oQ016D88m1xAKljMlBOOGWDZkes4kMhgGFlf8WcQ=
google.golang.org/genproto/googleapis/rpc v0.0.0-20240102182953-50ed04b92917/go.mod h1:xtjpI3tXFPP051KaWnhvxkiubL/6dJ18vLVf7q2pTOU=
google.golang.org/grpc v1.61.0 h1:TOvOcuXn30kRao+gfcvsebNEa5iZIiLkisYEkf7R7o0=
google.golang.org/grpc v1.61.0/go.mod h1:VUbo7IFqmF1QtCAstipjG0GIoq49KvMe9+h1jFLBNJs=
google.golang.org/grpc v1.61.1 h1:kLAiWrZs7YeDM6MumDe7m3y4aM6wacLzM1Y/wiLP9XY=
google.golang.org/grpc v1.61.1/go.mod h1:VUbo7IFqmF1QtCAstipjG0GIoq49KvMe9+h1jFLBNJs=
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
google.golang.org/protobuf v1.32.0 h1:pPC6BG5ex8PDFnkbrGU3EixyhKcQ2aDuBS36lqK/C7I=
Expand All @@ -187,20 +187,20 @@ gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
k8s.io/api v0.29.1 h1:DAjwWX/9YT7NQD4INu49ROJuZAAAP/Ijki48GUPzxqw=
k8s.io/api v0.29.1/go.mod h1:7Kl10vBRUXhnQQI8YR/R327zXC8eJ7887/+Ybta+RoQ=
k8s.io/apimachinery v0.29.1 h1:KY4/E6km/wLBguvCZv8cKTeOwwOBqFNjwJIdMkMbbRc=
k8s.io/apimachinery v0.29.1/go.mod h1:6HVkd1FwxIagpYrHSwJlQqZI3G9LfYWRPAkUvLnXTKU=
k8s.io/client-go v0.29.1 h1:19B/+2NGEwnFLzt0uB5kNJnfTsbV8w6TgQRz9l7ti7A=
k8s.io/client-go v0.29.1/go.mod h1:TDG/psL9hdet0TI9mGyHJSgRkW3H9JZk2dNEUS7bRks=
k8s.io/api v0.29.2 h1:hBC7B9+MU+ptchxEqTNW2DkUosJpp1P+Wn6YncZ474A=
k8s.io/api v0.29.2/go.mod h1:sdIaaKuU7P44aoyyLlikSLayT6Vb7bvJNCX105xZXY0=
k8s.io/apimachinery v0.29.2 h1:EWGpfJ856oj11C52NRCHuU7rFDwxev48z+6DSlGNsV8=
k8s.io/apimachinery v0.29.2/go.mod h1:6HVkd1FwxIagpYrHSwJlQqZI3G9LfYWRPAkUvLnXTKU=
k8s.io/client-go v0.29.2 h1:FEg85el1TeZp+/vYJM7hkDlSTFZ+c5nnK44DJ4FyoRg=
k8s.io/client-go v0.29.2/go.mod h1:knlvFZE58VpqbQpJNbCbctTVXcd35mMyAAwBdpt4jrA=
k8s.io/cloud-provider-openstack v1.29.0 h1:sZL8WSEBZnHKtBvZw5+KAl958QCnS4+FTfbPd9eRq+E=
k8s.io/cloud-provider-openstack v1.29.0/go.mod h1:vIQLIYKLqEbEXO2H4FtY+0QmV3lPzS2P5iRIMNr8x6k=
k8s.io/klog/v2 v2.120.1 h1:QXU6cPEOIslTGvZaXvFWiP9VKyeet3sawzTOvdXb4Vw=
k8s.io/klog/v2 v2.120.1/go.mod h1:3Jpz1GvMt720eyJH1ckRHK1EDfpxISzJ7I9OYgaDtPE=
k8s.io/kube-openapi v0.0.0-20240103160808-8a9faedaf1cd h1:mbo+LIupdi4bqfqC+C8nsyFwvSgmw4eK3yVyEGGPrmc=
k8s.io/kube-openapi v0.0.0-20240103160808-8a9faedaf1cd/go.mod h1:sIV51WBTkZrlGOJMCDZDA1IaPBUDTulPpD4y7oe038k=
k8s.io/mount-utils v0.29.1 h1:veXlIm52Y4tm3H0pG03cOdkw0KOJxYDa0fQqhJCoqvQ=
k8s.io/mount-utils v0.29.1/go.mod h1:9IWJTMe8tG0MYMLEp60xK9GYVeCdA3g4LowmnVi+t9Y=
k8s.io/mount-utils v0.29.2 h1:FrUfgvOo63nqJRPXKoqN/DW1lMnR/y0pzpFErKh6p2o=
k8s.io/mount-utils v0.29.2/go.mod h1:9IWJTMe8tG0MYMLEp60xK9GYVeCdA3g4LowmnVi+t9Y=
k8s.io/utils v0.0.0-20240102154912-e7106e64919e h1:eQ/4ljkx21sObifjzXwlPKpdGLrCfRziVtos3ofG/sQ=
k8s.io/utils v0.0.0-20240102154912-e7106e64919e/go.mod h1:OLgZIPagt7ERELqWJFomSt595RzquPNLL48iOWgYOg0=
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd h1:EDPBXCAspyGV4jQlpZSudPeMmr1bNJefnuqLsRAsHZo=
Expand Down
50 changes: 38 additions & 12 deletions pkg/csi/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,11 @@ import (
"google.golang.org/grpc/status"

proxmox "github.com/sergelogvinov/proxmox-cloud-controller-manager/pkg/cluster"
"github.com/sergelogvinov/proxmox-csi-plugin/pkg/tools"
volume "github.com/sergelogvinov/proxmox-csi-plugin/pkg/volume"

corev1 "k8s.io/api/core/v1"
clientkubernetes "k8s.io/client-go/kubernetes"
"k8s.io/cloud-provider-openstack/pkg/util"
"k8s.io/klog/v2"
)
Expand All @@ -55,12 +57,13 @@ var controllerCaps = []csi.ControllerServiceCapability_RPC_Type{
// ControllerService is the controller service for the CSI driver
type ControllerService struct {
Cluster *proxmox.Cluster
Kclient clientkubernetes.Interface

volumeLocks sync.Mutex
}

// NewControllerService returns a new controller service
func NewControllerService(cloudConfig string) (*ControllerService, error) {
func NewControllerService(kclient *clientkubernetes.Clientset, cloudConfig string) (*ControllerService, error) {
cfg, err := proxmox.ReadCloudConfigFromFile(cloudConfig)
if err != nil {
return nil, fmt.Errorf("failed to read config: %v", err)
Expand All @@ -73,6 +76,7 @@ func NewControllerService(cloudConfig string) (*ControllerService, error) {

return &ControllerService{
Cluster: cluster,
Kclient: kclient,
}, nil
}

Expand Down Expand Up @@ -288,7 +292,7 @@ func (d *ControllerService) ControllerGetCapabilities(_ context.Context, request
}

// ControllerPublishVolume publish a volume
func (d *ControllerService) ControllerPublishVolume(_ context.Context, request *csi.ControllerPublishVolumeRequest) (*csi.ControllerPublishVolumeResponse, error) {
func (d *ControllerService) ControllerPublishVolume(ctx context.Context, request *csi.ControllerPublishVolumeRequest) (*csi.ControllerPublishVolumeResponse, error) {
klog.V(4).Infof("ControllerPublishVolume: called with args %+v", protosanitizer.StripSecrets(*request))

volumeID := request.GetVolumeId()
Expand Down Expand Up @@ -322,14 +326,25 @@ func (d *ControllerService) ControllerPublishVolume(_ context.Context, request *
return nil, status.Error(codes.Internal, err.Error())
}

vm, err := cl.GetVmRefByName(nodeID)
var vmr *pxapi.VmRef

vmrid, zone, err := tools.ProxmoxVMID(ctx, d.Kclient, nodeID)
if err != nil {
klog.Errorf("failed to get vm ref by name: %v", err)
klog.Warningf("failed to get proxmox vmid from ProviderID: %v", err)

return nil, status.Error(codes.Internal, err.Error())
vmr, err = cl.GetVmRefByName(nodeID)
if err != nil {
klog.Errorf("failed to get vm ref by name: %v", err)

return nil, status.Error(codes.Internal, err.Error())
}
} else {
vmr = pxapi.NewVmRef(vmrid)
vmr.SetNode(zone)
vmr.SetVmType("qemu")
}

// if vm.Node() != vol.Node() {
// if vmr.Node() != vol.Node() {
// return nil, status.Error(codes.InvalidArgument, fmt.Sprintf("volume %s does not exist on the node %s", volumeID, nodeID))
// }

Expand Down Expand Up @@ -387,7 +402,7 @@ func (d *ControllerService) ControllerPublishVolume(_ context.Context, request *
d.volumeLocks.Lock()
defer d.volumeLocks.Unlock()

pvInfo, err := attachVolume(cl, vm, vol.Storage(), vol.Disk(), options)
pvInfo, err := attachVolume(cl, vmr, vol.Storage(), vol.Disk(), options)
if err != nil {
klog.Errorf("failed to attach volume: %v", err)

Expand All @@ -398,7 +413,7 @@ func (d *ControllerService) ControllerPublishVolume(_ context.Context, request *
}

// ControllerUnpublishVolume unpublish a volume
func (d *ControllerService) ControllerUnpublishVolume(_ context.Context, request *csi.ControllerUnpublishVolumeRequest) (*csi.ControllerUnpublishVolumeResponse, error) {
func (d *ControllerService) ControllerUnpublishVolume(ctx context.Context, request *csi.ControllerUnpublishVolumeRequest) (*csi.ControllerUnpublishVolumeResponse, error) {
klog.V(4).Infof("ControllerUnpublishVolume: called with args %+v", protosanitizer.StripSecrets(*request))

volumeID := request.GetVolumeId()
Expand All @@ -423,14 +438,25 @@ func (d *ControllerService) ControllerUnpublishVolume(_ context.Context, request
return nil, status.Error(codes.Internal, err.Error())
}

vm, err := cl.GetVmRefByName(nodeID)
var vmr *pxapi.VmRef

vmrid, zone, err := tools.ProxmoxVMID(ctx, d.Kclient, nodeID)
if err != nil {
klog.Errorf("failed to get vm ref by name: %v", err)
klog.Warningf("failed to get proxmox vmid from ProviderID: %v", err)

return nil, status.Error(codes.Internal, err.Error())
vmr, err = cl.GetVmRefByName(nodeID)
if err != nil {
klog.Errorf("failed to get vm ref by name: %v", err)

return nil, status.Error(codes.Internal, err.Error())
}
} else {
vmr = pxapi.NewVmRef(vmrid)
vmr.SetNode(zone)
vmr.SetVmType("qemu")
}

if err := detachVolume(cl, vm, vol.Disk()); err != nil {
if err := detachVolume(cl, vmr, vol.Disk()); err != nil {
klog.Errorf("failed to detachVolume: %v", err)

return nil, status.Error(codes.Internal, err.Error())
Expand Down
Loading

0 comments on commit 7dcde72

Please sign in to comment.