-
Notifications
You must be signed in to change notification settings - Fork 165
/
spec.go
128 lines (103 loc) · 3 KB
/
spec.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
package helm
import (
"fmt"
"strings"
"github.com/Masterminds/semver"
)
const (
// Version of the current Chartfile implementation
Version = 1
// Filename of the Chartfile
Filename = "chartfile.yaml"
// DefaultDir is the directory used for storing Charts if not specified
// otherwise
DefaultDir = "charts"
)
// Chartfile is the schema used to declaratively define locally required Helm
// Charts
type Chartfile struct {
// Version of the Chartfile schema (for future use)
Version uint `json:"version"`
// Repositories to source from
Repositories Repos `json:"repositories"`
// Requires lists Charts expected to be present in the charts folder
Requires Requirements `json:"requires"`
// Folder to use for storing Charts. Defaults to 'charts'
Directory string `json:"directory,omitempty"`
}
// Repo describes a single Helm repository
type Repo struct {
Name string `json:"name,omitempty"`
URL string `json:"url,omitempty"`
CAFile string `json:"caFile,omitempty"`
CertFile string `json:"certFile,omitempty"`
KeyFile string `json:"keyFile,omitempty"`
Username string `json:"username,omitempty"`
Password string `json:"password,omitempty"`
}
type Repos []Repo
// Has reports whether 'repo' is already part of the repositories
func (r Repos) Has(repo Repo) bool {
for _, x := range r {
if x == repo {
return true
}
}
return false
}
// Has reports whether one of the repos has the given name
func (r Repos) HasName(repoName string) bool {
for _, x := range r {
if x.Name == repoName {
return true
}
}
return false
}
// Requirement describes a single required Helm Chart.
// Both, Chart and Version are required
type Requirement struct {
Chart string `json:"chart"`
Version semver.Version `json:"version"`
Directory string `json:"directory,omitempty"`
}
func (r Requirement) String() string {
dir := r.Directory
if dir == "" {
dir = parseReqName(r.Chart)
}
return fmt.Sprintf("%s@%s (dir: %s)", r.Chart, r.Version.String(), dir)
}
// Requirements is an aggregate of all required Charts
type Requirements []Requirement
// Has reports whether 'req' is already part of the requirements
func (r Requirements) Has(req Requirement) bool {
for _, x := range r {
if x == req {
return true
}
}
return false
}
func (r Requirements) Validate() error {
outputDirs := make(map[string]Requirement)
errs := make([]string, 0)
for _, req := range r {
if !strings.Contains(req.Chart, "/") {
errs = append(errs, fmt.Sprintf("Chart name %q is not valid. Expecting a repo/name format.", req.Chart))
continue
}
dir := req.Directory
if dir == "" {
dir = parseReqName(req.Chart)
}
if previous, ok := outputDirs[dir]; ok {
errs = append(errs, fmt.Sprintf(`output directory %q is used twice, by charts "%s@%s" and "%s@%s"`, dir, previous.Chart, previous.Version.String(), req.Chart, req.Version.String()))
}
outputDirs[dir] = req
}
if len(errs) > 0 {
return fmt.Errorf("Validation errors:\n - " + strings.Join(errs, "\n - "))
}
return nil
}