-
Notifications
You must be signed in to change notification settings - Fork 12
/
config_cluster_updates.go
319 lines (258 loc) · 11 KB
/
config_cluster_updates.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
package crossplane
import (
"context"
"encoding/json"
"fmt"
"os"
"path/filepath"
"github.com/intelops/go-common/logging"
"github.com/kube-tarian/kad/capten/common-pkg/k8s"
appConf "github.com/kube-tarian/kad/capten/config-worker/internal/app_config"
fileutil "github.com/kube-tarian/kad/capten/config-worker/internal/file_util"
"github.com/kube-tarian/kad/capten/model"
agentmodel "github.com/kube-tarian/kad/capten/model"
"github.com/pkg/errors"
"gopkg.in/yaml.v2"
)
func getAppNameNamespace(ctx context.Context, fileName string) (string, string, error) {
k8sclient, err := k8s.NewK8SClient(logging.NewLogger())
if err != nil {
return "", "", fmt.Errorf("failed to initalize k8s client: %v", err)
}
data, err := os.ReadFile(fileName)
if err != nil {
return "", "", err
}
jsonData, err := k8s.ConvertYamlToJson(data)
if err != nil {
return "", "", err
}
// For the testing change the reqrepo to template one
ns, resName, err := k8sclient.DynamicClient.GetNameNamespace(jsonData)
if err != nil {
return "", "", fmt.Errorf("failed to create the k8s custom resource: %v", err)
}
return ns, resName, nil
}
func (cp *CrossPlaneApp) configureClusterUpdate(ctx context.Context, req *model.CrossplaneClusterUpdate) (status string, err error) {
logger.Infof("configuring crossplane project for cluster %s update", req.ManagedClusterName)
endpoint, err := cp.helper.CreateCluster(ctx, req.ManagedClusterId, req.ManagedClusterName)
if err != nil {
return string(agentmodel.WorkFlowStatusFailed), errors.WithMessage(err, "failed to CreateCluster in argocd app")
}
logger.Infof("cloning default templates %s to project %s", cp.pluginConfig.TemplateGitRepo, req.RepoURL)
templateRepo, err := cp.helper.CloneTemplateRepo(ctx, cp.pluginConfig.TemplateGitRepo, req.GitProjectId)
if err != nil {
return string(agentmodel.WorkFlowStatusFailed), errors.WithMessage(err, "failed to clone repos")
}
defer os.RemoveAll(templateRepo)
customerRepo, err := cp.helper.CloneUserRepo(ctx, req.RepoURL, req.GitProjectId)
if err != nil {
return string(agentmodel.WorkFlowStatusFailed), errors.WithMessage(err, "failed to clone repos")
}
logger.Infof("cloned default templates to project %s", req.RepoURL)
defer os.RemoveAll(customerRepo)
clusterValuesFile := filepath.Join(customerRepo, cp.pluginConfig.ClusterEndpointUpdates.ClusterValuesFile)
defaultAppListFile := filepath.Join(templateRepo, cp.pluginConfig.ClusterEndpointUpdates.DefaultAppListFile)
err = updateClusterEndpointDetials(clusterValuesFile, req.ManagedClusterName, endpoint, defaultAppListFile)
if err != nil {
return string(agentmodel.WorkFlowStatusFailed), errors.WithMessage(err, "failed to replace the file")
}
defaultAppValPath := filepath.Join(templateRepo, cp.pluginConfig.ClusterEndpointUpdates.DefaultAppValuesPath)
clusterDefaultAppValPath := filepath.Join(customerRepo,
cp.pluginConfig.ClusterEndpointUpdates.ClusterDefaultAppValuesPath, req.ManagedClusterName)
err = cp.syncDefaultAppVaules(req.ManagedClusterName, defaultAppValPath, clusterDefaultAppValPath)
if err != nil {
return string(agentmodel.WorkFlowStatusFailed), errors.WithMessage(err, "failed to sync default app value files")
}
templateValues, err := cp.prepareTemplateVaules(ctx, req.ManagedClusterName)
if err != nil {
return string(agentmodel.WorkFlowStatusFailed), errors.WithMessage(err, "failed to prepare template values")
}
if err := fileutil.UpdateFilesInFolderWithTempaltes(clusterDefaultAppValPath, templateValues); err != nil {
return string(agentmodel.WorkFlowStatusFailed), errors.WithMessage(err, "failed to update default app template values")
}
if err := fileutil.UpdateFileWithTempaltes(clusterValuesFile, templateValues); err != nil {
return string(agentmodel.WorkFlowStatusFailed), errors.WithMessage(err, "failed to update cluster config template values")
}
logger.Infof("default app vaules synched for cluster %s", req.ManagedClusterName)
err = cp.helper.AddFilesToRepo([]string{"."})
if err != nil {
return string(agentmodel.WorkFlowStatusFailed), errors.WithMessage(err, "failed to add git repo")
}
err = cp.helper.CommitRepoChanges()
if err != nil {
return string(agentmodel.WorkFlowStatusFailed), errors.WithMessage(err, "failed to commit git repo")
}
logger.Infof("added cloned project %s changed to git", req.RepoURL)
ns, resName, err := getAppNameNamespace(ctx, filepath.Join(customerRepo, cp.pluginConfig.ClusterEndpointUpdates.MainAppGitPath))
if err != nil {
return string(agentmodel.WorkFlowStatusFailed), errors.WithMessage(err, "failed to get name and namespace from")
}
err = cp.helper.SyncArgoCDApp(ctx, ns, resName)
if err != nil {
return string(agentmodel.WorkFlowStatusFailed), errors.WithMessage(err, "failed to sync argocd app")
}
logger.Infof("synched provider config main-app %s", resName)
err = cp.helper.WaitForArgoCDToSync(ctx, ns, resName)
if err != nil {
return string(agentmodel.WorkFlowStatusFailed), errors.WithMessage(err, "failed to fetch argocd app")
}
err = cp.configureExternalSecretsOnCluster(ctx, req.ManagedClusterName, req.ManagedClusterId,
cp.pluginConfig.ClusterEndpointUpdates.AppRoleTokenVaultPaths,
cp.pluginConfig.ClusterEndpointUpdates.ExternalSecrets)
if err != nil {
logger.Errorf("%v", errors.WithMessage(err, "failed to create cluster secrets"))
}
return string(agentmodel.WorkFlowStatusCompleted), nil
}
func updateClusterEndpointDetials(valuesFileName, clusterName, clusterEndpoint, defaultAppFile string) error {
data, err := os.ReadFile(valuesFileName)
if err != nil {
return err
}
jsonData, err := k8s.ConvertYamlToJson(data)
if err != nil {
return err
}
var clusterConfig ClusterConfigValues
err = json.Unmarshal(jsonData, &clusterConfig)
if err != nil {
return err
}
defaultApps, err := readClusterDefaultApps(defaultAppFile)
if err != nil {
return err
}
clusters := []Cluster{}
if clusterConfig.Clusters != nil {
clusters = *clusterConfig.Clusters
}
var clusterFound bool
for index := range clusters {
if clusters[index].Name == clusterName {
clusters[index] = prepareClusterData(clusterName, clusterEndpoint, defaultApps)
clusterFound = true
break
}
}
if !clusterFound {
clusters = append(clusters, prepareClusterData(clusterName, clusterEndpoint, defaultApps))
}
clusterConfig.Clusters = &clusters
jsonBytes, err := json.Marshal(clusterConfig)
if err != nil {
return err
}
yamlBytes, err := k8s.ConvertJsonToYaml(jsonBytes)
if err != nil {
return err
}
err = os.WriteFile(valuesFileName, yamlBytes, os.ModeAppend)
return err
}
func (cp *CrossPlaneApp) syncDefaultAppVaules(clusterName, defaultAppVaulesPath, clusterDefaultAppVaulesPath string) error {
if err := fileutil.CreateFolderIfNotExist(clusterDefaultAppVaulesPath); err != nil {
return err
}
if err := fileutil.SyncFiles(defaultAppVaulesPath, clusterDefaultAppVaulesPath); err != nil {
return err
}
return nil
}
func (cp *CrossPlaneApp) configureClusterDelete(ctx context.Context, req *model.CrossplaneClusterUpdate) (status string, err error) {
logger.Infof("configuring crossplane project for cluster %s delete", req.ManagedClusterName)
customerRepo, err := cp.helper.CloneUserRepo(ctx, req.RepoURL, req.GitProjectId)
if err != nil {
return string(agentmodel.WorkFlowStatusFailed), errors.WithMessage(err, "failed to clone repos")
}
logger.Infof("cloned default templates to project %s", req.RepoURL)
defer os.RemoveAll(customerRepo)
clusterValuesFile := filepath.Join(customerRepo, cp.pluginConfig.ClusterEndpointUpdates.ClusterValuesFile)
err = removeClusterValues(clusterValuesFile, req.ManagedClusterName)
if err != nil {
return string(agentmodel.WorkFlowStatusFailed), errors.WithMessage(err, "failed to replace the file")
}
dirToDeleteInRepoPath := filepath.Join(".", cp.pluginConfig.ClusterEndpointUpdates.ClusterDefaultAppValuesPath, req.ManagedClusterName)
logger.Infof("removing the cluster '%s' from git repo path %s", req.ManagedClusterName, dirToDeleteInRepoPath)
err = cp.helper.RemoveFilesFromRepo([]string{dirToDeleteInRepoPath})
if err != nil {
return string(agentmodel.WorkFlowStatusFailed), errors.WithMessage(err, "failed to remove from git repo")
}
dirToDelete := filepath.Join(customerRepo, cp.pluginConfig.ClusterEndpointUpdates.ClusterDefaultAppValuesPath, req.ManagedClusterName)
if err := os.RemoveAll(dirToDelete); err != nil {
return string(agentmodel.WorkFlowStatusFailed), errors.WithMessage(err, "failed to remove cluster folder")
}
if err := cp.helper.AddFilesToRepo([]string{"."}); err != nil {
return string(agentmodel.WorkFlowStatusFailed), errors.WithMessage(err, "failed to add git repo")
}
err = cp.helper.CommitRepoChanges()
if err != nil {
return string(agentmodel.WorkFlowStatusFailed), errors.WithMessage(err, "failed to commit git repo")
}
logger.Infof("commited project %s changed to git", req.RepoURL)
return string(agentmodel.WorkFlowStatusCompleted), nil
}
func removeClusterValues(valuesFileName, clusterName string) error {
logger.Infof("for the culster %s, removing the cluster values from %s file", clusterName, valuesFileName)
data, err := os.ReadFile(valuesFileName)
if err != nil {
return err
}
jsonData, err := k8s.ConvertYamlToJson(data)
if err != nil {
return err
}
var clusterConfig ClusterConfigValues
err = json.Unmarshal(jsonData, &clusterConfig)
if err != nil {
return err
}
clusters := []Cluster{}
if clusterConfig.Clusters != nil {
clusters = *clusterConfig.Clusters
}
newclusters := []Cluster{}
for _, cluster := range clusters {
if cluster.Name != clusterName {
newclusters = append(newclusters, cluster)
}
}
clusterConfig.Clusters = &newclusters
jsonBytes, err := json.Marshal(clusterConfig)
if err != nil {
return err
}
yamlBytes, err := k8s.ConvertJsonToYaml(jsonBytes)
if err != nil {
return err
}
err = os.WriteFile(valuesFileName, yamlBytes, os.ModeAppend)
return err
}
func (cp *CrossPlaneApp) prepareTemplateVaules(ctx context.Context, clusterName string) (map[string]string, error) {
val := map[string]string{
appConf.DomainName: cp.cfg.DomainName,
"ClusterName": clusterName,
}
return cp.helper.GetClusterGlobalValues(ctx, val)
}
func prepareClusterData(clusterName, endpoint string, defaultApps []DefaultApps) Cluster {
return Cluster{
Name: clusterName,
Server: endpoint,
DefApps: defaultApps,
}
}
func readClusterDefaultApps(clusterDefaultAppsFile string) ([]DefaultApps, error) {
data, err := os.ReadFile(filepath.Clean(clusterDefaultAppsFile))
if err != nil {
return nil, fmt.Errorf("failed to read default applist File: %s, err: %w", clusterDefaultAppsFile, err)
}
var appList DefaultAppList
err = yaml.Unmarshal(data, &appList)
if err != nil {
return nil, fmt.Errorf("failed to unmarshall default apps file: %s, err: %w", clusterDefaultAppsFile, err)
}
return appList.DefaultApps, nil
}