Skip to content

Commit a2d7d43

Browse files
author
Jeff Bornemann
committed
Runtime checks around providerID
1 parent 0c1466e commit a2d7d43

File tree

5 files changed

+50
-15
lines changed

5 files changed

+50
-15
lines changed

pkg/oci/instances.go

+12-3
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,10 @@ func (cp *CloudProvider) NodeAddresses(ctx context.Context, name types.NodeName)
9797
// in this method to obtain nodeaddresses.
9898
func (cp *CloudProvider) NodeAddressesByProviderID(ctx context.Context, providerID string) ([]api.NodeAddress, error) {
9999
glog.V(4).Infof("NodeAddressesByProviderID(%q) called", providerID)
100-
instanceID := util.MapProviderIDToInstanceID(providerID)
100+
instanceID, err := util.MapProviderIDToInstanceID(providerID)
101+
if err != nil {
102+
return nil, errors.Wrap(err, "MapProviderIDToInstanceID")
103+
}
101104
vnic, err := cp.client.Compute().GetPrimaryVNICForInstance(ctx, cp.config.CompartmentID, instanceID)
102105
if err != nil {
103106
return nil, errors.Wrap(err, "GetPrimaryVNICForInstance")
@@ -156,7 +159,10 @@ func (cp *CloudProvider) InstanceType(ctx context.Context, name types.NodeName)
156159
func (cp *CloudProvider) InstanceTypeByProviderID(ctx context.Context, providerID string) (string, error) {
157160
glog.V(4).Infof("InstanceTypeByProviderID(%q) called", providerID)
158161

159-
instanceID := util.MapProviderIDToInstanceID(providerID)
162+
instanceID, err := util.MapProviderIDToInstanceID(providerID)
163+
if err != nil {
164+
return "", errors.Wrap(err, "MapProviderIDToInstanceID")
165+
}
160166
inst, err := cp.client.Compute().GetInstance(ctx, instanceID)
161167
if err != nil {
162168
return "", errors.Wrap(err, "GetInstance")
@@ -183,7 +189,10 @@ func (cp *CloudProvider) CurrentNodeName(ctx context.Context, hostname string) (
183189
// instance will be immediately deleted by the cloud controller manager.
184190
func (cp *CloudProvider) InstanceExistsByProviderID(ctx context.Context, providerID string) (bool, error) {
185191
glog.V(4).Infof("InstanceExistsByProviderID(%q) called", providerID)
186-
instanceID := util.MapProviderIDToInstanceID(providerID)
192+
instanceID, err := util.MapProviderIDToInstanceID(providerID)
193+
if err != nil {
194+
return false, err
195+
}
187196
instance, err := cp.client.Compute().GetInstance(ctx, instanceID)
188197
if client.IsNotFound(err) {
189198
return false, nil

pkg/oci/load_balancer.go

+9-1
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,15 @@ func getSubnetsForNodes(ctx context.Context, nodes []*v1.Node, client client.Int
148148
continue
149149
}
150150

151-
id := util.MapProviderIDToInstanceID(node.Spec.ProviderID)
151+
if node.Spec.ProviderID == "" {
152+
return nil, errors.Errorf("ProviderID was not present on node %q", node.Name)
153+
}
154+
155+
id, err := util.MapProviderIDToInstanceID(node.Spec.ProviderID)
156+
if err != nil {
157+
return nil, errors.Wrap(err, "MapProviderIDToInstanceID")
158+
}
159+
152160
vnic, err := client.Compute().GetPrimaryVNICForInstance(ctx, compartmentID, id)
153161
if err != nil {
154162
return nil, err

pkg/oci/util/util.go

+7-3
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"strings"
1919

2020
api "k8s.io/api/core/v1"
21+
"github.com/pkg/errors"
2122
)
2223

2324
const (
@@ -28,11 +29,14 @@ const (
2829
)
2930

3031
// MapProviderIDToInstanceID parses the provider id and returns the instance ocid.
31-
func MapProviderIDToInstanceID(providerID string) string {
32+
func MapProviderIDToInstanceID(providerID string) (string, error) {
33+
if providerID == "" {
34+
return providerID, errors.New("provider ID is empty")
35+
}
3236
if strings.HasPrefix(providerID, providerPrefix) {
33-
return strings.TrimPrefix(providerID, providerPrefix)
37+
return strings.TrimPrefix(providerID, providerPrefix), nil
3438
}
35-
return providerID
39+
return providerID, nil
3640
}
3741

3842
// NodeInternalIP returns the nodes internal ip

pkg/oci/util/util_test.go

+18-7
Original file line numberDiff line numberDiff line change
@@ -18,24 +18,35 @@ import "testing"
1818

1919
func TestMapProviderIDToInstanceID(t *testing.T) {
2020
testCases := map[string]struct {
21-
providerID string
22-
expected string
21+
providerID string
22+
instanceID string
23+
error bool
2324
}{
2425
"no cloud prefix": {
2526
providerID: "testid",
26-
expected: "testid",
27+
instanceID: "testid",
28+
error: false,
2729
},
2830
"cloud prefix": {
2931
providerID: providerPrefix + "testid",
30-
expected: "testid",
32+
instanceID: "testid",
33+
error: false,
34+
},
35+
"empty string": {
36+
providerID: "",
37+
instanceID: "",
38+
error: true,
3139
},
3240
}
3341

3442
for name, tc := range testCases {
3543
t.Run(name, func(t *testing.T) {
36-
result := MapProviderIDToInstanceID(tc.providerID)
37-
if result != tc.expected {
38-
t.Errorf("Expected instance id %q, but got %q", tc.expected, result)
44+
result, err := MapProviderIDToInstanceID(tc.providerID)
45+
if result != tc.instanceID {
46+
t.Errorf("Expected instance id %q, but got %q", tc.instanceID, result)
47+
}
48+
if (err == nil && tc.error) || (!tc.error && err != nil) {
49+
t.Errorf("Expected an error condition for input %q, but did no receive one; or received one, when not expecting", tc.providerID)
3950
}
4051
})
4152
}

pkg/oci/zones.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,10 @@ func (cp *CloudProvider) GetZone(ctx context.Context) (cloudprovider.Zone, error
4747
// particularly used in the context of external cloud providers where node
4848
// initialization must be down outside the kubelets.
4949
func (cp *CloudProvider) GetZoneByProviderID(ctx context.Context, providerID string) (cloudprovider.Zone, error) {
50-
instanceID := util.MapProviderIDToInstanceID(providerID)
50+
instanceID, err := util.MapProviderIDToInstanceID(providerID)
51+
if err != nil {
52+
return cloudprovider.Zone{}, err
53+
}
5154
instance, err := cp.client.Compute().GetInstance(ctx, instanceID)
5255
if err != nil {
5356
return cloudprovider.Zone{}, err

0 commit comments

Comments
 (0)