lukas-at-harren commented on issue #22727:
URL: https://github.com/apache/airflow/issues/22727#issuecomment-1136440722
```python
from typing import Sequence
from airflow.providers.cncf.kubernetes.operators.kubernetes_pod import
KubernetesPodOperator
from tempfile import NamedTemporaryFile
from google.cloud.container_v1 import ClusterManagerClient
import yaml
class GKEJobOperator(KubernetesPodOperator):
template_fields: Sequence[str] = (
'image',
'cmds',
'arguments',
'env_vars',
'labels',
'config_file',
'pod_template_file',
'namespace',
)
def __init__(
self,
*,
project_id = None,
location = None,
cluster_name = None,
config_file = None,
**kwargs,
) -> None:
super().__init__(**{'in_cluster': False, **kwargs})
self.project_id = project_id
self.location = location
self.cluster_name = cluster_name
self.config_file = config_file
def execute(self, context):
if self.config_file is None:
self.config_file = self.tmp_config_file()
return super().execute(context)
def tmp_config_file(self):
cluster_manager = ClusterManagerClient()
cluster =
cluster_manager.get_cluster(name=f'projects/{self.project_id}/locations/{self.location}/clusters/{self.cluster_name}')
kubeconfig = {
'apiVersion': 'v1',
'current-context': 'this-cluster',
'clusters': [
{
'name': 'this-cluster',
'cluster': {
'certificate-authority-data':
cluster.master_auth.cluster_ca_certificate,
'server': f'https://{cluster.endpoint}'
}
}
],
'contexts': [
{'name': 'this-cluster', 'context': {'cluster':
'this-cluster', 'user': 'this-user'}}
],
'users': [
{
'name': 'this-user',
'user': {
'auth-provider': {'name': 'gcp'}
}
}
]
}
tf = NamedTemporaryFile(delete=False)
with open(tf.name, 'w') as f:
yaml.dump(kubeconfig, f)
return tf.name
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]