georgew5656 commented on code in PR #13896:
URL: https://github.com/apache/druid/pull/13896#discussion_r1131520570


##########
extensions-contrib/kubernetes-overlord-extensions/src/main/java/org/apache/druid/k8s/overlord/common/TaskAdapter.java:
##########
@@ -19,15 +19,17 @@
 
 package org.apache.druid.k8s.overlord.common;
 
+import io.fabric8.kubernetes.api.model.Pod;
+import io.fabric8.kubernetes.api.model.batch.v1.Job;
 import org.apache.druid.indexing.common.task.Task;
 
 import java.io.IOException;
 
-public interface TaskAdapter<K, V>
+public interface TaskAdapter
 {
 
-  V fromTask(Task task, PeonCommandContext context) throws IOException;

Review Comment:
   i think there was some idea that we would want to leave open the option of 
non K8s Task Runners (e.g. fargate) which would imply the need for non K8s 
TaskAdapters, is there a need to override this class? wouldn't K8sTaskAdapter 
implements TaskAdapter<Job, Pod> still work here?



##########
extensions-contrib/kubernetes-overlord-extensions/src/main/java/org/apache/druid/k8s/overlord/common/PodTemplateTaskAdapter.java:
##########
@@ -0,0 +1,222 @@
+/*
+ * 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.fasterxml.jackson.databind.cfg.MapperConfig;
+import com.fasterxml.jackson.databind.introspect.AnnotatedClass;
+import com.fasterxml.jackson.databind.introspect.AnnotatedClassResolver;
+import com.fasterxml.jackson.databind.jsontype.NamedType;
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableMap;
+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.Pod;
+import io.fabric8.kubernetes.api.model.PodTemplate;
+import io.fabric8.kubernetes.api.model.batch.v1.Job;
+import io.fabric8.kubernetes.api.model.batch.v1.JobBuilder;
+import org.apache.druid.guice.IndexingServiceModuleHelper;
+import org.apache.druid.indexing.common.config.TaskConfig;
+import org.apache.druid.indexing.common.task.Task;
+import org.apache.druid.java.util.common.IAE;
+import org.apache.druid.java.util.common.IOE;
+import org.apache.druid.java.util.common.ISE;
+import org.apache.druid.java.util.common.StringUtils;
+import org.apache.druid.java.util.common.logger.Logger;
+import org.apache.druid.k8s.overlord.KubernetesTaskRunnerConfig;
+import org.apache.druid.server.DruidNode;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Optional;
+import java.util.Properties;
+
+public class PodTemplateTaskAdapter implements TaskAdapter
+{
+  public static String TYPE = "PodTemplate";
+
+  private static final Logger log = new Logger(PodTemplateTaskAdapter.class);
+  private static final String TASK_PROPERTY = 
IndexingServiceModuleHelper.INDEXER_RUNNER_PROPERTY_PREFIX + 
".k8s.podTemplate.%s";
+
+  private final KubernetesClientApi client;
+  private final KubernetesTaskRunnerConfig taskRunnerConfig;
+  private final TaskConfig taskConfig;
+  private final DruidNode node;
+  private final ObjectMapper mapper;
+  private final HashMap<String, PodTemplate> templates;
+
+  public PodTemplateTaskAdapter(
+      KubernetesClientApi client,
+      KubernetesTaskRunnerConfig taskRunnerConfig,
+      TaskConfig taskConfig,
+      DruidNode node,
+      ObjectMapper mapper,
+      Properties properties
+  )
+  {
+    this.client = client;
+    this.taskRunnerConfig = taskRunnerConfig;
+    this.taskConfig = taskConfig;
+    this.node = node;
+    this.mapper = mapper;
+    this.templates = initializePodTemplates(properties);
+  }
+
+  @Override
+  public Job fromTask(Task task) throws IOException
+  {
+    PodTemplate podTemplate = templates.getOrDefault(task.getType(), 
templates.get("base"));
+    if (podTemplate == null) {
+      throw new ISE("Pod template spec not found for task type [%s]", 
task.getType());
+    }
+
+    return new JobBuilder()
+        .withNewMetadata()
+        .withName(new K8sTaskId(task).getK8sTaskId())
+        .addToLabels(getJobLabels(taskRunnerConfig))
+        .addToAnnotations(getJobAnnotations(taskRunnerConfig, task))
+        .endMetadata()
+        .withNewSpec()
+        .withTemplate(podTemplate.getTemplate())
+        .editTemplate()
+        .editOrNewMetadata()
+        .addToAnnotations(getPodTemplateAnnotations(task))
+        .addToLabels(getPodLabels(taskRunnerConfig))
+        .endMetadata()
+        .editSpec()
+        .editFirstContainer()
+        .addAllToEnv(getEnv())
+        .endContainer()
+        .endSpec()
+        .endTemplate()
+        
.withActiveDeadlineSeconds(taskRunnerConfig.maxTaskDuration.toStandardDuration().getStandardSeconds())
+        .withBackoffLimit(0)
+        .withTtlSecondsAfterFinished((int) 
taskRunnerConfig.taskCleanupDelay.toStandardDuration().getStandardSeconds())
+        .endSpec()
+        .build();
+  }
+
+  @Override
+  public Task toTask(Pod from) throws IOException
+  {
+    Map<String, String> annotations = from.getMetadata().getAnnotations();
+    if (annotations == null) {
+      throw new IOE("No annotations found on pod [%s]", 
from.getMetadata().getName());
+    }
+    String task = annotations.get(DruidK8sConstants.TASK);
+    if (task == null) {
+      throw new IOE("No task annotation found on pod [%s]", 
from.getMetadata().getName());
+    }
+    return mapper.readValue(Base64Compression.decompressBase64(task), 
Task.class);
+  }
+
+  private HashMap<String, PodTemplate> initializePodTemplates(Properties 
properties)
+  {
+    HashMap<String, PodTemplate> podTemplateMap = new HashMap<>();
+    Optional<PodTemplate> basePodTemplate = loadPodTemplate("base", 
properties);
+    if (!basePodTemplate.isPresent()) {
+      throw new IAE("Pod template task adapter requires a base pod template to 
be specified");
+    }
+    podTemplateMap.put("base", basePodTemplate.get());
+
+    MapperConfig config = mapper.getDeserializationConfig();
+    AnnotatedClass cls = 
AnnotatedClassResolver.resolveWithoutSuperTypes(config, Task.class);
+    Collection<NamedType> taskSubtypes = 
mapper.getSubtypeResolver().collectAndResolveSubtypesByClass(config, cls);
+    for (NamedType namedType : taskSubtypes) {

Review Comment:
   is there a use case that we'd want multiple specs for a single task type? 
like e.g. a bigger index_kafka job vs a smaller index_kafka job? I don't think 
that needs to be supported in this PR but it would be good to still have the 
option to implement something like that in the future.
   
   I guess with this implementation you could just set 
druid.indexer.runner.k8s.podTemplate.index_parallel_custom or something, and 
assuming there's some field on the task (customJobTemplate or similar) that 
specifies to use this template, to reference that field?



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