Skip to content

Commit 82bdba9

Browse files
committed
cloud provider: remove redundant implementation of InstanceMetadataByProviderID since instanceV2 is disabled for all providers
Signed-off-by: Andrew Sy Kim <[email protected]>
1 parent cc1e0d4 commit 82bdba9

File tree

9 files changed

+5
-534
lines changed

9 files changed

+5
-534
lines changed

staging/src/k8s.io/legacy-cloud-providers/aws/aws.go

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1395,6 +1395,7 @@ func (c *Cloud) Instances() (cloudprovider.Instances, bool) {
13951395
}
13961396

13971397
// InstancesV2 returns an implementation of InstancesV2 for Amazon Web Services.
1398+
// TODO: implement ONLY for external cloud provider
13981399
func (c *Cloud) InstancesV2() (cloudprovider.InstancesV2, bool) {
13991400
return nil, false
14001401
}
@@ -1670,31 +1671,6 @@ func (c *Cloud) InstanceShutdownByProviderID(ctx context.Context, providerID str
16701671
return false, nil
16711672
}
16721673

1673-
// InstanceMetadataByProviderID returns metadata of the specified instance.
1674-
func (c *Cloud) InstanceMetadataByProviderID(ctx context.Context, providerID string) (*cloudprovider.InstanceMetadata, error) {
1675-
instanceID, err := KubernetesInstanceID(providerID).MapToAWSInstanceID()
1676-
if err != nil {
1677-
return nil, err
1678-
}
1679-
1680-
instance, err := describeInstance(c.ec2, instanceID)
1681-
if err != nil {
1682-
return nil, err
1683-
}
1684-
1685-
// TODO ignore checking whether `*instance.State.Name == ec2.InstanceStateNameTerminated` here.
1686-
// If not behave as expected, add it.
1687-
addresses, err := extractNodeAddresses(instance)
1688-
if err != nil {
1689-
return nil, err
1690-
}
1691-
return &cloudprovider.InstanceMetadata{
1692-
ProviderID: providerID,
1693-
Type: aws.StringValue(instance.InstanceType),
1694-
NodeAddresses: addresses,
1695-
}, nil
1696-
}
1697-
16981674
// InstanceID returns the cloud provider ID of the node with the specified nodeName.
16991675
func (c *Cloud) InstanceID(ctx context.Context, nodeName types.NodeName) (string, error) {
17001676
// In the future it is possible to also return an endpoint as:

staging/src/k8s.io/legacy-cloud-providers/aws/aws_test.go

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -681,23 +681,6 @@ func TestNodeAddressesWithMetadata(t *testing.T) {
681681
}
682682
}
683683

684-
func TestInstanceMetadataByProviderID(t *testing.T) {
685-
instance0 := makeInstance(0, "192.168.0.1", "1.2.3.4", "instance-same.ec2.internal", "instance-same.ec2.external", true)
686-
aws1, _ := mockInstancesResp(&instance0, []*ec2.Instance{&instance0})
687-
// change node name so it uses the instance instead of metadata
688-
aws1.selfAWSInstance.nodeName = "foo"
689-
690-
md, err := aws1.InstanceMetadataByProviderID(context.TODO(), "/us-east-1a/i-0")
691-
if err != nil {
692-
t.Errorf("should not error when instance found")
693-
}
694-
if md.ProviderID != "/us-east-1a/i-0" || md.Type != "c3.large" {
695-
t.Errorf("expect providerID %s get %s, expect type %s get %s", "/us-east-1a/i-0", md.ProviderID, "c3.large", md.Type)
696-
}
697-
testHasNodeAddress(t, md.NodeAddresses, v1.NodeInternalIP, "192.168.0.1")
698-
testHasNodeAddress(t, md.NodeAddresses, v1.NodeExternalIP, "1.2.3.4")
699-
}
700-
701684
func TestParseMetadataLocalHostname(t *testing.T) {
702685
tests := []struct {
703686
name string

staging/src/k8s.io/legacy-cloud-providers/azure/azure.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -665,6 +665,7 @@ func (az *Cloud) Instances() (cloudprovider.Instances, bool) {
665665
}
666666

667667
// InstancesV2 returns an instancesV2 interface. Also returns true if the interface is supported, false otherwise.
668+
// TODO: implement ONLY for external cloud provider
668669
func (az *Cloud) InstancesV2() (cloudprovider.InstancesV2, bool) {
669670
return nil, false
670671
}

staging/src/k8s.io/legacy-cloud-providers/azure/azure_instances.go

Lines changed: 0 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -239,80 +239,6 @@ func (az *Cloud) InstanceShutdownByProviderID(ctx context.Context, providerID st
239239
return status == vmPowerStateStopped || status == vmPowerStateDeallocated || status == vmPowerStateDeallocating, nil
240240
}
241241

242-
// InstanceMetadataByProviderID returns metadata of the specified instance.
243-
// InstanceMetadataByProviderID is part of InstancesV2 interface and is only used in cloud node-controller.
244-
func (az *Cloud) InstanceMetadataByProviderID(ctx context.Context, providerID string) (*cloudprovider.InstanceMetadata, error) {
245-
if providerID == "" {
246-
return nil, errNodeNotInitialized
247-
}
248-
249-
// Returns nil for unmanaged nodes because azure cloud provider couldn't fetch information for them.
250-
if az.IsNodeUnmanagedByProviderID(providerID) {
251-
klog.V(4).Infof("NodeAddressesByProviderID: omitting unmanaged node %q", providerID)
252-
return nil, nil
253-
}
254-
255-
nodeName, err := az.vmSet.GetNodeNameByProviderID(providerID)
256-
if err != nil {
257-
return nil, err
258-
}
259-
260-
md := &cloudprovider.InstanceMetadata{}
261-
md.ProviderID = providerID
262-
if az.UseInstanceMetadata {
263-
metadata, err := az.metadata.GetMetadata(azcache.CacheReadTypeDefault)
264-
if err != nil {
265-
return nil, err
266-
}
267-
268-
if metadata.Compute == nil || metadata.Network == nil {
269-
return nil, fmt.Errorf("failure of getting instance metadata")
270-
}
271-
272-
isLocalInstance, err := az.isCurrentInstance(nodeName, metadata.Compute.Name)
273-
if err != nil {
274-
return nil, err
275-
}
276-
277-
// Not local instance, get metadata from Azure ARM API.
278-
if !isLocalInstance {
279-
if az.vmSet != nil {
280-
if md.Type, err = az.vmSet.GetInstanceTypeByNodeName(string(nodeName)); err != nil {
281-
return nil, err
282-
}
283-
if md.NodeAddresses, err = az.addressGetter(nodeName); err != nil {
284-
return nil, err
285-
}
286-
return md, nil
287-
}
288-
// vmSet == nil indicates credentials are not provided.
289-
return nil, fmt.Errorf("no credentials provided for Azure cloud provider")
290-
}
291-
292-
// Get instance metadata from IMDS for local instance.
293-
if metadata.Compute.VMSize != "" {
294-
md.Type = metadata.Compute.VMSize
295-
} else {
296-
if md.Type, err = az.vmSet.GetInstanceTypeByNodeName(string(nodeName)); err != nil {
297-
return nil, err
298-
}
299-
}
300-
if md.NodeAddresses, err = az.getLocalInstanceNodeAddresses(metadata.Network.Interface, string(nodeName)); err != nil {
301-
return nil, err
302-
}
303-
return md, nil
304-
}
305-
306-
// Get instance metadata from ARM API when UseInstanceMetadata is disabled.
307-
if md.Type, err = az.vmSet.GetInstanceTypeByNodeName(string(nodeName)); err != nil {
308-
return nil, err
309-
}
310-
if md.NodeAddresses, err = az.addressGetter(nodeName); err != nil {
311-
return nil, err
312-
}
313-
return md, err
314-
}
315-
316242
func (az *Cloud) isCurrentInstance(name types.NodeName, metadataVMName string) (bool, error) {
317243
var err error
318244
nodeName := mapNodeNameToVMName(name)

0 commit comments

Comments
 (0)