diff --git a/README.md b/README.md index abfb2e0..eeaae27 100644 --- a/README.md +++ b/README.md @@ -39,6 +39,7 @@ api-url = api-key = secret-key = ssl-no-verify = +project-id = ``` Create a secret named `cloudstack-secret` in namespace `kube-system`: diff --git a/pkg/cloud/cloud.go b/pkg/cloud/cloud.go index a8b5417..dc45a05 100644 --- a/pkg/cloud/cloud.go +++ b/pkg/cloud/cloud.go @@ -55,11 +55,15 @@ var ( // client is the implementation of Interface. type client struct { *cloudstack.CloudStackClient + projectID string } // New creates a new cloud connector, given its configuration. func New(config *Config) Interface { - csClient := cloudstack.NewAsyncClient(config.APIURL, config.APIKey, config.SecretKey, config.VerifySSL) + csClient := &client{ + projectID: config.ProjectID, + } + csClient.CloudStackClient = cloudstack.NewAsyncClient(config.APIURL, config.APIKey, config.SecretKey, config.VerifySSL) - return &client{csClient} + return csClient } diff --git a/pkg/cloud/config.go b/pkg/cloud/config.go index 86e8c57..ce7dc25 100644 --- a/pkg/cloud/config.go +++ b/pkg/cloud/config.go @@ -12,6 +12,7 @@ type Config struct { APIKey string SecretKey string VerifySSL bool + ProjectID string } // csConfig wraps the config for the CloudStack cloud provider. @@ -42,5 +43,6 @@ func ReadConfig(configFilePath string) (*Config, error) { APIKey: cfg.Global.APIKey, SecretKey: cfg.Global.SecretKey, VerifySSL: !cfg.Global.SSLNoVerify, + ProjectID: cfg.Global.ProjectID, }, nil } diff --git a/pkg/cloud/vms.go b/pkg/cloud/vms.go index 68a0505..80b8dd9 100644 --- a/pkg/cloud/vms.go +++ b/pkg/cloud/vms.go @@ -10,6 +10,9 @@ func (c *client) GetVMByID(ctx context.Context, vmID string) (*VM, error) { logger := klog.FromContext(ctx) p := c.VirtualMachine.NewListVirtualMachinesParams() p.SetId(vmID) + if c.projectID != "" { + p.SetProjectid(c.projectID) + } logger.V(2).Info("CloudStack API call", "command", "ListVirtualMachines", "params", map[string]string{ "id": vmID, }) @@ -35,6 +38,9 @@ func (c *client) getVMByName(ctx context.Context, name string) (*VM, error) { logger := klog.FromContext(ctx) p := c.VirtualMachine.NewListVirtualMachinesParams() p.SetName(name) + if c.projectID != "" { + p.SetProjectid(c.projectID) + } logger.V(2).Info("CloudStack API call", "command", "ListVirtualMachines", "params", map[string]string{ "name": name, }) diff --git a/pkg/cloud/volumes.go b/pkg/cloud/volumes.go index f367796..e2927d1 100644 --- a/pkg/cloud/volumes.go +++ b/pkg/cloud/volumes.go @@ -41,6 +41,9 @@ func (c *client) GetVolumeByID(ctx context.Context, volumeID string) (*Volume, e logger := klog.FromContext(ctx) p := c.Volume.NewListVolumesParams() p.SetId(volumeID) + if c.projectID != "" { + p.SetProjectid(c.projectID) + } logger.V(2).Info("CloudStack API call", "command", "ListVolumes", "params", map[string]string{ "id": volumeID, }) @@ -52,6 +55,9 @@ func (c *client) GetVolumeByName(ctx context.Context, name string) (*Volume, err logger := klog.FromContext(ctx) p := c.Volume.NewListVolumesParams() p.SetName(name) + if c.projectID != "" { + p.SetProjectid(c.projectID) + } logger.V(2).Info("CloudStack API call", "command", "ListVolumes", "params", map[string]string{ "name": name, }) @@ -66,6 +72,9 @@ func (c *client) CreateVolume(ctx context.Context, diskOfferingID, zoneID, name p.SetZoneid(zoneID) p.SetName(name) p.SetSize(sizeInGB) + if c.projectID != "" { + p.SetProjectid(c.projectID) + } logger.V(2).Info("CloudStack API call", "command", "CreateVolume", "params", map[string]string{ "diskofferingid": diskOfferingID, "zoneid": zoneID,