/
rollback.go
56 lines (43 loc) · 1.41 KB
/
rollback.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
package helmclient
import (
"context"
"time"
"github.com/giantswarm/microerror"
"github.com/prometheus/client_golang/prometheus"
"helm.sh/helm/v3/pkg/action"
)
// Rollback executes a rollback to a previous revision of a Helm release.
func (c *Client) Rollback(ctx context.Context, namespace, releaseName string, revision int, options RollbackOptions) error {
eventName := "rollback"
t := prometheus.NewTimer(histogram.WithLabelValues(eventName))
defer t.ObserveDuration()
err := c.rollback(ctx, namespace, releaseName, revision, options)
if err != nil {
errorGauge.WithLabelValues(eventName).Inc()
return microerror.Mask(err)
}
return nil
}
func (c *Client) rollback(ctx context.Context, namespace, releaseName string, revision int, options RollbackOptions) error {
cfg, err := c.newActionConfig(ctx, namespace)
if err != nil {
return microerror.Mask(err)
}
rollback := action.NewRollback(cfg)
// Configure action with supported rollback options.
options.configure(rollback, namespace, revision)
err = rollback.Run(releaseName)
if err != nil {
return microerror.Mask(err)
}
return nil
}
func (options RollbackOptions) configure(action *action.Rollback, namespace string, revision int) {
if options.Timeout == 0 {
options.Timeout = time.Second * defaultK8sClientTimeout
}
action.Force = options.Force
action.Timeout = options.Timeout
action.Version = revision
action.Wait = options.Wait
}