-
Notifications
You must be signed in to change notification settings - Fork 201
/
merger.go
102 lines (90 loc) · 2.75 KB
/
merger.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
/*
* Copyright (c) Microsoft Corporation.
* Licensed under the MIT license.
*/
package merger
import (
"github.com/pkg/errors"
v1 "k8s.io/api/core/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
"github.com/Azure/azure-service-operator/v2/pkg/genruntime"
"github.com/Azure/azure-service-operator/v2/pkg/genruntime/configmaps"
"github.com/Azure/azure-service-operator/v2/pkg/genruntime/secrets"
)
func MergeObjects(objs []client.Object) ([]client.Object, error) {
// We have special handling for secrets and configmaps
var secretSlice []*v1.Secret
var configMapSlice []*v1.ConfigMap
var other []client.Object
var namespace string
for _, obj := range objs {
if namespace == "" {
namespace = obj.GetNamespace()
}
switch typedObj := obj.(type) {
case *v1.Secret:
if namespace != obj.GetNamespace() {
return nil, errors.Errorf("cannot merge objects from different namespaces: %s : %s", namespace, obj.GetNamespace())
}
secretSlice = append(secretSlice, typedObj)
case *v1.ConfigMap:
if namespace != obj.GetNamespace() {
return nil, errors.Errorf("cannot merge objects from different namespaces: %s : %s", namespace, obj.GetNamespace())
}
configMapSlice = append(configMapSlice, typedObj)
default:
other = append(other, typedObj)
}
}
mergedSecrets, err := mergeSecrets(namespace, secretSlice)
if err != nil {
return nil, errors.Wrap(err, "failed merging secrets")
}
mergedConfigMaps, err := mergeConfigMaps(namespace, configMapSlice)
if err != nil {
return nil, errors.Wrap(err, "failed merging config maps")
}
result := append(other, secrets.SliceToClientObjectSlice(mergedSecrets)...)
result = append(result, configmaps.SliceToClientObjectSlice(mergedConfigMaps)...)
return result, nil
}
func mergeSecrets(namespace string, s []*v1.Secret) ([]*v1.Secret, error) {
collector := secrets.NewCollector(namespace)
for _, secret := range s {
for key, value := range secret.StringData {
collector.AddValue(
&genruntime.SecretDestination{
Name: secret.Name,
Key: key,
}, value)
}
for key, value := range secret.Data {
collector.AddBinaryValue(
&genruntime.SecretDestination{
Name: secret.Name,
Key: key,
}, value)
}
}
return collector.Values()
}
func mergeConfigMaps(namespace string, c []*v1.ConfigMap) ([]*v1.ConfigMap, error) {
collector := configmaps.NewCollector(namespace)
for _, configMap := range c {
for key, value := range configMap.Data {
collector.AddValue(
&genruntime.ConfigMapDestination{
Name: configMap.Name,
Key: key,
}, value)
}
for key, value := range configMap.BinaryData {
collector.AddBinaryValue(
&genruntime.ConfigMapDestination{
Name: configMap.Name,
Key: key,
}, value)
}
}
return collector.Values()
}