-
Notifications
You must be signed in to change notification settings - Fork 51
/
add.go
193 lines (181 loc) · 5.22 KB
/
add.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package helmfiles
import (
"strings"
"github.com/jenkins-x-plugins/jx-gitops/pkg/versionstreamer"
"github.com/jenkins-x/jx-helpers/v3/pkg/versionstream"
"github.com/pkg/errors"
"github.com/roboll/helmfile/pkg/state"
)
// ChartDetails the chart options when adding/updating charts
type ChartDetails struct {
Namespace string
Chart string
Repository string
Version string
ReleaseName string
Values []string
UpdateOnly bool
Prefixes *versionstream.RepositoryPrefixes
}
// NewChartDetails return new add chart options from an existing release
func NewChartDetails(helmState *state.HelmState, rel *state.ReleaseSpec, prefixes *versionstream.RepositoryPrefixes) *ChartDetails {
a := &ChartDetails{
Namespace: rel.Namespace,
Chart: rel.Chart,
Version: rel.Version,
ReleaseName: rel.Name,
Values: nil,
Prefixes: prefixes,
}
if a.Namespace == "" {
a.Namespace = helmState.OverrideNamespace
}
prefix, _ := SpitChartName(a.Chart)
if a.Repository == "" && prefix != "" {
for k := range helmState.Repositories {
r := helmState.Repositories[k]
if r.Name == prefix {
a.Repository = r.URL
}
}
}
return a
}
// SpitChartName splits the chart name into prefix and local name
func SpitChartName(name string) (string, string) {
prefix := ""
local := name
parts := strings.Split(name, "/")
if len(parts) > 1 {
prefix = parts[0]
local = parts[1]
}
return prefix, local
}
// String returns the string representation of the chart options
func (o *ChartDetails) String() string {
return o.Chart + " in namespace " + o.Namespace
}
// Add adds or updates the chart details in the helm state
func (o *ChartDetails) Add(helmState *state.HelmState) (bool, error) {
modified := false
found := false
var err error
prefix, localName := SpitChartName(o.Chart)
repository := o.Repository
if o.ReleaseName == "" {
o.ReleaseName = localName
}
// lets resolve the chart prefix from a local repository from the file or from a
// prefix in the versions stream
if repository == "" && prefix != "" {
for k := range helmState.Repositories {
r := helmState.Repositories[k]
if r.Name == prefix {
repository = r.URL
}
}
}
if repository == "" && prefix != "" {
repository, err = versionstreamer.MatchRepositoryPrefix(o.Prefixes, prefix)
if err != nil {
return false, errors.Wrapf(err, "failed to match prefix %s with repositories from versionstream", prefix)
}
}
if repository == "" && prefix != "" {
return false, errors.Wrapf(err, "failed to find repository URL, not defined in helmfile.yaml or versionstream")
}
if repository != "" && prefix != "" {
// lets ensure we've got a repository for this URL in the apps file
found = false
for k := range helmState.Repositories {
r := helmState.Repositories[k]
if r.Name == prefix {
if r.URL != repository {
return false, errors.Errorf("release %s has prefix %s for repository URL %s which is also mapped to prefix %s", o.Chart, prefix, r.URL, r.Name)
}
found = true
break
}
}
if !found {
helmState.Repositories = append(helmState.Repositories, state.RepositorySpec{
Name: prefix,
URL: repository,
})
}
}
// lets only set the namespace if its different to the default to keep the helmfiles DRY
namespace := o.Namespace
if namespace == helmState.OverrideNamespace {
namespace = ""
}
for i := range helmState.Releases {
release := &helmState.Releases[i]
if release.Chart == o.Chart && release.Name == o.ReleaseName {
found = true
if release.Namespace != "" && release.Namespace != namespace {
release.Namespace = namespace
modified = true
}
if release.Version != o.Version && o.Version != "" {
release.Version = o.Version
modified = true
}
// lets add any missing values
for _, v := range o.Values {
foundValue := false
for j := range release.Values {
if release.Values[j] == v {
foundValue = true
break
}
}
if !foundValue {
release.Values = append(release.Values, v)
modified = true
}
}
break
}
}
if !found && !o.UpdateOnly {
release := state.ReleaseSpec{
Chart: o.Chart,
Version: o.Version,
Name: o.ReleaseName,
Namespace: namespace,
}
for _, v := range o.Values {
release.Values = append(release.Values, v)
}
helmState.Releases = append(helmState.Releases, release)
modified = true
}
return modified, nil
}
// Delete removes the releases for the given details from the given helm state
func (o *ChartDetails) Delete(helmState *state.HelmState) (bool, error) {
modified := false
last := len(helmState.Releases) - 1
for i := last; i >= 0; i-- {
release := &helmState.Releases[i]
if MatchesChartName(release.Chart, o.Chart) && (o.ReleaseName == "" || release.Name == o.ReleaseName) {
r2 := helmState.Releases[0:i]
if i < last {
r2 = append(r2, helmState.Releases[i+1:]...)
}
helmState.Releases = r2
modified = true
}
}
return modified, nil
}
// MatchesChartName if name has a prefix then match on prefix and name otherwise just match on the local name only
func MatchesChartName(releaseChart, name string) bool {
if strings.Contains(name, "/") {
return releaseChart == name
}
_, localName := SpitChartName(releaseChart)
return localName == name
}