You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by po...@apache.org on 2023/02/20 01:02:55 UTC

[airflow] branch main updated: Add ttlSecondsAfterFinished to migrateDatabaseJob and createUserJob (#29314)

This is an automated email from the ASF dual-hosted git repository.

potiuk pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/airflow.git


The following commit(s) were added to refs/heads/main by this push:
     new cd01650192 Add ttlSecondsAfterFinished to migrateDatabaseJob and createUserJob (#29314)
cd01650192 is described below

commit cd01650192b74573b49a20803e4437e611a4cf33
Author: Alexander Malyga <am...@gmail.com>
AuthorDate: Mon Feb 20 02:02:46 2023 +0100

    Add ttlSecondsAfterFinished to migrateDatabaseJob and createUserJob (#29314)
    
    * add ttlSecondsAfterFinished parameter to migrate-database-job and create-user-job
    
    * fixed ttlSecondsAfterFinished tests
    
    * fix ttlSecondsAfterFinished population when value is zero
    
    * fixed createUserJob helm tests
    
    * add default value of 300 to ttlSecondsAfterFinished
    
    ---------
    
    Co-authored-by: Oleksandr Malyga <ol...@revolut.com>
---
 chart/templates/jobs/create-user-job.yaml      |  3 +++
 chart/templates/jobs/migrate-database-job.yaml |  3 +++
 chart/values.schema.json                       | 16 ++++++++++++++++
 chart/values.yaml                              |  4 ++++
 tests/charts/test_create_user_job.py           | 24 ++++++++++++++++++++++++
 tests/charts/test_migrate_database_job.py      | 24 ++++++++++++++++++++++++
 6 files changed, 74 insertions(+)

diff --git a/chart/templates/jobs/create-user-job.yaml b/chart/templates/jobs/create-user-job.yaml
index aae0c40318..c9f57d5aa9 100644
--- a/chart/templates/jobs/create-user-job.yaml
+++ b/chart/templates/jobs/create-user-job.yaml
@@ -48,6 +48,9 @@ metadata:
     {{- $annotations | toYaml | nindent 4 }}
   {{- end }}
 spec:
+  {{- if not (kindIs "invalid" .Values.createUserJob.ttlSecondsAfterFinished) }}
+  ttlSecondsAfterFinished: {{ .Values.createUserJob.ttlSecondsAfterFinished }}
+  {{- end }}
   template:
     metadata:
       labels:
diff --git a/chart/templates/jobs/migrate-database-job.yaml b/chart/templates/jobs/migrate-database-job.yaml
index a37e0f5345..2d12c6fbed 100644
--- a/chart/templates/jobs/migrate-database-job.yaml
+++ b/chart/templates/jobs/migrate-database-job.yaml
@@ -48,6 +48,9 @@ metadata:
     {{- $annotations | toYaml | nindent 4 }}
   {{- end }}
 spec:
+  {{- if not (kindIs "invalid" .Values.migrateDatabaseJob.ttlSecondsAfterFinished) }}
+  ttlSecondsAfterFinished: {{ .Values.migrateDatabaseJob.ttlSecondsAfterFinished }}
+  {{- end }}
   template:
     metadata:
       labels:
diff --git a/chart/values.schema.json b/chart/values.schema.json
index 84aa3e26e6..9e64448e35 100644
--- a/chart/values.schema.json
+++ b/chart/values.schema.json
@@ -2884,6 +2884,14 @@
                     "type": "boolean",
                     "default": true
                 },
+                "ttlSecondsAfterFinished": {
+                    "description": "Limit the lifetime of the job object after it finished execution",
+                    "type": [
+                        "integer",
+                        "null"
+                    ],
+                    "default": 300
+                },
                 "env": {
                     "description": "Add additional env vars to the create user job pod.",
                     "type": "array",
@@ -3083,6 +3091,14 @@
                     "description": "Specify if you want additional configured env vars applied to this job",
                     "type": "boolean",
                     "default": true
+                },
+                "ttlSecondsAfterFinished": {
+                    "description": "Limit the lifetime of the job object after it finished execution",
+                    "type": [
+                        "integer",
+                        "null"
+                    ],
+                    "default": 300
                 }
             }
         },
diff --git a/chart/values.yaml b/chart/values.yaml
index ff5ddec1e4..c9407c7347 100644
--- a/chart/values.yaml
+++ b/chart/values.yaml
@@ -759,6 +759,8 @@ scheduler:
 
 # Airflow create user job settings
 createUserJob:
+  # Limit the lifetime of the job object after it finished execution.
+  ttlSecondsAfterFinished: 300
   # Command to use when running the create user job (templated).
   command: ~
   # Args to use when running the create user job (templated).
@@ -837,6 +839,8 @@ createUserJob:
 # Airflow database migration job settings
 migrateDatabaseJob:
   enabled: true
+  # Limit the lifetime of the job object after it finished execution.
+  ttlSecondsAfterFinished: 300
   # Command to use when running the migrate database job (templated).
   command: ~
   # Args to use when running the migrate database job (templated).
diff --git a/tests/charts/test_create_user_job.py b/tests/charts/test_create_user_job.py
index 92fe5e22ce..5c1e94dfd3 100644
--- a/tests/charts/test_create_user_job.py
+++ b/tests/charts/test_create_user_job.py
@@ -241,6 +241,30 @@ class TestCreateUserJob:
         assert {"name": "foo", "value": "bar"} not in envs
         assert {"name": "extraFoo", "value": "extraBar"} not in envs
 
+    def test_job_ttl_after_finished(self):
+        docs = render_chart(
+            values={"createUserJob": {"ttlSecondsAfterFinished": 1}},
+            show_only=["templates/jobs/create-user-job.yaml"],
+        )
+        ttl = jmespath.search("spec.ttlSecondsAfterFinished", docs[0])
+        assert ttl == 1
+
+    def test_job_ttl_after_finished_zero(self):
+        docs = render_chart(
+            values={"createUserJob": {"ttlSecondsAfterFinished": 0}},
+            show_only=["templates/jobs/create-user-job.yaml"],
+        )
+        ttl = jmespath.search("spec.ttlSecondsAfterFinished", docs[0])
+        assert ttl == 0
+
+    def test_job_ttl_after_finished_nil(self):
+        docs = render_chart(
+            values={"createUserJob": {"ttlSecondsAfterFinished": None}},
+            show_only=["templates/jobs/create-user-job.yaml"],
+        )
+        spec = jmespath.search("spec", docs[0])
+        assert "ttlSecondsAfterFinished" not in spec
+
     @pytest.mark.parametrize(
         "airflow_version, expected_arg",
         [
diff --git a/tests/charts/test_migrate_database_job.py b/tests/charts/test_migrate_database_job.py
index d25fdbb4b8..45a1b378df 100644
--- a/tests/charts/test_migrate_database_job.py
+++ b/tests/charts/test_migrate_database_job.py
@@ -235,6 +235,30 @@ class TestMigrateDatabaseJob:
             "spec.template.spec.containers[0].volumeMounts[-1]", docs[0]
         )
 
+    def test_job_ttl_after_finished(self):
+        docs = render_chart(
+            values={"migrateDatabaseJob": {"ttlSecondsAfterFinished": 1}},
+            show_only=["templates/jobs/migrate-database-job.yaml"],
+        )
+        ttl = jmespath.search("spec.ttlSecondsAfterFinished", docs[0])
+        assert ttl == 1
+
+    def test_job_ttl_after_finished_zero(self):
+        docs = render_chart(
+            values={"migrateDatabaseJob": {"ttlSecondsAfterFinished": 0}},
+            show_only=["templates/jobs/migrate-database-job.yaml"],
+        )
+        ttl = jmespath.search("spec.ttlSecondsAfterFinished", docs[0])
+        assert ttl == 0
+
+    def test_job_ttl_after_finished_nil(self):
+        docs = render_chart(
+            values={"migrateDatabaseJob": {"ttlSecondsAfterFinished": None}},
+            show_only=["templates/jobs/migrate-database-job.yaml"],
+        )
+        spec = jmespath.search("spec", docs[0])
+        assert "ttlSecondsAfterFinished" not in spec
+
     @pytest.mark.parametrize(
         "airflow_version, expected_arg",
         [