-
Notifications
You must be signed in to change notification settings - Fork 558
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #681 from jonathanrainer/fix-imds-dependency
Removing Dependency on IMDS, allowing `hostNetwork: true` to be removed
- Loading branch information
Showing
227 changed files
with
22,862 additions
and
167 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package cloud | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/aws/aws-sdk-go/aws/ec2metadata" | ||
) | ||
|
||
type EC2Metadata interface { | ||
Available() bool | ||
GetInstanceIdentityDocument() (ec2metadata.EC2InstanceIdentityDocument, error) | ||
} | ||
|
||
type ec2MetadataProvider struct { | ||
ec2MetadataService EC2Metadata | ||
} | ||
|
||
func (e ec2MetadataProvider) getMetadata() (MetadataService, error) { | ||
doc, err := e.ec2MetadataService.GetInstanceIdentityDocument() | ||
if err != nil { | ||
return nil, fmt.Errorf("could not get EC2 instance identity metadata") | ||
} | ||
|
||
if len(doc.InstanceID) == 0 { | ||
return nil, fmt.Errorf("could not get valid EC2 instance ID") | ||
} | ||
|
||
if len(doc.Region) == 0 { | ||
return nil, fmt.Errorf("could not get valid EC2 region") | ||
} | ||
|
||
if len(doc.AvailabilityZone) == 0 { | ||
return nil, fmt.Errorf("could not get valid EC2 availavility zone") | ||
} | ||
|
||
return &metadata{ | ||
instanceID: doc.InstanceID, | ||
region: doc.Region, | ||
availabilityZone: doc.AvailabilityZone, | ||
}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
package cloud | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/aws/aws-sdk-go/aws/ec2metadata" | ||
"github.com/golang/mock/gomock" | ||
|
||
"github.com/kubernetes-sigs/aws-efs-csi-driver/pkg/cloud/mocks" | ||
) | ||
|
||
var ( | ||
stdInstanceID = "instance-1" | ||
stdRegionName = "instance-1" | ||
stdAvailabilityZone = "az-1" | ||
) | ||
|
||
func TestRetrieveMetadataFromEC2MetadataService(t *testing.T) { | ||
testCases := []struct { | ||
name string | ||
isAvailable bool | ||
isPartial bool | ||
identityDocument ec2metadata.EC2InstanceIdentityDocument | ||
err error | ||
}{ | ||
{ | ||
name: "success: normal", | ||
isAvailable: true, | ||
identityDocument: ec2metadata.EC2InstanceIdentityDocument{ | ||
InstanceID: stdInstanceID, | ||
Region: stdRegionName, | ||
AvailabilityZone: stdAvailabilityZone, | ||
}, | ||
err: nil, | ||
}, | ||
{ | ||
name: "fail: GetInstanceIdentityDocument returned error", | ||
isAvailable: true, | ||
identityDocument: ec2metadata.EC2InstanceIdentityDocument{ | ||
InstanceID: stdInstanceID, | ||
Region: stdRegionName, | ||
AvailabilityZone: stdAvailabilityZone, | ||
}, | ||
err: fmt.Errorf(""), | ||
}, | ||
{ | ||
name: "fail: GetInstanceIdentityDocument returned empty instance", | ||
isAvailable: true, | ||
isPartial: true, | ||
identityDocument: ec2metadata.EC2InstanceIdentityDocument{ | ||
InstanceID: "", | ||
Region: stdRegionName, | ||
AvailabilityZone: stdAvailabilityZone, | ||
}, | ||
err: nil, | ||
}, | ||
{ | ||
name: "fail: GetInstanceIdentityDocument returned empty region", | ||
isAvailable: true, | ||
isPartial: true, | ||
identityDocument: ec2metadata.EC2InstanceIdentityDocument{ | ||
InstanceID: stdInstanceID, | ||
Region: "", | ||
AvailabilityZone: stdAvailabilityZone, | ||
}, | ||
err: nil, | ||
}, | ||
{ | ||
name: "fail: GetInstanceIdentityDocument returned empty az", | ||
isAvailable: true, | ||
isPartial: true, | ||
identityDocument: ec2metadata.EC2InstanceIdentityDocument{ | ||
InstanceID: stdInstanceID, | ||
Region: stdRegionName, | ||
AvailabilityZone: "", | ||
}, | ||
err: nil, | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
mockCtrl := gomock.NewController(t) | ||
mockEC2Metadata := mocks.NewMockEC2Metadata(mockCtrl) | ||
|
||
if tc.isAvailable { | ||
mockEC2Metadata.EXPECT().GetInstanceIdentityDocument().Return(tc.identityDocument, tc.err) | ||
} | ||
|
||
ec2Mp := ec2MetadataProvider{ec2MetadataService: mockEC2Metadata} | ||
m, err := ec2Mp.getMetadata() | ||
|
||
if tc.isAvailable && tc.err == nil && !tc.isPartial { | ||
if err != nil { | ||
t.Fatalf("getEC2Metadata() failed: expected no error, got %v", err) | ||
} | ||
|
||
if m.GetInstanceID() != tc.identityDocument.InstanceID { | ||
t.Fatalf("GetInstanceID() failed: expected %v, got %v", tc.identityDocument.InstanceID, m.GetInstanceID()) | ||
} | ||
|
||
if m.GetRegion() != tc.identityDocument.Region { | ||
t.Fatalf("GetRegion() failed: expected %v, got %v", tc.identityDocument.Region, m.GetRegion()) | ||
} | ||
|
||
if m.GetAvailabilityZone() != tc.identityDocument.AvailabilityZone { | ||
t.Fatalf("GetAvailabilityZone() failed: expected %v, got %v", tc.identityDocument.AvailabilityZone, m.GetAvailabilityZone()) | ||
} | ||
} else { | ||
if err == nil { | ||
t.Fatal("getEC2Metadata() failed: expected error when GetInstanceIdentityDocument returns partial data, got nothing") | ||
} | ||
} | ||
|
||
mockCtrl.Finish() | ||
}) | ||
} | ||
} |
Oops, something went wrong.