-
Notifications
You must be signed in to change notification settings - Fork 345
/
Copy pathdaemonset.go
68 lines (55 loc) · 1.58 KB
/
daemonset.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
package inventory
import (
"fmt"
appsv1 "k8s.io/api/apps/v1"
"github.com/jaegertracing/jaeger-operator/pkg/util"
)
// DaemonSet represents the daemon set inventory based on the current and desired states
type DaemonSet struct {
Create []appsv1.DaemonSet
Update []appsv1.DaemonSet
Delete []appsv1.DaemonSet
}
// ForDaemonSets builds a new daemon set inventory based on the existing and desired states
func ForDaemonSets(existing []appsv1.DaemonSet, desired []appsv1.DaemonSet) DaemonSet {
update := []appsv1.DaemonSet{}
mcreate := daemonsetMap(desired)
mdelete := daemonsetMap(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.Spec = v.Spec
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 DaemonSet{
Create: daemonsetList(mcreate),
Update: update,
Delete: daemonsetList(mdelete),
}
}
func daemonsetMap(deps []appsv1.DaemonSet) map[string]appsv1.DaemonSet {
m := map[string]appsv1.DaemonSet{}
for _, d := range deps {
m[fmt.Sprintf("%s.%s", d.Namespace, d.Name)] = d
}
return m
}
func daemonsetList(m map[string]appsv1.DaemonSet) []appsv1.DaemonSet {
l := []appsv1.DaemonSet{}
for _, v := range m {
l = append(l, v)
}
return l
}