-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathhelm_actions.go
186 lines (170 loc) · 5.13 KB
/
helm_actions.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
package main
import (
"context"
"fmt"
"time"
"github.com/glasskube/distr/api"
"github.com/glasskube/distr/internal/agentauth"
"helm.sh/helm/v3/pkg/action"
"helm.sh/helm/v3/pkg/chart"
"helm.sh/helm/v3/pkg/chart/loader"
"helm.sh/helm/v3/pkg/cli"
"helm.sh/helm/v3/pkg/registry"
"helm.sh/helm/v3/pkg/release"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
)
var (
helmEnvSettings = cli.New()
helmActionConfigCache = make(map[string]*action.Configuration)
)
func GetHelmActionConfig(
ctx context.Context,
namespace string,
deployment *api.AgentDeployment,
) (*action.Configuration, error) {
if cfg, ok := helmActionConfigCache[namespace]; ok {
return cfg, nil
}
var cfg action.Configuration
if deployment != nil {
if authorizer, err :=
agentauth.EnsureAuth(ctx, distrRegistryHost, agentClient.RawToken(), *deployment); err != nil {
return nil, err
} else if rc, err := registry.NewClient(registry.ClientOptAuthorizer(authorizer)); err != nil {
return nil, err
} else {
cfg.RegistryClient = rc
}
}
if err := cfg.Init(
k8sConfigFlags,
namespace,
"secret",
func(format string, v ...interface{}) { logger.Sugar().Debugf(format, v...) },
); err != nil {
return nil, err
} else {
return &cfg, nil
}
}
func GetLatestHelmRelease(
ctx context.Context,
namespace string,
deployment api.KubernetesAgentDeployment,
) (*release.Release, error) {
cfg, err := GetHelmActionConfig(ctx, namespace, &deployment.AgentDeployment)
if err != nil {
return nil, err
}
historyAction := action.NewHistory(cfg)
if releases, err := historyAction.Run(deployment.ReleaseName); err != nil {
return nil, err
} else {
return releases[len(releases)-1], nil
}
}
func RunHelmPreflight(
action *action.ChartPathOptions,
deployment api.KubernetesAgentDeployment,
) (*chart.Chart, error) {
chartName := deployment.ChartName
action.Version = deployment.ChartVersion
if registry.IsOCI(deployment.ChartUrl) {
chartName = deployment.ChartUrl
} else {
action.RepoURL = deployment.ChartUrl
}
if chartPath, err := action.LocateChart(chartName, helmEnvSettings); err != nil {
return nil, fmt.Errorf("could not locate chart: %w", err)
} else if chart, err := loader.Load(chartPath); err != nil {
return nil, fmt.Errorf("chart loading failed: %w", err)
} else {
return chart, nil
}
}
func RunHelmInstall(
ctx context.Context,
namespace string,
deployment api.KubernetesAgentDeployment,
) (*AgentDeployment, error) {
config, err := GetHelmActionConfig(ctx, namespace, &deployment.AgentDeployment)
if err != nil {
return nil, err
}
installAction := action.NewInstall(config)
installAction.ReleaseName = deployment.ReleaseName
installAction.Timeout = 5 * time.Minute
installAction.Wait = true
installAction.Atomic = true
installAction.Namespace = namespace
if chart, err := RunHelmPreflight(&installAction.ChartPathOptions, deployment); err != nil {
return nil, fmt.Errorf("helm preflight failed: %w", err)
} else if release, err := installAction.RunWithContext(ctx, chart, deployment.Values); err != nil {
return nil, fmt.Errorf("helm install failed: %w", err)
} else {
return &AgentDeployment{
ReleaseName: release.Name,
HelmRevision: release.Version,
RevisionID: deployment.RevisionID,
}, nil
}
}
func RunHelmUpgrade(
ctx context.Context,
namespace string,
deployment api.KubernetesAgentDeployment,
) (*AgentDeployment, error) {
cfg, err := GetHelmActionConfig(ctx, namespace, &deployment.AgentDeployment)
if err != nil {
return nil, err
}
upgradeAction := action.NewUpgrade(cfg)
upgradeAction.CleanupOnFail = true
upgradeAction.Timeout = 5 * time.Minute
upgradeAction.Wait = true
upgradeAction.Atomic = true
upgradeAction.Namespace = namespace
if chart, err := RunHelmPreflight(&upgradeAction.ChartPathOptions, deployment); err != nil {
return nil, fmt.Errorf("helm preflight failed: %w", err)
} else if release, err := upgradeAction.RunWithContext(
ctx, deployment.ReleaseName, chart, deployment.Values); err != nil {
return nil, fmt.Errorf("helm upgrade failed: %w", err)
} else {
return &AgentDeployment{
ReleaseName: release.Name,
HelmRevision: release.Version,
RevisionID: deployment.RevisionID,
}, nil
}
}
func RunHelmUninstall(ctx context.Context, namespace, releaseName string) error {
config, err := GetHelmActionConfig(ctx, namespace, nil)
if err != nil {
return err
}
uninstallAction := action.NewUninstall(config)
uninstallAction.Timeout = 5 * time.Minute
uninstallAction.Wait = true
uninstallAction.IgnoreNotFound = true
if _, err := uninstallAction.Run(releaseName); err != nil {
return fmt.Errorf("helm uninstall failed: %w", err)
}
return nil
}
func GetHelmManifest(
ctx context.Context,
namespace string,
deployment api.KubernetesAgentDeployment,
) ([]*unstructured.Unstructured, error) {
cfg, err := GetHelmActionConfig(ctx, namespace, &deployment.AgentDeployment)
if err != nil {
return nil, err
}
getAction := action.NewGet(cfg)
if release, err := getAction.Run(deployment.ReleaseName); err != nil {
return nil, err
} else {
// decode the release manifests which is represented as multi-document YAML
return DecodeResourceYaml([]byte(release.Manifest))
}
}