-
Notifications
You must be signed in to change notification settings - Fork 1
/
create_k8s_config.sh
executable file
·75 lines (70 loc) · 1.63 KB
/
create_k8s_config.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
#!/bin/bash
echo "
apiVersion: v1
kind: ServiceAccount
metadata:
name: worker-account
" > service_account.yaml
kubectl create -f service_account.yaml
rm service_account.yaml
secrets=$(kubectl get ServiceAccount worker-account -o jsonpath='{.secrets[0].name}')
token=$(kubectl get secrets ${secrets} -o jsonpath='{.data.token}' | base64 --decode)
cluster_name=$(kubectl config current-context)
echo $cluster_name
certificate=$(kubectl config view --flatten -o jsonpath="{.clusters[?(@.name == '$cluster_name')].cluster.certificate-authority-data}")
server_ip=$(kubectl config view --flatten -o jsonpath="{.clusters[?(@.name == '$cluster_name')].cluster.server}")
echo "
kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: worker-role
rules:
- apiGroups:
- '*'
resources:
- '*'
verbs:
- '*'
- nonResourceURLs:
- '*'
verbs:
- '*'
" > role.yaml
kubectl apply -f role.yaml
rm role.yaml
echo "
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: worker-role-binding
subjects:
- kind: ServiceAccount
name: worker-account
namespace: default
apiGroup: ""
roleRef:
kind: ClusterRole
name: worker-role
apiGroup: rbac.authorization.k8s.io
" > role_binding.yaml
kubectl apply -f role_binding.yaml
rm role_binding.yaml
echo "
apiVersion: v1
kind: Config
users:
- name: worker-account
user:
token: ${token}
clusters:
- cluster:
certificate-authority-data: $certificate
server: ${server_ip}
name: ${cluster_name}
contexts:
- context:
cluster: ${cluster_name}
user: worker-account
name: worker-account-context
current-context: worker-account-context
" > config.yaml