diff --git a/cloud-controller-manager/osc/instances_v2.go b/cloud-controller-manager/osc/instances_v2.go index bf4092a2..20ebaffa 100644 --- a/cloud-controller-manager/osc/instances_v2.go +++ b/cloud-controller-manager/osc/instances_v2.go @@ -245,6 +245,11 @@ func parseInstanceIDFromProviderIDV2(providerID string) (string, error) { // * // // * instanceID := "" + if !strings.HasPrefix(providerID, "i-") && !strings.HasPrefix(providerID, "aws://") { + // providerID does not have the expected prefix + return "", errors.New("invalid providerID format: missing 'aws://' prefix or 'i-' prefix") + } + metadata := strings.Split(strings.TrimPrefix(providerID, "aws://"), "/") if len(metadata) == 1 { // instance-id @@ -255,6 +260,11 @@ func parseInstanceIDFromProviderIDV2(providerID string) (string, error) { } else if len(metadata) == 3 { // /az/instance-id instanceID = metadata[2] + } else { + return "", errors.New("invalid providerID format") + } + if instanceID == "" { + return "", fmt.Errorf("instance ID not found in providerID: %s", providerID) } return instanceID, nil diff --git a/cloud-controller-manager/osc/osc_test.go b/cloud-controller-manager/osc/osc_test.go index dd894104..ffff83bf 100644 --- a/cloud-controller-manager/osc/osc_test.go +++ b/cloud-controller-manager/osc/osc_test.go @@ -1803,6 +1803,20 @@ func TestNodeNameToProviderID(t *testing.T) { assert.NoError(t, err) _, err = c.nodeNameToProviderID(testNodeName) assert.NoError(t, err) + + // Test case for invalid providerID format + testInvalidProviderID := "invalid-format" + err = c.nodeInformer.Informer().GetStore().Add(&v1.Node{ + ObjectMeta: metav1.ObjectMeta{ + Name: string(testNodeName) + "-invalid", + }, + Spec: v1.NodeSpec{ + ProviderID: testInvalidProviderID, + }, + }) + assert.NoError(t, err) + _, err = c.nodeNameToProviderID(types.NodeName(string(testNodeName) + "-invalid")) + assert.Error(t, err) } func informerSynced() bool {