|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# Copyright 2024 The Kubernetes Authors. |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | + |
| 17 | +set -o errexit |
| 18 | +set -o nounset |
| 19 | +set -o pipefail |
| 20 | + |
| 21 | +KUBECONFIG_OUT=${1:-kubeconfig} |
| 22 | +MULTIKUEUE_SA=multikueue-sa |
| 23 | +NAMESPACE=kueue-system |
| 24 | + |
| 25 | +# Creating a restricted MultiKueue role, service account and role binding" |
| 26 | +kubectl apply -f - <<EOF |
| 27 | +apiVersion: v1 |
| 28 | +kind: ServiceAccount |
| 29 | +metadata: |
| 30 | + name: ${MULTIKUEUE_SA} |
| 31 | + namespace: ${NAMESPACE} |
| 32 | +--- |
| 33 | +apiVersion: rbac.authorization.k8s.io/v1 |
| 34 | +kind: ClusterRole |
| 35 | +metadata: |
| 36 | + name: ${MULTIKUEUE_SA}-role |
| 37 | +rules: |
| 38 | +- apiGroups: |
| 39 | + - batch |
| 40 | + resources: |
| 41 | + - jobs |
| 42 | + verbs: |
| 43 | + - create |
| 44 | + - delete |
| 45 | + - get |
| 46 | + - list |
| 47 | + - watch |
| 48 | +- apiGroups: |
| 49 | + - batch |
| 50 | + resources: |
| 51 | + - jobs/status |
| 52 | + verbs: |
| 53 | + - get |
| 54 | +- apiGroups: |
| 55 | + - jobset.x-k8s.io |
| 56 | + resources: |
| 57 | + - jobsets |
| 58 | + verbs: |
| 59 | + - create |
| 60 | + - delete |
| 61 | + - get |
| 62 | + - list |
| 63 | + - watch |
| 64 | +- apiGroups: |
| 65 | + - jobset.x-k8s.io |
| 66 | + resources: |
| 67 | + - jobsets/status |
| 68 | + verbs: |
| 69 | + - get |
| 70 | +- apiGroups: |
| 71 | + - kueue.x-k8s.io |
| 72 | + resources: |
| 73 | + - workloads |
| 74 | + verbs: |
| 75 | + - create |
| 76 | + - delete |
| 77 | + - get |
| 78 | + - list |
| 79 | + - watch |
| 80 | + - update |
| 81 | +- apiGroups: |
| 82 | + - kueue.x-k8s.io |
| 83 | + resources: |
| 84 | + - workloads/status |
| 85 | + verbs: |
| 86 | + - get |
| 87 | + - patch |
| 88 | + - update |
| 89 | +- apiGroups: |
| 90 | + - kubeflow.org |
| 91 | + resources: |
| 92 | + - tfjobs |
| 93 | + verbs: |
| 94 | + - create |
| 95 | + - delete |
| 96 | + - get |
| 97 | + - list |
| 98 | + - watch |
| 99 | +- apiGroups: |
| 100 | + - kubeflow.org |
| 101 | + resources: |
| 102 | + - tfjobs/status |
| 103 | + verbs: |
| 104 | + - get |
| 105 | +- apiGroups: |
| 106 | + - kubeflow.org |
| 107 | + resources: |
| 108 | + - paddlejobs |
| 109 | + verbs: |
| 110 | + - create |
| 111 | + - delete |
| 112 | + - get |
| 113 | + - list |
| 114 | + - watch |
| 115 | +- apiGroups: |
| 116 | + - kubeflow.org |
| 117 | + resources: |
| 118 | + - paddlejobs/status |
| 119 | + verbs: |
| 120 | + - get |
| 121 | +- apiGroups: |
| 122 | + - kubeflow.org |
| 123 | + resources: |
| 124 | + - pytorchjobs |
| 125 | + verbs: |
| 126 | + - create |
| 127 | + - delete |
| 128 | + - get |
| 129 | + - list |
| 130 | + - watch |
| 131 | +- apiGroups: |
| 132 | + - kubeflow.org |
| 133 | + resources: |
| 134 | + - pytorchjobs/status |
| 135 | + verbs: |
| 136 | + - get |
| 137 | +- apiGroups: |
| 138 | + - kubeflow.org |
| 139 | + resources: |
| 140 | + - xgboostjobs |
| 141 | + verbs: |
| 142 | + - create |
| 143 | + - delete |
| 144 | + - get |
| 145 | + - list |
| 146 | + - watch |
| 147 | +- apiGroups: |
| 148 | + - kubeflow.org |
| 149 | + resources: |
| 150 | + - xgboostjobs/status |
| 151 | + verbs: |
| 152 | + - get |
| 153 | +- apiGroups: |
| 154 | + - kubeflow.org |
| 155 | + resources: |
| 156 | + - mpijobs |
| 157 | + verbs: |
| 158 | + - create |
| 159 | + - delete |
| 160 | + - get |
| 161 | + - list |
| 162 | + - watch |
| 163 | +- apiGroups: |
| 164 | + - kubeflow.org |
| 165 | + resources: |
| 166 | + - mpijobs/status |
| 167 | + verbs: |
| 168 | + - get |
| 169 | +--- |
| 170 | +apiVersion: rbac.authorization.k8s.io/v1 |
| 171 | +kind: ClusterRoleBinding |
| 172 | +metadata: |
| 173 | + name: ${MULTIKUEUE_SA}-crb |
| 174 | +roleRef: |
| 175 | + apiGroup: rbac.authorization.k8s.io |
| 176 | + kind: ClusterRole |
| 177 | + name: ${MULTIKUEUE_SA}-role |
| 178 | +subjects: |
| 179 | +- kind: ServiceAccount |
| 180 | + name: ${MULTIKUEUE_SA} |
| 181 | + namespace: ${NAMESPACE} |
| 182 | +EOF |
| 183 | + |
| 184 | +# Get or create a secret bound to the new service account. |
| 185 | +SA_SECRET_NAME=$(kubectl get -n ${NAMESPACE} sa/${MULTIKUEUE_SA} -o "jsonpath={.secrets[0]..name}") |
| 186 | +if [ -z "$SA_SECRET_NAME" ]; then |
| 187 | + kubectl apply -f - <<EOF |
| 188 | +apiVersion: v1 |
| 189 | +kind: Secret |
| 190 | +type: kubernetes.io/service-account-token |
| 191 | +metadata: |
| 192 | + name: ${MULTIKUEUE_SA} |
| 193 | + namespace: ${NAMESPACE} |
| 194 | + annotations: |
| 195 | + kubernetes.io/service-account.name: "${MULTIKUEUE_SA}" |
| 196 | +EOF |
| 197 | + |
| 198 | + SA_SECRET_NAME=${MULTIKUEUE_SA} |
| 199 | +fi |
| 200 | + |
| 201 | +# Note: service account token is stored base64-encoded in the secret but must |
| 202 | +# be plaintext in kubeconfig. |
| 203 | +SA_TOKEN=$(kubectl get -n ${NAMESPACE} "secrets/${SA_SECRET_NAME}" -o "jsonpath={.data['token']}" | base64 -d) |
| 204 | +CA_CERT=$(kubectl get -n ${NAMESPACE} "secrets/${SA_SECRET_NAME}" -o "jsonpath={.data['ca\.crt']}") |
| 205 | + |
| 206 | +# Extract cluster IP from the current context |
| 207 | +CURRENT_CONTEXT=$(kubectl config current-context) |
| 208 | +CURRENT_CLUSTER=$(kubectl config view -o jsonpath="{.contexts[?(@.name == \"${CURRENT_CONTEXT}\"})].context.cluster}") |
| 209 | +CURRENT_CLUSTER_ADDR=$(kubectl config view -o jsonpath="{.clusters[?(@.name == \"${CURRENT_CLUSTER}\"})].cluster.server}") |
| 210 | + |
| 211 | +# Create the Kubeconfig file |
| 212 | +echo "Writing kubeconfig in ${KUBECONFIG_OUT}" |
| 213 | +cat >"${KUBECONFIG_OUT}" <<EOF |
| 214 | +apiVersion: v1 |
| 215 | +clusters: |
| 216 | +- cluster: |
| 217 | + certificate-authority-data: ${CA_CERT} |
| 218 | + server: ${CURRENT_CLUSTER_ADDR} |
| 219 | + name: ${CURRENT_CLUSTER} |
| 220 | +contexts: |
| 221 | +- context: |
| 222 | + cluster: ${CURRENT_CLUSTER} |
| 223 | + user: ${CURRENT_CLUSTER}-${MULTIKUEUE_SA} |
| 224 | + name: ${CURRENT_CONTEXT} |
| 225 | +current-context: ${CURRENT_CONTEXT} |
| 226 | +kind: Config |
| 227 | +preferences: {} |
| 228 | +users: |
| 229 | +- name: ${CURRENT_CLUSTER}-${MULTIKUEUE_SA} |
| 230 | + user: |
| 231 | + token: ${SA_TOKEN} |
| 232 | +EOF |
0 commit comments