forked from derailed/k9s
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsa.go
67 lines (55 loc) · 1.8 KB
/
sa.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
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of K9s
package xray
import (
"context"
"fmt"
"github.com/derailed/k9s/internal"
"github.com/derailed/k9s/internal/client"
"github.com/derailed/k9s/internal/dao"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
)
// ServiceAccount represents an xray renderer.
type ServiceAccount struct{}
// Render renders an xray node.
func (s *ServiceAccount) Render(ctx context.Context, ns string, o interface{}) error {
raw, ok := o.(*unstructured.Unstructured)
if !ok {
return fmt.Errorf("ServiceAccount render expecting *Unstructured, but got %T", o)
}
var sa v1.ServiceAccount
err := runtime.DefaultUnstructuredConverter.FromUnstructured(raw.Object, &sa)
if err != nil {
return err
}
f, ok := ctx.Value(internal.KeyFactory).(dao.Factory)
if !ok {
return fmt.Errorf("no factory found in context")
}
node := NewTreeNode("v1/serviceaccounts", client.FQN(sa.Namespace, sa.Name))
parent, ok := ctx.Value(KeyParent).(*TreeNode)
if !ok {
return fmt.Errorf("Expecting a TreeNode but got %T", ctx.Value(KeyParent))
}
parent.Add(node)
for _, sec := range sa.Secrets {
addRef(f, node, "v1/secrets", client.FQN(sa.Namespace, sec.Name), nil)
}
for _, sec := range sa.ImagePullSecrets {
addRef(f, node, "v1/secrets", client.FQN(sa.Namespace, sec.Name), nil)
}
auto, _ := ctx.Value(KeySAAutomount).(*bool)
return s.validate(node, sa, auto)
}
func (*ServiceAccount) validate(node *TreeNode, sa v1.ServiceAccount, auto *bool) error {
node.Extras[StatusKey] = OkStatus
if sa.AutomountServiceAccountToken != nil {
node.Extras[InfoKey] = fmt.Sprintf("automount=%t", *sa.AutomountServiceAccountToken)
}
if auto != nil {
node.Extras[InfoKey] = fmt.Sprintf("automount=%t", *auto)
}
return nil
}