Skip to content

Commit

Permalink
requested changes
Browse files Browse the repository at this point in the history
  • Loading branch information
vdhanan authored and Varun Dhananjaya committed Apr 27, 2021
1 parent 2e93d5c commit 44161e7
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 17 deletions.
10 changes: 7 additions & 3 deletions pkg/cloud/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
33 changes: 19 additions & 14 deletions pkg/cloud/metadata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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",
Expand All @@ -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,
},

{
Expand Down Expand Up @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand Down Expand Up @@ -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")
Expand Down

0 comments on commit 44161e7

Please sign in to comment.