-
Notifications
You must be signed in to change notification settings - Fork 53
/
rbac_store.go
126 lines (116 loc) · 3.15 KB
/
rbac_store.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
package crds
import (
"context"
monitoringv1beta1 "github.com/rancher/opni/apis/monitoring/v1beta1"
corev1 "github.com/rancher/opni/pkg/apis/core/v1"
"github.com/rancher/opni/pkg/storage"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
)
func (c *CRDStore) CreateRole(ctx context.Context, role *corev1.Role) error {
err := c.client.Create(ctx, &monitoringv1beta1.Role{
ObjectMeta: metav1.ObjectMeta{
Name: role.Id,
Namespace: c.namespace,
},
Spec: role,
})
if k8serrors.IsAlreadyExists(err) {
return storage.ErrAlreadyExists
}
return err
}
func (c *CRDStore) DeleteRole(ctx context.Context, ref *corev1.Reference) error {
err := c.client.Delete(ctx, &monitoringv1beta1.Role{
ObjectMeta: metav1.ObjectMeta{
Name: ref.Id,
Namespace: c.namespace,
},
})
if k8serrors.IsNotFound(err) {
return storage.ErrNotFound
}
return err
}
func (c *CRDStore) GetRole(ctx context.Context, ref *corev1.Reference) (*corev1.Role, error) {
role := &monitoringv1beta1.Role{}
err := c.client.Get(ctx, client.ObjectKey{
Name: ref.Id,
Namespace: c.namespace,
}, role)
if err != nil {
if k8serrors.IsNotFound(err) {
return nil, storage.ErrNotFound
}
return nil, err
}
return role.Spec, nil
}
func (c *CRDStore) CreateRoleBinding(ctx context.Context, rb *corev1.RoleBinding) error {
err := c.client.Create(ctx, &monitoringv1beta1.RoleBinding{
ObjectMeta: metav1.ObjectMeta{
Name: rb.Id,
Namespace: c.namespace,
},
Spec: rb,
})
if k8serrors.IsAlreadyExists(err) {
return storage.ErrAlreadyExists
}
return err
}
func (c *CRDStore) DeleteRoleBinding(ctx context.Context, ref *corev1.Reference) error {
err := c.client.Delete(ctx, &monitoringv1beta1.RoleBinding{
ObjectMeta: metav1.ObjectMeta{
Name: ref.Id,
Namespace: c.namespace,
},
})
if k8serrors.IsNotFound(err) {
return storage.ErrNotFound
}
return err
}
func (c *CRDStore) GetRoleBinding(ctx context.Context, ref *corev1.Reference) (*corev1.RoleBinding, error) {
rb := &monitoringv1beta1.RoleBinding{}
err := c.client.Get(ctx, client.ObjectKey{
Name: ref.Id,
Namespace: c.namespace,
}, rb)
if err != nil {
if k8serrors.IsNotFound(err) {
return nil, storage.ErrNotFound
}
return nil, err
}
return rb.Spec, nil
}
func (c *CRDStore) ListRoles(ctx context.Context) (*corev1.RoleList, error) {
list := &monitoringv1beta1.RoleList{}
err := c.client.List(ctx, list, client.InNamespace(c.namespace))
if err != nil {
return nil, err
}
roles := &corev1.RoleList{
Items: make([]*corev1.Role, 0, len(list.Items)),
}
for _, item := range list.Items {
roles.Items = append(roles.Items, item.Spec)
}
return roles, nil
}
func (c *CRDStore) ListRoleBindings(ctx context.Context) (*corev1.RoleBindingList, error) {
list := &monitoringv1beta1.RoleBindingList{}
err := c.client.List(ctx, list, client.InNamespace(c.namespace))
if err != nil {
return nil, err
}
rb := &corev1.RoleBindingList{
Items: make([]*corev1.RoleBinding, 0, len(list.Items)),
}
for _, item := range list.Items {
rb.Items = append(rb.Items, item.Spec)
}
return rb, nil
}