forked from Praqma/helmsman
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrelease.go
149 lines (137 loc) · 5.77 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
package main
import (
"fmt"
"log"
"os"
"strings"
)
// release type representing Helm releases which are described in the desired state
type release struct {
Name string
Description string
Namespace string
Enabled bool
Chart string
Version string
ValuesFile string `yaml:"valuesFile"`
ValuesFiles []string `yaml:"valuesFiles"`
SecretFile string `yaml:"secretFile"`
SecretFiles []string `yaml:"secretFiles"`
Purge bool
Test bool
Protected bool
Wait bool
Priority int
TillerNamespace string
Set map[string]string
NoHooks bool
Timeout int
}
// 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) {
_, err := os.Stat(pwd + "/" + relativeDir + "/" + r.ValuesFile)
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."
} else {
r.Version = substituteEnv(r.Version)
}
if r.ValuesFile != "" && (!isOfType(r.ValuesFile, ".yaml") || err != nil) {
return false, "valuesFile must be a valid relative (from your first dsf file) file path for a yaml file, Or can be left empty."
} 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 _, filePath := range r.ValuesFiles {
if _, pathErr := os.Stat(pwd + "/" + relativeDir + "/" + filePath); !isOfType(filePath, ".yaml") || pathErr != nil {
return false, "the value for valueFile '" + filePath + "' must be a valid relative (from your first dsf file) file path for a yaml file."
}
}
}
if r.SecretFile != "" && (!isOfType(r.SecretFile, ".yaml") || err != nil) {
return false, "secretFile must be a valid relative (from your first dsf file) file path for a yaml file, Or can be left empty."
} else if r.SecretFile != "" && len(r.SecretFiles) > 0 {
return false, "secretFile and secretFiles should not be used together."
} else if len(r.SecretFiles) > 0 {
for _, filePath := range r.SecretFiles {
if _, pathErr := os.Stat(pwd + "/" + relativeDir + "/" + filePath); !isOfType(filePath, ".yaml") || pathErr != nil {
return false, "the value for valueFile '" + filePath + "' must be a valid relative (from your first dsf file) file path for a yaml file."
}
}
}
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
}
return true, ""
}
// checkNamespaceDefined checks if a given namespace is defined in the namespaces section of the desired state file
func checkNamespaceDefined(ns string, s state) bool {
_, ok := s.Namespaces[ns]
if !ok {
return false
}
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)
fmt.Println("------------------- ")
}