From f6137e5ec7c196a89fa3fb98794bfdf1f3cf02f2 Mon Sep 17 00:00:00 2001 From: Varun Dhananjaya Date: Tue, 27 Apr 2021 16:50:27 -0700 Subject: [PATCH] requested changes --- pkg/cloud/metadata.go | 10 +++++++--- pkg/cloud/metadata_test.go | 33 +++++++++++++++++++-------------- 2 files changed, 26 insertions(+), 17 deletions(-) diff --git a/pkg/cloud/metadata.go b/pkg/cloud/metadata.go index d2c8b0c01d..80085ad7de 100644 --- a/pkg/cloud/metadata.go +++ b/pkg/cloud/metadata.go @@ -77,15 +77,19 @@ func NewMetadata() (MetadataService, error) { svc := ec2metadata.New(sess) // creates the in-cluster config config, err := rest.InClusterConfig() - if err != nil { + if err != nil && !svc.Available() { return nil, err } // creates the clientset clientset, err := kubernetes.NewForConfig(config) - if err != nil { + if err != nil && !svc.Available() { return nil, err } - return NewMetadataService(svc, clientset) + metadataService, err := NewMetadataService(svc, clientset) + if err != nil { + return nil, fmt.Errorf("error getting information from metadata service or node object: %w", err) + } + return metadataService, err } // NewMetadataService returns a new MetadataServiceImplementation. diff --git a/pkg/cloud/metadata_test.go b/pkg/cloud/metadata_test.go index 6c061e61bb..11afa7b512 100644 --- a/pkg/cloud/metadata_test.go +++ b/pkg/cloud/metadata_test.go @@ -40,6 +40,11 @@ var ( stdAvailabilityZone = "az-1" ) +const ( + nodeName = "ip-123-45-67-890.us-west-2.compute.internal" + nodeObjectInstanceID = "i-abcdefgh123456789" +) + func TestNewMetadataService(t *testing.T) { validRawOutpostArn := "arn:aws:outposts:us-west-2:111111111111:outpost/op-0aaa000a0aaaa00a0" @@ -121,14 +126,14 @@ func TestNewMetadataService(t *testing.T) { APIVersion: "v1", }, ObjectMeta: metav1.ObjectMeta{ - Name: "ip-123-45-67-890.us-west-2.compute.internal", + Name: nodeName, }, Spec: v1.NodeSpec{ ProviderID: "aws:///us-west-2b/i-abcdefgh123456789", }, Status: v1.NodeStatus{}, }, - nodeNameEnvVar: "ip-123-45-67-890.us-west-2.compute.internal", + nodeNameEnvVar: nodeName, }, { name: "failure: metadata not available, k8s client error", @@ -141,7 +146,7 @@ func TestNewMetadataService(t *testing.T) { }, getInstanceDocErr: nil, getNodeErr: fmt.Errorf("client failure"), - nodeNameEnvVar: "ip-123-45-67-890.us-west-2.compute.internal", + nodeNameEnvVar: nodeName, }, { @@ -174,14 +179,14 @@ func TestNewMetadataService(t *testing.T) { APIVersion: "v1", }, ObjectMeta: metav1.ObjectMeta{ - Name: "ip-123-45-67-890.us-west-2.compute.internal", + Name: nodeName, }, Spec: v1.NodeSpec{ ProviderID: "", }, Status: v1.NodeStatus{}, }, - nodeNameEnvVar: "ip-123-45-67-890.us-west-2.compute.internal", + nodeNameEnvVar: nodeName, }, { name: "failure: metadata not available, invalid region", @@ -200,14 +205,14 @@ func TestNewMetadataService(t *testing.T) { APIVersion: "v1", }, ObjectMeta: metav1.ObjectMeta{ - Name: "ip-123-45-67-890.us-west-2.compute.internal", + Name: nodeName, }, Spec: v1.NodeSpec{ ProviderID: "aws:///us-est-2b/i-abcdefgh123456789", // invalid region }, Status: v1.NodeStatus{}, }, - nodeNameEnvVar: "ip-123-45-67-890.us-west-2.compute.internal", + nodeNameEnvVar: nodeName, }, { name: "failure: metadata not available, invalid az", @@ -226,14 +231,14 @@ func TestNewMetadataService(t *testing.T) { APIVersion: "v1", }, ObjectMeta: metav1.ObjectMeta{ - Name: "ip-123-45-67-890.us-west-2.compute.internal", + Name: nodeName, }, Spec: v1.NodeSpec{ ProviderID: "aws:///us-west-21/i-abcdefgh123456789", // invalid AZ }, Status: v1.NodeStatus{}, }, - nodeNameEnvVar: "ip-123-45-67-890.us-west-2.compute.internal", + nodeNameEnvVar: nodeName, }, { name: "failure: metadata not available, invalid instance id", @@ -252,14 +257,14 @@ func TestNewMetadataService(t *testing.T) { APIVersion: "v1", }, ObjectMeta: metav1.ObjectMeta{ - Name: "ip-123-45-67-890.us-west-2.compute.internal", + Name: nodeName, }, Spec: v1.NodeSpec{ - ProviderID: "aws:///us-west-2b/i-", + ProviderID: "aws:///us-west-2b/i-", // invalid instance ID }, Status: v1.NodeStatus{}, }, - nodeNameEnvVar: "ip-123-45-67-890.us-west-2.compute.internal", + nodeNameEnvVar: nodeName, }, { name: "fail: GetInstanceIdentityDocument returned error", @@ -373,8 +378,8 @@ func TestNewMetadataService(t *testing.T) { if err != nil { t.Fatalf("NewMetadataService() failed: expected no error, got %v", err) } - if m.GetInstanceID() != "i-abcdefgh123456789" { - t.Fatalf("NewMetadataService() failed: got wrong instance ID %v, expected %v", m.GetInstanceID(), "i-abcdefgh123456789") + if m.GetInstanceID() != nodeObjectInstanceID { + t.Fatalf("NewMetadataService() failed: got wrong instance ID %v, expected %v", m.GetInstanceID(), nodeObjectInstanceID) } if m.GetRegion() != "us-west-2" { t.Fatalf("NewMetadataService() failed: got wrong region %v, expected %v", m.GetRegion(), "us-west-2")