mik-laj commented on a change in pull request #7163: [AIRFLOW-6542] add 
spark-on-k8s operator/hook/sensor
URL: https://github.com/apache/airflow/pull/7163#discussion_r371041407
 
 

 ##########
 File path: airflow/contrib/sensors/spark_kubernetes_sensor.py
 ##########
 @@ -0,0 +1,80 @@
+# -*- coding: utf-8 -*-
+#
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+from typing import Dict
+
+from kubernetes import client
+
+from airflow.contrib.hooks.kubernetes_hook import Kuberneteshook
+from airflow.exceptions import AirflowException
+from airflow.sensors.base_sensor_operator import BaseSensorOperator
+from airflow.utils.decorators import apply_defaults
+
+
+class SparkKubernetesSensor(BaseSensorOperator):
+    """
+    checks sparkApplication object in kubernetes cluster:
+    
https://github.com/GoogleCloudPlatform/spark-on-k8s-operator/blob/master/docs/api-docs.md#sparkapplication
+    :param sparkapplication_name: sparkapplication resource name
+    :type str
+    :param namespace: the kubernetes namespace where the sparkApplication 
reside in
+    :type str
+    :param conn_id: the connection to Kubernetes cluster
+    :type str
+    """
+
+    template_fields = ('sparkapplication_name', 'namespace')
+    INTERMEDIATE_STATES = ('SUBMITTED', 'RUNNING',)
+    FAILURE_STATES = ('FAILED', 'SUBMISSION_FAILED', 'UNKNOWN')
+    SUCCESS_STATES = 'COMPLETED'
+
+    @apply_defaults
+    def __init__(self,
+                 sparkapplication_name: str,
+                 namespace: str = 'default',
+                 conn_id: str = 'kubernetes_default',
+                 *args, **kwargs):
+        super().__init__(*args, **kwargs)
+        self.sparkapplication_name = sparkapplication_name
+        self.namespace = namespace
+        self.conn_id = conn_id
+
+    def poke(self, context: Dict):
+        self.log.info("Poking: %s", self.sparkapplication_name)
+        api_client = Kuberneteshook(conn_id=self.conn_id).get_conn()
+        custom_resource_definition_api = client.CustomObjectsApi(api_client)
+        try:
+            response = 
custom_resource_definition_api.get_namespaced_custom_object(
+                group="sparkoperator.k8s.io",
+                version="v1beta2",
+                namespace=self.namespace,
+                plural="sparkapplications",
+                name=self.sparkapplication_name
+            )
+            sparkapplication_state = 
response['status']['applicationState']['state']
+            if sparkapplication_state in self.FAILURE_STATES:
+                raise AirflowException("spark application failed with state: 
%s" % sparkapplication_state)
+            if sparkapplication_state in self.INTERMEDIATE_STATES:
+                self.log.info("spark application is still in state: %s", 
sparkapplication_state)
+                return False
+            if sparkapplication_state in self.SUCCESS_STATES:
+                self.log.info("spark application ended successfully")
+                return True
+            raise AirflowException("unknown spark application state: %s" % 
sparkapplication_state)
 
 Review comment:
   ```suggestion
               raise AirflowException("Unknown spark application state: %s" % 
sparkapplication_state)
   ```

----------------------------------------------------------------
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.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to