forked from redhat-developer/gitops-generator
/
gitops.go
421 lines (365 loc) · 19.2 KB
/
gitops.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
//
// Copyright 2021-2022 Red Hat, Inc.
//
// 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 gitops
import (
"context"
"fmt"
"net/url"
"os/exec"
"path/filepath"
"strings"
"github.com/go-logr/logr"
"github.com/redhat-developer/gitops-generator/pkg/util"
"go.uber.org/zap/zapcore"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
"github.com/jenkins-x/go-scm/scm"
"github.com/jenkins-x/go-scm/scm/factory"
gitopsv1alpha1 "github.com/redhat-developer/gitops-generator/api/v1alpha1"
"github.com/spf13/afero"
)
const defaultRepoDescription = "Bootstrapped GitOps Repository based on Components"
type CommandType string
const (
GitCommand CommandType = "git"
RmCommand CommandType = "rm"
unsupportedCmdMsg = "Unsupported command \"%s\" "
)
type Generator interface {
CloneGenerateAndPush(outputPath string, remote string, options gitopsv1alpha1.GeneratorOptions, appFs afero.Afero, branch string, context string, doPush bool) error
CommitAndPush(outputPath string, repoPathOverride string, remote string, componentName string, branch string, commitMessage string) error
GenerateAndPush(outputPath string, remote string, options gitopsv1alpha1.GeneratorOptions, appFs afero.Afero, branch string, doPush bool, createdBy string) error
GenerateOverlaysAndPush(outputPath string, clone bool, remote string, options gitopsv1alpha1.GeneratorOptions, applicationName, environmentName, imageName, namespace string, appFs afero.Afero, branch string, context string, doPush bool, componentGeneratedResources map[string][]string) error
GitRemoveComponent(outputPath string, remote string, componentName string, branch string, context string) error
CloneRepo(outputPath string, remote string, componentName string, branch string) error
GetCommitIDFromRepo(fs afero.Afero, repoPath string) (string, error)
}
// NewGitopsGen returns a Generator implementation
func NewGitopsGen() Gen {
return Gen{
Log: zap.New(zap.UseFlagOptions(&zap.Options{
Development: true,
TimeEncoder: zapcore.ISO8601TimeEncoder,
})),
}
}
func NewGitopsGenWithLogger(log logr.Logger) Gen {
return Gen{
Log: log,
}
}
type Gen struct {
Log logr.Logger
}
// expose as a global variable for the purpose of running mock tests
// only "git" and "rm" are supported
/* #nosec G204 -- used internally to execute various gitops actions and eventual cleanup of artifacts. Calling methods validate user input to ensure commands are used appropriately */
var execute = func(baseDir string, cmd CommandType, args ...string) ([]byte, error) {
if cmd == GitCommand || cmd == RmCommand {
c := exec.Command(string(cmd), args...)
c.Dir = baseDir
output, err := c.CombinedOutput()
return output, err
}
return []byte(""), fmt.Errorf(unsupportedCmdMsg, string(cmd))
}
// CloneGenerateAndPush takes in the following args and generates the gitops resources for a given component
// 1. outputPath: Where to output the gitops resources to
// 2. remote: A string of the form https://$token@<domain>/<org>/<repo>, where <domain> is either github.com or gitlab.com and $token is optional. Corresponds to the component's gitops repository
// 3. options: Options for resource generation
// 4. The filesystem object used to create (either ioutils.NewFilesystem() or ioutils.NewMemoryFilesystem())
// 5. The branch to push to
// 6. The path within the repository to generate the resources in
// 7. The gitops config containing the build bundle;
// Adapted from https://github.com/redhat-developer/kam/blob/master/pkg/pipelines/utils.go#L79
func (s Gen) CloneGenerateAndPush(outputPath string, remote string, options gitopsv1alpha1.GeneratorOptions, appFs afero.Afero, branch string, context string, doPush bool) error {
componentName := options.Name
invalidRemoteErr := util.ValidateRemote(remote)
if invalidRemoteErr != nil {
return invalidRemoteErr
}
s.Log.V(6).Info("Cloning GitOps repository")
if out, err := execute(outputPath, GitCommand, "clone", remote, componentName); err != nil {
return &GitCmdError{path: outputPath, cmdResult: string(out), err: err, cmdType: cloneRepo}
}
s.Log.V(6).Info("GitOps repository cloned")
repoPath := filepath.Join(outputPath, componentName)
gitopsFolder := filepath.Join(repoPath, context)
componentPath := filepath.Join(gitopsFolder, "components", componentName, "base")
// Checkout the specified branch
s.Log.V(6).Info(fmt.Sprintf("Checking out branch %s", branch))
if _, err := execute(repoPath, GitCommand, "switch", branch); err != nil {
if out, err := execute(repoPath, GitCommand, "checkout", "-b", branch); err != nil {
return &GitBranchError{branch: branch, repoPath: repoPath, cmdResult: string(out), err: err, cmdType: checkoutBranch}
}
}
s.Log.V(6).Info(fmt.Sprintf("Branch %s checked out", branch))
if out, err := execute(repoPath, RmCommand, "-rf", filepath.Join("components", componentName, "base")); err != nil {
return &DeleteFolderError{componentPath: filepath.Join("components", componentName, "base"), repoPath: repoPath, cmdResult: string(out), err: err}
}
// Generate the gitops resources and update the parent kustomize yaml file
s.Log.V(6).Info(fmt.Sprintf("Generating GitOps resources under %s", componentPath))
if err := Generate(appFs, gitopsFolder, componentPath, options); err != nil {
return &GitGenResourcesAndOverlaysError{path: componentPath, componentName: componentName, err: err}
}
s.Log.V(6).Info(fmt.Sprintf("GitOps resources generated under %s", componentPath))
if doPush {
s.Log.V(6).Info("Pushing GitOps resources to repository")
return s.CommitAndPush(outputPath, "", remote, componentName, branch, fmt.Sprintf("Generate GitOps base resources for component %s", componentName))
}
return nil
}
// CommitAndPush pushes any new changes to the GitOps repo. The folder should already be cloned in the target output folder.
// 1. outputPath: Where the gitops resources are
// 2. repoPathOverride: The default path is the componentName. Use this to override the default folder.
// 3. remote: A string of the form https://$token@github.com/<org>/<repo>. Corresponds to the component's gitops repository
// 4. componentName: The component name corresponding to a single Component in an Application in AS. eg. component.Name
// 5. The branch to push to
// 6. The path within the repository to generate the resources in
func (s Gen) CommitAndPush(outputPath string, repoPathOverride string, remote string, componentName string, branch string, commitMessage string) error {
invalidRemoteErr := util.ValidateRemote(remote)
if invalidRemoteErr != nil {
return invalidRemoteErr
}
repoPath := filepath.Join(outputPath, componentName)
if repoPathOverride != "" {
repoPath = filepath.Join(outputPath, repoPathOverride)
}
if out, err := execute(repoPath, GitCommand, "add", "."); err != nil {
return &GitAddFilesError{componentName: componentName, repoPath: repoPath, cmdResult: string(out), err: err}
}
if out, err := execute(repoPath, GitCommand, "--no-pager", "diff", "--cached"); err != nil {
return &GitCmdError{path: repoPath, cmdResult: string(out), err: err, cmdType: checkGitDiff}
} else if string(out) != "" {
// Pull from remote if branch is present
if out, err := execute(repoPath, GitCommand, "ls-remote", "--heads", remote, branch); err != nil {
return &GitLsRemoteError{err: err, cmdResult: string(out), remote: remote}
} else if strings.Contains(string(out), "refs/heads/"+branch) {
// only if the git repository contains the branch, pull
if out, err := execute(repoPath, GitCommand, "pull"); err != nil {
return &GitPullError{err: err, cmdResult: string(out), remote: remote}
}
}
// Commit the changes and push
if out, err := execute(repoPath, GitCommand, "commit", "-m", commitMessage); err != nil {
return &GitCmdError{path: repoPath, cmdResult: string(out), err: err, cmdType: commitFiles}
}
if out, err := execute(repoPath, GitCommand, "push", "origin", branch); err != nil {
return &GitCmdError{path: remote, cmdResult: string(out), err: err, cmdType: pushRemote}
}
}
return nil
}
// GenerateAndPush generates a new gitops folder with one component, and optionally pushes to Git. Note: this does not
// clone an existing gitops repo.
// 1. outputPath: Where the gitops resources are
// 2. remote: A string of the form https://$token@github.com/<org>/<repo>. Corresponds to the component's gitops repository
// 3. options: Options for resource generation
// 4. The filesystem object used to create (either ioutils.NewFilesystem() or ioutils.NewMemoryFilesystem())
// 5. The branch to push to
// 6. Optionally push to the GitOps repository or not. Default is not to push.
// 7. createdBy: Use a unique name to identify that clients are generating the GitOps repository. Default is "application-service" and should be overwritten.
func (s Gen) GenerateAndPush(outputPath string, remote string, options gitopsv1alpha1.GeneratorOptions, appFs afero.Afero, branch string, doPush bool, createdBy string) error {
CreatedBy = createdBy
componentName := options.Name
repoPath := filepath.Join(outputPath, options.Application)
// Generate the gitops resources and update the parent kustomize yaml file
gitopsFolder := repoPath
gitHostAccessToken := options.Secret
componentPath := filepath.Join(gitopsFolder, "components", componentName, "base")
if err := Generate(appFs, gitopsFolder, componentPath, options); err != nil {
return &GitGenResourcesAndOverlaysError{path: componentPath, componentName: componentName, err: err}
}
// Commit the changes and push
if doPush {
invalidRemoteErr := util.ValidateRemote(remote)
if invalidRemoteErr != nil {
return invalidRemoteErr
}
gitOpsRepoURL := ""
if options.GitSource != nil {
gitOpsRepoURL = options.GitSource.URL
}
if gitOpsRepoURL == "" {
return fmt.Errorf("the GitOps repo URL is not set")
}
u, err := url.Parse(gitOpsRepoURL)
if err != nil {
return &GitOpsRepoGenError{gitopsURL: gitOpsRepoURL, errMsg: "failed to parse GitOps repo URL %q: %w", err: err}
}
parts := strings.Split(u.Path, "/")
var org, repoName string
//Check length to avoid panic
if len(parts) > 3 {
org = parts[1]
repoName = strings.TrimSuffix(strings.Join(parts[2:], "/"), ".git")
}
u.User = url.UserPassword("", gitHostAccessToken)
client, err := factory.FromRepoURL(u.String())
if err != nil {
return &GitOpsRepoGenError{gitopsURL: gitOpsRepoURL, errMsg: "failed to create a client to access %q: %w", err: err}
}
ctx := context.Background()
// If we're creating the repository in a personal user's account, it's a
// different API call that's made, clearing the org triggers go-scm to use
// the "create repo in personal account" endpoint.
currentUser, _, err := client.Users.Find(ctx)
if err != nil {
return &GitOpsRepoGenUserError{err: err}
}
if currentUser.Login == org {
org = ""
}
ri := &scm.RepositoryInput{
Private: true,
Description: defaultRepoDescription,
Namespace: org,
Name: repoName,
}
_, _, err = client.Repositories.Create(context.Background(), ri)
if err != nil {
repo := fmt.Sprintf("%s/%s", org, repoName)
if org == "" {
repo = fmt.Sprintf("%s/%s", currentUser.Login, repoName)
}
if _, resp, err := client.Repositories.Find(context.Background(), repo); err == nil && resp.Status == 200 {
return fmt.Errorf("failed to create repository, repo already exists")
}
return &GitCreateRepoError{repoName: repoName, org: org, err: err}
}
if out, err := execute(repoPath, GitCommand, "init", "."); err != nil {
return &GitCmdError{path: repoPath, cmdResult: string(out), err: err, cmdType: initializeGit}
}
if out, err := execute(repoPath, GitCommand, "add", "."); err != nil {
return &GitCmdError{path: repoPath, cmdResult: string(out), err: err, cmdType: addComponents}
}
if out, err := execute(repoPath, GitCommand, "commit", "-m", "Generate GitOps resources"); err != nil {
return &GitCmdError{path: repoPath, cmdResult: string(out), err: err, cmdType: commitFiles}
}
if out, err := execute(repoPath, GitCommand, "branch", "-m", branch); err != nil {
return &GitBranchError{branch: branch, repoPath: repoPath, cmdResult: string(out), err: err, cmdType: switchBranch}
}
if out, err := execute(repoPath, GitCommand, "remote", "add", "origin", remote); err != nil {
return &GitAddFilesToRemoteError{componentName: componentName, remoteURL: remote, repoPath: repoPath, cmdResult: string(out), err: err}
}
if out, err := execute(repoPath, GitCommand, "push", "-u", "origin", branch); err != nil {
return &GitCmdError{path: remote, cmdResult: string(out), err: err, cmdType: pushRemote}
}
}
return nil
}
// GenerateOverlaysAndPush generates the overlays kustomize from App Env Snapshot Binding Spec
// 1. outputPath: Where to output the gitops resources to
// 2. clone: Optionally clone the repository first
// 3. remote: A string of the form https://$token@github.com/<org>/<repo>. Corresponds to the component's gitops repository
// 4. options: Options for resource generation
// 5. applicationName: The name of the application
// 6. environmentName: The name of the environment
// 7. imageName: The image name of the source
// 8 namespace: The namespace of the component. This is used in as the namespace of the deployment yaml.
// 9. The filesystem object used to create (either ioutils.NewFilesystem() or ioutils.NewMemoryFilesystem())
// 10. The branch to push to
// 11. The path within the repository to generate the resources in
// 12. Push the changes to the repository or not.
// 13. The gitops config containing the build bundle;
func (s Gen) GenerateOverlaysAndPush(outputPath string, clone bool, remote string, options gitopsv1alpha1.GeneratorOptions, applicationName, environmentName, imageName, namespace string, appFs afero.Afero, branch string, context string, doPush bool, componentGeneratedResources map[string][]string) error {
if clone || doPush {
invalidRemoteErr := util.ValidateRemote(remote)
if invalidRemoteErr != nil {
return invalidRemoteErr
}
}
componentName := options.Name
repoPath := filepath.Join(outputPath, applicationName)
if clone {
if out, err := execute(outputPath, GitCommand, "clone", remote, applicationName); err != nil {
return &GitCmdError{path: outputPath, cmdResult: string(out), err: err, cmdType: cloneRepo}
}
// Checkout the specified branch
if _, err := execute(repoPath, GitCommand, "switch", branch); err != nil {
if out, err := execute(repoPath, GitCommand, "checkout", "-b", branch); err != nil {
return &GitBranchError{branch: branch, repoPath: repoPath, cmdResult: string(out), err: err, cmdType: checkoutBranch}
}
}
}
// Generate the gitops resources and update the parent kustomize yaml file
gitopsFolder := filepath.Join(repoPath, context)
componentEnvOverlaysPath := filepath.Join(gitopsFolder, "components", componentName, "overlays", environmentName)
if err := GenerateOverlays(appFs, gitopsFolder, componentEnvOverlaysPath, options, imageName, namespace, componentGeneratedResources); err != nil {
return &GitGenResourcesAndOverlaysError{path: componentEnvOverlaysPath, componentName: componentName, err: err, cmdType: genOverlays}
}
if doPush {
return s.CommitAndPush(outputPath, applicationName, remote, componentName, branch, fmt.Sprintf("Generate %s environment overlays for component %s", environmentName, componentName))
}
return nil
}
// GitRemoveComponent clones the repo, removes the component, and pushes the changes back to the repository. It takes in the following args and updates the gitops resources by removing the given component
// 1. outputPath: Where to output the gitops resources to
// 2. remote: A string of the form https://$token@<domain>/<org>/<repo>, where <domain> is either github.com or gitlab.com and $token is optional. Corresponds to the component's gitops repository
// 3. componentName: The component name corresponding to a single Component in an Application. eg. component.Name
// 4. The branch to push to
// 5. The path within the repository to generate the resources in
func (s Gen) GitRemoveComponent(outputPath string, remote string, componentName string, branch string, context string) error {
if cloneError := s.CloneRepo(outputPath, remote, componentName, branch); cloneError != nil {
return cloneError
}
if removeComponentError := removeComponent(outputPath, componentName, context); removeComponentError != nil {
return removeComponentError
}
return s.CommitAndPush(outputPath, "", remote, componentName, branch, fmt.Sprintf("Removed component %s", componentName))
}
// CloneRepo clones the repo, and switches to the branch
// 1. outputPath: Where to output the gitops resources to
// 2. remote: A string of the form https://$token@<domain>/<org>/<repo>, where <domain> is either github.com or gitlab.com and $token is optional. Corresponds to the component's gitops repository
// 3. componentName: The component name corresponding to a single Component in an Application. eg. component.Name
// 4. The branch to push to switch to
func (s Gen) CloneRepo(outputPath string, remote string, componentName string, branch string) error {
invalidRemoteErr := util.ValidateRemote(remote)
if invalidRemoteErr != nil {
return invalidRemoteErr
}
repoPath := filepath.Join(outputPath, componentName)
if out, err := execute(outputPath, GitCommand, "clone", remote, componentName); err != nil {
return &GitCmdError{path: outputPath, cmdResult: string(out), err: err, cmdType: cloneRepo}
}
// Checkout the specified branch
if _, err := execute(repoPath, GitCommand, "switch", branch); err != nil {
if out, err := execute(repoPath, GitCommand, "checkout", "-b", branch); err != nil {
return &GitBranchError{branch: branch, repoPath: repoPath, cmdResult: string(out), err: err, cmdType: checkoutBranch}
}
}
return nil
}
// removeComponent removes the component from the local folder. This expects the git repo to be already cloned
// 1. outputPath: Where the gitops repo contents have been cloned
// 2. componentName: The component name corresponding to a single Component in an Application. eg. component.Name
// 3. The path within the repository to generate the resources in
func removeComponent(outputPath string, componentName string, context string) error {
repoPath := filepath.Join(outputPath, componentName)
gitopsFolder := filepath.Join(repoPath, context)
componentPath := filepath.Join(gitopsFolder, "components", componentName)
if out, err := execute(repoPath, RmCommand, "-rf", componentPath); err != nil {
return &DeleteFolderError{componentPath: componentPath, repoPath: repoPath, cmdResult: string(out), err: err}
}
return nil
}
// GetCommitIDFromRepo returns the commit ID for the given repository
func (s Gen) GetCommitIDFromRepo(fs afero.Afero, repoPath string) (string, error) {
var out []byte
var err error
if out, err = execute(repoPath, GitCommand, "rev-parse", "HEAD"); err != nil {
return "", &GitCmdError{path: repoPath, cmdResult: string(out), err: err, cmdType: getCommitID}
}
return string(out), nil
}