-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreatesakubeconfig.sh
executable file
·89 lines (81 loc) · 1.86 KB
/
createsakubeconfig.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
#!/bin/bash
SANAME="testsa"
NSNAME="testns"
namespace() {
kubectl create ns $NSNAME
}
serviceaccount(){
kubectl create serviceaccount $SANAME -n $NSNAME
kubectl apply -f - << EOF
apiVersion: v1
kind: Secret
metadata:
name: ${SANAME}-secret
namespace: ${NSNAME}
annotations:
kubernetes.io/service-account.name: ${SANAME}
type: kubernetes.io/service-account-token
EOF
}
role() {
kubectl apply -f - << EOF
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
namespace: ${NSNAME}
name: ${SANAME}-role
rules:
- apiGroups: [""] # "" indicates the core API group
resources: ["pods"]
verbs: ["get", "watch", "list", "create"]
- apiGroups: [""] # "" indicates the core API group
resources: ["pods/exec"]
verbs: ["create"]
EOF
kubectl apply -f - << EOF
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: ${SANAME}-rb
namespace: ${NSNAME}
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: Role
name: ${SANAME}-role
subjects:
- kind: ServiceAccount
name: ${SANAME}
namespace: ${NSNAME}
EOF
}
kubeconfig() {
clustername=$(kubectl config view -o jsonpath='{.clusters[].name}')
clusterserver=$(kubectl config view -o jsonpath='{.clusters[].cluster.server}')
ca=$(kubectl --namespace="${NSNAME}" get secret/"${SANAME}-secret" -o=jsonpath='{.data.ca\.crt}')
token=$(kubectl --namespace="${NSNAME}" get secret/"${SANAME}-secret" -o=jsonpath='{.data.token}' | base64 --decode)
cat << EOF > kubeconfig
apiVersion: v1
kind: Config
clusters:
- name: ${clustername}
cluster:
certificate-authority-data: ${ca}
server: ${clusterserver}
contexts:
- name: ${SANAME}@${clustername}
context:
cluster: ${clustername}
namespace: ${NSNAME}
user: ${SANAME}
users:
- name: ${SANAME}
user:
token: ${token}
current-context: ${SANAME}@${clustername}
EOF
}
set -o errexit
namespace
serviceaccount
role
kubeconfig