-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchart.go
169 lines (141 loc) · 3.91 KB
/
chart.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
package helm
import (
"fmt"
"helm.sh/helm/v3/pkg/action"
"helm.sh/helm/v3/pkg/chart"
"helm.sh/helm/v3/pkg/chartutil"
"helm.sh/helm/v3/pkg/cli"
"helm.sh/helm/v3/pkg/getter"
"helm.sh/helm/v3/pkg/release"
"helm.sh/helm/v3/pkg/releaseutil"
"helm.sh/helm/v3/pkg/repo"
)
const (
secretsStorageDriver = "secrets"
)
var (
noopLogger = func(_ string, _ ...interface{}) {}
)
type ActionableChartInterface interface {
Apply(
chart *chart.Chart,
values map[string]interface{},
) (*release.Release, error)
Install(
chart *chart.Chart,
values map[string]interface{},
) (*release.Release, error)
History() ([]*release.Release, error)
Rollback(version int) error
Upgrade(chart *chart.Chart, values map[string]interface{}) (*release.Release, error)
Uninstall() (*release.UninstallReleaseResponse, error)
GetRelease(version int) (*release.Release, error)
}
// GetConfig returns a helm action configuration. Namespace is used to determine
// where to store the versions
func InitConfig(namespace string) (*cli.EnvSettings, *action.Configuration, error) {
settings := cli.New()
settings.SetNamespace(namespace)
config := new(action.Configuration)
err := config.Init(
settings.RESTClientGetter(),
settings.Namespace(),
secretsStorageDriver,
noopLogger,
)
return settings, config, err
}
func DownloadChart(repoURL string, name string) (string, error) {
settings := cli.New()
providers := getter.All(settings)
entry := new(repo.Entry)
entry.URL = repoURL
entry.Name = name
file := repo.NewFile()
file.Update(entry)
chartRepo, err := repo.NewChartRepository(entry, providers)
if err != nil {
return "", err
}
_, err = chartRepo.DownloadIndexFile()
if err != nil {
return "", err
}
chartURL, err := repo.FindChartInRepoURL(
repoURL, name,
"", "", "", "",
providers,
)
if err != nil {
return "", err
}
client := action.NewPull()
client.Settings = settings
return client.Run(chartURL)
}
func NewActionableChart(releaseName string, namespace string) (*ActionableChart, error) {
if err := chartutil.ValidateReleaseName(releaseName); err != nil {
return nil, fmt.Errorf("release name %q", releaseName)
}
_, config, err := InitConfig(namespace)
if err != nil {
return nil, err
}
return &ActionableChart{
releaseName: releaseName,
config: config,
namespace: namespace,
}, nil
}
type ActionableChart struct {
releaseName string
config *action.Configuration
namespace string
}
func (c *ActionableChart) isInstalled() bool {
h, err := c.config.Releases.History(c.releaseName)
if err != nil || len(h) < 1 {
return false
}
releaseutil.Reverse(h, releaseutil.SortByRevision)
rel := h[0]
st := rel.Info.Status
return st != release.StatusUninstalled
}
func (c *ActionableChart) Apply(
chart *chart.Chart,
values map[string]interface{},
) (*release.Release, error) {
if c.isInstalled() {
return c.Upgrade(chart, values)
}
return c.Install(chart, values)
}
func (c *ActionableChart) Install(
chart *chart.Chart,
values map[string]interface{},
) (*release.Release, error) {
client := action.NewInstall(c.config)
client.ReleaseName = c.releaseName
client.Namespace = c.namespace
return client.Run(chart, values)
}
func (c *ActionableChart) History() ([]*release.Release, error) {
return c.config.Releases.History(c.releaseName)
}
func (c *ActionableChart) Rollback(version int) error {
client := action.NewRollback(c.config)
return client.Run(c.releaseName)
}
func (c *ActionableChart) Upgrade(chart *chart.Chart, values map[string]interface{}) (*release.Release, error) {
client := action.NewUpgrade(c.config)
client.Namespace = c.namespace
return client.Run(c.releaseName, chart, values)
}
func (c *ActionableChart) Uninstall() (*release.UninstallReleaseResponse, error) {
client := action.NewUninstall(c.config)
return client.Run(c.releaseName)
}
func (c *ActionableChart) GetRelease(version int) (*release.Release, error) {
return c.config.Releases.Get(c.releaseName, version)
}