metadata

package module
v0.0.0-...-67e37ae Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 21, 2022 License: MIT Imports: 7 Imported by: 8

README

AWS ECS Metadata Go

A minimal wrapper library to fetch Elastic Container Service (ECS) Task metadata from any Go service running in container provisioned by AWS Fargate.

Based on the Fargate platform version, you'll have access to different versions of the Task Metadata Endpoint. If you're running on 1.4.0, you'll be able to access Version 4, Fargate 1.3.0 and later support Version 3 of the endpoint.

installation

go get github.com/brunoscheufler/aws-ecs-metadata-go

usage

This library allows you to retrieve the most recent metadata format available in your environment based on the environment variables Fargate will provide. This means, that using GetTaskMetadata you'll receive an empty interface which maps to either Version 3 or Version 4 of the Task Metadata structure.

package main

import (
    "context"
    metadata "github.com/brunoscheufler/aws-ecs-metadata-go"
    "log"
    "net/http"
)

func main() {
    // Fetch ECS Task metadata from environment
    meta, err := metadata.Get(context.Background(), &http.Client{})
    if err != nil {
        panic(err)
    }

    // Based on the Fargate platform version, we'll have access
    // to v3 or v4 of the ECS Metadata format
    switch m := meta.(type) {
    case *metadata.TaskMetadataV3:
        log.Printf("%s %s:%s", m.Cluster, m.Family, m.Revision)
    case *metadata.TaskMetadataV4:
        log.Printf("%s(%s) %s:%s", m.Cluster, m.AvailabilityZone, m.Family, m.Revision)
    }
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Get

func Get(ctx context.Context, client *http.Client) (interface{}, error)

Will retrieve the task metadata for compatibility

func GetContainer

func GetContainer(ctx context.Context, client *http.Client) (interface{}, error)

Will retrieve the container metadata for your current Fargate environment (either V3 or V4) based on the environment variables that are present

func GetTask

func GetTask(ctx context.Context, client *http.Client) (interface{}, error)

Will retrieve the task metadata for your current Fargate environment (either V3 or V4) based on the environment variables that are present

func Has

func Has() bool

Will check whether the environment has available metadata URIs to use for fetching

Types

type ContainerMetadataV3

type ContainerMetadataV3 struct {
	DockerID   string `json:"DockerId"`
	Name       string `json:"Name"`
	DockerName string `json:"DockerName"`
	Image      string `json:"Image"`
	ImageID    string `json:"ImageID"`
	Labels     struct {
		EcsCluster               string `json:"com.amazonaws.ecs.cluster"`
		EcsContainerName         string `json:"com.amazonaws.ecs.container-name"`
		EcsTaskArn               string `json:"com.amazonaws.ecs.task-arn"`
		EcsTaskDefinitionFamily  string `json:"com.amazonaws.ecs.task-definition-family"`
		EcsTaskDefinitionVersion string `json:"com.amazonaws.ecs.task-definition-version"`
	} `json:"Labels"`
	DesiredStatus string `json:"DesiredStatus"`
	KnownStatus   string `json:"KnownStatus"`
	Limits        struct {
		CPU    int `json:"CPU"`
		Memory int `json:"Memory"`
	} `json:"Limits"`
	CreatedAt time.Time `json:"CreatedAt"`
	StartedAt time.Time `json:"StartedAt,omitempty"`
	Type      string    `json:"Type"`
	Networks  []struct {
		NetworkMode   string   `json:"NetworkMode"`
		IPv4Addresses []string `json:"IPv4Addresses"`
	} `json:"Networks"`
}

func GetContainerV3

func GetContainerV3(ctx context.Context, client *http.Client) (*ContainerMetadataV3, error)

Retrieve ECS Container Metadata in V3 format

type ContainerMetadataV4

type ContainerMetadataV4 struct {
	DockerID      string    `json:"DockerId"`
	Name          string    `json:"Name"`
	DockerName    string    `json:"DockerName"`
	Image         string    `json:"Image"`
	ImageID       string    `json:"ImageID"`
	Labels        LabelsV4  `json:"Labels"`
	DesiredStatus string    `json:"DesiredStatus"`
	KnownStatus   string    `json:"KnownStatus"`
	Limits        Limits    `json:"Limits"`
	CreatedAt     time.Time `json:"CreatedAt"`
	StartedAt     time.Time `json:"StartedAt"`
	Type          string    `json:"Type"`
	ContainerARN  string    `json:"ContainerARN"`
	LogDriver     string    `json:"LogDriver"`
	LogOptions    struct {
		AwsLogsCreateGroup string `json:"awslogs-create-group"`
		AwsLogsGroup       string `json:"awslogs-group"`
		AwsLogsStream      string `json:"awslogs-stream"`
		AwsRegion          string `json:"awslogs-region"`
	} `json:"LogOptions"`
	Networks []struct {
		NetworkMode              string   `json:"NetworkMode"`
		IPv4Addresses            []string `json:"IPv4Addresses"`
		AttachmentIndex          int      `json:"AttachmentIndex"`
		IPv4SubnetCIDRBlock      string   `json:"IPv4SubnetCIDRBlock"`
		MACAddress               string   `json:"MACAddress"`
		DomainNameServers        []string `json:"DomainNameServers"`
		DomainNameSearchList     []string `json:"DomainNameSearchList"`
		PrivateDNSName           string   `json:"PrivateDNSName"`
		SubnetGatewayIpv4Address string   `json:"SubnetGatewayIpv4Address"`
	} `json:"Networks"`
}

func GetContainerV4

func GetContainerV4(ctx context.Context, client *http.Client) (*ContainerMetadataV4, error)

Retrieve ECS Container Metadata in V4 format

type LabelsV4

type LabelsV4 struct {
	EcsCluster               string
	EcsContainerName         string
	EcsTaskArn               string
	EcsTaskDefinitionFamily  string
	EcsTaskDefinitionVersion string
	// contains filtered or unexported fields
}

func (LabelsV4) Get

func (l LabelsV4) Get(name string) string

func (*LabelsV4) UnmarshalJSON

func (l *LabelsV4) UnmarshalJSON(b []byte) error

type Limits

type Limits struct {
	CPU    float64 `json:"CPU"`
	Memory int     `json:"Memory"`
}

type TaskMetadataV3

type TaskMetadataV3 struct {
	Cluster       string                `json:"Cluster"`
	TaskARN       string                `json:"TaskARN"`
	Family        string                `json:"Family"`
	Revision      string                `json:"Revision"`
	DesiredStatus string                `json:"DesiredStatus"`
	KnownStatus   string                `json:"KnownStatus"`
	Containers    []ContainerMetadataV3 `json:"Containers"`
	Limits        struct {
		CPU    float64 `json:"CPU"`
		Memory int     `json:"Memory"`
	} `json:"Limits"`
	PullStartedAt time.Time `json:"PullStartedAt"`
	PullStoppedAt time.Time `json:"PullStoppedAt"`
}

func GetTaskV3

func GetTaskV3(ctx context.Context, client *http.Client) (*TaskMetadataV3, error)

Retrieve ECS Task Metadata in V3 format

type TaskMetadataV4

type TaskMetadataV4 struct {
	Cluster          string                `json:"Cluster"`
	TaskARN          string                `json:"TaskARN"`
	Family           string                `json:"Family"`
	Revision         string                `json:"Revision"`
	DesiredStatus    string                `json:"DesiredStatus"`
	KnownStatus      string                `json:"KnownStatus"`
	Limits           Limits                `json:"Limits"`
	PullStartedAt    time.Time             `json:"PullStartedAt"`
	PullStoppedAt    time.Time             `json:"PullStoppedAt"`
	AvailabilityZone string                `json:"AvailabilityZone"`
	LaunchType       string                `json:"LaunchType"`
	Containers       []ContainerMetadataV4 `json:"Containers"`
}

func GetTaskV4

func GetTaskV4(ctx context.Context, client *http.Client) (*TaskMetadataV4, error)

Retrieve ECS Task Metadata in V4 format

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL