Skip to content

Commit 2afd7af

Browse files
author
Zav Shotan
committed
Added auto cluster detection
1 parent cc8a558 commit 2afd7af

File tree

3 files changed

+9
-5
lines changed

3 files changed

+9
-5
lines changed

airflow_kubernetes_job_operator/job_runner.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,20 +31,24 @@ def __init__(self):
3131
super().__init__()
3232

3333
def load_kuberntes_configuration(
34-
self, in_cluster: bool = False, config_file: str = None, context: str = None
34+
self, in_cluster: bool = None, config_file: str = None, context: str = None
3535
):
3636
"""Loads the appropriate kubernetes configuration into the global
3737
context.
3838
3939
Keyword Arguments:
4040
4141
in_cluster {bool} -- If true, load the configuration from the cluster
42-
(default: {False})
42+
(default: {None}, will try and autodetect)
4343
config_file {str} -- the path to the file to load from,
4444
if None, loads the kubernetes default ~/.kube. (default: {None})
4545
context {str} -- The context to load. If None loads the current
4646
context (default: {None})
4747
"""
48+
in_cluster = (
49+
in_cluster or os.environ.get("KUBERNETES_SERVICE_HOST", None) is not None
50+
)
51+
4852
# loading the current config to use.
4953
if in_cluster:
5054
kubernetes.config.load_incluster_config()

airflow_kubernetes_job_operator/kubernetes_job_operator.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def __init__(
4242
job_yaml=None,
4343
job_yaml_filepath=None,
4444
delete_policy: str = "IfSucceeded",
45-
in_cluster: bool = False,
45+
in_cluster: bool = None,
4646
config_file: str = None,
4747
get_logs: bool = True,
4848
cluster_context: str = None,

airflow_kubernetes_job_operator/kubernetes_legacy_job_operator.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def __init__(
2727
volumes: List[Volume] = None,
2828
env_vars: dict = None,
2929
secrets: List[Secret] = None,
30-
in_cluster: bool = False,
30+
in_cluster: bool = None,
3131
cluster_context: str = None,
3232
labels: dict = None,
3333
startup_timeout_seconds: float = 120,
@@ -100,7 +100,7 @@ def __init__(
100100
:param secrets: Kubernetes secrets to inject in the container,
101101
They can be exposed as environment vars or files in a volume.
102102
:type secrets: list[airflow.contrib.kubernetes.secret.Secret]
103-
:param in_cluster: run kubernetes client with in_cluster configuration
103+
:param in_cluster: run kubernetes client with in_cluster configuration (if None autodetect)
104104
:type in_cluster: bool
105105
:param cluster_context: context that points to kubernetes cluster.
106106
Ignored when in_cluster is True. If None, current-context is used.

0 commit comments

Comments
 (0)