xinyuiscool commented on a change in pull request #1197: SAMZA-2067: Support 
Samza's running on Kubernetes
URL: https://github.com/apache/samza/pull/1197#discussion_r347048363
 
 

 ##########
 File path: 
samza-kubernetes/src/main/java/org/apache/samza/job/kubernetes/KubeClusterResourceManager.java
 ##########
 @@ -0,0 +1,298 @@
+/*
+ * 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.samza.job.kubernetes;
+
+import io.fabric8.kubernetes.api.model.*;
+import io.fabric8.kubernetes.client.KubernetesClient;
+import io.fabric8.kubernetes.client.KubernetesClientException;
+import io.fabric8.kubernetes.client.Watcher;
+import java.net.URL;
+import java.util.*;
+import org.apache.samza.clustermanager.ClusterResourceManager;
+import org.apache.samza.clustermanager.ResourceRequestState;
+import org.apache.samza.clustermanager.SamzaApplicationState;
+import org.apache.samza.clustermanager.SamzaResourceRequest;
+import org.apache.samza.clustermanager.SamzaResource;
+import org.apache.samza.config.ClusterManagerConfig;
+import org.apache.samza.config.Config;
+import org.apache.samza.config.TaskConfig;
+import org.apache.samza.coordinator.JobModelManager;
+import org.apache.samza.coordinator.stream.messages.SetContainerHostMapping;
+import org.apache.samza.job.CommandBuilder;
+import org.apache.samza.job.ShellCommandBuilder;
+import org.apache.samza.util.Util;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import static org.apache.samza.config.ApplicationConfig.*;
+import static org.apache.samza.config.KubeConfig.*;
+
+/**
+ * An {@link KubeClusterResourceManager} implements a ClusterResourceManager 
using Kubernetes as the underlying
+ * resource manager.
+ */
+public class KubeClusterResourceManager extends ClusterResourceManager {
+  private static final Logger LOG = 
LoggerFactory.getLogger(KubeClusterResourceManager.class);
+  private final Map<String, String> podLabels = new HashMap<>();
+  private KubernetesClient client;
+  private String appId;
+  private String appName;
+  private String image;
+  private String namespace;
+  private OwnerReference ownerReference;
+  private JobModelManager jobModelManager;
+  private boolean hostAffinityEnabled;
+  private Config config;
+  private String jcPodName;
+
+  KubeClusterResourceManager(Config config, JobModelManager jobModelManager, 
ClusterResourceManager.Callback callback) {
+    super(callback);
+    this.config = config;
+    this.client = KubeClientFactory.create();
+    this.jobModelManager = jobModelManager;
+    this.image = config.get(APP_IMAGE, DEFAULT_IMAGE);
+    this.namespace = config.get(K8S_API_NAMESPACE, "default");
+    this.appId = config.get(APP_ID, "001");
+    this.appName = config.get(APP_NAME, "samza");
+    ClusterManagerConfig clusterManagerConfig = new 
ClusterManagerConfig(config);
+    this.hostAffinityEnabled = clusterManagerConfig.getHostAffinityEnabled();
+    createOwnerReferences();
+  }
+
+  @Override
+  public void start() {
+    LOG.info("Kubernetes Cluster ResourceManager started, starting watcher");
+    startPodWatcher();
+    jobModelManager.start();
+  }
+
+  // Create the owner reference for the samza-job-coordinator pod
+  private void createOwnerReferences() {
+    this.jcPodName = System.getenv(COORDINATOR_POD_NAME);
+    LOG.info("job coordinator pod name is: {}, namespace is: {}", jcPodName, 
namespace);
+    Pod pod = client.pods().inNamespace(namespace).withName(jcPodName).get();
+    ownerReference = new OwnerReferenceBuilder()
+        .withName(pod.getMetadata().getName())
+        .withApiVersion(pod.getApiVersion())
+        .withUid(pod.getMetadata().getUid())
+        .withKind(pod.getKind())
+        .withController(true).build();
+    podLabels.put("jc-pod-name", jcPodName);
+  }
+
+  public void startPodWatcher() {
+    Watcher watcher = new Watcher<Pod>() {
+      @Override
+      public void eventReceived(Action action, Pod pod) {
+        if 
(!pod.getMetadata().getLabels().get("jc-pod-name").equals(jcPodName)) {
+          LOG.warn("This JC pod name is " + jcPodName + ", received pods for a 
different JC "
+              + pod.getMetadata().getLabels().get("jc-pod-name"));
+          return;
+        }
+        LOG.info("Pod watcher received action " + action + " for pod " + 
pod.getMetadata().getName());
+        switch (action) {
+          case ADDED:
+            LOG.info("Pod " + pod.getMetadata().getName() + " is added.");
+            break;
+          case MODIFIED:
+            LOG.info("Pod " + pod.getMetadata().getName() + " is modified.");
+            if (isPodFailed(pod)) {
+              deletePod(pod);
+            }
+            break;
+          case ERROR:
+            LOG.info("Pod " + pod.getMetadata().getName() + " received 
error.");
+            if (isPodFailed(pod)) {
+              deletePod(pod);
+            }
+            break;
+          case DELETED:
+            LOG.info("Pod " + pod.getMetadata().getName() + " is deleted.");
+            createNewStreamProcessor(pod);
+            break;
+        }
+      }
+      @Override
+      public void onClose(KubernetesClientException e) {
+        LOG.error("Pod watcher closed", e);
+      }
+    };
+
+    // TODO: SAMZA-2367: "podLabels" is empty. Need to add labels when 
creating Pod
+    client.pods().withLabels(podLabels).watch(watcher);
+  }
+
+  private boolean isPodFailed(Pod pod) {
+    return pod.getStatus() != null && 
pod.getStatus().getPhase().equals("Failed");
+  }
+
+  private void deletePod(Pod pod) {
+    boolean deleted = client.pods().delete(pod);
+    if (deleted) {
+      LOG.info("Deleted pod " + pod.getMetadata().getName());
+    } else {
+      LOG.info("Failed to deleted pod " + pod.getMetadata().getName());
 
 Review comment:
   DO we need to handle failure here?

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