|
| 1 | +# Copyright 2025 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +- name: Assert variables are defined |
| 16 | + ansible.builtin.assert: |
| 17 | + that: |
| 18 | + - region is defined |
| 19 | + - custom_vars.project is defined |
| 20 | + - custom_vars.expected_tpu_count is defined |
| 21 | + |
| 22 | +- name: Get cluster credentials for kubectl |
| 23 | + delegate_to: localhost |
| 24 | + ansible.builtin.command: gcloud container clusters get-credentials {{ deployment_name }} --region {{ region }} --project {{ custom_vars.project }} --verbosity=debug |
| 25 | + |
| 26 | +- name: Identify Job File |
| 27 | + delegate_to: localhost |
| 28 | + ansible.builtin.shell: ls {{ workspace }}/{{ deployment_name }}/primary/my-job*.yaml | head -n 1 |
| 29 | + register: job_file_result |
| 30 | + changed_when: False |
| 31 | + |
| 32 | +- name: Register Job Variables |
| 33 | + ansible.builtin.set_fact: |
| 34 | + job_file_path: "{{ job_file_result.stdout }}" |
| 35 | + job_name: "{{ job_file_result.stdout | basename | splitext | first }}" |
| 36 | + |
| 37 | +- name: Execute the job |
| 38 | + delegate_to: localhost |
| 39 | + ansible.builtin.command: kubectl create -f {{ job_file_path }} -v=9 |
| 40 | + changed_when: False |
| 41 | + |
| 42 | +- name: Block to wait for job completion and capture failures |
| 43 | + block: |
| 44 | + - name: Wait for job to complete |
| 45 | + delegate_to: localhost |
| 46 | + ansible.builtin.command: kubectl wait --for=condition=complete "job/{{ job_name }}" --timeout=10m |
| 47 | + register: job_wait_result |
| 48 | + |
| 49 | + - name: Get job pods |
| 50 | + delegate_to: localhost |
| 51 | + ansible.builtin.shell: | |
| 52 | + kubectl get pods --selector=job-name={{ job_name }} -o jsonpath='{.items[0].metadata.name}' |
| 53 | + register: job_pod_name |
| 54 | + changed_when: false |
| 55 | + |
| 56 | + - name: Get job logs |
| 57 | + delegate_to: localhost |
| 58 | + ansible.builtin.shell: | |
| 59 | + kubectl logs {{ job_pod_name.stdout }} |
| 60 | + register: job_logs |
| 61 | + changed_when: false |
| 62 | + |
| 63 | + - name: Print job output |
| 64 | + ansible.builtin.debug: |
| 65 | + msg: "Job Output: {{ job_logs.stdout }}" |
| 66 | + |
| 67 | + - name: Verify TPU count |
| 68 | + ansible.builtin.assert: |
| 69 | + that: |
| 70 | + - (custom_vars.expected_tpu_count | string ~ ' TPU cores') in job_logs.stdout |
| 71 | + fail_msg: "Expected '{{ custom_vars.expected_tpu_count }} TPU cores' in logs, but got: {{ job_logs.stdout }}" |
| 72 | + |
| 73 | + rescue: |
| 74 | + - name: "FAILURE: Job did not complete or verification failed. Capturing debug info." |
| 75 | + ansible.builtin.debug: |
| 76 | + msg: "The Job failed to complete within the expected time or failed verification. See debug output below." |
| 77 | + |
| 78 | + - name: Get all pods status on failure |
| 79 | + delegate_to: localhost |
| 80 | + ansible.builtin.shell: | |
| 81 | + kubectl get pods -o wide |
| 82 | + register: pods_on_failure |
| 83 | + changed_when: false |
| 84 | + |
| 85 | + - name: Print all pods status on failure |
| 86 | + ansible.builtin.debug: |
| 87 | + msg: "{{ pods_on_failure.stdout_lines }}" |
| 88 | + |
| 89 | + - name: Describe the job on failure |
| 90 | + delegate_to: localhost |
| 91 | + ansible.builtin.shell: | |
| 92 | + kubectl describe job {{ job_name }} |
| 93 | + register: job_describe_on_failure |
| 94 | + changed_when: false |
| 95 | + |
| 96 | + - name: Print job description on failure |
| 97 | + ansible.builtin.debug: |
| 98 | + msg: "{{ job_describe_on_failure.stdout_lines }}" |
| 99 | + |
| 100 | + - name: Get pod name on failure |
| 101 | + delegate_to: localhost |
| 102 | + ansible.builtin.shell: | |
| 103 | + kubectl get pods --selector=job-name={{ job_name }} --no-headers -o custom-columns=":metadata.name" |
| 104 | + register: pod_name_on_failure |
| 105 | + changed_when: false |
| 106 | + |
| 107 | + - name: Describe the pod on failure |
| 108 | + delegate_to: localhost |
| 109 | + ansible.builtin.shell: | |
| 110 | + kubectl describe pod {{ item }} |
| 111 | + loop: "{{ pod_name_on_failure.stdout_lines }}" |
| 112 | + register: pod_describe_on_failure |
| 113 | + changed_when: false |
| 114 | + when: pod_name_on_failure.stdout_lines | length > 0 |
| 115 | + |
| 116 | + - name: Print pod description on failure |
| 117 | + ansible.builtin.debug: |
| 118 | + msg: "{{ pod_describe_on_failure.results }}" |
| 119 | + when: pod_name_on_failure.stdout_lines | length > 0 |
| 120 | + |
| 121 | + - name: Get logs from the pod on failure |
| 122 | + delegate_to: localhost |
| 123 | + ansible.builtin.shell: | |
| 124 | + kubectl logs {{ item }} |
| 125 | + loop: "{{ pod_name_on_failure.stdout_lines }}" |
| 126 | + register: pod_logs_on_failure |
| 127 | + changed_when: false |
| 128 | + when: pod_name_on_failure.stdout_lines | length > 0 |
| 129 | + |
| 130 | + - name: Print pod logs on failure |
| 131 | + ansible.builtin.debug: |
| 132 | + msg: "{{ pod_logs_on_failure.results }}" |
| 133 | + when: pod_name_on_failure.stdout_lines | length > 0 |
| 134 | + |
| 135 | + - name: Fail the playbook |
| 136 | + ansible.builtin.fail: |
| 137 | + msg: "GKE Job failed." |
| 138 | + |
| 139 | +- name: Print job_completion debug output |
| 140 | + ansible.builtin.debug: |
| 141 | + var: job_wait_result.stdout_lines |
| 142 | + |
| 143 | +- name: Clean up |
| 144 | + delegate_to: localhost |
| 145 | + ansible.builtin.shell: | |
| 146 | + kubectl delete job {{ job_name }} -v=9 |
0 commit comments