churromorales commented on code in PR #13156:
URL: https://github.com/apache/druid/pull/13156#discussion_r995063507


##########
extensions-contrib/kubernetes-overlord-extensions/src/main/java/org/apache/druid/k8s/overlord/common/K8sTaskAdapter.java:
##########
@@ -0,0 +1,277 @@
+/*
+ * 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.druid.k8s.overlord.common;
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.google.common.annotations.VisibleForTesting;
+import com.google.common.base.Joiner;
+import com.google.common.collect.ImmutableMap;
+import com.google.common.collect.Iterables;
+import com.google.common.collect.Lists;
+import io.fabric8.kubernetes.api.model.Container;
+import io.fabric8.kubernetes.api.model.ContainerPort;
+import io.fabric8.kubernetes.api.model.EnvVar;
+import io.fabric8.kubernetes.api.model.EnvVarBuilder;
+import io.fabric8.kubernetes.api.model.EnvVarSourceBuilder;
+import io.fabric8.kubernetes.api.model.ObjectFieldSelector;
+import io.fabric8.kubernetes.api.model.ObjectMeta;
+import io.fabric8.kubernetes.api.model.Pod;
+import io.fabric8.kubernetes.api.model.PodSpec;
+import io.fabric8.kubernetes.api.model.PodTemplateSpec;
+import io.fabric8.kubernetes.api.model.Quantity;
+import io.fabric8.kubernetes.api.model.ResourceRequirementsBuilder;
+import io.fabric8.kubernetes.api.model.batch.v1.Job;
+import io.fabric8.kubernetes.api.model.batch.v1.JobBuilder;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.druid.indexing.common.task.Task;
+import org.apache.druid.java.util.common.HumanReadableBytes;
+import org.apache.druid.java.util.emitter.EmittingLogger;
+import org.apache.druid.k8s.overlord.KubernetesTaskRunnerConfig;
+
+import java.io.IOException;
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+
+/**
+ * This class transforms tasks to pods, and pods to tasks to assist with 
creating the job spec for a
+ * peon task.
+ * The two subclasses of this class are the SingleContainerTaskAdapter and the 
MultiContainerTaskAdapter
+ * This class runs on the overlord, to convert a task into a job, it will take 
its own podSpec (the current running overlord)
+ * keep volumees, secrets, env variables, config maps, etc. and add some 
additional information as well as provide a new
+ * command for running the task.
+ * The SingleContainerTaskAdapter only runs a task in a single container (no 
sidecars)
+ * The MultiContainerTaskAdapter runs with all the sidecars the current 
running overlord runs with.  Thus, it needs
+ * to add some extra coordination to shut down sidecar containers when the 
main pod exits.
+ */
+
+public abstract class K8sTaskAdapter implements TaskAdapter<Pod, Job>
+{
+
+  private static final EmittingLogger log = new 
EmittingLogger(K8sTaskAdapter.class);
+
+  protected final KubernetesClientApi client;
+  protected final KubernetesTaskRunnerConfig config;
+  protected final ObjectMapper mapper;
+
+  public K8sTaskAdapter(
+      KubernetesClientApi client,
+      KubernetesTaskRunnerConfig config,
+      ObjectMapper mapper
+  )
+  {
+    this.client = client;
+    this.config = config;
+    this.mapper = mapper;
+  }
+
+  @Override
+  public Job fromTask(Task task, PeonCommandContext context) throws IOException
+  {
+    String myPodName = System.getenv("HOSTNAME");
+    Pod pod = client.executeRequest(client -> 
client.pods().inNamespace(config.namespace).withName(myPodName).get());
+    return createJobFromPodSpec(pod.getSpec(), task, context);
+  }
+
+  @Override
+  public Task toTask(Pod from) throws IOException
+  {
+    List<EnvVar> envVars = from.getSpec().getContainers().get(0).getEnv();
+    Optional<EnvVar> taskJson = envVars.stream().filter(x -> 
"TASK_JSON".equals(x.getName())).findFirst();
+    String contents = taskJson.map(envVar -> 
taskJson.get().getValue()).orElse(null);
+    if (contents == null) {
+      throw new IOException("No TASK_JSON environment variable found in pod: " 
+ from.getMetadata().getName());
+    }
+    return mapper.readValue(Base64Compression.decompressBase64(contents), 
Task.class);
+  }
+
+  @VisibleForTesting
+  public abstract Job createJobFromPodSpec(PodSpec podSpec, Task task, 
PeonCommandContext context) throws IOException;
+
+  protected Job buildJob(
+      K8sTaskId k8sTaskId,
+      Map<String, String> labels,
+      Map<String, String> annotations, PodTemplateSpec podTemplate
+  )
+  {
+    return new JobBuilder()
+        .withNewMetadata()
+        .withName(k8sTaskId.getK8sTaskId())
+        .addToLabels(labels)
+        .addToAnnotations(annotations)
+        .endMetadata()
+        .withNewSpec()
+        .withTemplate(podTemplate)
+        
.withActiveDeadlineSeconds(config.maxTaskDuration.toStandardDuration().getStandardSeconds())
+        .withBackoffLimit(0)
+        .withTtlSecondsAfterFinished((int) 
config.taskCleanupDelay.toStandardDuration().getStandardSeconds())
+        .endSpec()
+        .build();
+  }
+
+  @VisibleForTesting
+  static Optional<Long> getJavaOptValueBytes(String qualifier, List<String> 
commands)
+  {
+    Long result = null;
+    Optional<String> lastHeapValue = commands.stream().filter(x -> 
x.startsWith(qualifier)).reduce((x, y) -> y);
+    if (lastHeapValue.isPresent()) {
+      result = 
HumanReadableBytes.parse(StringUtils.removeStart(lastHeapValue.get(), 
qualifier));
+    }
+    return Optional.ofNullable(result);
+  }
+
+  @VisibleForTesting
+  static long getContainerMemory(PeonCommandContext context)
+  {
+    List<String> javaOpts = context.getJavaOpts();
+    Optional<Long> optionalXmx = getJavaOptValueBytes("-Xmx", javaOpts);
+    long heapSize = HumanReadableBytes.parse("1g");
+    if (optionalXmx.isPresent()) {
+      heapSize = optionalXmx.get();
+    }
+    Optional<Long> optionalDbb = 
getJavaOptValueBytes("-XX:MaxDirectMemorySize=", javaOpts);
+    long dbbSize = heapSize;
+    if (optionalDbb.isPresent()) {
+      dbbSize = optionalDbb.get();
+    }
+    return (long) ((dbbSize + heapSize) * 1.2);
+
+  }
+
+  protected void setupPorts(Container mainContainer)
+  {
+    mainContainer.getPorts().clear();
+    ContainerPort tcpPort = new ContainerPort();
+    tcpPort.setContainerPort(DruidK8sConstants.PORT);
+    tcpPort.setName("druid-port");
+    tcpPort.setProtocol("TCP");
+    ContainerPort httpsPort = new ContainerPort();
+    httpsPort.setContainerPort(DruidK8sConstants.TLS_PORT);
+    httpsPort.setName("druid-tls-port");
+    httpsPort.setProtocol("TCP");
+    mainContainer.setPorts(Lists.newArrayList(httpsPort, tcpPort));
+  }
+
+  protected void addEnvironmentVariables(Container mainContainer, 
PeonCommandContext context, String taskContents)

Review Comment:
   annotations and labels are configurable, if you can provide me with an 
example of how we would need env variables, i would be happy to add it as 
configurable.  But I personally think that could cause more harm than good 
potentially. 



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

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to