-
Notifications
You must be signed in to change notification settings - Fork 214
/
Copy pathcontext.go
213 lines (175 loc) · 4.79 KB
/
context.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: MIT
package context
import (
"log"
"os"
"github.com/aws/amazon-cloudwatch-agent/translator/config"
)
var ctx *Context
func CurrentContext() *Context {
if ctx == nil {
ctx = &Context{
credentials: make(map[string]string),
proxy: make(map[string]string),
cloudWatchLogConfig: make(map[string]interface{}),
runInContainer: os.Getenv(config.RUN_IN_CONTAINER) == config.RUN_IN_CONTAINER_TRUE,
agentLogFile: "",
}
}
return ctx
}
// Testing only
func ResetContext() {
ctx = nil
}
type Context struct {
os string
inputJsonFilePath string
inputJsonDirPath string
multiConfig string
outputTomlFilePath string
mode string
kubernetesMode string
workloadType string
shortMode string
credentials map[string]string
proxy map[string]string
ssl map[string]string
cloudWatchLogConfig map[string]interface{}
runInContainer bool
agentLogFile string
omitHostname bool
}
func (ctx *Context) Os() string {
return ctx.os
}
func (ctx *Context) SetOs(os string) {
ctx.os = config.ToValidOs(os)
}
func (ctx *Context) InputJsonFilePath() string {
return ctx.inputJsonFilePath
}
func (ctx *Context) SetInputJsonFilePath(inputJsonFilePath string) {
ctx.inputJsonFilePath = inputJsonFilePath
}
func (ctx *Context) InputJsonDirPath() string {
return ctx.inputJsonDirPath
}
func (ctx *Context) SetInputJsonDirPath(inputJsonDirPath string) {
ctx.inputJsonDirPath = inputJsonDirPath
}
func (ctx *Context) MultiConfig() string {
return ctx.multiConfig
}
func (ctx *Context) SetMultiConfig(multiConfig string) {
ctx.multiConfig = multiConfig
}
func (ctx *Context) OutputTomlFilePath() string {
return ctx.outputTomlFilePath
}
func (ctx *Context) SetOutputTomlFilePath(outputTomlFilePath string) {
ctx.outputTomlFilePath = outputTomlFilePath
}
func (ctx *Context) Mode() string {
if ctx.mode == "" {
ctx.mode = config.ModeEC2
}
return ctx.mode
}
func (ctx *Context) KubernetesMode() string {
return ctx.kubernetesMode
}
func (ctx *Context) WorkloadType() string {
return ctx.workloadType
}
func (ctx *Context) ShortMode() string {
return ctx.shortMode
}
func (ctx *Context) Credentials() map[string]string {
return ctx.credentials
}
func (ctx *Context) SSL() map[string]string {
return ctx.ssl
}
func (ctx *Context) Proxy() map[string]string {
return ctx.proxy
}
func (ctx *Context) SetMode(mode string) {
switch mode {
case config.ModeEC2:
ctx.mode = config.ModeEC2
ctx.shortMode = config.ShortModeEC2
case config.ModeOnPrem:
ctx.mode = config.ModeOnPrem
ctx.shortMode = config.ShortModeOnPrem
case config.ModeOnPremise:
ctx.mode = config.ModeOnPremise
ctx.shortMode = config.ShortModeOnPrem
case config.ModeWithIRSA:
ctx.mode = config.ModeWithIRSA
ctx.shortMode = config.ShortModeWithIRSA
default:
log.Panicf("Invalid mode %s. Valid mode values are %s, %s, %s, and %s.", mode, config.ModeEC2, config.ModeOnPrem, config.ModeOnPremise, config.ModeWithIRSA)
}
}
func (ctx *Context) SetKubernetesMode(mode string) {
switch mode {
case config.ModeEKS:
ctx.kubernetesMode = config.ModeEKS
ctx.shortMode = config.ShortModeEKS
case config.ModeK8sEC2:
ctx.kubernetesMode = config.ModeK8sEC2
ctx.shortMode = config.ShortModeK8sEC2
case config.ModeK8sOnPrem:
ctx.kubernetesMode = config.ModeK8sOnPrem
ctx.shortMode = config.ShortModeK8sOnPrem
default:
ctx.kubernetesMode = ""
}
}
func (ctx *Context) SetWorkloadType(workloadType string) {
switch workloadType {
case config.DaemonSet:
ctx.workloadType = config.DaemonSet
case config.Deployment:
ctx.workloadType = config.Deployment
case config.StatefulSet:
ctx.workloadType = config.StatefulSet
default:
ctx.workloadType = ""
}
}
func (ctx *Context) SetCredentials(creds map[string]string) {
ctx.credentials = creds
}
func (ctx *Context) SetSSL(ssl map[string]string) {
ctx.ssl = ssl
}
func (ctx *Context) SetProxy(proxy map[string]string) {
ctx.proxy = proxy
}
func (ctx *Context) SetCloudWatchLogConfig(config map[string]interface{}) {
ctx.cloudWatchLogConfig = config
}
func (ctx *Context) CloudWatchLogConfig() map[string]interface{} {
return ctx.cloudWatchLogConfig
}
func (ctx *Context) RunInContainer() bool {
return ctx.runInContainer
}
func (ctx *Context) SetRunInContainer(runInContainer bool) {
ctx.runInContainer = runInContainer
}
func (ctx *Context) GetAgentLogFile() string {
return ctx.agentLogFile
}
func (ctx *Context) SetAgentLogFile(agentLogFile string) {
ctx.agentLogFile = agentLogFile
}
func (ctx *Context) GetOmitHostname() bool {
return ctx.omitHostname
}
func (ctx *Context) SetOmitHostname(omitHostname bool) {
ctx.omitHostname = omitHostname
}