Skip to content

Commit d2ddd95

Browse files
committed
Fix usage of provider ID
1 parent 90adbee commit d2ddd95

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

cloudstack.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ func (cs *CSCloud) GetZoneByProviderID(ctx context.Context, providerID string) (
200200
zone := cloudprovider.Zone{}
201201

202202
instance, count, err := cs.client.VirtualMachine.GetVirtualMachineByID(
203-
providerID,
203+
cs.getInstanceIDFromProviderID(providerID),
204204
cloudstack.WithProject(cs.projectID),
205205
)
206206
if err != nil {

cloudstack_instances.go

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"errors"
2525
"fmt"
2626
"regexp"
27+
"strings"
2728

2829
"github.com/apache/cloudstack-go/v2/cloudstack"
2930
corev1 "k8s.io/api/core/v1"
@@ -53,7 +54,7 @@ func (cs *CSCloud) NodeAddresses(ctx context.Context, name types.NodeName) ([]co
5354
// NodeAddressesByProviderID returns the addresses of the specified instance.
5455
func (cs *CSCloud) NodeAddressesByProviderID(ctx context.Context, providerID string) ([]corev1.NodeAddress, error) {
5556
instance, count, err := cs.client.VirtualMachine.GetVirtualMachineByID(
56-
providerID,
57+
cs.getInstanceIDFromProviderID(providerID),
5758
cloudstack.WithProject(cs.projectID),
5859
)
5960
if err != nil {
@@ -125,7 +126,7 @@ func (cs *CSCloud) InstanceType(ctx context.Context, name types.NodeName) (strin
125126
// InstanceTypeByProviderID returns the type of the specified instance.
126127
func (cs *CSCloud) InstanceTypeByProviderID(ctx context.Context, providerID string) (string, error) {
127128
instance, count, err := cs.client.VirtualMachine.GetVirtualMachineByID(
128-
providerID,
129+
cs.getInstanceIDFromProviderID(providerID),
129130
cloudstack.WithProject(cs.projectID),
130131
)
131132
if err != nil {
@@ -151,7 +152,7 @@ func (cs *CSCloud) CurrentNodeName(ctx context.Context, hostname string) (types.
151152
// InstanceExistsByProviderID returns if the instance still exists.
152153
func (cs *CSCloud) InstanceExistsByProviderID(ctx context.Context, providerID string) (bool, error) {
153154
_, count, err := cs.client.VirtualMachine.GetVirtualMachineByID(
154-
providerID,
155+
cs.getInstanceIDFromProviderID(providerID),
155156
cloudstack.WithProject(cs.projectID),
156157
)
157158
if err != nil {
@@ -185,6 +186,11 @@ func (cs *CSCloud) InstanceShutdown(ctx context.Context, node *corev1.Node) (boo
185186

186187
func (cs *CSCloud) InstanceMetadata(ctx context.Context, node *corev1.Node) (*cloudprovider.InstanceMetadata, error) {
187188

189+
instanceID, err := cs.InstanceID(ctx, types.NodeName(node.Name))
190+
if err != nil {
191+
return nil, err
192+
}
193+
188194
instanceType, err := cs.InstanceType(ctx, types.NodeName(node.Name))
189195
if err != nil {
190196
return nil, err
@@ -201,10 +207,22 @@ func (cs *CSCloud) InstanceMetadata(ctx context.Context, node *corev1.Node) (*cl
201207
}
202208

203209
return &cloudprovider.InstanceMetadata{
204-
ProviderID: cs.ProviderName(),
210+
ProviderID: cs.getProviderIDFromInstanceID(instanceID),
205211
InstanceType: instanceType,
206212
NodeAddresses: addresses,
207213
Zone: cs.zone,
208214
Region: zone.Region,
209215
}, nil
210216
}
217+
218+
func (cs *CSCloud) getProviderIDFromInstanceID(instanceID string) string {
219+
return fmt.Sprintf("%s://%s", cs.ProviderName(), instanceID)
220+
}
221+
222+
func (cs *CSCloud) getInstanceIDFromProviderID(providerID string) string {
223+
parts := strings.Split(providerID, "://")
224+
if len(parts) == 1 {
225+
return providerID
226+
}
227+
return parts[1]
228+
}

0 commit comments

Comments
 (0)