-
Notifications
You must be signed in to change notification settings - Fork 619
/
Copy pathstate.go
150 lines (120 loc) · 5.61 KB
/
state.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License"). You may
// not use this file except in compliance with the License. A copy of the
// License is located at
//
// http://aws.amazon.com/apache2.0/
//
// or in the "license" file accompanying this file. This file 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 state
import (
"fmt"
"github.com/aws/amazon-ecs-agent/ecs-agent/tmds/utils/netconfig"
)
// Error to be returned when container or task lookup failed
type ErrorLookupFailure struct {
externalReason string // Reason to be included in the response
}
func NewErrorLookupFailure(externalReason string) *ErrorLookupFailure {
return &ErrorLookupFailure{externalReason: externalReason}
}
func (e *ErrorLookupFailure) ExternalReason() string {
return e.externalReason
}
func (e *ErrorLookupFailure) Error() string {
return fmt.Sprintf("container lookup failed: %s", e.externalReason)
}
// General "catch-all" error to be returned when container or task metadata could not be
// fetched for some reason
type ErrorMetadataFetchFailure struct {
externalReason string // Reason to be included in the response
}
func NewErrorMetadataFetchFailure(externalReason string) *ErrorMetadataFetchFailure {
return &ErrorMetadataFetchFailure{externalReason: externalReason}
}
func (e *ErrorMetadataFetchFailure) Error() string {
return fmt.Sprintf("container lookup failed: %s", e.externalReason)
}
func (e *ErrorMetadataFetchFailure) ExternalReason() string {
return e.externalReason
}
// Error to be returned when container or task stats lookup failed due to a lookup failure
type ErrorStatsLookupFailure struct {
externalReason string // Reason to be returned in TMDS response
}
func NewErrorStatsLookupFailure(externalReason string) *ErrorStatsLookupFailure {
return &ErrorStatsLookupFailure{externalReason}
}
func (e *ErrorStatsLookupFailure) ExternalReason() string {
return e.externalReason
}
func (e *ErrorStatsLookupFailure) Error() string {
return fmt.Sprintf("stats lookup failed: %s", e.externalReason)
}
// General "catch-all" error to be returned when container or task stats could not
// be fetched for some reason.
type ErrorStatsFetchFailure struct {
externalReason string // Reason to be returned in TMDS response
cause error
}
func NewErrorStatsFetchFailure(externalReason string, cause error) *ErrorStatsFetchFailure {
return &ErrorStatsFetchFailure{externalReason, cause}
}
func (e *ErrorStatsFetchFailure) ExternalReason() string {
return e.externalReason
}
func (e *ErrorStatsFetchFailure) Error() string {
return fmt.Sprintf("failed to get stats: %s: %v", e.externalReason, e.cause)
}
func (e *ErrorStatsFetchFailure) Unwrap() error {
return e.cause
}
// Error to be returned when we're unable to obtain the default network interface name on the host namespace for a task
type ErrorDefaultNetworkInterfaceName struct {
externalReason string // Reason to be returned in TMDS response
}
func NewErrorDefaultNetworkInterfaceName(externalReason string) *ErrorDefaultNetworkInterfaceName {
return &ErrorDefaultNetworkInterfaceName{externalReason: externalReason}
}
func (e *ErrorDefaultNetworkInterfaceName) ExternalReason() string {
return e.externalReason
}
func (e *ErrorDefaultNetworkInterfaceName) Error() string {
return fmt.Sprintf("failed to obtain default network interface name: %s", e.externalReason)
}
// Interface for interacting with Agent State relevant to TMDS
type AgentState interface {
// Returns container metadata in v4 format for the container identified by the
// provided endpointContinerID.
// Returns ErrorLookupFailure if container lookup fails.
// Returns ErrorMetadataFetchFailure if something else goes wrong.
GetContainerMetadata(endpointContainerID string) (ContainerResponse, error)
// Returns task metadata in v4 format for the task identified by the provided endpointContainerID.
// Returns ErrorTaskLookupFailed if task lookup fails.
// Returns ErrorMetadataFetchFailure if something else goes wrong.
GetTaskMetadata(endpointContainerID string) (TaskResponse, error)
// Returns task metadata including task and container instance tags (if applicable) in v4 format
// for the task identified by the provided endpointContainerID.
// Returns ErrorTaskLookupFailed if task lookup fails.
// Returns ErrorMetadataFetchFailure if something else goes wrong.
GetTaskMetadataWithTags(endpointContainerID string) (TaskResponse, error)
// Returns task metadata including the task network configuration (if applicable) in v4 format
// for the task identified by the provided endpointContainerID.
// Returns ErrorTaskLookupFailed if task lookup fails.
// Returns ErrorMetadataFetchFailure if something else goes wrong.
GetTaskMetadataWithTaskNetworkConfig(endpointContainerID string, networkConfigClient *netconfig.NetworkConfigClient) (TaskResponse, error)
// Returns container stats in v4 format for the container identified by the provided
// endpointContainerID.
// Returns ErrorStatsLookupFailure if container lookup fails.
// Returns ErrorStatsFetchFailure if something else goes wrong.
GetContainerStats(endpointContainerID string) (StatsResponse, error)
// Returns task stats in v4 format for the task identified by the provided
// endpointContainerID.
// Returns ErrorStatsLookupFailure if container lookup fails.
// Returns ErrorStatsFetchFailure if something else goes wrong.
GetTaskStats(endpointContainerID string) (map[string]*StatsResponse, error)
}