This repository has been archived by the owner on Jul 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 30
/
deployment.go
108 lines (93 loc) · 3.03 KB
/
deployment.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
package deployment
import (
"os"
"github.com/akitasoftware/akita-cli/printer"
"github.com/akitasoftware/akita-libs/tags"
)
// Internal type of the deployment, automatically discovered.
// The most-specific one is returned. (Or do we need to
// support categorizing the deployment in more than one way?)
// This does not correspond to a tag.
type Deployment string
const (
None Deployment = ""
Any Deployment = "any"
Unknown Deployment = "unknown"
AWS Deployment = "aws"
AWS_ECS Deployment = "aws-ecs"
Kubernetes Deployment = "kubernetes"
)
// Map of environment variables to tags, grouped by deployment type.
var environmentToTag map[Deployment]map[string]tags.Key = map[Deployment]map[string]tags.Key{
Any: {
"AKITA_DEPLOYMENT_COMMIT": tags.XAkitaGitCommit,
},
AWS: {
"AKITA_AWS_REGION": tags.XAkitaAWSRegion,
},
AWS_ECS: {
"AKITA_AWS_REGION": tags.XAkitaAWSRegion,
"AKITA_ECS_TASK": tags.XAkitaECSTask,
"AKITA_ECS_SERVICE": tags.XAkitaECSService,
},
Kubernetes: {
"AKITA_K8S_NAMESPACE": tags.XAkitaKubernetesNamespace,
"AKITA_K8S_NODE": tags.XAkitaKubernetesNode,
"AKITA_K8S_HOST_IP": tags.XAkitaKubernetesHostIP,
"AKITA_K8S_POD": tags.XAkitaKubernetesPod,
"AKITA_K8S_POD_IP": tags.XAkitaKubernetesPodIP,
"AKITA_K8S_DAEMONSET": tags.XAkitaKubernetesDaemonset,
},
}
func (d Deployment) String() string {
return string(d)
}
// Use envToTag map to see if any of the environment variables are present.
// Return true if so, and update the tagset.
func (d Deployment) getTagsFromEnvironment(tagset map[tags.Key]string) bool {
found := false
for envVar, tag := range environmentToTag[d] {
if v := os.Getenv(envVar); v != "" {
tagset[tag] = v
found = true
}
}
return found
}
func GetDeploymentInfo() (Deployment, map[tags.Key]string) {
deploymentType := None
tagset := make(map[tags.Key]string)
// Allow the user to specify the name (not type) of deployment environment,
// even if it's of an unknown type.
// If there is a git commit associated with this deployment, then record it.
if Any.getTagsFromEnvironment(tagset) {
deploymentType = Unknown
}
if AWS_ECS.getTagsFromEnvironment(tagset) {
printer.Infof("Found AWS ECS environment variables.\n")
deploymentType = AWS_ECS
} else if AWS.getTagsFromEnvironment(tagset) {
printer.Infof("Found AWS environment variables.\n")
deploymentType = AWS
}
if Kubernetes.getTagsFromEnvironment(tagset) {
printer.Infof("Found Kubernetes environment variables.\n")
deploymentType = Kubernetes
}
return deploymentType, tagset
}
// Import information about production or staging environment
// if it is available in environment variables.
func UpdateTags(argsTags map[tags.Key]string) {
deploymentType, deploymentTags := GetDeploymentInfo()
// Only specify source if no source is already set.
if deploymentType != None {
if _, present := argsTags[tags.XAkitaSource]; !present {
argsTags[tags.XAkitaSource] = tags.DeploymentSource
}
}
// Copy into existing map
for k, v := range deploymentTags {
argsTags[k] = v
}
}