-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
rbac_subject.go
94 lines (79 loc) 路 1.78 KB
/
rbac_subject.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
package dao
import (
"context"
"errors"
"github.com/derailed/k9s/internal"
"github.com/derailed/k9s/internal/render"
"k8s.io/apimachinery/pkg/runtime"
)
var (
_ Accessor = (*Subject)(nil)
_ Nuker = (*Subject)(nil)
)
// Subject represents a subject model.
type Subject struct {
Resource
}
// List returns a collection of subjects.
func (s *Subject) List(ctx context.Context, ns string) ([]runtime.Object, error) {
kind, ok := ctx.Value(internal.KeySubjectKind).(string)
if !ok {
return nil, errors.New("expecting a SubjectKind")
}
crbs, err := s.listClusterRoleBindings(kind)
if err != nil {
return nil, err
}
rbs, err := s.listRoleBindings(kind)
if err != nil {
return nil, err
}
for _, rb := range rbs {
crbs = crbs.Upsert(rb)
}
oo := make([]runtime.Object, len(crbs))
for i, o := range crbs {
oo[i] = o
}
return oo, nil
}
func (s *Subject) listClusterRoleBindings(kind string) (render.Subjects, error) {
crbs, err := fetchClusterRoleBindings(s.Factory)
if err != nil {
return nil, err
}
oo := make(render.Subjects, 0, len(crbs))
for _, crb := range crbs {
for _, su := range crb.Subjects {
if su.Kind != kind {
continue
}
oo = oo.Upsert(render.SubjectRes{
Name: su.Name,
Kind: "ClusterRoleBinding",
FirstLocation: crb.Name,
})
}
}
return oo, nil
}
func (s *Subject) listRoleBindings(kind string) (render.Subjects, error) {
rbs, err := fetchRoleBindings(s.Factory)
if err != nil {
return nil, err
}
oo := make(render.Subjects, 0, len(rbs))
for _, rb := range rbs {
for _, su := range rb.Subjects {
if su.Kind != kind {
continue
}
oo = oo.Upsert(render.SubjectRes{
Name: su.Name,
Kind: "RoleBinding",
FirstLocation: rb.Name,
})
}
}
return oo, nil
}