|
14 | 14 | from airflow.providers.cncf.kubernetes.operators.pod import KubernetesPodOperator
|
15 | 15 | from airflow.utils.trigger_rule import TriggerRule
|
16 | 16 | from kubernetes.client import models as k8s
|
| 17 | +from airflow.kubernetes.secret import Secret |
17 | 18 | from unity_sps_utils import (
|
18 | 19 | DEFAULT_LOG_LEVEL,
|
19 | 20 | EC2_TYPES,
|
|
24 | 25 | build_ec2_type_label,
|
25 | 26 | get_affinity,
|
26 | 27 | )
|
| 28 | +from kubernetes import client |
| 29 | +import base64 |
27 | 30 |
|
28 | 31 | from airflow import DAG
|
29 | 32 |
|
|
92 | 95 | )
|
93 | 96 |
|
94 | 97 | app_gen_env_vars = [
|
95 |
| - k8s.V1EnvVar( |
96 |
| - name="DOCKERHUB_USERNAME", value="{{ ti.xcom_pull(task_ids='Setup', key='dockerhub_username') }}" |
97 |
| - ), |
| 98 | + # k8s.V1EnvVar( |
| 99 | + # name="DOCKERHUB_USERNAME", value="{{ ti.xcom_pull(task_ids='Setup', key='dockerhub_username') }}" |
| 100 | + # ), |
98 | 101 | k8s.V1EnvVar(name="DOCKERHUB_TOKEN", value="{{ ti.xcom_pull(task_ids='Setup', key='dockerhub_token') }}"),
|
99 | 102 | k8s.V1EnvVar(name="DOCKSTORE_TOKEN", value="{{ ti.xcom_pull(task_ids='Setup', key='dockstore_token') }}"),
|
100 | 103 | k8s.V1EnvVar(
|
|
104 | 107 | k8s.V1EnvVar(name="GITHUB_REPO", value="{{ params.repository }}"),
|
105 | 108 | ]
|
106 | 109 |
|
| 110 | +kube_secrets = [ |
| 111 | + Secret(deploy_type="env", deploy_target="DOCKERHUB_USERNAME", secret="jplmdps", key="DOCKER_USERNAME") |
| 112 | + ] |
| 113 | + |
107 | 114 |
|
108 | 115 | def setup(ti=None, **context):
|
109 | 116 | """
|
@@ -137,6 +144,31 @@ def setup(ti=None, **context):
|
137 | 144 | ti.xcom_push(key="dockerhub_token", value=credentials_dict["dockerhub_token"])
|
138 | 145 | ti.xcom_push(key="dockstore_token", value=credentials_dict["dockstore_token"])
|
139 | 146 |
|
| 147 | + # Create the Kubernetes secret |
| 148 | + v1 = client.CoreV1Api() |
| 149 | + secret = client.V1Secret( |
| 150 | + metadata=client.V1ObjectMeta(name="dockerhub_username"), |
| 151 | + type="Opaque", |
| 152 | + data={ |
| 153 | + "token": base64.b64encode(credentials_dict["dockerhub_username"].encode("utf-8")).decode("utf-8") |
| 154 | + } |
| 155 | + ) |
| 156 | + |
| 157 | + logging.info(f"v1: {v1}") |
| 158 | + |
| 159 | + # Create the secret in the 'default' namespace |
| 160 | + # try: |
| 161 | + # v1.create_namespaced_secret(namespace="default", body=secret) |
| 162 | + # print("Secret created.") |
| 163 | + # except client.exceptions.ApiException as e: |
| 164 | + # print(f"Exception when creating secret: {e}") |
| 165 | + # print(f"Status: {e.status}, Reason: {e.reason}") |
| 166 | + # print(f"Body: {e.body}") |
| 167 | + # if e.status == 409: |
| 168 | + # print("Secret already exists.") |
| 169 | + # else: |
| 170 | + # raise |
| 171 | + |
140 | 172 | context = get_current_context()
|
141 | 173 | logging.info(f"DAG Run parameters: {json.dumps(context['params'], sort_keys=True, indent=4)}")
|
142 | 174 |
|
@@ -176,7 +208,16 @@ def setup(ti=None, **context):
|
176 | 208 | retries=1,
|
177 | 209 | task_id="appgen_task",
|
178 | 210 | namespace=POD_NAMESPACE,
|
179 |
| - env_vars=app_gen_env_vars, |
| 211 | + # env_vars=app_gen_env_vars, |
| 212 | + secrets=kube_secrets, |
| 213 | + # env_from=[ |
| 214 | + # k8s.V1EnvFromSource( |
| 215 | + # secret_ref=k8s.V1SecretEnvSource( |
| 216 | + # # Dynamically get the secret name created by the previous task |
| 217 | + # name="{{ ti.xcom_pull(task_ids='Setup', key='dockerhub_username') }}" |
| 218 | + # ) |
| 219 | + # ) |
| 220 | + # ], |
180 | 221 | name="appgen-task-pod",
|
181 | 222 | image=DOCKER_IMAGE,
|
182 | 223 | service_account_name="airflow-worker",
|
|
0 commit comments