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_r370951969
 
 

 ##########
 File path: airflow/contrib/operators/spark_kubernetes_operator.py
 ##########
 @@ -0,0 +1,77 @@
+# -*- 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 Optional
+
+from kubernetes import client
+
+from airflow.contrib.hooks.kubernetes_hook import Kuberneteshook
+from airflow.exceptions import AirflowException
+from airflow.models import BaseOperator
+from airflow.utils.decorators import apply_defaults
+
+
+class SparkKubernetesOperator(BaseOperator):
+    """
+    creates sparkapplication object in kubernetes cluster
+
+    :param sparkapplication_object: kubernetes custom_resource_definition of 
sparkApplication
+    :param namespace: kubernetes namespace to put sparkApplication
+    :param kube_config: kubernetes kube_config path
+    :param in_cluster: if airflow runs inside kubernetes pod take 
configuration from inside the cluster.
+    """
+
+    template_fields = ['sparkapplication_object', 'namespace', 'kube_config']
+    template_ext = ()
+    ui_color = '#f4a460'
+
+    @apply_defaults
+    def __init__(self,
+                 sparkapplication_object: dict,
+                 namespace: str = 'default',
+                 kube_config: Optional[str] = None,
+                 in_cluster: bool = False,
+                 *args, **kwargs) -> None:
+        super().__init__(*args, **kwargs)
+        self.sparkapplication_object = sparkapplication_object
+        self.namespace = namespace
+        self.kube_config = kube_config
+        self.in_cluster = in_cluster
+        if kwargs.get('xcom_push') is not None:
+            raise AirflowException("'xcom_push' was deprecated, use 
'BaseOperator.do_xcom_push' instead")
+
+    def execute(self, context):
+        self.log.info("creating sparkApplication")
+        hook = Kuberneteshook(
+            kube_config=self.kube_config,
+            in_cluster=self.in_cluster
+        )
+        api_client = hook.get_conn()
+        api = client.CustomObjectsApi(api_client)
 
 Review comment:
   Hook should allow reuse of code. This hook ensures the best integration of 
Airflow with an external library.  It is perfectly fine if you need to create 
more methods, but you will not need to create all the methods that are 
available in the client, because not all methods make sense in the case of 
Airflow e.g. no one will perform cluster administrative operations using 
Airflow. If you create a method, you will be able to provide additional unique 
improvements for Airflow, e.g. wait for the correct completion of the 
operation, or provide a default value for some parameters (e.g. project ID or 
namespace). A possible improvement that I can now imagine is storing the 
namespace value in the connection configuration.  A similar improvement exists 
in the integration of Google Cloud Platform - 
https://github.com/apache/airflow/blob/43dcc13/airflow/gcp/hooks/base.py#L332-L358

----------------------------------------------------------------
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