Bài này chúng ta lab về vấn dể đệ quy trong workflow.
Nó kiểu như là chúng ta đang chờ thư của bạn gái. một lần cần nhòm vào hộp thư xem có ai gửi cho mình hông?
apiVersion: argoproj.io/v1alpha1
kind: Workflow
metadata:
generateName: wf-recursion-
spec:
entrypoint: dag-template
templates:
- name: dag-template
dag:
tasks:
- name: roll-dice
template: task-roll-dice
- name: six
template: task-six
depends: roll-dice.Succeeded
when: "{{tasks.roll-dice.outputs.result}} == 6"
- name: not-six
template: dag-template
depends: roll-dice.Succeeded
when: "{{tasks.roll-dice.outputs.result}} != 6"
- name: task-roll-dice
script:
image: python:3.8-slim
command: [python]
source: |
import random
number = random.randint(1,6)
print(number)
- name: task-six
script:
image: python:3.8-slim
command: [python]
source: |
print("Hurra... a 6.")
Bài trên là nếu task roll-dice không trả về số 6 thì task not-six sẽ chạy lại task roll-dice
Bạn sẽ thấy nó chỉ dừng khi số 6 hiện ta
Ở đây mình thêm 1 ví dụ là mình curl vào 1 trang web và parse JSON. Tiếp đến nếu result ok thì mình đi tiếp:
apiVersion: argoproj.io/v1alpha1
kind: Workflow
metadata:
generateName: wf-recursion-
spec:
entrypoint: dag-template
templates:
- name: dag-template
dag:
tasks:
- name: roll-dice
template: task-roll-dice
- name: six
template: task-six
depends: roll-dice.Succeeded
when: "{{tasks.roll-dice.outputs.result}} == 6"
- name: not-six
template: dag-template
depends: roll-dice.Succeeded
when: "{{tasks.roll-dice.outputs.result}} != 6"
- name: task-roll-dice
script:
image: python:3.8-slim
command: [python]
source: |
import urllib.request
import json
# URL to call
url = "http://engine-controller:8088/status"
try:
# Perform the GET request
with urllib.request.urlopen(url) as response:
# Read the response and decode it to a string
response_body = response.read().decode('utf-8')
# Parse the JSON string into a Python dictionary
data = json.loads(response_body)
# Check the services_health value
if data.get("data", {}).get("services_health") == "good":
print(6)
else:
print(0)
except Exception as e:
# In case of any error, print 0
print(0)
- name: task-six
script:
image: python:3.8-slim
command: [python]
source: |
print("Hurra... a 6.")