-
Notifications
You must be signed in to change notification settings - Fork 12
/
adjustDriver.go
282 lines (245 loc) · 8.08 KB
/
adjustDriver.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
/*
Copyright (C) 2021-2023, Kubefirst
This program is licensed under MIT.
See the LICENSE file for more details.
*/
package providerConfigs
import (
"fmt"
"os"
"strings"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/config"
"github.com/go-git/go-git/v5/plumbing"
"github.com/kubefirst/runtime/pkg"
"github.com/kubefirst/runtime/pkg/gitClient"
cp "github.com/otiai10/copy"
"github.com/rs/zerolog/log"
)
// AdjustGitopsRepo
func AdjustGitopsRepo(
cloudProvider string,
clusterName string,
clusterType string,
gitopsRepoDir string,
gitProvider string,
k1Dir string,
apexContentExists bool,
) error {
//* clean up all other platforms
for _, platform := range pkg.SupportedPlatforms {
if platform != fmt.Sprintf("%s-%s", cloudProvider, gitProvider) {
os.RemoveAll(gitopsRepoDir + "/" + platform)
}
}
//* copy options
opt := cp.Options{
Skip: func(src string) (bool, error) {
if strings.HasSuffix(src, ".git") {
return true, nil
} else if strings.Index(src, "/.terraform") > 0 {
return true, nil
}
//Add more stuff to be ignored here
return false, nil
},
}
//* copy $cloudProvider-$gitProvider/* $HOME/.k1/gitops/
driverContent := fmt.Sprintf("%s/%s-%s/", gitopsRepoDir, cloudProvider, gitProvider)
err := cp.Copy(driverContent, gitopsRepoDir, opt)
if err != nil {
log.Info().Msgf("Error populating gitops repository with driver content: %s. error: %s", fmt.Sprintf("%s-%s", cloudProvider, gitProvider), err.Error())
return err
}
os.RemoveAll(driverContent)
//* copy $HOME/.k1/gitops/cluster-types/${clusterType}/* $HOME/.k1/gitops/registry/${clusterName}
clusterContent := fmt.Sprintf("%s/cluster-types/%s", gitopsRepoDir, clusterType)
// Remove apex content if apex content already exists
if apexContentExists {
log.Warn().Msgf("removing nginx-apex since apexContentExists was %v", apexContentExists)
os.Remove(fmt.Sprintf("%s/nginx-apex.yaml", clusterContent))
os.RemoveAll(fmt.Sprintf("%s/nginx-apex", clusterContent))
} else {
log.Warn().Msgf("will create nginx-apex since apexContentExists was %v", apexContentExists)
}
err = cp.Copy(clusterContent, fmt.Sprintf("%s/registry/%s", gitopsRepoDir, clusterName), opt)
if err != nil {
log.Info().Msgf("Error populating cluster content with %s. error: %s", clusterContent, err.Error())
return err
}
os.RemoveAll(fmt.Sprintf("%s/cluster-types", gitopsRepoDir))
os.RemoveAll(fmt.Sprintf("%s/services", gitopsRepoDir))
return nil
}
// AdjustMetaphorRepo
func AdjustMetaphorRepo(
destinationMetaphorRepoGitURL string,
gitopsRepoDir string,
gitProvider string,
k1Dir string,
) error {
//* create ~/.k1/metaphor
metaphorDir := fmt.Sprintf("%s/metaphor", k1Dir)
os.Mkdir(metaphorDir, 0700)
//* git init
metaphorRepo, err := git.PlainInit(metaphorDir, false)
if err != nil {
return err
}
//* copy options
opt := cp.Options{
Skip: func(src string) (bool, error) {
if strings.HasSuffix(src, ".git") {
return true, nil
} else if strings.Index(src, "/.terraform") > 0 {
return true, nil
}
//Add more stuff to be ignored here
return false, nil
},
}
//* copy ci content
switch gitProvider {
case "github":
//* copy $HOME/.k1/gitops/ci/.github/* $HOME/.k1/metaphor/.github
githubActionsFolderContent := fmt.Sprintf("%s/gitops/ci/.github", k1Dir)
log.Info().Msgf("copying github content: %s", githubActionsFolderContent)
err := cp.Copy(githubActionsFolderContent, fmt.Sprintf("%s/.github", metaphorDir), opt)
if err != nil {
log.Info().Msgf("error populating metaphor repository with %s: %s", githubActionsFolderContent, err)
return err
}
case "gitlab":
//* copy $HOME/.k1/gitops/ci/.gitlab-ci.yml/* $HOME/.k1/metaphor/.github
gitlabCIContent := fmt.Sprintf("%s/gitops/ci/.gitlab-ci.yml", k1Dir)
log.Info().Msgf("copying gitlab content: %s", gitlabCIContent)
err := cp.Copy(gitlabCIContent, fmt.Sprintf("%s/.gitlab-ci.yml", metaphorDir), opt)
if err != nil {
log.Info().Msgf("error populating metaphor repository with %s: %s", gitlabCIContent, err)
return err
}
}
//* metaphor app source
metaphorContent := fmt.Sprintf("%s/metaphor", gitopsRepoDir)
err = cp.Copy(metaphorContent, metaphorDir, opt)
if err != nil {
log.Info().Msgf("Error populating metaphor content with %s. error: %s", metaphorContent, err.Error())
return err
}
//* copy $HOME/.k1/gitops/ci/.argo/* $HOME/.k1/metaphor/.argo
argoWorkflowsFolderContent := fmt.Sprintf("%s/gitops/ci/.argo", k1Dir)
log.Info().Msgf("copying argo workflows content: %s", argoWorkflowsFolderContent)
err = cp.Copy(argoWorkflowsFolderContent, fmt.Sprintf("%s/.argo", metaphorDir), opt)
if err != nil {
log.Info().Msgf("error populating metaphor repository with %s: %s", argoWorkflowsFolderContent, err)
return err
}
//* copy $HOME/.k1/gitops/metaphor/Dockerfile $HOME/.k1/metaphor/build/Dockerfile
dockerfileContent := fmt.Sprintf("%s/Dockerfile", metaphorDir)
os.Mkdir(metaphorDir+"/build", 0700)
log.Info().Msgf("copying dockerfile content: %s", argoWorkflowsFolderContent)
err = cp.Copy(dockerfileContent, fmt.Sprintf("%s/build/Dockerfile", metaphorDir), opt)
if err != nil {
log.Info().Msgf("error populating metaphor repository with %s: %s", argoWorkflowsFolderContent, err)
return err
}
// Remove metaphor content from gitops repository directory
os.RemoveAll(fmt.Sprintf("%s/metaphor", gitopsRepoDir))
err = gitClient.Commit(metaphorRepo, "init commit pre ref change")
if err != nil {
return err
}
metaphorRepo, err = gitClient.SetRefToMainBranch(metaphorRepo)
if err != nil {
return err
}
// remove old git ref
err = metaphorRepo.Storer.RemoveReference(plumbing.NewBranchReferenceName("master"))
if err != nil {
return fmt.Errorf("error removing previous git ref: %s", err)
}
// create remote
_, err = metaphorRepo.CreateRemote(&config.RemoteConfig{
Name: "origin",
URLs: []string{destinationMetaphorRepoGitURL},
})
if err != nil {
return fmt.Errorf("error creating remote for metaphor repository: %s", err)
}
return nil
}
// PrepareGitRepositories
func PrepareGitRepositories(
cloudProvider string,
gitProvider string,
clusterName string,
clusterType string,
destinationGitopsRepoGitURL string,
gitopsDir string,
gitopsTemplateBranch string,
gitopsTemplateURL string,
destinationMetaphorRepoGitURL string,
k1Dir string,
gitopsTokens *GitOpsDirectoryValues,
metaphorDir string,
metaphorTokens *MetaphorTokenValues,
apexContentExists bool,
) error {
//* clone the gitops-template repo
gitopsRepo, err := gitClient.CloneRefSetMain(gitopsTemplateBranch, gitopsDir, gitopsTemplateURL)
if err != nil {
log.Info().Msgf("error opening repo at: %s", gitopsDir)
}
log.Info().Msg("gitops repository clone complete")
// ADJUST CONTENT
//* adjust the content for the gitops repo
err = AdjustGitopsRepo(cloudProvider, clusterName, clusterType, gitopsDir, gitProvider, k1Dir, apexContentExists)
if err != nil {
return err
}
// DETOKENIZE
//* detokenize the gitops repo
DetokenizeGitGitops(gitopsDir, gitopsTokens)
if err != nil {
return err
}
// ADJUST CONTENT
//* adjust the content for the metaphor repo
err = AdjustMetaphorRepo(destinationMetaphorRepoGitURL, gitopsDir, gitProvider, k1Dir)
if err != nil {
return err
}
// DETOKENIZE
//* detokenize the metaphor repo
DetokenizeGitMetaphor(metaphorDir, metaphorTokens)
if err != nil {
return err
}
// COMMIT
//* commit initial gitops-template content
err = gitClient.Commit(gitopsRepo, "committing initial detokenized gitops-template repo content")
if err != nil {
return err
}
//* commit initial metaphor content
metaphorRepo, err := git.PlainOpen(metaphorDir)
if err != nil {
return fmt.Errorf("error opening metaphor git repository: %s", err)
}
err = gitClient.Commit(metaphorRepo, "committing initial detokenized metaphor repo content")
if err != nil {
return err
}
// ADD REMOTE(S)
//* add new remote for gitops repo
err = gitClient.AddRemote(destinationGitopsRepoGitURL, gitProvider, gitopsRepo)
if err != nil {
return err
}
//* add new remote for metaphor repo
err = gitClient.AddRemote(destinationMetaphorRepoGitURL, gitProvider, metaphorRepo)
if err != nil {
return err
}
return nil
}