Skip to content

Commit b1d560a

Browse files
authored
Update GKE job to support the new cloud credentials (#5946)
* Add a new credential job to generate temporary credentials. * Add a new job for ccredential renewal. * GKE job is now triggered as a post-build action of the credential job. * Update the GKE script to support the new login method. Signed-off-by: Shuyang Xin <[email protected]>
1 parent 9d2eaf2 commit b1d560a

File tree

3 files changed

+126
-3
lines changed

3 files changed

+126
-3
lines changed

ci/jenkins/jobs/job-templates.yaml

+24
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,30 @@
284284
publishers: '{publishers}'
285285
wrappers: '{wrappers}'
286286

287+
- job-template:
288+
name: 'cloud-{name}-{test_name}-credential'
289+
node: '{node}'
290+
triggers: '{triggers}'
291+
builders: '{builders}'
292+
parameters:
293+
- string:
294+
default: ''
295+
description: The cloud token ID of the cloud provider.
296+
name: ID_TOKEN
297+
trim: 'false'
298+
description: '{description}'
299+
block-downstream: false
300+
block-upstream: false
301+
project-type: freestyle
302+
properties:
303+
- build-discarder:
304+
artifact-days-to-keep: -1
305+
artifact-num-to-keep: -1
306+
days-to-keep: 30
307+
num-to-keep: 30
308+
publishers: '{publishers}'
309+
wrappers: '{wrappers}'
310+
287311
- job-template:
288312
name: 'cloud-{name}-{test_name}-cleanup'
289313
node: '{node}'

ci/jenkins/jobs/projects-cloud.yaml

+95-3
Original file line numberDiff line numberDiff line change
@@ -718,10 +718,22 @@
718718
builders:
719719
- shell: |-
720720
#!/bin/bash
721-
# gcloud auth login requires verification code from url
722-
${{GCLOUD_SDK_PATH}}/bin/gcloud auth activate-service-account --key-file=${{GCLOUD_KEY_PATH}}
721+
# Prevent exposing credentials in the console output by adding set +x
722+
# This is to avoid other developers removing this set+x by accident
723+
set +x
724+
token_file="/var/lib/jenkins/gcp_cred"
725+
cp $token_file ${{WORKSPACE}}/gcp_cred
726+
sudo gcloud iam workforce-pools create-cred-config \
727+
locations/global/workforcePools/${{WORKFORCE_POOL}}/providers/oidc-provider \
728+
--subject-token-type urn:ietf:params:oauth:token-type:id_token \
729+
--credential-source-file gcp_cred \
730+
--workforce-pool-user-project antrea\
731+
--project antrea\
732+
--output-file gcp_cred_config.json
733+
sudo gcloud auth login --cred-file=gcp_cred_config.json
734+
sudo gcloud config set project antrea
723735
sudo ./ci/test-conformance-gke.sh --cluster-name antrea-gke-${{BUILD_NUMBER}} \
724-
--svc-account antrea-gcp@antrea.iam.gserviceaccount.com --gcloud-sdk-path ${{GCLOUD_SDK_PATH}} \
736+
--skip-iam-policy-binding --gcloud-sdk-path ${{GCLOUD_SDK_PATH}} \
725737
--log-mode detail --setup-only
726738
triggers:
727739
- timed: H H */2 * *
@@ -741,6 +753,86 @@
741753
- email:
742754
notify-every-unstable-build: true
743755
recipients: [email protected]
756+
wrappers:
757+
- credentials-binding:
758+
- text:
759+
credential-id: WORKFORCE_POOL # Jenkins secret that stores the cloud resource pool id
760+
variable: WORKFORCE_POOL
761+
- 'cloud-{name}-{test_name}-credential':
762+
test_name: gke
763+
node: antrea-credential
764+
description: 'This is the cloud credential job for antrea gke tests.'
765+
builders:
766+
- shell: |-
767+
#!/bin/bash
768+
# Prevent exposing credentials in the console output by adding set +x
769+
# This is to avoid other developers removing this set+x by accident
770+
set +x
771+
token_file="/var/lib/jenkins/ci_properties.txt"
772+
if [ -e "$token_file" ]; then
773+
modification_timestamp=$(stat -c %Y "$token_file")
774+
current_timestamp=$(date +%s)
775+
time_difference=$((current_timestamp - modification_timestamp))
776+
else
777+
time_difference=14401
778+
fi
779+
# The credentials are valid for 12 hours, to allow sufficient time for job execution, the reuse threshold is set to 4 hours.
780+
threshold=14400
781+
if [ "$time_difference" -gt "$threshold" ]; then
782+
echo "Generate the new Cloud Credential"
783+
json_data=$(python3 get_access_using_api_client.py ${{CLOUD_CLIENT_ID}} ${{CLOUD_CLIENT_TOKEN}} PowerUser gcp ${{GKE_RESOURCE_ID}} 43200 prd)
784+
json_data=$(echo $json_data | sed "s/'/\"/g" | sed 's/True/true/')
785+
id_token=$(echo $json_data | jq -r '.credential.idToken')
786+
echo "ID_TOKEN=$id_token" > ${{WORKSPACE}}/ci_properties.txt
787+
cp ${{WORKSPACE}}/ci_properties.txt $token_file
788+
else
789+
echo "Reuse the unexpired Cloud Credential"
790+
cp $token_file ${{WORKSPACE}}/ci_properties.txt
791+
fi
792+
triggers:
793+
- timed: H H/6 * * *
794+
publishers:
795+
- trigger-parameterized-builds:
796+
- project:
797+
- cloud-{name}-renew-credential
798+
current-parameters: true
799+
property-file: 'ci_properties.txt'
800+
- email:
801+
notify-every-unstable-build: true
802+
recipients: [email protected]
803+
wrappers:
804+
- credentials-binding:
805+
- text:
806+
credential-id: CLOUD_CLIENT_ID # Jenkins secret that stores client id
807+
variable: CLOUD_CLIENT_ID
808+
- text:
809+
credential-id: CLOUD_CLIENT_TOKEN # Jenkins secret that stores client secret token
810+
variable: CLOUD_CLIENT_TOKEN
811+
- text:
812+
credential-id: GKE_RESOURCE_ID
813+
variable: GKE_RESOURCE_ID
814+
- 'cloud-{name}-{test_name}-credential':
815+
test_name: renew
816+
node: antrea-cloud
817+
description: 'This is a periodic job to renew the credential on cloud node.'
818+
builders:
819+
- shell: |-
820+
#!/bin/bash
821+
# Prevent exposing credentials in the console output by adding set +x
822+
# This is to avoid other developers removing this set+x by accident
823+
set +x
824+
cd /var/lib/jenkins
825+
if [ -z "$ID_TOKEN" ]; then
826+
echo "No Token ID Found"
827+
exit 1
828+
else
829+
echo "${{ID_TOKEN}}" > gcp_cred
830+
fi
831+
publishers:
832+
- email:
833+
notify-every-unstable-build: true
834+
recipients: [email protected]
835+
triggers: []
744836
wrappers: []
745837
- 'cloud-{name}-{test_name}-cleanup':
746838
test_name: gke

ci/test-conformance-gke.sh

+7
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ MODE="report"
2929
RUN_ALL=true
3030
RUN_SETUP_ONLY=false
3131
RUN_CLEANUP_ONLY=false
32+
SKIP_IAM_POLICY_BINDING=false
3233
TEST_SCRIPT_RC=0
3334
KUBE_CONFORMANCE_IMAGE_VERSION=auto
3435

@@ -89,6 +90,10 @@ case $key in
8990
USER_EMAIL="$2"
9091
shift 2
9192
;;
93+
--skip-iam-policy-binding)
94+
SKIP_IAM_POLICY_BINDING=true
95+
shift
96+
;;
9297
--gke-zone)
9398
GKE_ZONE="$2"
9499
shift 2
@@ -244,6 +249,8 @@ function deliver_antrea_to_gke() {
244249
elif [[ -n ${USER_EMAIL+x} ]]; then
245250
gcloud projects add-iam-policy-binding ${GKE_PROJECT} --member user:${USER_EMAIL} --role roles/container.admin
246251
kubectl create clusterrolebinding cluster-admin-binding --clusterrole cluster-admin --user ${USER_EMAIL}
252+
elif [[ "$SKIP_IAM_POLICY_BINDING" == true ]]; then
253+
echo "Skipping the IAM Policy Binding for Cluster Management."
247254
else
248255
echo "Neither service account or user email info is set, cannot create cluster-admin-binding!"
249256
echo "Please refer to --help for more information."

0 commit comments

Comments
 (0)