-
Notifications
You must be signed in to change notification settings - Fork 7
/
profiles.go
70 lines (61 loc) · 1.76 KB
/
profiles.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
package ops
import (
"os"
"runtime"
"strconv"
"strings"
)
// A Profiles struct holds all loaded profiles of the application.
// A profile can be used at runtime to customize behavior of the application.
// This was inspired by other frameworks, such as Micronaut (https://github.com/micronaut-projects/micronaut-core/blob/v3.6.3/inject/src/main/java/io/micronaut/context/env/DefaultEnvironment.java)
type Profiles struct {
profiles map[string]bool
}
func NewProfiles() *Profiles {
return &Profiles{profiles: map[string]bool{}}
}
// List returns the profiles of p
func (p *Profiles) List() []string {
list := []string{}
for profile := range p.profiles {
list = append(list, profile)
}
return list
}
// Has returns true if profile is present in p
func (p *Profiles) Has(profile string) bool {
return p.profiles[profile]
}
// Load profiles explicitly set from VALK_PROFILES=dev,linux,cloud,gcp
//
// Try to deduce profiles unless disabled by VALK_PROFILES_DEDUCE=false
func (p *Profiles) Load() *Profiles {
if val, found := os.LookupEnv("VALK_PROFILES_DEDUCE"); found {
deduceProfiles, err := strconv.ParseBool(val)
if err != nil && deduceProfiles {
p.deduce()
}
} else {
p.deduce()
}
if val, found := os.LookupEnv("VALK_PROFILES"); found {
for _, profile := range strings.Split(strings.ToLower(val), ",") {
p.profiles[profile] = true
}
}
return p
}
func (p *Profiles) deduce() {
if _, found := os.LookupEnv("KUBERNETES_SERVICE_HOST"); found {
p.profiles["k8s"] = true
}
if _, found := os.LookupEnv("GOOGLE_COMPUTE_METADATA"); found || OnGCE() {
p.profiles["gcp"] = true
p.profiles["cloud"] = true
}
if _, err := os.Stat("/.dockerenv"); err == nil {
p.profiles["docker"] = true
}
p.profiles[runtime.GOARCH] = true
p.profiles[runtime.GOOS] = true
}