Skip to content

Commit 1c9b988

Browse files
committed
Validate cloud provider id and return error
1 parent 645b367 commit 1c9b988

File tree

3 files changed

+39
-5
lines changed

3 files changed

+39
-5
lines changed

pkg/cloudprovider/aws/aws.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,15 @@ func instanceToProviderID(instance *autoscaling.Instance) string {
4040
return fmt.Sprintf("aws:///%s/%s", *instance.AvailabilityZone, *instance.InstanceId)
4141
}
4242

43-
func providerIDToInstanceID(providerID string) string {
44-
return strings.Split(providerID, "/")[4]
43+
func providerIDToInstanceID(providerID string) (string, error) {
44+
if providerID == "" {
45+
return "", fmt.Errorf("empty providerID, it may be set later by cloud controller")
46+
}
47+
parts := strings.Split(providerID, "/")
48+
if len(parts) < 5 {
49+
return "", fmt.Errorf("malformed providerID %s: expected at least 4 slashes", providerID)
50+
}
51+
return strings.Split(providerID, "/")[4], nil
4552
}
4653

4754
// CloudProvider providers an aws cloud provider implementation
@@ -136,8 +143,10 @@ type Instance struct {
136143
func (c *CloudProvider) GetInstance(node *v1.Node) (cloudprovider.Instance, error) {
137144
var instance *Instance
138145

139-
id := providerIDToInstanceID(node.Spec.ProviderID)
140-
146+
id, err := providerIDToInstanceID(node.Spec.ProviderID)
147+
if err != nil {
148+
return instance, errors.New("no cloud provider id found, it may be set later by cloud controller")
149+
}
141150
input := &ec2.DescribeInstancesInput{
142151
InstanceIds: []*string{&id},
143152
}

pkg/cloudprovider/aws/aws_test.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,21 @@ func TestInstanceToProviderId(t *testing.T) {
7070
}
7171

7272
func TestProviderIdToInstanceId(t *testing.T) {
73-
assert.Equal(t, "abc123", providerIDToInstanceID("aws:///us-east-1b/abc123"))
73+
id, err := providerIDToInstanceID("aws:///us-east-1b/abc123")
74+
assert.Nil(t, err)
75+
assert.Equal(t, "abc123", id)
76+
}
77+
78+
func TestProviderIdToInstanceIdEmpty(t *testing.T) {
79+
_, err := providerIDToInstanceID("")
80+
assert.NotNil(t, err)
81+
assert.Contains(t, err.Error(), "empty providerID, it may be set later by cloud controller")
82+
}
83+
84+
func TestProviderIdToInstanceIdMalformed(t *testing.T) {
85+
_, err := providerIDToInstanceID("fake://provider/id")
86+
assert.NotNil(t, err)
87+
assert.Contains(t, err.Error(), "malformed providerID fake://provider/id: expected at least 4 slashes")
7488
}
7589

7690
func newMockCloudProvider(nodeGroups []string, service *test.MockAutoscalingService, ec2Service *test.MockEc2Service) (*CloudProvider, error) {

pkg/cloudprovider/aws/cloud_provider_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,3 +308,14 @@ func TestCloudProvider_GetInstance(t *testing.T) {
308308
})
309309
}
310310
}
311+
312+
func TestCloudProvider_GetInstance_No_Provider_ID(t *testing.T) {
313+
nodeGroups := []string{"1"}
314+
node := &v1.Node{
315+
Spec: v1.NodeSpec{},
316+
}
317+
ec2Service := &test.MockEc2Service{}
318+
awsCloudProvider, err := newMockCloudProvider(nodeGroups, nil, ec2Service)
319+
_, err = awsCloudProvider.GetInstance(node)
320+
assert.NotNil(t, err)
321+
}

0 commit comments

Comments
 (0)