forked from Praqma/helmsman
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
172 lines (146 loc) · 3.72 KB
/
main.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
package main
import (
"log"
"os"
)
// Allow parsing of multiple string command line options into an array of strings
type stringArray []string
func (i *stringArray) String() string {
return "my string representation"
}
func (i *stringArray) Set(value string) error {
*i = append(*i, value)
return nil
}
var s state
var debug bool
var files stringArray
var envFiles stringArray
var kubeconfig string
var apply bool
var v bool
var verbose bool
var noBanner bool
var noColors bool
var noFancy bool
var noNs bool
var nsOverride string
var skipValidation bool
var applyLabels bool
var keepUntrackedReleases bool
var appVersion = "v1.9.1"
var helmVersion string
var kubectlVersion string
var dryRun bool
var target stringArray
var targetMap map[string]bool
var destroy bool
var showDiff bool
var suppressDiffSecrets bool
var noEnvSubst bool
const tempFilesDir = ".helmsman-tmp"
const stableHelmRepo = "https://kubernetes-charts.storage.googleapis.com"
const incubatorHelmRepo = "http://storage.googleapis.com/kubernetes-charts-incubator"
func main() {
// delete temp files with substituted env vars when the program terminates
defer os.RemoveAll(tempFilesDir)
defer cleanup()
// set the kubecontext to be used Or create it if it does not exist
if !setKubeContext(s.Settings.KubeContext) {
if r, msg := createContext(); !r {
logError(msg)
}
}
if apply || dryRun || destroy {
// add/validate namespaces
if !noNs {
addNamespaces(s.Namespaces)
}
if r, msg := initHelm(); !r {
logError(msg)
}
// check if helm Tiller is ready
for k, ns := range s.Namespaces {
if ns.InstallTiller || ns.UseTiller {
waitForTiller(k)
}
}
if _, ok := s.Namespaces["kube-system"]; !ok {
waitForTiller("kube-system")
}
} else {
initHelmClientOnly()
}
// add repos -- fails if they are not valid
if r, msg := addHelmRepos(s.HelmRepos); !r {
logError(msg)
}
if !skipValidation {
// validate charts-versions exist in defined repos
if r, msg := validateReleaseCharts(s.Apps); !r {
logError(msg)
}
} else {
log.Println("INFO: charts validation is skipped.")
}
log.Println("INFO: checking what I need to do for your charts ... ")
if destroy {
log.Println("WARN: --destroy is enabled. Your releases will be deleted!")
}
p := makePlan(&s)
if !keepUntrackedReleases {
cleanUntrackedReleases()
}
p.sortPlan()
p.printPlan()
p.sendPlanToSlack()
if apply || dryRun || destroy {
p.execPlan()
}
log.Println("INFO: completed successfully!")
}
// cleanup deletes the k8s certificates and keys files
// It also deletes any Tiller TLS certs and keys
// and secret files
func cleanup() {
log.Println("INFO: cleaning up sensitive and temp files")
if _, err := os.Stat("ca.crt"); err == nil {
deleteFile("ca.crt")
}
if _, err := os.Stat("ca.key"); err == nil {
deleteFile("ca.key")
}
if _, err := os.Stat("client.crt"); err == nil {
deleteFile("client.crt")
}
if _, err := os.Stat("bearer.token"); err == nil {
deleteFile("bearer.token")
}
for k := range s.Namespaces {
if _, err := os.Stat(k + "-tiller.cert"); err == nil {
deleteFile(k + "-tiller.cert")
}
if _, err := os.Stat(k + "-tiller.key"); err == nil {
deleteFile(k + "-tiller.key")
}
if _, err := os.Stat(k + "-ca.cert"); err == nil {
deleteFile(k + "-ca.cert")
}
if _, err := os.Stat(k + "-client.cert"); err == nil {
deleteFile(k + "-client.cert")
}
if _, err := os.Stat(k + "-client.key"); err == nil {
deleteFile(k + "-client.key")
}
}
for _, app := range s.Apps {
if _, err := os.Stat(app.SecretsFile + ".dec"); err == nil {
deleteFile(app.SecretsFile + ".dec")
}
for _, secret := range app.SecretsFiles {
if _, err := os.Stat(secret + ".dec"); err == nil {
deleteFile(secret + ".dec")
}
}
}
}