/
configmap.go
69 lines (56 loc) · 1.54 KB
/
configmap.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
package inventory
import (
"fmt"
v1 "k8s.io/api/core/v1"
"github.com/jaegertracing/jaeger-operator/pkg/util"
)
// ConfigMap represents the config maps inventory based on the current and desired states
type ConfigMap struct {
Create []v1.ConfigMap
Update []v1.ConfigMap
Delete []v1.ConfigMap
}
// ForConfigMaps builds a new Account inventory based on the existing and desired states
func ForConfigMaps(existing []v1.ConfigMap, desired []v1.ConfigMap) ConfigMap {
update := []v1.ConfigMap{}
mcreate := configsMap(desired)
mdelete := configsMap(existing)
for k, v := range mcreate {
if t, ok := mdelete[k]; ok {
tp := t.DeepCopy()
util.InitObjectMeta(tp)
// we can't blindly DeepCopyInto, so, we select what we bring from the new to the old object
tp.Data = v.Data
tp.BinaryData = v.BinaryData
tp.ObjectMeta.OwnerReferences = v.ObjectMeta.OwnerReferences
for k, v := range v.ObjectMeta.Annotations {
tp.ObjectMeta.Annotations[k] = v
}
for k, v := range v.ObjectMeta.Labels {
tp.ObjectMeta.Labels[k] = v
}
update = append(update, *tp)
delete(mcreate, k)
delete(mdelete, k)
}
}
return ConfigMap{
Create: configsList(mcreate),
Update: update,
Delete: configsList(mdelete),
}
}
func configsMap(deps []v1.ConfigMap) map[string]v1.ConfigMap {
m := map[string]v1.ConfigMap{}
for _, d := range deps {
m[fmt.Sprintf("%s.%s", d.Namespace, d.Name)] = d
}
return m
}
func configsList(m map[string]v1.ConfigMap) []v1.ConfigMap {
l := []v1.ConfigMap{}
for _, v := range m {
l = append(l, v)
}
return l
}