/
reference.go
87 lines (75 loc) · 1.91 KB
/
reference.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
package dao
import (
"context"
"errors"
"github.com/kswapd/k13s/internal"
"github.com/kswapd/k13s/internal/client"
"github.com/kswapd/k13s/internal/render"
"k8s.io/apimachinery/pkg/runtime"
)
var _ Accessor = (*Reference)(nil)
// Reference represents cluster resource references.
type Reference struct {
NonResource
}
// List collects all references.
func (r *Reference) List(ctx context.Context, ns string) ([]runtime.Object, error) {
gvr, ok := ctx.Value(internal.KeyGVR).(string)
if !ok {
return nil, errors.New("No context GVR found")
}
switch gvr {
case "v1/serviceaccounts":
return r.ScanSA(ctx)
default:
return r.Scan(ctx)
}
}
// Get fetch a given reference.
func (r *Reference) Get(ctx context.Context, path string) (runtime.Object, error) {
panic("NYI")
}
// Scan scan cluster resources for references.
func (r *Reference) Scan(ctx context.Context) ([]runtime.Object, error) {
refs, err := ScanForRefs(ctx, r.Factory)
if err != nil {
return nil, err
}
fqn, ok := ctx.Value(internal.KeyPath).(string)
if !ok {
return nil, errors.New("expecting context Path")
}
ns, _ := client.Namespaced(fqn)
oo := make([]runtime.Object, 0, len(refs))
for _, ref := range refs {
_, n := client.Namespaced(ref.FQN)
oo = append(oo, render.ReferenceRes{
Namespace: ns,
Name: n,
GVR: ref.GVR,
})
}
return oo, nil
}
// ScanSA scans for serviceaccount refs.
func (r *Reference) ScanSA(ctx context.Context) ([]runtime.Object, error) {
refs, err := ScanForSARefs(ctx, r.Factory)
if err != nil {
return nil, err
}
fqn, ok := ctx.Value(internal.KeyPath).(string)
if !ok {
return nil, errors.New("expecting context Path")
}
ns, _ := client.Namespaced(fqn)
oo := make([]runtime.Object, 0, len(refs))
for _, ref := range refs {
_, n := client.Namespaced(ref.FQN)
oo = append(oo, render.ReferenceRes{
Namespace: ns,
Name: n,
GVR: ref.GVR,
})
}
return oo, nil
}