-
Notifications
You must be signed in to change notification settings - Fork 173
/
main.go
145 lines (118 loc) · 2.96 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
// Package app contains the main logic for the application.
package app
import (
"os"
)
const (
helmBin = "helm"
kubectlBin = "kubectl"
appVersion = "v3.17.1"
tempFilesDir = ".helmsman-tmp"
defaultContextName = "default"
resourcePool = 10
)
const (
exitCodeSucceed = 0
exitCodeSucceedWithChanges = 2
)
var (
flags cli
settings *Config
curContext string
log = &Logger{}
)
func init() {
// Parse cli flags and read config files
flags.setup()
}
// Main is the app main function
func Main() int {
var s State
flags.parse()
// delete temp files with substituted env vars when the program terminates
defer os.RemoveAll(tempFilesDir)
if !flags.noCleanup {
defer s.cleanup()
}
if err := flags.readState(&s); err != nil {
log.Fatal(err.Error())
}
if len(flags.target) > 0 && len(s.targetMap) == 0 {
log.Info("No apps defined with -target flag were found, exiting")
os.Exit(0)
}
if len(flags.group) > 0 && len(s.targetMap) == 0 {
log.Info("No apps defined with -group flag were found, exiting")
os.Exit(0)
}
log.SlackWebhook = s.Settings.SlackWebhook
log.MSTeamsWebhook = s.Settings.MSTeamsWebhook
settings = &s.Settings
curContext = s.Context
// set the kubecontext to be used Or create it if it does not exist
log.Info("Setting up kubectl")
if !setKubeContext(s.Settings.KubeContext) {
if err := createContext(&s); err != nil {
log.Fatal(err.Error())
}
}
// add repos -- fails if they are not valid
log.Info("Setting up helm")
if err := addHelmRepos(s.HelmRepos); err != nil && !flags.destroy {
log.Fatal(err.Error())
}
if flags.apply || flags.dryRun || flags.destroy {
// add/validate namespaces
if !flags.noNs {
log.Info("Setting up namespaces")
if flags.nsOverride == "" {
addNamespaces(&s)
} else {
createNamespace(flags.nsOverride)
s.overrideAppsNamespace(flags.nsOverride)
}
}
}
log.Info("Getting chart information")
err := s.getReleaseChartsInfo()
if flags.skipValidation {
log.Info("Skipping charts' validation.")
} else if err != nil {
log.Fatal(err.Error())
} else {
log.Info("Charts validated.")
}
if flags.destroy {
log.Warning("Destroy flag is enabled. Your releases will be deleted!")
}
if flags.migrateContext {
log.Warning("migrate-context flag is enabled. Context will be changed to [ " + s.Context + " ] and Helmsman labels will be applied.")
s.updateContextLabels()
}
if flags.checkForChartUpdates {
for _, r := range s.Apps {
r.checkChartForUpdates()
}
}
log.Info("Preparing plan")
cs := s.getCurrentState()
p := cs.makePlan(&s)
if !flags.keepUntrackedReleases {
cs.cleanUntrackedReleases(&s, p)
}
p.sort()
p.print()
if flags.debug {
p.printCmds()
}
p.sendToSlack()
p.sendToMSTeams()
if flags.apply || flags.dryRun || flags.destroy {
p.exec()
}
exitCode := exitCodeSucceed
if flags.detailedExitCode && len(p.Commands) > 0 {
exitCode = exitCodeSucceedWithChanges
}
return exitCode
}