forked from mhausenblas/rbIAM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
k8s.go
178 lines (168 loc) · 4.18 KB
/
k8s.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
168
169
170
171
172
173
174
175
176
177
178
package main
import (
"encoding/json"
"fmt"
"strings"
"github.com/mhausenblas/kubecuddler"
)
// namespaceit joins Kubernetes namespace and name.
func namespaceit(ns, name string) string {
return fmt.Sprintf("%v:%v", ns, name)
}
// kubeIdentity queries the local Kube config to retrieve the configuration.
func (ag *AccessGraph) kubeIdentity() error {
res, err := kubecuddler.Kubectl(false, false, "", "config", "view", "--minify", "--output", "json")
if err != nil {
return err
}
sr := strings.NewReader(res)
decoder := json.NewDecoder(sr)
kconf := &Config{}
err = decoder.Decode(kconf)
if err != nil {
return err
}
ag.KubeConfig = kconf
return nil
}
// kubeServiceAccounts retrieves the service accounts in the cluster.
func (ag *AccessGraph) kubeServiceAccounts() error {
res, err := kubecuddler.Kubectl(false, false, "", "get", "sa", "--all-namespaces", "--output", "json")
if err != nil {
return err
}
sr := strings.NewReader(res)
decoder := json.NewDecoder(sr)
sal := ServiceAccountList{}
err = decoder.Decode(&sal)
if err != nil {
return err
}
ag.ServiceAccounts = make(map[string]ServiceAccount)
for _, sa := range sal.Items {
ag.ServiceAccounts[namespaceit(sa.Namespace, sa.Name)] = sa
}
return nil
}
// formatSA provides a textual rendering of the service account.
func formatSA(sa *ServiceAccount) string {
var secrets strings.Builder
for _, sec := range sa.Secrets {
secrets.WriteString(sec.Name + " ")
}
return fmt.Sprintf(
" Namespace: %v\n"+
" Name: %v\n"+
" Secrets: %v\n",
sa.Namespace,
sa.Name,
secrets.String(),
)
}
// kubeSecrets retrieves the secrets in the cluster.
func (ag *AccessGraph) kubeSecrets() error {
res, err := kubecuddler.Kubectl(false, false, "", "get", "secrets", "--all-namespaces", "--output", "json")
if err != nil {
return err
}
sr := strings.NewReader(res)
decoder := json.NewDecoder(sr)
secl := SecretList{}
err = decoder.Decode(&secl)
if err != nil {
return err
}
ag.Secrets = make(map[string]Secret)
for _, secret := range secl.Items {
ag.Secrets[namespaceit(secret.Namespace, secret.Name)] = secret
}
return nil
}
// formatSecret provides a textual rendering of the secret.
func formatSecret(secret *Secret) string {
strdatamap := ""
// case of string data present:
if len(secret.StringData) != 0 {
for k, v := range secret.StringData {
strdatamap += fmt.Sprintf("%v: %v ", k, v)
}
}
datamap := ""
// case of data present:
if len(secret.Data) != 0 {
for k, v := range secret.Data {
datamap += fmt.Sprintf("\n %v: %v ", k, string(v))
}
}
return fmt.Sprintf(
" Namespace: %v\n"+
" Name: %v\n"+
" Type: %v\n"+
" String data: %v\n"+
" Data: %v\n",
secret.Namespace,
secret.Name,
secret.Type,
strdatamap,
datamap,
)
}
// kubePods retrieves the pods in the cluster.
func (ag *AccessGraph) kubePods() error {
res, err := kubecuddler.Kubectl(false, false, "", "get", "pods", "--all-namespaces", "--output", "json")
if err != nil {
return err
}
sr := strings.NewReader(res)
decoder := json.NewDecoder(sr)
podl := PodList{}
err = decoder.Decode(&podl)
if err != nil {
return err
}
ag.Pods = make(map[string]Pod)
for _, pod := range podl.Items {
ag.Pods[namespaceit(pod.Namespace, pod.Name)] = pod
}
return nil
}
// formatPod provides a textual rendering of the pod.
func formatPod(pod *Pod) string {
strcontainers := ""
for _, container := range pod.Spec.Containers {
strcontainers += fmt.Sprintf(
" Name: %v\n"+
" Image pull policy: %v\n"+
" Image: %v\n"+
" Command: %v\n"+
" Args: %v\n"+
" Env: %v",
container.Name,
container.ImagePullPolicy,
container.Image,
container.Command,
container.Args,
container.Env,
)
}
return fmt.Sprintf(
" Namespace: %v\n"+
" Name: %v\n"+
" Service account: %v\n"+
" Image pull secrets: %v\n"+
" Volumes: %v\n"+
" Containers:\n %v\n"+
" Host IP: %v\n"+
" Pod IP: %v\n"+
" Phase: %v\n",
pod.Namespace,
pod.Name,
pod.Spec.ServiceAccountName,
pod.Spec.ImagePullSecrets,
pod.Spec.Volumes,
strcontainers,
pod.Status.HostIP,
pod.Status.PodIP,
pod.Status.Phase,
)
}