/
provider.go
200 lines (159 loc) · 5.07 KB
/
provider.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
194
195
196
197
198
199
200
package helm
import (
"fmt"
"time"
htypes "github.com/jumppad-labs/hclconfig/types"
"github.com/jumppad-labs/jumppad/pkg/clients"
"github.com/jumppad-labs/jumppad/pkg/clients/getter"
"github.com/jumppad-labs/jumppad/pkg/clients/helm"
"github.com/jumppad-labs/jumppad/pkg/clients/k8s"
"github.com/jumppad-labs/jumppad/pkg/clients/logger"
"github.com/jumppad-labs/jumppad/pkg/utils"
sdk "github.com/jumppad-labs/plugin-sdk"
"golang.org/x/xerrors"
)
type Provider struct {
config *Helm
kubeClient k8s.Kubernetes
helmClient helm.Helm
getterClient getter.Getter
log logger.Logger
}
func (p *Provider) Init(cfg htypes.Resource, l sdk.Logger) error {
h, ok := cfg.(*Helm)
if !ok {
return fmt.Errorf("unable to initialize Helm provider, resource is not of type Helm")
}
p.config = h
cli, err := clients.GenerateClients(l)
if err != nil {
return err
}
p.config = h
p.kubeClient = cli.Kubernetes
p.helmClient = cli.Helm
p.getterClient = cli.Getter
p.log = l
return nil
}
// Create implements the provider Create method
func (p *Provider) Create() error {
p.log.Info("Creating Helm chart", "ref", p.config.ResourceID)
// if the namespace is null set to default
if p.config.Namespace == "" {
p.config.Namespace = "default"
}
// is this chart ot be loaded from a repository?
if p.config.Repository != nil {
p.log.Debug("Updating Helm chart repository", "name", p.config.Repository.Name, "url", p.config.Repository.URL)
err := p.helmClient.UpsertChartRepository(p.config.Repository.Name, p.config.Repository.URL)
if err != nil {
return xerrors.Errorf("unable to initialize chart repository: %w", err)
}
}
// is the source a helm repo which should be downloaded?
if !utils.IsLocalFolder(p.config.Chart) && p.config.Repository == nil {
p.log.Debug("Fetching remote Helm chart", "ref", p.config.ResourceName, "chart", p.config.Chart)
helmFolder := utils.HelmLocalFolder(p.config.Chart)
err := p.getterClient.Get(p.config.Chart, helmFolder)
if err != nil {
return xerrors.Errorf("Unable to download remote chart: %w", err)
}
// set the config to the local path
p.config.Chart = helmFolder
}
// set the KubeConfig for the kubernetes client
// this is used by the health checks
var err error
p.log.Debug("Using Kubernetes config", "ref", p.config.ResourceID, "path", p.config.Cluster.KubeConfig)
p.kubeClient, err = p.kubeClient.SetConfig(p.config.Cluster.KubeConfig)
if err != nil {
return xerrors.Errorf("unable to create Kubernetes client: %w", err)
}
// sanitize the chart name
newName, _ := utils.ReplaceNonURIChars(p.config.ResourceName)
failCount := 0
to := time.Duration(300 * time.Second)
if p.config.Timeout != "" {
to, err = time.ParseDuration(p.config.Timeout)
if err != nil {
return xerrors.Errorf("unable to parse timeout duration: %w", err)
}
}
timeout := time.After(to)
errChan := make(chan error)
doneChan := make(chan struct{})
go func() {
for {
err = p.helmClient.Create(
p.config.Cluster.KubeConfig,
newName,
p.config.Namespace,
p.config.CreateNamespace,
p.config.SkipCRDs,
p.config.Chart,
p.config.Version,
p.config.Values,
p.config.ValuesString)
if err == nil {
doneChan <- struct{}{}
break
}
failCount++
if failCount >= p.config.Retry {
errChan <- err
} else {
p.log.Debug("Chart apply failed, retrying", "error", err)
time.Sleep(5 * time.Second)
}
}
}()
select {
case <-timeout:
return xerrors.Errorf("timeout waiting for helm chart to complete")
case createErr := <-errChan:
return createErr
case <-doneChan:
p.log.Debug("Helm chart applied", "ref", p.config.ResourceName)
}
// we can now health check the install
if p.config.HealthCheck != nil && len(p.config.HealthCheck.Pods) > 0 {
to, err := time.ParseDuration(p.config.HealthCheck.Timeout)
if err != nil {
return xerrors.Errorf("unable to parse health check duration: %w", err)
}
err = p.kubeClient.HealthCheckPods(p.config.HealthCheck.Pods, to)
if err != nil {
return xerrors.Errorf("health check failed after helm chart setup: %w", err)
}
}
return nil
}
// Destroy implements the provider Destroy method
func (p *Provider) Destroy() error {
p.log.Info("Destroy Helm chart", "ref", p.config.ResourceID)
// if the namespace is null set to default
if p.config.Namespace == "" {
p.config.Namespace = "default"
}
// sanitize the chart name
newName, _ := utils.ReplaceNonURIChars(p.config.ResourceName)
// get the target cluster
err := p.helmClient.Destroy(p.config.Cluster.KubeConfig, newName, p.config.Namespace)
if err != nil {
p.log.Debug("There was a problem destroying Helm chart, logging message but ignoring error", "ref", p.config.ResourceID, "error", err)
}
return nil
}
// Lookup implements the provider Lookup method
func (p *Provider) Lookup() ([]string, error) {
return []string{}, nil
}
func (p *Provider) Refresh() error {
p.log.Debug("Refresh Helm Chart", "ref", p.config.ResourceName)
return nil
}
func (p *Provider) Changed() (bool, error) {
p.log.Debug("Checking changes", "ref", p.config.ResourceName)
return false, nil
}