fix(inputs.ecs): Correct v4 metadata URLs (#14294)

This commit is contained in:
Joshua Powers 2023-11-15 02:10:21 -07:00 committed by GitHub
parent 05bb2777de
commit 004f81651b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 15 additions and 11 deletions

View File

@ -12,12 +12,13 @@ import (
var ( var (
// https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task-metadata-endpoint-v2.html // https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task-metadata-endpoint-v2.html
ecsMetadataPath = "/v2/metadata" ecsMetadataPathV2 = "/v2/metadata"
ecsMetaStatsPath = "/v2/stats" ecsMetaStatsPathV2 = "/v2/stats"
// https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task-metadata-endpoint-v3.html // https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task-metadata-endpoint-v3.html
ecsMetadataPathV3 = "/task" // https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task-metadata-endpoint-v4.html
ecsMetaStatsPathV3 = "/task/stats" ecsMetadataPath = "/task"
ecsMetaStatsPath = "/task/stats"
) )
// Client is the ECS client contract // Client is the ECS client contract
@ -32,8 +33,8 @@ type httpClient interface {
// NewClient constructs an ECS client with the passed configuration params // NewClient constructs an ECS client with the passed configuration params
func NewClient(timeout time.Duration, endpoint string, version int) (*EcsClient, error) { func NewClient(timeout time.Duration, endpoint string, version int) (*EcsClient, error) {
if version != 2 && version != 3 { if version < 2 || version > 4 {
const msg = "expected metadata version 2 or 3, got %d" const msg = "expected metadata version 2, 3 or 4, got %d"
return nil, fmt.Errorf(msg, version) return nil, fmt.Errorf(msg, version)
} }
@ -59,11 +60,12 @@ func resolveTaskURL(base *url.URL, version int) string {
var path string var path string
switch version { switch version {
case 2: case 2:
path = ecsMetadataPath path = ecsMetadataPathV2
case 3: case 3:
path = ecsMetadataPathV3 path = ecsMetadataPath
case 4:
path = ecsMetadataPath
default: default:
// Should never happen.
const msg = "resolveTaskURL: unexpected version %d" const msg = "resolveTaskURL: unexpected version %d"
panic(fmt.Errorf(msg, version)) panic(fmt.Errorf(msg, version))
} }
@ -74,9 +76,11 @@ func resolveStatsURL(base *url.URL, version int) string {
var path string var path string
switch version { switch version {
case 2: case 2:
path = ecsMetaStatsPath path = ecsMetaStatsPathV2
case 3: case 3:
path = ecsMetaStatsPathV3 path = ecsMetaStatsPath
case 4:
path = ecsMetaStatsPath
default: default:
// Should never happen. // Should never happen.
const msg = "resolveStatsURL: unexpected version %d" const msg = "resolveStatsURL: unexpected version %d"