forked from SAP/jenkins-library
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgitopsUpdateDeployment.go
475 lines (412 loc) · 15.3 KB
/
gitopsUpdateDeployment.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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
package cmd
import (
"bytes"
"fmt"
"github.com/SAP/jenkins-library/pkg/command"
"github.com/SAP/jenkins-library/pkg/docker"
gitUtil "github.com/SAP/jenkins-library/pkg/git"
"github.com/SAP/jenkins-library/pkg/log"
"github.com/SAP/jenkins-library/pkg/piperutils"
"github.com/SAP/jenkins-library/pkg/telemetry"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/plumbing/object"
"github.com/pkg/errors"
"io"
"os"
"path/filepath"
"regexp"
"strings"
"time"
)
const toolKubectl = "kubectl"
const toolHelm = "helm"
const toolKustomize = "kustomize"
type iGitopsUpdateDeploymentGitUtils interface {
CommitFiles(filePaths []string, commitMessage, author string) (plumbing.Hash, error)
PushChangesToRepository(username, password string, force *bool) error
PlainClone(username, password, serverURL, directory string) error
ChangeBranch(branchName string) error
}
type gitopsUpdateDeploymentFileUtils interface {
TempDir(dir, pattern string) (name string, err error)
RemoveAll(path string) error
FileWrite(path string, content []byte, perm os.FileMode) error
Glob(pattern string) ([]string, error)
}
type gitopsUpdateDeploymentExecRunner interface {
RunExecutable(executable string, params ...string) error
Stdout(out io.Writer)
Stderr(err io.Writer)
SetDir(dir string)
}
type gitopsUpdateDeploymentGitUtils struct {
worktree *git.Worktree
repository *git.Repository
}
func (g *gitopsUpdateDeploymentGitUtils) CommitFiles(filePaths []string, commitMessage, author string) (plumbing.Hash, error) {
for _, path := range filePaths {
_, err := g.worktree.Add(path)
if err != nil {
return [20]byte{}, errors.Wrap(err, "failed to add file to git")
}
}
commit, err := g.worktree.Commit(commitMessage, &git.CommitOptions{
All: true,
Author: &object.Signature{Name: author, When: time.Now()},
})
if err != nil {
return [20]byte{}, errors.Wrap(err, "failed to commit file")
}
return commit, nil
}
func (g *gitopsUpdateDeploymentGitUtils) PushChangesToRepository(username, password string, force *bool) error {
return gitUtil.PushChangesToRepository(username, password, force, g.repository)
}
func (g *gitopsUpdateDeploymentGitUtils) PlainClone(username, password, serverURL, directory string) error {
var err error
g.repository, err = gitUtil.PlainClone(username, password, serverURL, directory)
if err != nil {
return errors.Wrapf(err, "plain clone failed '%s'", serverURL)
}
g.worktree, err = g.repository.Worktree()
return errors.Wrap(err, "failed to retrieve worktree")
}
func (g *gitopsUpdateDeploymentGitUtils) ChangeBranch(branchName string) error {
return gitUtil.ChangeBranch(branchName, g.worktree)
}
func gitopsUpdateDeployment(config gitopsUpdateDeploymentOptions, _ *telemetry.CustomData) {
// for command execution use Command
var c gitopsUpdateDeploymentExecRunner = &command.Command{}
// reroute command output to logging framework
c.Stdout(log.Writer())
c.Stderr(log.Writer())
// for http calls import piperhttp "github.com/SAP/jenkins-library/pkg/http"
// and use a &piperhttp.Client{} in a custom system
// Example: step checkmarxExecuteScan.go
// error situations should stop execution through log.Entry().Fatal() call which leads to an os.Exit(1) in the end
err := runGitopsUpdateDeployment(&config, c, &gitopsUpdateDeploymentGitUtils{}, piperutils.Files{})
if err != nil {
log.Entry().WithError(err).Fatal("step execution failed")
}
}
func runGitopsUpdateDeployment(config *gitopsUpdateDeploymentOptions, command gitopsUpdateDeploymentExecRunner, gitUtils iGitopsUpdateDeploymentGitUtils, fileUtils gitopsUpdateDeploymentFileUtils) error {
err := checkRequiredFieldsForDeployTool(config)
if err != nil {
return err
}
temporaryFolder, err := fileUtils.TempDir(".", "temp-")
temporaryFolder = regexp.MustCompile(`^./`).ReplaceAllString(temporaryFolder, "")
if err != nil {
return errors.Wrap(err, "failed to create temporary directory")
}
defer func() {
err = fileUtils.RemoveAll(temporaryFolder)
if err != nil {
log.Entry().WithError(err).Error("error during temporary directory deletion")
}
}()
err = cloneRepositoryAndChangeBranch(config, gitUtils, temporaryFolder)
if err != nil {
return errors.Wrap(err, "repository could not get prepared")
}
filePath := filepath.Join(temporaryFolder, config.FilePath)
if config.Tool == toolHelm {
filePath = filepath.Join(temporaryFolder, config.ChartPath)
}
allFiles, err := fileUtils.Glob(filePath)
if err != nil {
return errors.Wrap(err, "unable to expand globbing pattern")
} else if len(allFiles) == 0 {
return errors.New("no matching files found for provided globbing pattern")
}
command.SetDir("./")
var outputBytes []byte
for _, currentFile := range allFiles {
if config.Tool == toolKubectl {
outputBytes, err = executeKubectl(config, command, outputBytes, currentFile)
if err != nil {
return errors.Wrap(err, "error on kubectl execution")
}
} else if config.Tool == toolHelm {
out, err := runHelmCommand(command, config, currentFile)
if err != nil {
return errors.Wrap(err, "failed to apply helm command")
}
// join all helm outputs into the same "FilePath"
outputBytes = append(outputBytes, []byte("---\n")...)
outputBytes = append(outputBytes, out...)
currentFile = filepath.Join(temporaryFolder, config.FilePath)
} else if config.Tool == toolKustomize {
_, err = runKustomizeCommand(command, config, currentFile)
if err != nil {
return errors.Wrap(err, "failed to apply kustomize command")
}
outputBytes = nil
} else if config.Tool == toolKustomize {
outputBytes, err = runKustomizeCommand(command, config, filePath)
if err != nil {
return errors.Wrap(err, "failed to apply kustomize command")
}
} else {
log.SetErrorCategory(log.ErrorConfiguration)
return errors.New("tool " + config.Tool + " is not supported")
}
if outputBytes != nil {
err = fileUtils.FileWrite(currentFile, outputBytes, 0755)
if err != nil {
return errors.Wrap(err, "failed to write file")
}
}
}
if config.Tool == toolHelm {
// helm only creates one output file.
allFiles = []string{config.FilePath}
} else {
// git expects the file path relative to its root:
for i := range allFiles {
allFiles[i] = strings.ReplaceAll(allFiles[i], temporaryFolder+"/", "")
}
}
commit, err := commitAndPushChanges(config, gitUtils, allFiles)
if err != nil {
return errors.Wrap(err, "failed to commit and push changes")
}
log.Entry().Infof("Changes committed with %s", commit.String())
return nil
}
func checkRequiredFieldsForDeployTool(config *gitopsUpdateDeploymentOptions) error {
if config.Tool == toolHelm {
err := checkRequiredFieldsForHelm(config)
if err != nil {
return errors.Wrap(err, "missing required fields for helm")
}
logNotRequiredButFilledFieldForHelm(config)
} else if config.Tool == toolKubectl {
err := checkRequiredFieldsForKubectl(config)
if err != nil {
return errors.Wrap(err, "missing required fields for kubectl")
}
logNotRequiredButFilledFieldForKubectl(config)
} else if config.Tool == toolKustomize {
err := checkRequiredFieldsForKustomize(config)
if err != nil {
return errors.Wrap(err, "missing required fields for kustomize")
}
logNotRequiredButFilledFieldForKustomize(config)
}
return nil
}
func checkRequiredFieldsForHelm(config *gitopsUpdateDeploymentOptions) error {
var missingParameters []string
if config.ChartPath == "" {
missingParameters = append(missingParameters, "chartPath")
}
if config.DeploymentName == "" {
missingParameters = append(missingParameters, "deploymentName")
}
if len(missingParameters) > 0 {
log.SetErrorCategory(log.ErrorConfiguration)
return errors.Errorf("the following parameters are necessary for helm: %v", missingParameters)
}
return nil
}
func checkRequiredFieldsForKustomize(config *gitopsUpdateDeploymentOptions) error {
var missingParameters []string
if config.FilePath == "" {
missingParameters = append(missingParameters, "filePath")
}
if config.DeploymentName == "" {
missingParameters = append(missingParameters, "deploymentName")
}
if len(missingParameters) > 0 {
log.SetErrorCategory(log.ErrorConfiguration)
return errors.Errorf("the following parameters are necessary for kustomize: %v", missingParameters)
}
return nil
}
func checkRequiredFieldsForKubectl(config *gitopsUpdateDeploymentOptions) error {
var missingParameters []string
if config.ContainerName == "" {
missingParameters = append(missingParameters, "containerName")
}
if len(missingParameters) > 0 {
log.SetErrorCategory(log.ErrorConfiguration)
return errors.Errorf("the following parameters are necessary for kubectl: %v", missingParameters)
}
return nil
}
func logNotRequiredButFilledFieldForHelm(config *gitopsUpdateDeploymentOptions) {
if config.ContainerName != "" {
log.Entry().Info("containerName is not used for helm and can be removed")
}
}
func logNotRequiredButFilledFieldForKubectl(config *gitopsUpdateDeploymentOptions) {
if config.ChartPath != "" {
log.Entry().Info("chartPath is not used for kubectl and can be removed")
}
if len(config.HelmValues) > 0 {
log.Entry().Info("helmValues is not used for kubectl and can be removed")
}
if len(config.DeploymentName) > 0 {
log.Entry().Info("deploymentName is not used for kubectl and can be removed")
}
}
func logNotRequiredButFilledFieldForKustomize(config *gitopsUpdateDeploymentOptions) {
if config.ChartPath != "" {
log.Entry().Info("chartPath is not used for kubectl and can be removed")
}
if len(config.HelmValues) > 0 {
log.Entry().Info("helmValues is not used for kubectl and can be removed")
}
}
func cloneRepositoryAndChangeBranch(config *gitopsUpdateDeploymentOptions, gitUtils iGitopsUpdateDeploymentGitUtils, temporaryFolder string) error {
err := gitUtils.PlainClone(config.Username, config.Password, config.ServerURL, temporaryFolder)
if err != nil {
return errors.Wrap(err, "failed to plain clone repository")
}
err = gitUtils.ChangeBranch(config.BranchName)
if err != nil {
return errors.Wrap(err, "failed to change branch")
}
return nil
}
func executeKubectl(config *gitopsUpdateDeploymentOptions, command gitopsUpdateDeploymentExecRunner, outputBytes []byte, filePath string) ([]byte, error) {
registryImage, err := buildRegistryPlusImage(config)
if err != nil {
return nil, errors.Wrap(err, "failed to apply kubectl command")
}
patchString := "{\"spec\":{\"template\":{\"spec\":{\"containers\":[{\"name\":\"" + config.ContainerName + "\",\"image\":\"" + registryImage + "\"}]}}}}"
log.Entry().Infof("[kubectl] updating '%s'", filePath)
outputBytes, err = runKubeCtlCommand(command, patchString, filePath)
if err != nil {
return nil, errors.Wrap(err, "failed to apply kubectl command")
}
return outputBytes, nil
}
func buildRegistryPlusImage(config *gitopsUpdateDeploymentOptions) (string, error) {
registryURL := config.ContainerRegistryURL
if registryURL == "" {
return config.ContainerImageNameTag, nil
}
url, err := docker.ContainerRegistryFromURL(registryURL)
if err != nil {
return "", errors.Wrap(err, "registry URL could not be extracted")
}
if url != "" {
url = url + "/"
}
return url + config.ContainerImageNameTag, nil
}
func runKubeCtlCommand(command gitopsUpdateDeploymentExecRunner, patchString string, filePath string) ([]byte, error) {
var kubectlOutput = bytes.Buffer{}
command.Stdout(&kubectlOutput)
kubeParams := []string{
"patch",
"--local",
"--output=yaml",
"--patch=" + patchString,
"--filename=" + filePath,
}
err := command.RunExecutable(toolKubectl, kubeParams...)
if err != nil {
return nil, errors.Wrap(err, "failed to apply kubectl command")
}
return kubectlOutput.Bytes(), nil
}
func runHelmCommand(command gitopsUpdateDeploymentExecRunner, config *gitopsUpdateDeploymentOptions, filePath string) ([]byte, error) {
var helmOutput = bytes.Buffer{}
command.Stdout(&helmOutput)
registryImage, imageTag, err := buildRegistryPlusImageAndTagSeparately(config)
if err != nil {
return nil, errors.Wrap(err, "failed to extract registry URL, image name, and image tag")
}
helmParams := []string{
"template",
config.DeploymentName,
filePath,
"--set=image.repository=" + registryImage,
"--set=image.tag=" + imageTag,
}
for _, value := range config.HelmValues {
helmParams = append(helmParams, "--values", value)
}
log.Entry().Infof("[helmn] updating '%s'", filePath)
err = command.RunExecutable(toolHelm, helmParams...)
if err != nil {
return nil, errors.Wrap(err, "failed to execute helm command")
}
return helmOutput.Bytes(), nil
}
func runKustomizeCommand(command gitopsUpdateDeploymentExecRunner, config *gitopsUpdateDeploymentOptions, filePath string) ([]byte, error) {
var kustomizeOutput = bytes.Buffer{}
command.Stdout(&kustomizeOutput)
registryImage, imageTag, err := buildRegistryPlusImageAndTagSeparately(config)
kustomizeParams := []string{
"edit",
"set",
"image",
config.DeploymentName + "=" + registryImage + ":" + imageTag,
}
command.SetDir(filepath.Dir(filePath))
log.Entry().Infof("[kustomize] updating '%s'", filePath)
err = command.RunExecutable(toolKustomize, kustomizeParams...)
if err != nil {
return nil, errors.Wrap(err, "failed to execute kustomize command")
}
return kustomizeOutput.Bytes(), nil
}
// buildRegistryPlusImageAndTagSeparately combines the registry together with the image name. Handles the tag separately.
// Tag is defined by everything on the right hand side of the colon sign. This looks weird for sha container versions but works for helm.
func buildRegistryPlusImageAndTagSeparately(config *gitopsUpdateDeploymentOptions) (string, string, error) {
registryURL := config.ContainerRegistryURL
url := ""
if registryURL != "" {
containerURL, err := docker.ContainerRegistryFromURL(registryURL)
if err != nil {
return "", "", errors.Wrap(err, "registry URL could not be extracted")
}
if containerURL != "" {
containerURL = containerURL + "/"
}
url = containerURL
}
imageNameTag := config.ContainerImageNameTag
var imageName, imageTag string
if strings.Contains(imageNameTag, ":") {
split := strings.Split(imageNameTag, ":")
if split[0] == "" {
log.SetErrorCategory(log.ErrorConfiguration)
return "", "", errors.New("image name could not be extracted")
}
if split[1] == "" {
log.SetErrorCategory(log.ErrorConfiguration)
return "", "", errors.New("tag could not be extracted")
}
imageName = split[0]
imageTag = split[1]
return url + imageName, imageTag, nil
}
log.SetErrorCategory(log.ErrorConfiguration)
return "", "", errors.New("image name and tag could not be extracted")
}
func commitAndPushChanges(config *gitopsUpdateDeploymentOptions, gitUtils iGitopsUpdateDeploymentGitUtils, filePaths []string) (plumbing.Hash, error) {
commitMessage := config.CommitMessage
if commitMessage == "" {
commitMessage = defaultCommitMessage(config)
}
commit, err := gitUtils.CommitFiles(filePaths, commitMessage, config.Username)
if err != nil {
return [20]byte{}, errors.Wrap(err, "committing changes failed")
}
err = gitUtils.PushChangesToRepository(config.Username, config.Password, &config.ForcePush)
if err != nil {
return [20]byte{}, errors.Wrap(err, "pushing changes failed")
}
return commit, nil
}
func defaultCommitMessage(config *gitopsUpdateDeploymentOptions) string {
image, tag, _ := buildRegistryPlusImageAndTagSeparately(config)
commitMessage := fmt.Sprintf("Updated %v to version %v", image, tag)
return commitMessage
}