chrissng commented on a change in pull request #3240: [ZEPPELIN-3840] Zeppelin 
on Kubernetes
URL: https://github.com/apache/zeppelin/pull/3240#discussion_r280949311
 
 

 ##########
 File path: 
zeppelin-plugins/launcher/k8s-standard/src/main/java/org/apache/zeppelin/interpreter/launcher/K8sStandardInterpreterLauncher.java
 ##########
 @@ -0,0 +1,177 @@
+/*
+ * 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.
+ */
+
+package org.apache.zeppelin.interpreter.launcher;
+
+import java.io.File;
+import java.net.InetAddress;
+import java.net.UnknownHostException;
+import java.nio.charset.Charset;
+import java.nio.file.Files;
+import java.nio.file.Paths;
+
+import com.google.common.annotations.VisibleForTesting;
+import org.apache.zeppelin.conf.ZeppelinConfiguration;
+import org.apache.zeppelin.interpreter.recovery.RecoveryStorage;
+import org.apache.zeppelin.interpreter.remote.RemoteInterpreterUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * Interpreter Launcher which use shell script to launch the interpreter 
process.
+ */
+public class K8sStandardInterpreterLauncher extends InterpreterLauncher {
+
+  private static final Logger LOGGER = 
LoggerFactory.getLogger(K8sStandardInterpreterLauncher.class);
+  private final Kubectl kubectl;
+  private InterpreterLaunchContext context;
+
+
+  public K8sStandardInterpreterLauncher(ZeppelinConfiguration zConf, 
RecoveryStorage recoveryStorage) throws IOException {
+    super(zConf, recoveryStorage);
+    kubectl = new Kubectl(zConf.getK8sKubectlCmd());
+    kubectl.setNamespace(getNamespace());
+  }
+
+  @VisibleForTesting
+  K8sStandardInterpreterLauncher(ZeppelinConfiguration zConf, RecoveryStorage 
recoveryStorage, Kubectl kubectl) {
+    super(zConf, recoveryStorage);
+    this.kubectl = kubectl;
+  }
+
+
+  /**
+   * Check if i'm running inside of kubernetes or not.
+   * It should return truth regardless of ZeppelinConfiguration.getRunMode().
+   *
+   * Normally, unless Zeppelin is running on Kubernetes, 
K8sStandardInterpreterLauncher shouldn't even have initialized.
+   * However, when ZeppelinConfiguration.getRunMode() is force 'k8s', 
InterpreterSetting.getLauncherPlugin() will try
+   * to use K8sStandardInterpreterLauncher. This is useful for development. It 
allows Zeppelin server running on your
+   * IDE and creates your interpreters in Kubernetes. So any code changes on 
Zeppelin server or kubernetes yaml spec
+   * can be applied without re-building docker image.
+   * @return
+   */
+  boolean isRunningOnKubernetes() {
+    if (new File("/var/run/secrets/kubernetes.io").exists()) {
+      return true;
+    } else {
+      return false;
+    }
+  }
+
+  /**
+   * Get current namespace
+   * @throws IOException
+   */
+  String getNamespace() throws IOException {
+    if (isRunningOnKubernetes()) {
+      return 
readFile("/var/run/secrets/kubernetes.io/serviceaccount/namespace", 
Charset.defaultCharset()).trim();
+    } else {
+      return "default";
+    }
+  }
+
+  /**
+   * Get hostname. It should be the same to Service name (and Pod name) of the 
Kubernetes
+   * @return
+   */
+  String getHostname() {
+    try {
+      return InetAddress.getLocalHost().getHostName();
+    } catch (UnknownHostException e) {
+      return "localhost";
+    }
+  }
+
+  /**
+   * get Zeppelin server host dns.
+   * return <hostname>.<namespace>.svc.cluster.local
+   * @throws IOException
+   */
+  private String getZeppelinServiceHost() throws IOException {
+    if (isRunningOnKubernetes()) {
+      return String.format("%s.%s.svc.cluster.local",
+              getHostname(), // service name and pod name should be the same
 
 Review comment:
   That should work 👍 

----------------------------------------------------------------
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:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to