-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
rbacbackup.go
167 lines (140 loc) · 4.38 KB
/
rbacbackup.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
package apply
import (
rbacv1 "k8s.io/api/rbac/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/tools/cache"
v1 "kubevirt.io/api/core/v1"
"kubevirt.io/client-go/log"
)
func (r *Reconciler) backupRBACs() error {
// Backup existing ClusterRoles
objects := r.stores.ClusterRoleCache.List()
for _, obj := range objects {
cachedCr, ok := obj.(*rbacv1.ClusterRole)
if !ok || !needsBackup(r.kv, r.stores.ClusterRoleCache, &cachedCr.ObjectMeta) {
continue
}
imageTag, imageRegistry, id, ok := getInstallStrategyAnnotations(&cachedCr.ObjectMeta)
if !ok {
continue
}
err := r.backupRBAC(cachedCr.DeepCopy(), cachedCr.Name, string(cachedCr.UID), imageTag, imageRegistry, id)
if err != nil {
return err
}
}
// Backup existing ClusterRoleBindings
objects = r.stores.ClusterRoleBindingCache.List()
for _, obj := range objects {
cachedCrb, ok := obj.(*rbacv1.ClusterRoleBinding)
if !ok || !needsBackup(r.kv, r.stores.ClusterRoleBindingCache, &cachedCrb.ObjectMeta) {
continue
}
imageTag, imageRegistry, id, ok := getInstallStrategyAnnotations(&cachedCrb.ObjectMeta)
if !ok {
continue
}
err := r.backupRBAC(cachedCrb.DeepCopy(), cachedCrb.Name, string(cachedCrb.UID), imageTag, imageRegistry, id)
if err != nil {
return err
}
}
// Backup existing Roles
objects = r.stores.RoleCache.List()
for _, obj := range objects {
cachedCr, ok := obj.(*rbacv1.Role)
if !ok || !needsBackup(r.kv, r.stores.RoleCache, &cachedCr.ObjectMeta) {
continue
}
imageTag, imageRegistry, id, ok := getInstallStrategyAnnotations(&cachedCr.ObjectMeta)
if !ok {
continue
}
err := r.backupRBAC(cachedCr.DeepCopy(), cachedCr.Name, string(cachedCr.UID), imageTag, imageRegistry, id)
if err != nil {
return err
}
}
// Backup existing RoleBindings
objects = r.stores.RoleBindingCache.List()
for _, obj := range objects {
cachedRb, ok := obj.(*rbacv1.RoleBinding)
if !ok || !needsBackup(r.kv, r.stores.RoleBindingCache, &cachedRb.ObjectMeta) {
continue
}
imageTag, imageRegistry, id, ok := getInstallStrategyAnnotations(&cachedRb.ObjectMeta)
if ok {
continue
}
err := r.backupRBAC(cachedRb.DeepCopy(), cachedRb.Name, string(cachedRb.UID), imageTag, imageRegistry, id)
if err != nil {
return err
}
}
return nil
}
func (r *Reconciler) backupRBAC(obj runtime.Object, name, UID, imageTag, imageRegistry, id string) error {
meta := getRbacMetaObject(obj)
*meta = metav1.ObjectMeta{
GenerateName: name,
}
injectOperatorMetadata(r.kv, meta, imageTag, imageRegistry, id, true)
meta.Annotations[v1.EphemeralBackupObject] = UID
// Create backup
createRole := getRbacCreateFunction(r, obj)
err := createRole()
if err != nil {
return err
}
kind := obj.GetObjectKind().GroupVersionKind().Kind
log.Log.V(2).Infof("backup %v %v created", kind, name)
return nil
}
func shouldBackupRBACObject(kv *v1.KubeVirt, objectMeta *metav1.ObjectMeta) bool {
curVersion, curImageRegistry, curID := getTargetVersionRegistryID(kv)
if objectMatchesVersion(objectMeta, curVersion, curImageRegistry, curID, kv.GetGeneration()) {
// matches current target version already, so doesn't need backup
return false
}
if objectMeta.Annotations == nil {
return false
}
_, ok := objectMeta.Annotations[v1.EphemeralBackupObject]
if ok {
// ephemeral backup objects don't need to be backed up because
// they are the backup
return false
}
return true
}
func needsBackup(kv *v1.KubeVirt, cache cache.Store, meta *metav1.ObjectMeta) bool {
shouldBackup := shouldBackupRBACObject(kv, meta)
imageTag, imageRegistry, id, ok := getInstallStrategyAnnotations(meta)
if !shouldBackup || !ok {
return false
}
// loop through cache and determine if there's an ephemeral backup
// for this object already
objects := cache.List()
for _, obj := range objects {
cachedObj, ok := obj.(*metav1.ObjectMeta)
if !ok ||
cachedObj.DeletionTimestamp != nil ||
meta.Annotations == nil {
continue
}
uid, ok := cachedObj.Annotations[v1.EphemeralBackupObject]
if !ok {
// this is not an ephemeral backup object
continue
}
if uid == string(meta.UID) && objectMatchesVersion(cachedObj, imageTag, imageRegistry, id, kv.GetGeneration()) {
// found backup. UID matches and versions match
// note, it's possible for a single UID to have multiple backups with
// different versions
return false
}
}
return true
}