-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.go
466 lines (404 loc) · 12.6 KB
/
utils.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
package api
import (
"encoding/json"
"errors"
"fmt"
"net/url"
"strings"
"github.com/taion809/haikunator"
synv1alpha1 "github.com/projectsyn/lieutenant-operator/api/v1alpha1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
const (
// ClusterIDPrefix is prefixed to all cluster IDs
ClusterIDPrefix = "c-"
// TenantIDPrefix is prefixed to all tenant IDs
TenantIDPrefix = "t-"
// ContentJSONPatch is the content type to do JSON updates
ContentJSONPatch = "application/merge-patch+json"
)
var (
h = haikunator.NewHaikunator()
)
// GenerateClusterID creates a new cluster id
func GenerateClusterID() (ClusterId, error) {
id, err := generateID(ClusterIDPrefix)
return ClusterId{
Id: &id,
}, err
}
// GenerateTenantID creates a new tenant id
func GenerateTenantID() (TenantId, error) {
id, err := generateID(TenantIDPrefix)
return TenantId{
Id: &id,
}, err
}
// GenerateID generates a new id from random alphanumeric characters
func generateID(prefix string) (Id, error) {
retry := 10
var id = ""
var i int
// let's try a few times
for i = 0; i < retry; i++ {
id = prefix + h.Haikunate()
if len(id) <= 63 {
return Id(id), nil
}
}
return "", errors.New("could not generate a DNS-compatible ID")
}
// NewAPITenantFromCRD transforms a CRD tenant into the API representation
func NewAPITenantFromCRD(tenant synv1alpha1.Tenant) *Tenant {
id := Id(tenant.Name)
apiTenant := &Tenant{
TenantId: TenantId{Id: &id},
TenantProperties: TenantProperties{
GitRepo: &RevisionedGitRepo{},
},
}
if len(tenant.Annotations) > 0 {
apiTenant.Annotations = &Annotations{}
for key, val := range tenant.Annotations {
(*apiTenant.Annotations)[key] = val
}
}
if len(tenant.Spec.DisplayName) > 0 {
apiTenant.DisplayName = &tenant.Spec.DisplayName
}
if len(tenant.Spec.GitRepoURL) > 0 {
apiTenant.GitRepo.Url = &tenant.Spec.GitRepoURL
}
if len(tenant.Spec.GitRepoRevision) > 0 {
apiTenant.GitRepo.Revision = Revision{&tenant.Spec.GitRepoRevision}
}
if len(tenant.Spec.GlobalGitRepoURL) > 0 {
apiTenant.GlobalGitRepoURL = &tenant.Spec.GlobalGitRepoURL
}
if len(tenant.Spec.GlobalGitRepoRevision) > 0 {
apiTenant.GlobalGitRepoRevision = &tenant.Spec.GlobalGitRepoRevision
}
if tenant.Spec.GitRepoTemplate != nil {
if len(tenant.Spec.GitRepoTemplate.RepoType) > 0 {
repoType := string(tenant.Spec.GitRepoTemplate.RepoType)
apiTenant.GitRepo.Type = &repoType
}
}
return apiTenant
}
// NewCRDFromAPITenant transforms an API tenant into the CRD representation
func NewCRDFromAPITenant(apiTenant Tenant) (*synv1alpha1.Tenant, error) {
if !strings.HasPrefix(apiTenant.Id.String(), TenantIDPrefix) {
if apiTenant.Id.String() == "" {
id, err := GenerateTenantID()
if err != nil {
return nil, err
}
apiTenant.TenantId = id
} else {
id := TenantIDPrefix + *apiTenant.Id
apiTenant.Id = &id
}
}
if apiTenant.GitRepo == nil ||
apiTenant.GitRepo.Url == nil ||
*apiTenant.GitRepo.Url == "" {
return nil, fmt.Errorf("GitRepo URL is required")
}
tenant := &synv1alpha1.Tenant{
ObjectMeta: metav1.ObjectMeta{
Name: apiTenant.Id.String(),
Annotations: map[string]string{},
},
}
if apiTenant.GitRepo != nil {
tmpl, err := newGitRepoTemplate(&apiTenant.GitRepo.GitRepo, apiTenant.Id.String())
if err != nil {
return nil, fmt.Errorf("failed to create git repo template: %w", err)
}
tenant.Spec.GitRepoTemplate = tmpl
}
SyncCRDFromAPITenant(apiTenant.TenantProperties, tenant)
return tenant, nil
}
func SyncCRDFromAPITenant(source TenantProperties, target *synv1alpha1.Tenant) {
if source.Annotations != nil {
if target.Annotations == nil {
target.Annotations = map[string]string{}
}
for key, val := range *source.Annotations {
if str, ok := val.(string); ok {
target.Annotations[key] = str
}
}
}
if source.DisplayName != nil {
target.Spec.DisplayName = *source.DisplayName
}
if source.GitRepo != nil {
if source.GitRepo.Url != nil {
target.Spec.GitRepoURL = *source.GitRepo.Url
}
if source.GitRepo.Revision.Revision != nil {
target.Spec.GitRepoRevision = *source.GitRepo.Revision.Revision
}
}
if source.GlobalGitRepoURL != nil {
target.Spec.GlobalGitRepoURL = *source.GlobalGitRepoURL
}
if source.GlobalGitRepoRevision != nil {
target.Spec.GlobalGitRepoRevision = *source.GlobalGitRepoRevision
}
}
// NewAPIClusterFromCRD transforms a CRD cluster into the API representation
func NewAPIClusterFromCRD(cluster synv1alpha1.Cluster) (*Cluster, error) {
id := Id(cluster.Name)
apiCluster := &Cluster{
ClusterId: ClusterId{Id: &id},
ClusterProperties: ClusterProperties{
GitRepo: &GitRepo{},
},
ClusterTenant: ClusterTenant{Tenant: cluster.Spec.TenantRef.Name},
}
if len(cluster.Annotations) > 0 {
apiCluster.Annotations = &Annotations{}
for key, val := range cluster.Annotations {
(*apiCluster.Annotations)[key] = val
}
}
if len(cluster.Spec.DisplayName) > 0 {
apiCluster.DisplayName = &cluster.Spec.DisplayName
}
if len(cluster.Spec.GitRepoURL) > 0 {
apiCluster.GitRepo.Url = &cluster.Spec.GitRepoURL
}
if len(cluster.Spec.GitHostKeys) > 0 {
apiCluster.GitRepo.HostKeys = &cluster.Spec.GitHostKeys
}
if len(cluster.Spec.TenantGitRepoRevision) > 0 {
apiCluster.TenantGitRepoRevision = &cluster.Spec.TenantGitRepoRevision
}
if len(cluster.Spec.GlobalGitRepoRevision) > 0 {
apiCluster.GlobalGitRepoRevision = &cluster.Spec.GlobalGitRepoRevision
}
if cluster.Spec.Facts != nil {
facts := ClusterFacts{}
for key, value := range cluster.Spec.Facts {
facts[key] = value
}
apiCluster.Facts = &facts
}
if cluster.Status.Facts != nil {
facts := DynamicClusterFacts{}
for key, value := range cluster.Status.Facts {
facts[key] = unmarshalFact(value)
}
apiCluster.DynamicFacts = &facts
}
acm, err := crdCompileMetaToAPICompileMeta(cluster.Status.CompileMeta)
if err != nil {
return nil, fmt.Errorf("failed to convert compile meta: %w", err)
}
apiCluster.ClusterProperties.CompileMeta = acm
if cluster.Spec.GitRepoTemplate != nil {
if stewardKey, ok := cluster.Spec.GitRepoTemplate.DeployKeys["steward"]; ok {
sshKey := fmt.Sprintf("%s %s", stewardKey.Type, stewardKey.Key)
apiCluster.GitRepo.DeployKey = &sshKey
}
if len(cluster.Spec.GitRepoTemplate.RepoType) > 0 {
repoType := string(cluster.Spec.GitRepoTemplate.RepoType)
apiCluster.GitRepo.Type = &repoType
}
}
return apiCluster, nil
}
func unmarshalFact(fact string) interface{} {
var intFact interface{}
err := json.Unmarshal([]byte(fact), &intFact)
if err != nil {
// The given string is not a JSON value
// Fall back to returning the raw string
return fact
}
return intFact
}
// NewCRDFromAPICluster transforms an API cluster into the CRD representation
func NewCRDFromAPICluster(apiCluster Cluster) (*synv1alpha1.Cluster, error) {
if !strings.HasPrefix(apiCluster.Id.String(), ClusterIDPrefix) {
if apiCluster.Id.String() == "" {
id, err := GenerateClusterID()
if err != nil {
return nil, err
}
apiCluster.ClusterId = id
} else {
id := Id(ClusterIDPrefix + apiCluster.Id.String())
apiCluster.Id = &id
}
}
cluster := &synv1alpha1.Cluster{
ObjectMeta: metav1.ObjectMeta{
Name: apiCluster.Id.String(),
Annotations: map[string]string{},
},
Spec: synv1alpha1.ClusterSpec{
TenantRef: corev1.LocalObjectReference{
Name: apiCluster.Tenant,
},
},
}
tmpl, err := newGitRepoTemplate(apiCluster.GitRepo, apiCluster.Id.String())
if err != nil {
return nil, fmt.Errorf("failed to create git repo template: %w", err)
}
cluster.Spec.GitRepoTemplate = tmpl
if apiCluster.GitRepo != nil && apiCluster.GitRepo.HostKeys != nil {
cluster.Spec.GitHostKeys = *apiCluster.GitRepo.HostKeys
}
err = SyncCRDFromAPICluster(apiCluster.ClusterProperties, cluster)
return cluster, err
}
func SyncCRDFromAPICluster(source ClusterProperties, target *synv1alpha1.Cluster) error {
if source.Annotations != nil {
if target.Annotations == nil {
target.Annotations = map[string]string{}
}
for key, val := range *source.Annotations {
if str, ok := val.(string); ok {
target.Annotations[key] = str
}
}
}
if source.DisplayName != nil {
target.Spec.DisplayName = *source.DisplayName
}
if source.GitRepo != nil {
if source.GitRepo.Url != nil {
target.Spec.GitRepoURL = *source.GitRepo.Url
}
if source.GitRepo.HostKeys != nil {
target.Spec.GitHostKeys = *source.GitRepo.HostKeys
}
if source.GitRepo.DeployKey != nil {
if target.Spec.GitRepoTemplate == nil {
return fmt.Errorf("Cannot set deploy key for unmanaged git repo")
}
if target.Spec.GitRepoTemplate.DeployKeys == nil {
target.Spec.GitRepoTemplate.DeployKeys = make(map[string]synv1alpha1.DeployKey)
}
k := strings.Split(*source.GitRepo.DeployKey, " ")
if len(k) != 2 {
return fmt.Errorf("Illegal deploy key format. Expected '<type> <public key>'")
}
target.Spec.GitRepoTemplate.DeployKeys["steward"] = synv1alpha1.DeployKey{
Type: k[0],
Key: k[1],
WriteAccess: false,
}
}
}
if source.TenantGitRepoRevision != nil {
target.Spec.TenantGitRepoRevision = *source.TenantGitRepoRevision
}
if source.GlobalGitRepoRevision != nil {
target.Spec.GlobalGitRepoRevision = *source.GlobalGitRepoRevision
}
if source.Facts != nil {
if target.Spec.Facts == nil {
target.Spec.Facts = synv1alpha1.Facts{}
}
for key, value := range *source.Facts {
if valueStr, ok := value.(string); ok {
target.Spec.Facts[key] = valueStr
}
}
}
if source.DynamicFacts != nil {
if target.Status.Facts == nil {
target.Status.Facts = synv1alpha1.Facts{}
}
for key, value := range *source.DynamicFacts {
encodedFact, err := json.Marshal(value)
if err != nil {
return err
}
target.Status.Facts[key] = string(encodedFact)
}
}
if source.CompileMeta != nil {
clcm, err := apiCompileMetaToCRDCompileMeta(source.CompileMeta)
if err != nil {
return fmt.Errorf("failed to convert compile meta: %w", err)
}
target.Status.CompileMeta = clcm
}
return nil
}
func newGitRepoTemplate(repo *GitRepo, name string) (*synv1alpha1.GitRepoTemplate, error) {
if repo == nil {
// No git info was specified
return nil, nil
}
if repo.Type == nil || *repo.Type != string(synv1alpha1.UnmanagedRepoType) {
if repo.Url != nil && len(*repo.Url) > 0 {
// It's not unmanaged and the URL was specified, take it apart
url, err := url.Parse(*repo.Url)
if err != nil {
return nil, fmt.Errorf("failed to parse git repo URL: %w", err)
}
pathParts := strings.Split(url.Path, "/")
pathParts = pathParts[1:]
if len(pathParts) < 2 {
return nil, fmt.Errorf("failed to parse git repo URL, expected 2+ path elements in '%s'", url.Path)
}
// remove .git extension
repoName := strings.ReplaceAll(pathParts[len(pathParts)-1], ".git", "")
repoPath := strings.Join(pathParts[:len(pathParts)-1], "/")
return &synv1alpha1.GitRepoTemplate{
RepoType: synv1alpha1.AutoRepoType,
Path: repoPath,
RepoName: repoName,
}, nil
}
} else if repo.Type != nil {
// Repo is unmanaged, remove name and path
return &synv1alpha1.GitRepoTemplate{
RepoType: synv1alpha1.UnmanagedRepoType,
}, nil
}
return nil, nil
}
// crdCompileMetaToAPICompileMeta converts a CRD compile meta to an API compile meta.
// Uses json marshalling to convert the structs since their codegen representations are very different.
// Errors only if the marshalling fails.
func crdCompileMetaToAPICompileMeta(crdCompileMeta synv1alpha1.CompileMeta) (*ClusterCompileMeta, error) {
j, err := json.Marshal(crdCompileMeta)
if err != nil {
return nil, fmt.Errorf("failed to marshal compile meta for conversion: %w", err)
}
var apiCompileMeta ClusterCompileMeta
err = json.Unmarshal(j, &apiCompileMeta)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal compile meta for conversion: %w", err)
}
return &apiCompileMeta, nil
}
// apiCompileMetaToCRDCompileMeta converts an API compile meta to a CRD compile meta.
// Uses json marshalling to convert the structs since their codegen representations are very different.
// Errors only if the marshalling fails.
func apiCompileMetaToCRDCompileMeta(apiCompileMeta *ClusterCompileMeta) (synv1alpha1.CompileMeta, error) {
if apiCompileMeta == nil {
return synv1alpha1.CompileMeta{}, nil
}
j, err := json.Marshal(apiCompileMeta)
if err != nil {
return synv1alpha1.CompileMeta{}, fmt.Errorf("failed to marshal compile meta for conversion: %w", err)
}
var crdCompileMeta synv1alpha1.CompileMeta
err = json.Unmarshal(j, &crdCompileMeta)
if err != nil {
return synv1alpha1.CompileMeta{}, fmt.Errorf("failed to unmarshal compile meta for conversion: %w", err)
}
return crdCompileMeta, nil
}