-
Notifications
You must be signed in to change notification settings - Fork 148
/
loader.go
228 lines (200 loc) · 6.25 KB
/
loader.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
// Copyright 2023 The PipeCD Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package kubernetes
import (
"context"
"errors"
"fmt"
"os"
"path/filepath"
"sort"
"strconv"
"sync"
"go.uber.org/zap"
"github.com/pipe-cd/pipecd/pkg/app/piped/toolregistry"
"github.com/pipe-cd/pipecd/pkg/config"
"github.com/pipe-cd/pipecd/pkg/git"
)
type TemplatingMethod string
const (
TemplatingMethodHelm TemplatingMethod = "helm"
TemplatingMethodKustomize TemplatingMethod = "kustomize"
TemplatingMethodNone TemplatingMethod = "none"
)
type Loader interface {
// LoadManifests renders and loads all manifests for application.
LoadManifests(ctx context.Context) ([]Manifest, error)
}
type gitClient interface {
Clone(ctx context.Context, repoID, remote, branch, destination string) (git.Repo, error)
}
type loader struct {
appName string
appDir string
repoDir string
configFileName string
input config.KubernetesDeploymentInput
gc gitClient
logger *zap.Logger
templatingMethod TemplatingMethod
kustomize *Kustomize
helm *Helm
initOnce sync.Once
initErr error
}
func NewLoader(
appName, appDir, repoDir, configFileName string,
input config.KubernetesDeploymentInput,
gc gitClient,
logger *zap.Logger,
) Loader {
return &loader{
appName: appName,
appDir: appDir,
repoDir: repoDir,
configFileName: configFileName,
input: input,
gc: gc,
logger: logger.Named("kubernetes-loader"),
}
}
// LoadManifests renders and loads all manifests for application.
func (l *loader) LoadManifests(ctx context.Context) (manifests []Manifest, err error) {
defer func() {
// Override namespace if set because ParseManifests does not parse it
// if namespace is not explicitly specified in the manifests.
setNamespace(manifests, l.input.Namespace)
sortManifests(manifests)
}()
l.initOnce.Do(func() {
var initErrorHelm, initErrorKustomize error
l.templatingMethod = determineTemplatingMethod(l.input, l.appDir)
if l.templatingMethod != TemplatingMethodNone {
l.helm, initErrorHelm = l.findHelm(ctx, l.input.HelmVersion)
l.kustomize, initErrorKustomize = l.findKustomize(ctx, l.input.KustomizeVersion)
l.initErr = errors.Join(initErrorHelm, initErrorKustomize)
}
})
if l.initErr != nil {
return nil, l.initErr
}
switch l.templatingMethod {
case TemplatingMethodHelm:
var data string
switch {
case l.input.HelmChart.GitRemote != "":
chart := helmRemoteGitChart{
GitRemote: l.input.HelmChart.GitRemote,
Ref: l.input.HelmChart.Ref,
Path: l.input.HelmChart.Path,
}
data, err = l.helm.TemplateRemoteGitChart(ctx,
l.appName,
l.appDir,
l.input.Namespace,
chart,
l.gc,
l.input.HelmOptions)
case l.input.HelmChart.Repository != "":
chart := helmRemoteChart{
Repository: l.input.HelmChart.Repository,
Name: l.input.HelmChart.Name,
Version: l.input.HelmChart.Version,
Insecure: l.input.HelmChart.Insecure,
}
data, err = l.helm.TemplateRemoteChart(ctx,
l.appName,
l.appDir,
l.input.Namespace,
chart,
l.input.HelmOptions)
default:
data, err = l.helm.TemplateLocalChart(ctx,
l.appName,
l.appDir,
l.input.Namespace,
l.input.HelmChart.Path,
l.input.HelmOptions)
}
if err != nil {
err = fmt.Errorf("unable to run helm template: %w", err)
return
}
manifests, err = ParseManifests(data)
case TemplatingMethodKustomize:
var data string
data, err = l.kustomize.Template(ctx, l.appName, l.appDir, l.input.KustomizeOptions)
if err != nil {
err = fmt.Errorf("unable to run kustomize template: %w", err)
return
}
manifests, err = ParseManifests(data)
case TemplatingMethodNone:
manifests, err = LoadPlainYAMLManifests(l.appDir, l.input.Manifests, l.configFileName)
default:
err = fmt.Errorf("unsupport templating method %v", l.templatingMethod)
}
return
}
func setNamespace(manifests []Manifest, namespace string) {
if namespace == "" {
return
}
for i := range manifests {
manifests[i].Key.Namespace = namespace
}
}
func sortManifests(manifests []Manifest) {
if len(manifests) < 2 {
return
}
sort.Slice(manifests, func(i, j int) bool {
iAns := manifests[i].GetAnnotations()
// Ignore the converting error since it is not so much important.
iIndex, _ := strconv.Atoi(iAns[AnnotationOrder])
jAns := manifests[j].GetAnnotations()
// Ignore the converting error since it is not so much important.
jIndex, _ := strconv.Atoi(jAns[AnnotationOrder])
return iIndex < jIndex
})
}
func (l *loader) findKustomize(ctx context.Context, version string) (*Kustomize, error) {
path, installed, err := toolregistry.DefaultRegistry().Kustomize(ctx, version)
if err != nil {
return nil, fmt.Errorf("no kustomize %s (%v)", version, err)
}
if installed {
l.logger.Info(fmt.Sprintf("kustomize %s has just been installed because of no pre-installed binary for that version", version))
}
return NewKustomize(version, path, l.logger), nil
}
func (l *loader) findHelm(ctx context.Context, version string) (*Helm, error) {
path, installed, err := toolregistry.DefaultRegistry().Helm(ctx, version)
if err != nil {
return nil, fmt.Errorf("no helm %s (%v)", version, err)
}
if installed {
l.logger.Info(fmt.Sprintf("helm %s has just been installed because of no pre-installed binary for that version", version))
}
return NewHelm(version, path, l.logger), nil
}
func determineTemplatingMethod(input config.KubernetesDeploymentInput, appDirPath string) TemplatingMethod {
if input.HelmChart != nil {
return TemplatingMethodHelm
}
if _, err := os.Stat(filepath.Join(appDirPath, kustomizationFileName)); err == nil {
return TemplatingMethodKustomize
}
return TemplatingMethodNone
}