-
Notifications
You must be signed in to change notification settings - Fork 686
/
env.go
309 lines (251 loc) · 7.85 KB
/
env.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
package entrypoint
import (
"context"
"errors"
"fmt"
"os"
"path"
"path/filepath"
"strings"
"github.com/datawire/dlib/dexec"
"github.com/datawire/dlib/dlog"
)
func GetAgentService() string {
// Using an agent service is no longer supported, so return empty.
// For good measure, we also set AGENT_SERVICE to empty in the entrypoint.
return ""
}
func GetAmbassadorId() string {
id := os.Getenv("AMBASSADOR_ID")
if id != "" {
return id
}
svc := GetAgentService()
if svc != "" {
return fmt.Sprintf("intercept-%s", svc)
}
return "default"
}
func GetAmbassadorNamespace() string {
return env("AMBASSADOR_NAMESPACE", "default")
}
func GetAmbassadorFieldSelector() string {
return env("AMBASSADOR_FIELD_SELECTOR", "")
}
func GetAmbassadorLabelSelector() string {
return env("AMBASSADOR_LABEL_SELECTOR", "")
}
func GetAmbassadorRoot() string {
return env("ambassador_root", "/ambassador")
}
func GetHomeDir() string {
return env("HOME", "/tmp/ambassador")
}
func GetAmbassadorConfigBaseDir() string {
return env("AMBASSADOR_CONFIG_BASE_DIR", GetAmbassadorRoot())
}
func GetEnvoyDir() string {
return env("ENVOY_DIR", path.Join(GetAmbassadorConfigBaseDir(), "envoy"))
}
func GetEnvoyConcurrency() string {
return env("ENVOY_CONCURRENCY", "")
}
func GetEnvoyBootstrapFile() string {
return env("ENVOY_BOOTSTRAP_FILE", path.Join(GetAmbassadorConfigBaseDir(), "bootstrap-ads.json"))
}
func GetEnvoyBaseId() string {
return env("AMBASSADOR_ENVOY_BASE_ID", "0")
}
func GetAppDir() string {
return env("APPDIR", GetAmbassadorRoot())
}
// GetConfigDir returns the path to the directory we should check for
// filesystem config.
func GetConfigDir(demoMode bool) string {
// XXX There was no way to override the config dir via the environment in
// entrypoint.sh.
configDir := env("AMBASSADOR_CONFIG_DIR", path.Join(GetAmbassadorConfigBaseDir(), "ambassador-config"))
if demoMode {
// There is _intentionally_ no way to override the demo-mode config directory,
// and it is _intentionally_ based on the root directory rather than on
// AMBASSADOR_CONFIG_BASE_DIR: it's baked into a specific location during
// the build process.
configDir = path.Join(GetAmbassadorRoot(), "ambassador-demo-config")
}
return configDir
}
// ConfigIsPresent checks to see if any configuration is actually present
// in the given configdir.
func ConfigIsPresent(ctx context.Context, configDir string) bool {
// Is there anything in this directory?
foundAny := false
_ = filepath.Walk(configDir, func(path string, info os.FileInfo, err error) error {
// If we're handed an error coming in, something has gone wrong and we _must_
// return the error to avoid a panic. (The most likely error, admittedly, is
// simply that the toplevel directory doesn't exist.)
if err != nil {
// Log it, but if it isn't an os.ErrNoExist().
if !os.IsNotExist(err) {
dlog.Errorf(ctx, "Error scanning config file %s: %s", filepath.Join(configDir, path), err)
}
return err
}
if (info.Mode() & os.ModeType) == 0 {
// This is a regular file, so we can consider this valid config.
foundAny = true
// Return an error in order to short-circuit the rest of the walk.
// This is kind of an abuse, honestly, but then we also don't want
// to spend a long time walking crap if someone sets the environment
// variable incorrectly -- and if we run into an actual error walking
// the config dir, see the comment above.
return errors.New("not really an errore")
}
return nil
})
// Done. We don't care what the walk actually returned, we only care
// about foundAny.
return foundAny
}
func GetSnapshotDir() string {
return env("snapshot_dir", path.Join(GetAmbassadorConfigBaseDir(), "snapshots"))
}
func GetEnvoyConfigFile() string {
return env("envoy_config_file", path.Join(GetEnvoyDir(), "envoy.json"))
}
func GetEnvoyAPIVersion() string {
return env("AMBASSADOR_ENVOY_API_VERSION", "V3")
}
func GetAmbassadorDebug() string {
return env("AMBASSADOR_DEBUG", "")
}
func isDebug(name string) bool {
return strings.Contains(GetAmbassadorDebug(), name)
}
func GetEnvoyFlags() []string {
result := []string{"-c", GetEnvoyBootstrapFile(), "--base-id", GetEnvoyBaseId()}
svc := GetAgentService()
if svc != "" {
result = append(result, "--drain-time-s", "1")
} else {
result = append(result, "--drain-time-s", env("AMBASSADOR_DRAIN_TIME", "600"))
}
if isDebug("envoy") {
result = append(result, "-l", "trace")
} else {
result = append(result, "-l", "error")
}
concurrency := GetEnvoyConcurrency()
if concurrency != "" {
result = append(result, "--concurrency", concurrency)
}
envoyAPIVersion := GetEnvoyAPIVersion()
if strings.ToUpper(envoyAPIVersion) == "V3" {
result = append(result, "--bootstrap-version", "3")
} else {
result = append(result, "--bootstrap-version", "2")
}
return result
}
func GetDiagdBindAddress() string {
return env("AMBASSADOR_DIAGD_BIND_ADDREASS", "")
}
func IsDiagdOnly() bool {
return envbool("DIAGD_ONLY")
}
// ForceEndpoints reflects AMBASSADOR_FORCE_ENDPOINTS, to determine whether
// we're forcing endpoint watching or (the default) not.
func ForceEndpoints() bool {
return envbool("AMBASSADOR_FORCE_ENDPOINTS")
}
func GetDiagdBindPort() string {
return env("AMBASSADOR_DIAGD_BIND_PORT", "8004")
}
func IsEnvoyAvailable() bool {
_, err := dexec.LookPath("envoy")
return err == nil
}
func GetDiagdFlags(ctx context.Context, demoMode bool) []string {
result := []string{"--notices", path.Join(GetAmbassadorConfigBaseDir(), "notices.json")}
if isDebug("diagd") {
result = append(result, "--debug")
}
diagdBind := GetDiagdBindAddress()
if diagdBind != "" {
result = append(result, "--host", diagdBind)
}
// XXX: this was not in entrypoint.sh
result = append(result, "--port", GetDiagdBindPort())
cdir := GetConfigDir(demoMode)
if (cdir != "") && ConfigIsPresent(ctx, cdir) {
result = append(result, "--config-path", cdir)
}
if IsDiagdOnly() {
result = append(result, "--no-checks", "--no-envoy")
} else {
result = append(result, "--kick", fmt.Sprintf("kill -HUP %d", os.Getpid()))
// XXX: this was not in entrypoint.sh
if !IsEnvoyAvailable() {
result = append(result, "--no-envoy")
}
}
return result
}
func GetDiagdArgs(ctx context.Context, demoMode bool) []string {
return append(
[]string{
GetSnapshotDir(),
GetEnvoyBootstrapFile(),
GetEnvoyConfigFile(),
},
GetDiagdFlags(ctx, demoMode)...,
)
}
func IsAmbassadorSingleNamespace() bool {
return envbool("AMBASSADOR_SINGLE_NAMESPACE")
}
func IsEdgeStack() (bool, error) {
if envbool("EDGE_STACK") {
return true, nil
}
_, err := os.Stat("/ambassador/.edge_stack")
if err == nil {
return true, nil
} else if os.IsNotExist(err) {
return false, nil
} else {
return false, err
}
}
func GetLicenseSecretName() string {
return env("AMBASSADOR_AES_SECRET_NAME", "ambassador-edge-stack")
}
func GetLicenseSecretNamespace() string {
return env("AMBASSADOR_AES_SECRET_NAMESPACE", GetAmbassadorNamespace())
}
func GetCloudConnectTokenResourceName() string {
return env("AGENT_CONFIG_RESOURCE_NAME", "ambassador-agent-cloud-token")
}
func GetCloudConnectTokenResourceNamespace() string {
return env("AGENT_NAMESPACE", GetAmbassadorNamespace())
}
func GetEventHost() string {
return env("DEV_AMBASSADOR_EVENT_HOST", fmt.Sprintf("http://localhost:%s", GetDiagdBindPort()))
}
func GetEventPath() string {
return env("DEV_AMBASSADOR_EVENT_PATH", fmt.Sprintf("_internal/v0"))
}
func GetSidecarHost() string {
return env("DEV_AMBASSADOR_SIDECAR_HOST", "http://localhost:8500")
}
func GetSidecarPath() string {
return env("DEV_AMBASSADOR_SIDECAR_PATH", "_internal/v0")
}
func GetEventUrl() string {
return fmt.Sprintf("%s/%s/watt", GetEventHost(), GetEventPath())
}
func GetSidecarUrl() string {
return fmt.Sprintf("%s/%s/watt", GetSidecarHost(), GetSidecarPath())
}
func IsKnativeEnabled() bool {
return strings.ToLower(env("AMBASSADOR_KNATIVE_SUPPORT", "")) == "true"
}