forked from Praqma/helmsman
-
Notifications
You must be signed in to change notification settings - Fork 0
/
release.go
161 lines (147 loc) · 6.95 KB
/
release.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
package main
import (
"fmt"
"log"
"os"
"strings"
"github.com/hashicorp/go-version"
)
// release type representing Helm releases which are described in the desired state
type release struct {
Name string `yaml:"name"`
Description string `yaml:"description"`
Namespace string `yaml:"namespace"`
Enabled bool `yaml:"enabled"`
Chart string `yaml:"chart"`
Version string `yaml:"version"`
ValuesFile string `yaml:"valuesFile"`
ValuesFiles []string `yaml:"valuesFiles"`
SecretsFile string `yaml:"secretsFile"`
SecretsFiles []string `yaml:"secretsFiles"`
Purge bool `yaml:"purge"`
Test bool `yaml:"test"`
Protected bool `yaml:"protected"`
Wait bool `yaml:"wait"`
Priority int `yaml:"priority"`
TillerNamespace string `yaml:"tillerNamespace"`
Set map[string]string `yaml:"set"`
SetString map[string]string `yaml:"setString"`
HelmFlags []string `yaml:"helmFlags"`
NoHooks bool `yaml:"noHooks"`
Timeout int `yaml:"timeout"`
}
// validateRelease validates if a release inside a desired state meets the specifications or not.
// check the full specification @ https://github.com/Praqma/helmsman/docs/desired_state_spec.md
func validateRelease(appLabel string, r *release, names map[string]map[string]bool, s state) (bool, string) {
if r.Name == "" {
r.Name = appLabel
}
if r.TillerNamespace != "" {
if ns, ok := s.Namespaces[r.TillerNamespace]; !ok {
return false, "tillerNamespace specified, but the namespace specified does not exist!"
} else if !ns.InstallTiller && !ns.UseTiller {
return false, "tillerNamespace specified, but that namespace does not have neither installTiller nor useTiller set to true."
}
} else if getDesiredTillerNamespace(r) == "kube-system" {
if ns, ok := s.Namespaces["kube-system"]; ok && !ns.InstallTiller && !ns.UseTiller {
return false, "app is desired to be deployed using Tiller from [[ kube-system ]] but kube-system is not desired to have a Tiller installed nor use an existing Tiller. You can use another Tiller with the 'tillerNamespace' option or deploy Tiller in kube-system. "
}
}
if names[r.Name][getDesiredTillerNamespace(r)] {
return false, "release name must be unique within a given Tiller."
}
if nsOverride == "" && r.Namespace == "" {
return false, "release targeted namespace can't be empty."
} else if nsOverride == "" && r.Namespace != "" && r.Namespace != "kube-system" && !checkNamespaceDefined(r.Namespace, s) {
return false, "release " + r.Name + " is using namespace [ " + r.Namespace + " ] which is not defined in the Namespaces section of your desired state file." +
" Release [ " + r.Name + " ] can't be installed in that Namespace until its defined."
}
if r.Chart == "" || !strings.ContainsAny(r.Chart, "/") {
return false, "chart can't be empty and must be of the format: repo/chart."
}
if r.Version == "" {
return false, "version can't be empty."
}
_, err := os.Stat(r.ValuesFile)
if r.ValuesFile != "" && (!isOfType(r.ValuesFile, []string{".yaml", ".yml", ".json"}) || err != nil) {
return false, fmt.Sprintf("valuesFile must be a valid relative (from dsf file) file path for a yaml file, or can be left empty (provided path resolved to %q).", r.ValuesFile)
} else if r.ValuesFile != "" && len(r.ValuesFiles) > 0 {
return false, "valuesFile and valuesFiles should not be used together."
} else if len(r.ValuesFiles) > 0 {
for i, filePath := range r.ValuesFiles {
if _, pathErr := os.Stat(filePath); !isOfType(filePath, []string{".yaml", ".yml", ".json"}) || pathErr != nil {
return false, fmt.Sprintf("valuesFiles must be valid relative (from dsf file) file paths for a yaml file; path at index %d provided path resolved to %q.", i, filePath)
}
}
}
_, err = os.Stat(r.SecretsFile)
if r.SecretsFile != "" && (!isOfType(r.SecretsFile, []string{".yaml", ".yml", ".json"}) || err != nil) {
return false, fmt.Sprintf("secretsFile must be a valid relative (from dsf file) file path for a yaml file, or can be left empty (provided path resolved to %q).", r.SecretsFile)
} else if r.SecretsFile != "" && len(r.SecretsFiles) > 0 {
return false, "secretsFile and secretsFiles should not be used together."
} else if len(r.SecretsFiles) > 0 {
for _, filePath := range r.SecretsFiles {
if i, pathErr := os.Stat(filePath); !isOfType(filePath, []string{".yaml", ".yml", ".json"}) || pathErr != nil {
return false, fmt.Sprintf("secretsFiles must be valid relative (from dsf file) file paths for a yaml file; path at index %d provided path resolved to %q.", i, filePath)
}
}
}
if r.Priority != 0 && r.Priority > 0 {
return false, "priority can only be 0 or negative value, positive values are not allowed."
}
if names[r.Name] == nil {
names[r.Name] = make(map[string]bool)
}
if r.TillerNamespace != "" {
names[r.Name][r.TillerNamespace] = true
} else if s.Namespaces[r.Namespace].InstallTiller {
names[r.Name][r.Namespace] = true
} else {
names[r.Name]["kube-system"] = true
}
if len(r.SetString) > 0 {
v1, _ := version.NewVersion(helmVersion)
setStringConstraint, _ := version.NewConstraint(">=2.9.0")
if !setStringConstraint.Check(v1) {
return false, "you are using setString in your desired state, but your helm client does not support it. You need helm v2.9.0 or above for this feature."
}
}
// add $$ escaping for $ strings
os.Setenv("HELMSMAN_DOLLAR", "$")
for k, v := range r.Set {
if strings.Contains(v, "$") {
if os.ExpandEnv(strings.Replace(v, "$$", "${HELMSMAN_DOLLAR}", -1)) == "" {
return false, "env var [ " + v + " ] is not set, but is wanted to be passed for [ " + k + " ] in [[ " + r.Name + " ]]"
}
}
}
return true, ""
}
// overrideNamespace overrides a release defined namespace with a new given one
func overrideNamespace(r *release, newNs string) {
log.Println("INFO: overriding namespace for app: " + r.Name)
r.Namespace = newNs
}
// print prints the details of the release
func (r release) print() {
fmt.Println("")
fmt.Println("\tname : ", r.Name)
fmt.Println("\tdescription : ", r.Description)
fmt.Println("\tnamespace : ", r.Namespace)
fmt.Println("\tenabled : ", r.Enabled)
fmt.Println("\tchart : ", r.Chart)
fmt.Println("\tversion : ", r.Version)
fmt.Println("\tvaluesFile : ", r.ValuesFile)
fmt.Println("\tvaluesFiles : ", strings.Join(r.ValuesFiles, ","))
fmt.Println("\tpurge : ", r.Purge)
fmt.Println("\ttest : ", r.Test)
fmt.Println("\tprotected : ", r.Protected)
fmt.Println("\twait : ", r.Wait)
fmt.Println("\tpriority : ", r.Priority)
fmt.Println("\ttiller namespace : ", r.TillerNamespace)
fmt.Println("\tno-hooks : ", r.NoHooks)
fmt.Println("\ttimeout : ", r.Timeout)
fmt.Println("\tvalues to override from env:")
printMap(r.Set, 2)
fmt.Println("------------------- ")
}