-
Notifications
You must be signed in to change notification settings - Fork 522
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
259 additions
and
12 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,84 @@ | ||
/* | ||
Copyright 2021 The Kubernetes Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package cloud | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"github.com/kubernetes-sigs/aws-efs-csi-driver/pkg/util" | ||
"net/http" | ||
"os" | ||
"strings" | ||
"time" | ||
) | ||
|
||
const ( | ||
taskMetadataV4EnvName = "ECS_CONTAINER_METADATA_URI_V4" | ||
) | ||
|
||
type TaskMetadataService interface { | ||
GetTMDSV4Response() ([]byte, error) | ||
} | ||
|
||
type taskMetadata struct { | ||
} | ||
|
||
type TMDSV4Response struct { | ||
Cluster string `json:"Cluster"` | ||
TaskARN string `json:"TaskARN"` | ||
AvailabilityZone string `json:"AvailabilityZone"` | ||
} | ||
|
||
func (taskMetadata taskMetadata) GetTMDSV4Response() ([]byte, error) { | ||
client := &http.Client{ | ||
Timeout: 5 * time.Second, | ||
} | ||
metadataUrl := os.Getenv(taskMetadataV4EnvName) | ||
if metadataUrl == "" { | ||
return nil, fmt.Errorf("unable to get taskMetadataV4 environment variable") | ||
} | ||
respBody, err := util.GetHttpResponse(client, metadataUrl+"/task") | ||
if err != nil { | ||
return nil, fmt.Errorf("unable to get task metadata response: %v", err) | ||
} | ||
|
||
return respBody, nil | ||
} | ||
|
||
// getTaskMetadata return a new ECS MetadataServiceImplementation | ||
func getTaskMetadata(svc TaskMetadataService) (MetadataService, error) { | ||
metadataResp, err := svc.GetTMDSV4Response() | ||
if err != nil { | ||
return nil, fmt.Errorf("unable to get TaskMetadataService %v", err) | ||
} | ||
|
||
tmdsResp := &TMDSV4Response{} | ||
err = json.Unmarshal(metadataResp, tmdsResp) | ||
if err != nil { | ||
return nil, fmt.Errorf("unable to parse task metadata response body %v", metadataResp) | ||
} | ||
taskSplit := strings.Split(tmdsResp.TaskARN, "/") | ||
taskId := taskSplit[len(taskSplit)-1] | ||
az := tmdsResp.AvailabilityZone | ||
region := az[:len(az)-1] | ||
return &metadata{ | ||
// does not need, but taskId would be a unique better choice | ||
instanceID: taskId, | ||
availabilityZone: az, | ||
region: region, | ||
}, 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,78 @@ | ||
package cloud | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"github.com/golang/mock/gomock" | ||
"github.com/kubernetes-sigs/aws-efs-csi-driver/pkg/cloud/mocks" | ||
"testing" | ||
) | ||
|
||
var ( | ||
clusterId = "default" | ||
taskId = "158d1c8083dd49d6b527399fd6414f5c" | ||
region = "us-west-2" | ||
az = fmt.Sprintf(`%sa`, region) | ||
taskArn = fmt.Sprintf(`arn:aws:ecs:us-west-2:111122223333:task/%s/%s`, clusterId, taskId) | ||
) | ||
|
||
func TestGetTaskMetadataService(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
returnTMDSV4Response TMDSV4Response | ||
err error | ||
}{ | ||
{ | ||
"success: normal", | ||
TMDSV4Response{ | ||
Cluster: clusterId, | ||
TaskARN: taskArn, | ||
AvailabilityZone: az, | ||
}, | ||
nil, | ||
}, | ||
{ | ||
"fail: GetTMDSV4Response returned error", | ||
TMDSV4Response{ | ||
Cluster: clusterId, | ||
TaskARN: taskArn, | ||
AvailabilityZone: az, | ||
}, | ||
fmt.Errorf(""), | ||
}, | ||
} | ||
for _, tc := range tests { | ||
t.Run(tc.name, func(t *testing.T) { | ||
mockCtrl := gomock.NewController(t) | ||
defer mockCtrl.Finish() | ||
|
||
mockTaskMetadata := mocks.NewMockTaskMetadataService(mockCtrl) | ||
jsonData, _ := json.Marshal(tc.returnTMDSV4Response) | ||
mockTaskMetadata.EXPECT().GetTMDSV4Response().Return(jsonData, tc.err) | ||
|
||
m, err := getTaskMetadata(mockTaskMetadata) | ||
|
||
if tc.err == nil { | ||
if err != nil { | ||
t.Fatalf("getTaskMetadata failed: expected no error, got %v", err) | ||
} | ||
|
||
if m.GetInstanceID() != taskId { | ||
t.Fatalf("GetInstanceID() failed: expeted %v, got %v", taskId, m.GetInstanceID()) | ||
} | ||
|
||
if m.GetRegion() != region { | ||
t.Fatalf("GetRegion() failed: expeted %v, got %v", region, m.GetRegion()) | ||
} | ||
|
||
if m.GetAvailabilityZone() != az { | ||
t.Fatalf("GetAvailabilityZone() failed: expeted %v, got %v", az, m.GetAvailabilityZone()) | ||
} | ||
} else { | ||
if err == nil { | ||
t.Fatalf("getTaskMetadata() failed: expected error") | ||
} | ||
} | ||
}) | ||
} | ||
} |
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