uds5501 commented on code in PR #18425: URL: https://github.com/apache/druid/pull/18425#discussion_r2289818817
########## embedded-tests/src/test/java/org/apache/druid/testing/embedded/k8s/K3sClusterResource.java: ########## @@ -0,0 +1,369 @@ +/* + * 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.testing.embedded.k8s; + +import com.github.dockerjava.api.DockerClient; +import com.github.dockerjava.api.exception.NotFoundException; +import com.github.dockerjava.core.DefaultDockerClientConfig; +import com.github.dockerjava.core.DockerClientImpl; +import com.github.dockerjava.httpclient5.ApacheDockerHttpClient; +import io.fabric8.kubernetes.api.model.ConfigMap; +import io.fabric8.kubernetes.api.model.ObjectMeta; +import io.fabric8.kubernetes.client.Config; +import io.fabric8.kubernetes.client.KubernetesClient; +import io.fabric8.kubernetes.client.KubernetesClientBuilder; +import io.fabric8.kubernetes.client.KubernetesClientTimeoutException; +import io.fabric8.kubernetes.client.dsl.PodResource; +import io.fabric8.kubernetes.client.utils.Serialization; +import org.apache.druid.error.InvalidInput; +import org.apache.druid.java.util.common.FileUtils; +import org.apache.druid.java.util.common.ISE; +import org.apache.druid.java.util.common.StringUtils; +import org.apache.druid.java.util.common.io.Closer; +import org.apache.druid.java.util.common.logger.Logger; +import org.apache.druid.testing.embedded.EmbeddedDruidCluster; +import org.apache.druid.testing.embedded.TestFolder; +import org.apache.druid.testing.embedded.TestcontainerResource; +import org.apache.druid.testing.embedded.docker.DruidContainerResource; +import org.apache.druid.testing.embedded.indexing.Resources; +import org.apache.druid.testing.tools.ITRetryUtil; +import org.testcontainers.k3s.K3sContainer; +import org.testcontainers.utility.DockerImageName; +import org.testcontainers.utility.MountableFile; + +import java.io.ByteArrayInputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.InputStream; +import java.io.StringWriter; +import java.net.URL; +import java.nio.charset.StandardCharsets; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.Properties; +import java.util.concurrent.TimeUnit; + +public class K3sClusterResource extends TestcontainerResource<K3sContainer> +{ + private static final Logger log = new Logger(K3sClusterResource.class); + + private static final String K3S_IMAGE_NAME = "rancher/k3s:v1.28.8-k3s1"; + + public static final String DRUID_NAMESPACE = "druid"; + private static final String NAMESPACE_MANIFEST = "manifests/druid-namespace.yaml"; + + private static final String COMMON_CONFIG_MAP = "druid-common-props"; + private static final String SERVICE_CONFIG_MAP = "druid-%s-props"; + + public static final long POD_READY_TIMEOUT_SECONDS = 300; + + private final List<File> manifestFiles = new ArrayList<>(); + private final List<K3sDruidService> services = new ArrayList<>(); + + private KubernetesClient client; + private String druidImageName; + + private final Closer closer = Closer.create(); + + public K3sClusterResource() + { + // Add the namespace manifest + manifestFiles.add(Resources.getFileForResource(NAMESPACE_MANIFEST)); + } + + public K3sClusterResource addService(K3sDruidService service) + { + services.add(service); + return this; + } + + public K3sClusterResource usingDruidImage(String druidImageName) + { + this.druidImageName = druidImageName; + return this; + } + + /** + * Uses the Docker test image specified by the system property + * {@link DruidContainerResource#PROPERTY_TEST_IMAGE} for this container. + */ + public K3sClusterResource usingTestImage() + { + final String imageName = System.getProperty(DruidContainerResource.PROPERTY_TEST_IMAGE); + InvalidInput.conditionalException( + imageName != null, + StringUtils.format( + "System property[%s] must be set while running Docker tests locally" + + " to specify which Druid image to use. Update your run configuration" + + " to include '-D%s=<your-test-image>'.", + DruidContainerResource.PROPERTY_TEST_IMAGE, + DruidContainerResource.PROPERTY_TEST_IMAGE + ) + ); + return usingDruidImage(imageName); + } + + @Override + protected K3sContainer createContainer() + { + Objects.requireNonNull(druidImageName, "No Druid image specified"); Review Comment: Nit: If this is a not null construct for container to be created, imo it makes more sense to pass it in the constructor itself rather than via `.usingDruidImage()` ########## embedded-tests/src/test/java/org/apache/druid/testing/embedded/k8s/K3sClusterResource.java: ########## @@ -0,0 +1,369 @@ +/* + * 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.testing.embedded.k8s; + +import com.github.dockerjava.api.DockerClient; +import com.github.dockerjava.api.exception.NotFoundException; +import com.github.dockerjava.core.DefaultDockerClientConfig; +import com.github.dockerjava.core.DockerClientImpl; +import com.github.dockerjava.httpclient5.ApacheDockerHttpClient; +import io.fabric8.kubernetes.api.model.ConfigMap; +import io.fabric8.kubernetes.api.model.ObjectMeta; +import io.fabric8.kubernetes.client.Config; +import io.fabric8.kubernetes.client.KubernetesClient; +import io.fabric8.kubernetes.client.KubernetesClientBuilder; +import io.fabric8.kubernetes.client.KubernetesClientTimeoutException; +import io.fabric8.kubernetes.client.dsl.PodResource; +import io.fabric8.kubernetes.client.utils.Serialization; +import org.apache.druid.error.InvalidInput; +import org.apache.druid.java.util.common.FileUtils; +import org.apache.druid.java.util.common.ISE; +import org.apache.druid.java.util.common.StringUtils; +import org.apache.druid.java.util.common.io.Closer; +import org.apache.druid.java.util.common.logger.Logger; +import org.apache.druid.testing.embedded.EmbeddedDruidCluster; +import org.apache.druid.testing.embedded.TestFolder; +import org.apache.druid.testing.embedded.TestcontainerResource; +import org.apache.druid.testing.embedded.docker.DruidContainerResource; +import org.apache.druid.testing.embedded.indexing.Resources; +import org.apache.druid.testing.tools.ITRetryUtil; +import org.testcontainers.k3s.K3sContainer; +import org.testcontainers.utility.DockerImageName; +import org.testcontainers.utility.MountableFile; + +import java.io.ByteArrayInputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.InputStream; +import java.io.StringWriter; +import java.net.URL; +import java.nio.charset.StandardCharsets; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.Properties; +import java.util.concurrent.TimeUnit; + +public class K3sClusterResource extends TestcontainerResource<K3sContainer> +{ + private static final Logger log = new Logger(K3sClusterResource.class); + + private static final String K3S_IMAGE_NAME = "rancher/k3s:v1.28.8-k3s1"; + + public static final String DRUID_NAMESPACE = "druid"; + private static final String NAMESPACE_MANIFEST = "manifests/druid-namespace.yaml"; + + private static final String COMMON_CONFIG_MAP = "druid-common-props"; + private static final String SERVICE_CONFIG_MAP = "druid-%s-props"; + + public static final long POD_READY_TIMEOUT_SECONDS = 300; + + private final List<File> manifestFiles = new ArrayList<>(); + private final List<K3sDruidService> services = new ArrayList<>(); + + private KubernetesClient client; + private String druidImageName; + + private final Closer closer = Closer.create(); + + public K3sClusterResource() + { + // Add the namespace manifest + manifestFiles.add(Resources.getFileForResource(NAMESPACE_MANIFEST)); + } + + public K3sClusterResource addService(K3sDruidService service) + { + services.add(service); + return this; + } + + public K3sClusterResource usingDruidImage(String druidImageName) + { + this.druidImageName = druidImageName; + return this; + } + + /** + * Uses the Docker test image specified by the system property + * {@link DruidContainerResource#PROPERTY_TEST_IMAGE} for this container. + */ + public K3sClusterResource usingTestImage() + { + final String imageName = System.getProperty(DruidContainerResource.PROPERTY_TEST_IMAGE); + InvalidInput.conditionalException( + imageName != null, + StringUtils.format( + "System property[%s] must be set while running Docker tests locally" + + " to specify which Druid image to use. Update your run configuration" + + " to include '-D%s=<your-test-image>'.", + DruidContainerResource.PROPERTY_TEST_IMAGE, + DruidContainerResource.PROPERTY_TEST_IMAGE + ) + ); + return usingDruidImage(imageName); + } + + @Override + protected K3sContainer createContainer() + { + Objects.requireNonNull(druidImageName, "No Druid image specified"); + final K3sContainer container = new K3sContainer(DockerImageName.parse(K3S_IMAGE_NAME)); + + final List<String> portBindings = new ArrayList<>(); + for (K3sDruidService service : services) { + for (int port : service.getCommand().getExposedPorts()) { + container.addExposedPorts(port); + + // Bind the ports statically (rather than using a mapped port) so that this + // Druid service is discoverable with the Druid service discovery + portBindings.add(port + ":" + port); + } + } + + container.setPortBindings(portBindings); + return container; + } + + @Override + public void onStarted(EmbeddedDruidCluster cluster) + { + client = new KubernetesClientBuilder() + .withConfig(Config.fromKubeconfig(getContainer().getKubeConfigYaml())) + .build(); + closer.register(client); + + loadLocalDockerImageIntoContainer(druidImageName, cluster.getTestFolder()); + + manifestFiles.forEach(this::applyManifest); + + // Create common config map + final Properties commonProperties = new Properties(); + commonProperties.putAll(cluster.getCommonProperties()); + commonProperties.remove("druid.extensions.modulesForEmbeddedTests"); + applyConfigMap( + newConfigMap(COMMON_CONFIG_MAP, commonProperties, "common.runtime.properties") + ); + + // Create config maps and manifests for each service + for (K3sDruidService druidService : services) { + final String serviceConfigMap = StringUtils.format(SERVICE_CONFIG_MAP, druidService.getName()); + applyConfigMap( Review Comment: nice usage of config maps for k8s instead of direct yaml creation! -- 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]
