上下文 - 基本 PG docker 正在使用所描述的凭据运行,想要执行 Argo 工作流程,该工作流程创建一个表,分别添加一些数据等。
在为上述要求定义 argo yaml 时,得到“未找到 psycopg 库”(PFB)
在哪里可以 pip install 所需的库?我知道我可以使用此脚本创建一个 docker 容器,并在之前安装 CMD 库。有没有办法安装库来纯粹使用“脚本”模板执行简单的Python脚本?
参考:https://github.com/argoproj/argo-workflows/blob/master/examples/scripts-python.yaml
apiVersion: argoproj.io/v1alpha1
kind: Workflow
metadata:
generateName: scripts-python
spec:
entrypoint: python-script-example
templates:
- name: python-script-example
steps:
- - name: creating-emp-tbl
template: create-emp-tbl
- - name: print
template: print-message
arguments:
parameters:
- name: message
value: "{{steps.creating-emp-tbl.outputs.result}}"
- name: create-emp-tbl
script:
image: python:alpine3.6
command: [python]
source: |
from psycopg2 import connect
conn = connect(
database="postgres",
user="postgres",
host="localhost",
port=5432,
password="mysecretpassword",
)
cursor = conn.cursor()
try:
cursor.execute(
"CREATE TABLE EMPLOYEES (id serial PRIMARY KEY, age integer, team varchar);"
)
print("created")
except:
print("couldn't create table")
conn.commit()
cursor.close()
conn.close()
- name: print-message
inputs:
parameters:
- name: message
container:
image: alpine:latest
command: [sh, -c]
args: ["echo result was: {{inputs.parameters.message}}"]
实现此目的的一种方法是在脚本开头以编程方式安装依赖项,如在代码中安装 python 模块
中所述作为安装依赖项的脚本工作流程示例,例如 ulid:
apiVersion: argoproj.io/v1alpha1
kind: Workflow
metadata:
generateName: workflow-draft-
spec:
entrypoint: draft-script
templates:
- name: draft-script
steps:
- - name: try-out-pip-install
template: draft
- name: draft
script:
image: python:alpine3.16
command: ["python"]
source: |
import pip
pip.main(['install', 'ulid'])
import ulid
print("ulid", ulid.ulid())