forked from dmrub/volcano-k8s-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate-kubectl-access-rbac.sh
executable file
·161 lines (146 loc) · 3.85 KB
/
generate-kubectl-access-rbac.sh
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
#!/usr/bin/env bash
set -eou pipefail
error() {
echo >&2 "* Error: $*"
}
fatal() {
error "$@"
exit 1
}
message() {
echo >&2 "$*"
}
print-help() {
echo "$(basename "$0") [<options>] NAMESPACE
Creates kubectl-access ServiceAccess object and RBAC rules for it in the specified namespace.
ServiceAccess kubectl-access will allow the Kubernetes pod to access all resources in the namespace.
By default, the generated objects are output to stdout.
options:
-o,--output= FILENAME Output generated RBAC rules to file FILENAME instead of stdout
--help Display this help and exit
-- End of options
"
}
OUTPUT=
while [[ $# -gt 0 ]]; do
case "$1" in
--help)
print-help
exit
;;
-o | --output)
OUTPUT="$2"
shift 2
;;
--output=*)
OUTPUT="${1#*=}"
shift
;;
--)
shift
break
;;
-*)
fatal "Unknown option $1"
;;
*)
break
;;
esac
done
if [[ $# -ne 1 ]]; then
error "Single namespace argument required"
print-help
exit 1
fi
NS=$1
message "Set namespace to '$NS'"
print-rbac() {
echo "---
apiVersion: v1
kind: ServiceAccount
metadata:
name: kubectl-access
namespace: ${NS}
---
kind: Role
apiVersion: rbac.authorization.k8s.io/v1
metadata:
namespace: ${NS}
name: cluster-ns-owner
rules:
- apiGroups: [\"\"]
resources: [\"nodes\", \"namespaces\", \"secrets\", \"configmaps\"]
verbs: [\"list\", \"get\", \"watch\"]
- apiGroups: [\"\", \"extensions\", \"apps\"]
resources: [\"deployments\", \"replicasets\", \"pods\", \"persistentvolumeclaims\", \"services\", \"endpoints\", \"replicationcontrollers\", \"daemonsets\", \"statefulsets\", \"events\", \"secrets\", \"configmaps\", \"deployments/scale\"]
verbs: [\"get\", \"list\", \"watch\", \"create\", \"update\", \"patch\", \"delete\"]
- apiGroups: [\"autoscaling\"]
resources: [\"horizontalpodautoscalers\"]
verbs: [\"get\", \"list\", \"watch\", \"create\", \"update\", \"patch\", \"delete\"]
- apiGroups: [\"\"]
resources: [\"pods\", \"pods/log\"]
verbs: [\"get\", \"list\", \"watch\"]
- apiGroups: [\"\"]
resources: [\"pods/exec\", \"pods/portforward\", \"pods/attach\"]
verbs: [\"create\"]
- apiGroups: [\"batch\"]
resources: [\"jobs\", \"cronjobs\"]
verbs: [\"get\", \"list\", \"watch\", \"create\", \"update\", \"patch\", \"delete\"]
- apiGroups: [\"batch.volcano.sh\"]
resources: [\"jobs\", \"cronjobs\"]
verbs: [\"get\", \"list\", \"watch\", \"create\", \"update\", \"patch\", \"delete\"]
- apiGroups: [\"metrics.k8s.io\"]
resources: [\"pods\"]
verbs: [\"get\", \"list\", \"watch\"]
- apiGroups: [\"kubeflow.org\"]
resources: [\"poddefaults\"]
verbs: [\"get\", \"list\", \"watch\", \"create\", \"update\", \"patch\", \"delete\"]
---
kind: RoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: cluster-ns-owner-rb
namespace: ${NS}
subjects:
- kind: ServiceAccount
name: kubectl-access
namespace: ${NS}
roleRef:
kind: Role
name: cluster-ns-owner
apiGroup: \"\"
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
annotations:
rbac.authorization.kubernetes.io/autoupdate: \"true\"
name: \"${NS}-cluster-ns-owner\"
rules:
- apiGroups: [\"\"]
resources: [\"nodes\"]
verbs: [\"get\", \"watch\", \"list\"]
- apiGroups: [\"metrics.k8s.io\"]
resources: [\"nodes\"]
verbs: [\"get\", \"list\", \"watch\"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: \"${NS}-cluster-ns-owner-crb\"
subjects:
- kind: ServiceAccount
name: kubectl-access
namespace: ${NS}
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: \"${NS}-cluster-ns-owner\""
}
if [[ -n "$OUTPUT" && "$OUTPUT" != "-" ]]; then
message "Output to file '$OUTPUT'"
print-rbac >"$OUTPUT"
else
print-rbac
fi