|
16 | 16 |
|
17 | 17 | # [START composer_kubernetespodoperator] |
18 | 18 | import datetime |
19 | | - |
20 | 19 | from airflow import models |
21 | | -# [END composer_kubernetespodoperator] |
22 | 20 | from airflow.contrib.kubernetes import pod |
23 | 21 | from airflow.contrib.kubernetes import secret |
24 | | -# [START composer_kubernetespodoperator] |
25 | 22 | from airflow.contrib.operators import kubernetes_pod_operator |
26 | 23 |
|
27 | | -# [END composer_kubernetespodoperator] |
28 | 24 |
|
29 | 25 | # A Secret is an object that contains a small amount of sensitive data such as |
30 | 26 | # a password, a token, or a key. Such information might otherwise be put in a |
31 | 27 | # Pod specification or in an image; putting it in a Secret object allows for |
32 | 28 | # more control over how it is used, and reduces the risk of accidental |
33 | 29 | # exposure. |
34 | | -secret_file = secret.Secret( |
35 | | - # Mounts the secret as a file in RAM-backed tmpfs. |
36 | | - deploy_type='volume', |
37 | | - # File path of where to deploy the target, since deploy_type is 'volume' |
38 | | - # rather than 'env'. |
39 | | - deploy_target='/etc/sql_conn', |
40 | | - # Name of secret in Kubernetes, if the secret is not already defined in |
41 | | - # Kubernetes using kubectl the Pod will fail to find the secret, and in |
42 | | - # turn, fail to launch. |
43 | | - secret='airflow-secrets', |
44 | | - # Key of the secret within Kubernetes. |
45 | | - key='sql_alchemy_conn') |
46 | 30 |
|
| 31 | +# [START composer_kubernetespodoperator_secretobject] |
47 | 32 | secret_env = secret.Secret( |
48 | 33 | # Expose the secret as environment variable. |
49 | 34 | deploy_type='env', |
50 | 35 | # The name of the environment variable, since deploy_type is `env` rather |
51 | 36 | # than `volume`. |
52 | 37 | deploy_target='SQL_CONN', |
| 38 | + # Name of the Kubernetes Secret |
53 | 39 | secret='airflow-secrets', |
| 40 | + # Key of a secret stored in this Secret object |
54 | 41 | key='sql_alchemy_conn') |
| 42 | +# [END composer_kubernetespodoperator_secretobject] |
55 | 43 |
|
56 | | -# [START composer_kubernetespodoperator] |
57 | 44 | YESTERDAY = datetime.datetime.now() - datetime.timedelta(days=1) |
58 | 45 |
|
59 | 46 | # If a Pod fails to launch, or has an error occur in the container, Airflow |
|
69 | 56 | # no `config_file` parameter is specified. By default it will contain the |
70 | 57 | # credentials for Cloud Composer's Google Kubernetes Engine cluster that is |
71 | 58 | # created upon environment creation. |
| 59 | + |
| 60 | + # [START composer_kubernetespodoperator_minconfig] |
72 | 61 | kubernetes_min_pod = kubernetes_pod_operator.KubernetesPodOperator( |
73 | 62 | # The ID specified for the task. |
74 | 63 | task_id='pod-ex-minimum', |
|
89 | 78 | # gcr.io images if the Composer Environment is under the same |
90 | 79 | # project-id as the gcr.io images. |
91 | 80 | image='gcr.io/gcp-runtimes/ubuntu_16_0_4') |
92 | | - # [END composer_kubernetespodoperator] |
93 | | - |
| 81 | + # [END composer_kubernetespodoperator_minconfig] |
| 82 | + # [START composer_kubernetespodoperator_templateconfig] |
94 | 83 | kubenetes_template_ex = kubernetes_pod_operator.KubernetesPodOperator( |
95 | 84 | task_id='ex-kube-templates', |
96 | 85 | name='ex-kube-templates', |
|
113 | 102 | # setting the environment variable `MY_VALUE`. The pod will fail if |
114 | 103 | # `my_value` is not set in the Airflow UI. |
115 | 104 | env_vars={'MY_VALUE': '{{ var.value.my_value }}'}, |
116 | | - # Sets the config file to the specified airflow.cfg airflow home. If |
117 | | - # the configuration file does not exist or does not provide valid |
118 | | - # credentials the pod will fail to launch. |
119 | | - config_file="{{ conf.get('core', 'airflow_home') }}/config") |
120 | | - |
| 105 | + # Sets the config file to a kubernetes config file specified in |
| 106 | + # airflow.cfg. If the configuration file does not exist or does |
| 107 | + # not provide validcredentials the pod will fail to launch. If not |
| 108 | + # specified, config_file defaults to ~/.kube/config |
| 109 | + config_file="{{ conf.get('core', 'kube_config') }}") |
| 110 | + # [END composer_kubernetespodoperator_templateconfig] |
| 111 | + # [START composer_kubernetespodoperator_secretconfig] |
121 | 112 | kubernetes_secret_vars_ex = kubernetes_pod_operator.KubernetesPodOperator( |
122 | 113 | task_id='ex-kube-secrets', |
123 | 114 | name='ex-kube-secrets', |
124 | 115 | namespace='default', |
125 | 116 | image='ubuntu', |
| 117 | + startup_timeout_seconds=300, |
126 | 118 | # The secrets to pass to Pod, the Pod will fail to create if the |
127 | 119 | # secrets you specify in a Secret object do not exist in Kubernetes. |
128 | | - secrets=[secret_env, secret_file], |
| 120 | + secrets=[secret_env], |
129 | 121 | # env_vars allows you to specify environment variables for your |
130 | 122 | # container to use. env_vars is templated. |
131 | 123 | env_vars={'EXAMPLE_VAR': '/example/value'}) |
132 | | - |
| 124 | + # [END composer_kubernetespodoperator_secretconfig] |
133 | 125 | # [START composer_kubernetespodaffinity] |
134 | 126 | kubernetes_affinity_ex = kubernetes_pod_operator.KubernetesPodOperator( |
135 | 127 | task_id='ex-pod-affinity', |
|
163 | 155 | # The label key's value that pods can be scheduled |
164 | 156 | # on. |
165 | 157 | 'values': [ |
166 | | - 'node-pool-name-1', |
167 | | - 'node-pool-name-2', |
| 158 | + 'pool-0', |
| 159 | + 'pool-1', |
168 | 160 | ] |
169 | 161 | }] |
170 | 162 | }] |
171 | 163 | } |
172 | 164 | } |
173 | 165 | }) |
174 | 166 | # [END composer_kubernetespodaffinity] |
175 | | - |
| 167 | + # [START composer_kubernetespodoperator_fullconfig] |
176 | 168 | kubernetes_full_pod = kubernetes_pod_operator.KubernetesPodOperator( |
177 | 169 | task_id='ex-all-configs', |
178 | 170 | name='pi', |
|
221 | 213 | # config. For more information see: |
222 | 214 | # https://kubernetes.io/docs/concepts/configuration/assign-pod-node/ |
223 | 215 | affinity={}) |
| 216 | + # [END composer_kubernetespodoperator_fullconfig] |
| 217 | + # [END composer_kubernetespodoperator] |
0 commit comments