-
Notifications
You must be signed in to change notification settings - Fork 207
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add scrapping ECS_CONTAINER_METADATA_URI_V4 for ECS #453
Changes from all commits
41bcb1a
771ab69
4a3d227
dc97cff
13ca933
bb5ec63
39d79d1
8302a6e
a080fcd
16d8af8
8713b5b
d750aed
5644636
d77b840
50c4774
e28fef6
a483a6f
90ab672
e013ce1
a5737cc
84f36d8
5d6f694
866f1b5
38845aa
1bbd660
c3bfe4e
b328b91
30098af
9d414c9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,7 @@ import ( | |
const ( | ||
v2MetadataEndpoint = "http://169.254.170.2/v2/metadata" | ||
v3MetadataEndpointEnv = "ECS_CONTAINER_METADATA_URI" | ||
v4MetadataEndpointEnv = "ECS_CONTAINER_METADATA_URI_V4" | ||
) | ||
|
||
type ecsMetadataResponse struct { | ||
|
@@ -32,6 +33,7 @@ type ecsUtil struct { | |
} | ||
|
||
var ecsUtilInstance *ecsUtil | ||
|
||
var ecsUtilOnce sync.Once | ||
|
||
func GetECSUtilSingleton() *ecsUtil { | ||
|
@@ -48,13 +50,14 @@ func initECSUtilSingleton() (newInstance *ecsUtil) { | |
} | ||
log.Println("I! attempt to access ECS task metadata to determine whether I'm running in ECS.") | ||
ecsMetadataResponse, err := newInstance.getECSMetadata() | ||
|
||
if err != nil { | ||
log.Printf("I! access ECS task metadata fail with response %v, assuming I'm not running in ECS.\n", err) | ||
return | ||
} | ||
|
||
newInstance.parseRegion(ecsMetadataResponse) | ||
newInstance.Cluster = ecsMetadataResponse.Cluster | ||
newInstance.parseClusterName(ecsMetadataResponse) | ||
newInstance.TaskARN = ecsMetadataResponse.TaskARN | ||
return | ||
|
||
|
@@ -65,26 +68,29 @@ func (e *ecsUtil) IsECS() bool { | |
} | ||
|
||
func (e *ecsUtil) getECSMetadata() (em *ecsMetadataResponse, err error) { | ||
// choose available endpoint | ||
if v3MetadataEndpoint, ok := os.LookupEnv(v3MetadataEndpointEnv); !ok { | ||
em, err = e.getMetadataResponse(v2MetadataEndpoint) | ||
} else { | ||
// Based on endpoint to get ECS metadata, for more information on the respond, https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task-metadata-endpoint.html | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we still need to support v2 and v3? If endpoint is there but get metadata fails should we fall back? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IDC either way. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we still need to support v2 and v3 so long as people can create ECS clusters that still rely on them. I think we'd have to investigate further where the drop off points are for v2 and v3 before we could take out support for them. |
||
if v4MetadataEndpoint, ok := os.LookupEnv(v4MetadataEndpointEnv); ok { | ||
em, err = e.getMetadataResponse(v4MetadataEndpoint + "/task") | ||
khanhntd marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} else if v3MetadataEndpoint, ok := os.LookupEnv(v3MetadataEndpointEnv); ok { | ||
em, err = e.getMetadataResponse(v3MetadataEndpoint + "/task") | ||
} else { | ||
em, err = e.getMetadataResponse(v2MetadataEndpoint) | ||
} | ||
return | ||
} | ||
|
||
func (e *ecsUtil) getMetadataResponse(endpoint string) (em *ecsMetadataResponse, err error) { | ||
em = &ecsMetadataResponse{} | ||
resp, err := e.httpClient.Request(endpoint) | ||
|
||
if err != nil { | ||
return | ||
} | ||
|
||
err = json.Unmarshal(resp, em) | ||
if err != nil { | ||
log.Printf("E! unable to parse resp from ecsmetadata endpoint, error: %v", err) | ||
log.Printf("D! resp content is %s", string(resp)) | ||
log.Printf("E! Unable to parse response from ecsmetadata endpoint, error: %v", err) | ||
log.Printf("D! Content is %s", string(resp)) | ||
} | ||
return | ||
} | ||
|
@@ -97,7 +103,18 @@ func (e *ecsUtil) parseRegion(em *ecsMetadataResponse) { | |
splitedContent := strings.Split(em.TaskARN, ":") | ||
// When splitting the ARN with ":", the 4th segment is the region | ||
if len(splitedContent) < 4 { | ||
log.Printf("E! invalid ecs task arn: %s", em.TaskARN) | ||
log.Printf("E! Invalid ecs task arn: %s", em.TaskARN) | ||
} | ||
e.Region = splitedContent[3] | ||
} | ||
|
||
// There is only one format for ClusterArn (https://docs.aws.amazon.com/AmazonECS/latest/APIReference/API_Cluster.html) | ||
// arn:aws:ecs:region:aws_account_id:cluster/cluster-name | ||
func (e *ecsUtil) parseClusterName(em *ecsMetadataResponse) { | ||
splitedContent := strings.Split(em.Cluster, "/") | ||
// When splitting the ClusterName with /, the last is always the cluster name | ||
if len(splitedContent) == 0 { | ||
log.Printf("E! Invalid cluster arn: %s", em.Cluster) | ||
} | ||
Comment on lines
+116
to
+118
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If the ARN is invalid, should we still try to assign the cluster ARN to the last element? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Never mind. I see it in the old code. I would feel better if it was ported over more accurately to the existing code in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wrote this code based on behavior of parsing Region from taskARN. However, I have not had more information regarding this to change behavior. However, for one short notice is that even though the |
||
e.Cluster = splitedContent[len(splitedContent)-1] | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Personally, I prefer explicitly setting the value like it did originally rather than expecting that the
parseClusterName
sets the value for theCluster
field, but I'm fine with this.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would prefer the same since its does not directly use the Cluster Name but the
Cluster ARN
in this case. Therefore, implicitly setting the value would be fine as long as its clarify the meaning though.