Search in sources :

Example 1 with ImageConfig

use of com.bakdata.quick.manager.k8s.ImageConfig in project quick by bakdata.

the class GatewayResourceLoader method forCreation.

/**
 * Creates resources that can be deployed.
 *
 * <p>
 * This function creates all the Kubernetes resources that a gateway needs for its deployment. Deployment, Service,
 * Ingress, Middleware, and ConfigMap templates are filled with the data passed through the {@link
 * GatewayCreationData}.
 */
@Override
public GatewayResources forCreation(final GatewayCreationData gatewayCreationData, final ResourcePrefix resourcePrefix) {
    final String imageTag = Objects.requireNonNullElse(gatewayCreationData.getTag(), this.deploymentConfig.getDefaultImageTag());
    final int imageReplicas = Objects.requireNonNullElse(gatewayCreationData.getReplicas(), this.deploymentConfig.getDefaultReplicas());
    final String dockerRegistry = this.deploymentConfig.getDockerRegistry();
    final ImageConfig imageConfig = ImageConfig.of(dockerRegistry, GATEWAY_IMAGE, imageReplicas, imageTag);
    final String gatewayName = gatewayCreationData.getName();
    final String resourceName = getResourceName(gatewayName);
    final boolean hasFixedTag = gatewayCreationData.getTag() != null;
    final GatewayDeployment deployment = new GatewayDeployment(this.createGatewayDeployment(resourceName, imageConfig, this.resourceConfig, hasFixedTag));
    final GatewayService service = new GatewayService(this.createGatewayService(resourceName));
    final GatewayIngress ingress = new GatewayIngress(this.createGatewayIngress(resourceName, gatewayCreationData.getName(), this.namespace, this.deploymentConfig.getIngressHost(), this.deploymentConfig.isIngressSsl(), this.deploymentConfig.getIngressEntrypoint()));
    final GatewayMiddleware middleware = new GatewayMiddleware(this.createGatewayMiddleware(resourceName, gatewayCreationData.getName()));
    final GatewayConfigMap configMap = new GatewayConfigMap(this.createGatewayConfigMap(resourceName, gatewayCreationData.getSchema()));
    return new GatewayResources(gatewayName, deployment, service, ingress, middleware, configMap);
}
Also used : ImageConfig(com.bakdata.quick.manager.k8s.ImageConfig)

Example 2 with ImageConfig

use of com.bakdata.quick.manager.k8s.ImageConfig in project quick by bakdata.

the class ApplicationResourceLoader method forCreation.

/**
 * Creates resources that can be deployed.
 *
 * <p>
 * This function creates all the Kubernetes resources that an application needs for its deployment. Deployment and
 * Service templates are filled with the data passed.
 */
@Override
public ApplicationResources forCreation(final ApplicationCreationData appCreationData, final ResourcePrefix resourcePrefix) {
    final int replicas = Objects.requireNonNullElse(appCreationData.getReplicas(), this.deploymentConfig.getDefaultReplicas());
    final ImageConfig config = ImageConfig.of(appCreationData.getRegistry(), appCreationData.getImageName(), replicas, appCreationData.getTag());
    final String applicationName = appCreationData.getName();
    final String deploymentName = getDeploymentName(applicationName);
    final Map<String, String> arguments = Objects.requireNonNullElse(appCreationData.getArguments(), Map.of());
    final List<String> listArgs = CliArgHandler.convertArgs(arguments, this.kafkaConfig);
    final ApplicationDeployment deployment = new ApplicationDeployment(this.createAppDeployment(deploymentName, listArgs, config, this.resourceConfig, appCreationData.getPort(), appCreationData.getImagePullSecret()));
    if (appCreationData.getPort() != null) {
        final ApplicationService service = new ApplicationService(this.createApplicationService(deploymentName, appCreationData.getPort()));
        return new ApplicationResources(applicationName, deployment, Optional.of(service));
    }
    return new ApplicationResources(applicationName, deployment, Optional.empty());
}
Also used : ImageConfig(com.bakdata.quick.manager.k8s.ImageConfig)

Example 3 with ImageConfig

use of com.bakdata.quick.manager.k8s.ImageConfig in project quick by bakdata.

the class MirrorResourceLoaderTest method shouldCreateMirrorDeployment.

@Test
void shouldCreateMirrorDeployment() {
    final ImageConfig imageConfig = ImageConfig.of(DOCKER_REGISTRY, EXPECTED_MIRROR_IMAGE, 3, DEFAULT_IMAGE_TAG);
    final MirrorCreationData mirrorCreationData = new MirrorCreationData(DEFAULT_NAME, DEFAULT_TOPIC_NAME, 3, null, Duration.of(1, ChronoUnit.MINUTES));
    final MirrorResources mirrorResources = this.loader.forCreation(mirrorCreationData, ResourcePrefix.MIRROR);
    final Optional<HasMetadata> hasMetadata = findResource(mirrorResources, ResourceKind.DEPLOYMENT);
    final Map<String, String> labels = Map.of("app.kubernetes.io/name", DEFAULT_DEPLOYMENT_NAME, "app.kubernetes.io/managed-by", "quick", "app.kubernetes.io/component", "mirror");
    final Map<String, String> selectors = Map.of("app.kubernetes.io/name", DEFAULT_DEPLOYMENT_NAME, "app.kubernetes.io/component", "mirror");
    final Map<String, String> annotations = Map.of("d9p.io/fixed-tag", "false");
    assertThat(hasMetadata).isPresent().get(InstanceOfAssertFactories.type(Deployment.class)).satisfies(deployment -> {
        assertThat(deployment.getMetadata()).satisfies(metadata -> {
            assertThat(metadata.getName()).isEqualTo(DEFAULT_DEPLOYMENT_NAME);
            assertThat(metadata.getLabels()).containsExactlyInAnyOrderEntriesOf(labels);
            assertThat(metadata.getAnnotations()).containsExactlyInAnyOrderEntriesOf(annotations);
        });
        assertThat(deployment.getSpec()).satisfies(spec -> {
            assertThat(spec.getReplicas()).isEqualTo(3);
            assertThat(spec.getSelector().getMatchLabels()).containsExactlyInAnyOrderEntriesOf(selectors);
            assertThat(spec.getTemplate().getMetadata().getLabels()).containsExactlyInAnyOrderEntriesOf(labels);
        });
        final DeploymentSpec deploymentSpec = deployment.getSpec();
        assertThat(deploymentSpec).hasFieldOrPropertyWithValue("replicas", 3).extracting(spec -> spec.getTemplate().getMetadata().getLabels(), MAP).containsExactlyInAnyOrderEntriesOf(labels);
        final PodSpec podSpec = deploymentSpec.getTemplate().getSpec();
        assertThat(podSpec.getContainers()).isNotNull().hasSize(1).first().hasFieldOrPropertyWithValue("image", imageConfig.asImageString()).hasFieldOrPropertyWithValue("imagePullPolicy", "Always").hasFieldOrPropertyWithValue("name", DEFAULT_DEPLOYMENT_NAME).satisfies(container -> {
            assertThat(container.getPorts()).isNotNull().hasSize(1).first().hasFieldOrPropertyWithValue("containerPort", KubernetesResources.CONTAINER_PORT);
            assertThat(container.getArgs()).contains(String.format("--input-topics=%s", DEFAULT_TOPIC_NAME), "--retention-time=PT1M");
            assertThat(container.getEnv()).isNotNull().hasSize(2).first().hasFieldOrPropertyWithValue("name", "POD_IP").extracting(EnvVar::getValueFrom).isNotNull().extracting(EnvVarSource::getFieldRef).hasFieldOrPropertyWithValue("fieldPath", "status.podIP");
            assertThat(container.getEnvFrom()).isNotNull().hasSize(1).first().extracting(EnvFromSource::getConfigMapRef).isNotNull().hasFieldOrPropertyWithValue("name", KubernetesResources.QUICK_CONFIG_NAME);
        });
    });
}
Also used : ImageConfig(com.bakdata.quick.manager.k8s.ImageConfig) MirrorCreationData(com.bakdata.quick.common.api.model.manager.creation.MirrorCreationData) ServiceSpec(io.fabric8.kubernetes.api.model.ServiceSpec) BeforeEach(org.junit.jupiter.api.BeforeEach) EnvVar(io.fabric8.kubernetes.api.model.EnvVar) Container(io.fabric8.kubernetes.api.model.Container) IntOrString(io.fabric8.kubernetes.api.model.IntOrString) MAP(org.assertj.core.api.InstanceOfAssertFactories.MAP) InstanceOfAssertFactories(org.assertj.core.api.InstanceOfAssertFactories) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) EnvFromSource(io.fabric8.kubernetes.api.model.EnvFromSource) PodSpec(io.fabric8.kubernetes.api.model.PodSpec) Duration(java.time.Duration) Map(java.util.Map) Service(io.fabric8.kubernetes.api.model.Service) DeploymentSpec(io.fabric8.kubernetes.api.model.apps.DeploymentSpec) ResourcePrefix(com.bakdata.quick.manager.k8s.resource.QuickResources.ResourcePrefix) KubernetesTest(com.bakdata.quick.manager.k8s.KubernetesTest) KubernetesResources(com.bakdata.quick.manager.k8s.KubernetesResources) HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) Test(org.junit.jupiter.api.Test) ChronoUnit(java.time.temporal.ChronoUnit) TestUtil(com.bakdata.quick.manager.TestUtil) EnvVarSource(io.fabric8.kubernetes.api.model.EnvVarSource) LIST(org.assertj.core.api.InstanceOfAssertFactories.LIST) ObjectMeta(io.fabric8.kubernetes.api.model.ObjectMeta) Optional(java.util.Optional) Deployment(io.fabric8.kubernetes.api.model.apps.Deployment) ImageConfig(com.bakdata.quick.manager.k8s.ImageConfig) HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) DeploymentSpec(io.fabric8.kubernetes.api.model.apps.DeploymentSpec) PodSpec(io.fabric8.kubernetes.api.model.PodSpec) IntOrString(io.fabric8.kubernetes.api.model.IntOrString) MirrorCreationData(com.bakdata.quick.common.api.model.manager.creation.MirrorCreationData) EnvFromSource(io.fabric8.kubernetes.api.model.EnvFromSource) KubernetesTest(com.bakdata.quick.manager.k8s.KubernetesTest) Test(org.junit.jupiter.api.Test)

Example 4 with ImageConfig

use of com.bakdata.quick.manager.k8s.ImageConfig in project quick by bakdata.

the class KubernetesApplicationServiceTest method shouldCreateDeletionJob.

@Test
void shouldCreateDeletionJob() {
    final ImageConfig imageConfig = ImageConfig.of(DOCKER_REGISTRY, IMAGE_NAME, 1, DEFAULT_IMAGE_TAG);
    this.deployApplication(DEFAULT_PORT, Map.of("--input-topics", "test"));
    assertThat(this.getDeployments()).isNotNull().hasSize(1);
    final Completable deleteRequest = this.service.deleteApplication(APP_NAME);
    Optional.ofNullable(deleteRequest.blockingGet()).ifPresent(Assertions::fail);
    assertThat(this.client.batch().v1().jobs().list().getItems()).isNotNull().hasSize(1).first().satisfies(job -> assertThat(job.getMetadata().getName()).isEqualTo(DEPLOYMENT_NAME + "-deletion")).extracting(job -> job.getSpec().getTemplate().getSpec().getContainers(), InstanceOfAssertFactories.list(Container.class)).hasSize(1).first().satisfies(container -> {
        assertThat(container.getImage()).isEqualTo(imageConfig.asImageString());
        assertThat(container.getArgs()).contains("--input-topics=test");
    });
}
Also used : ImageConfig(com.bakdata.quick.manager.k8s.ImageConfig) BeforeEach(org.junit.jupiter.api.BeforeEach) ResourcePrefix(com.bakdata.quick.manager.k8s.resource.QuickResources.ResourcePrefix) Container(io.fabric8.kubernetes.api.model.Container) InstanceOfAssertFactories(org.assertj.core.api.InstanceOfAssertFactories) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Completable(io.reactivex.Completable) KubernetesTest(com.bakdata.quick.manager.k8s.KubernetesTest) KubernetesResources(com.bakdata.quick.manager.k8s.KubernetesResources) BadArgumentException(com.bakdata.quick.common.exception.BadArgumentException) ApplicationResourceLoader(com.bakdata.quick.manager.application.resources.ApplicationResourceLoader) ApplicationCreationData(com.bakdata.quick.common.api.model.manager.creation.ApplicationCreationData) Test(org.junit.jupiter.api.Test) KafkaConfig(com.bakdata.quick.common.config.KafkaConfig) List(java.util.List) Map(java.util.Map) Nullable(edu.umd.cs.findbugs.annotations.Nullable) Assertions(org.junit.jupiter.api.Assertions) Optional(java.util.Optional) Service(io.fabric8.kubernetes.api.model.Service) Deployment(io.fabric8.kubernetes.api.model.apps.Deployment) ImageConfig(com.bakdata.quick.manager.k8s.ImageConfig) Completable(io.reactivex.Completable) Assertions(org.junit.jupiter.api.Assertions) KubernetesTest(com.bakdata.quick.manager.k8s.KubernetesTest) Test(org.junit.jupiter.api.Test)

Example 5 with ImageConfig

use of com.bakdata.quick.manager.k8s.ImageConfig in project quick by bakdata.

the class ApplicationResourceLoaderTest method shouldCreateDeploymentForAppDefaults.

@Test
void shouldCreateDeploymentForAppDefaults() {
    final ImageConfig imageConfig = ImageConfig.of(DOCKER_REGISTRY, "test", 3, "snapshot");
    final ApplicationResources applicationResources = this.createApplicationResource(null, null, null, Map.of());
    final Optional<HasMetadata> hasMetadata = findResource(applicationResources, ResourceKind.DEPLOYMENT);
    final Map<String, String> labels = Map.of("app.kubernetes.io/name", DEFAULT_DEPLOYMENT_NAME, "app.kubernetes.io/component", "streamsApp");
    assertThat(hasMetadata).isPresent().get(InstanceOfAssertFactories.type(Deployment.class)).satisfies(deployment -> {
        assertThat(deployment.getMetadata()).hasFieldOrPropertyWithValue("name", DEFAULT_DEPLOYMENT_NAME);
        final DeploymentSpec deploymentSpec = deployment.getSpec();
        assertThat(deploymentSpec).hasFieldOrPropertyWithValue("replicas", 1).extracting(spec -> spec.getTemplate().getMetadata().getLabels(), MAP).contains(Map.entry("app.kubernetes.io/name", DEFAULT_DEPLOYMENT_NAME));
        assertThat(deployment.getMetadata()).satisfies(metadata -> {
            assertThat(metadata.getName()).isEqualTo(DEFAULT_DEPLOYMENT_NAME);
            assertThat(metadata.getLabels()).containsExactlyInAnyOrderEntriesOf(labels);
        });
        final PodSpec podSpec = deploymentSpec.getTemplate().getSpec();
        assertThat(podSpec.getImagePullSecrets()).isNullOrEmpty();
        assertThat(podSpec.getContainers()).isNotNull().hasSize(1).first().hasFieldOrPropertyWithValue("image", imageConfig.asImageString()).hasFieldOrPropertyWithValue("imagePullPolicy", "Always").hasFieldOrPropertyWithValue("name", DEFAULT_DEPLOYMENT_NAME).satisfies(container -> {
            assertThat(container.getPorts()).isNullOrEmpty();
            assertThat(container.getEnv()).hasSize(1).first().extracting(EnvVar::getName).isEqualTo("JAVA_TOOL_OPTIONS");
            assertThat(container.getEnvFrom()).isNotNull().hasSize(1).first().extracting(EnvFromSource::getConfigMapRef).isNotNull().hasFieldOrPropertyWithValue("name", KubernetesResources.QUICK_CONFIG_NAME);
            assertThat(container.getArgs()).hasSize(2).contains("--brokers=bootstrap").contains("--schema-registry-url=http://dummy:123");
        });
    });
}
Also used : ImageConfig(com.bakdata.quick.manager.k8s.ImageConfig) BeforeEach(org.junit.jupiter.api.BeforeEach) EnvVar(io.fabric8.kubernetes.api.model.EnvVar) Container(io.fabric8.kubernetes.api.model.Container) IntOrString(io.fabric8.kubernetes.api.model.IntOrString) MAP(org.assertj.core.api.InstanceOfAssertFactories.MAP) InstanceOfAssertFactories(org.assertj.core.api.InstanceOfAssertFactories) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) EnvFromSource(io.fabric8.kubernetes.api.model.EnvFromSource) LocalObjectReference(io.fabric8.kubernetes.api.model.LocalObjectReference) KafkaConfig(com.bakdata.quick.common.config.KafkaConfig) PodSpec(io.fabric8.kubernetes.api.model.PodSpec) Map(java.util.Map) Service(io.fabric8.kubernetes.api.model.Service) DeploymentSpec(io.fabric8.kubernetes.api.model.apps.DeploymentSpec) ResourcePrefix(com.bakdata.quick.manager.k8s.resource.QuickResources.ResourcePrefix) KubernetesTest(com.bakdata.quick.manager.k8s.KubernetesTest) KubernetesResources(com.bakdata.quick.manager.k8s.KubernetesResources) HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) ApplicationCreationData(com.bakdata.quick.common.api.model.manager.creation.ApplicationCreationData) Test(org.junit.jupiter.api.Test) List(java.util.List) TestUtil(com.bakdata.quick.manager.TestUtil) Nullable(edu.umd.cs.findbugs.annotations.Nullable) Optional(java.util.Optional) Deployment(io.fabric8.kubernetes.api.model.apps.Deployment) ImageConfig(com.bakdata.quick.manager.k8s.ImageConfig) HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) DeploymentSpec(io.fabric8.kubernetes.api.model.apps.DeploymentSpec) PodSpec(io.fabric8.kubernetes.api.model.PodSpec) IntOrString(io.fabric8.kubernetes.api.model.IntOrString) EnvFromSource(io.fabric8.kubernetes.api.model.EnvFromSource) KubernetesTest(com.bakdata.quick.manager.k8s.KubernetesTest) Test(org.junit.jupiter.api.Test)

Aggregations

ImageConfig (com.bakdata.quick.manager.k8s.ImageConfig)7 KubernetesResources (com.bakdata.quick.manager.k8s.KubernetesResources)4 ResourcePrefix (com.bakdata.quick.manager.k8s.resource.QuickResources.ResourcePrefix)4 Deployment (io.fabric8.kubernetes.api.model.apps.Deployment)4 KubernetesTest (com.bakdata.quick.manager.k8s.KubernetesTest)3 Container (io.fabric8.kubernetes.api.model.Container)3 Service (io.fabric8.kubernetes.api.model.Service)3 Map (java.util.Map)3 Optional (java.util.Optional)3 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)3 InstanceOfAssertFactories (org.assertj.core.api.InstanceOfAssertFactories)3 BeforeEach (org.junit.jupiter.api.BeforeEach)3 Test (org.junit.jupiter.api.Test)3 ApplicationCreationData (com.bakdata.quick.common.api.model.manager.creation.ApplicationCreationData)2 MirrorCreationData (com.bakdata.quick.common.api.model.manager.creation.MirrorCreationData)2 KafkaConfig (com.bakdata.quick.common.config.KafkaConfig)2 TestUtil (com.bakdata.quick.manager.TestUtil)2 Nullable (edu.umd.cs.findbugs.annotations.Nullable)2 EnvFromSource (io.fabric8.kubernetes.api.model.EnvFromSource)2 EnvVar (io.fabric8.kubernetes.api.model.EnvVar)2