Skip to content

Instantly share code, notes, and snippets.

@suhussai
Created October 21, 2023 21:09
Show Gist options
  • Save suhussai/dc139c4d15f4445686781c172b914bc9 to your computer and use it in GitHub Desktop.
Save suhussai/dc139c4d15f4445686781c172b914bc9 to your computer and use it in GitHub Desktop.
Add another role to aws-auth configmap using kubectl and jq
ROLE_ARN="arn:aws:iam::123456789123:role/my-role"
GROUP="system:masters"
# 1. read ".data.mapRoles" from aws-auth
# 2. convert it to json
# 3. add the role as a value to the json array
# 4. convert the json back to a string
MAP_ROLES=$(kubectl get configmap aws-auth -n kube-system -o json \
| jq --arg role "$ROLE_ARN" --arg group "$GROUP" -r '.data.mapRoles | fromjson | . += [{"rolearn": $role,"groups": [$group]} ] | tojson')
# 5. overwrite the existing value for ".data.mapRoles" with the one computed above
kubectl get configmap aws-auth -n kube-system -o json \
| jq --arg m "$MAP_ROLES" '.data.mapRoles = $m' | kubectl apply -f -
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment