-
Notifications
You must be signed in to change notification settings - Fork 339
/
account.go
67 lines (54 loc) · 1.54 KB
/
account.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
package inventory
import (
"fmt"
v1 "k8s.io/api/core/v1"
"github.com/jaegertracing/jaeger-operator/pkg/util"
)
// Account represents the service account inventory based on the current and desired states
type Account struct {
Create []v1.ServiceAccount
Update []v1.ServiceAccount
Delete []v1.ServiceAccount
}
// ForAccounts builds a new Account inventory based on the existing and desired states
func ForAccounts(existing []v1.ServiceAccount, desired []v1.ServiceAccount) Account {
update := []v1.ServiceAccount{}
mcreate := accountMap(desired)
mdelete := accountMap(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.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 Account{
Create: accountList(mcreate),
Update: update,
Delete: accountList(mdelete),
}
}
func accountMap(deps []v1.ServiceAccount) map[string]v1.ServiceAccount {
m := map[string]v1.ServiceAccount{}
for _, d := range deps {
m[fmt.Sprintf("%s.%s", d.Namespace, d.Name)] = d
}
return m
}
func accountList(m map[string]v1.ServiceAccount) []v1.ServiceAccount {
l := []v1.ServiceAccount{}
for _, v := range m {
l = append(l, v)
}
return l
}