Search in sources :

Example 1 with ResourceConfig

use of io.fabric8.maven.core.config.ResourceConfig in project fabric8-maven-plugin by fabric8io.

the class DefaultControllerEnricher method addMissingResources.

@Override
public void addMissingResources(KubernetesListBuilder builder) {
    final String name = getConfig(Config.name, MavenUtil.createDefaultResourceName(getProject()));
    final ResourceConfig config = new ResourceConfig.Builder().controllerName(name).imagePullPolicy(getConfig(Config.pullPolicy)).withReplicas(Configs.asInt(getConfig(Config.replicaCount))).build();
    final List<ImageConfiguration> images = getImages();
    // Check if at least a replica set is added. If not add a default one
    if (!KubernetesResourceUtil.checkForKind(builder, POD_CONTROLLER_KINDS)) {
        // At least one image must be present, otherwise the resulting config will be invalid
        if (!Lists.isNullOrEmpty(images)) {
            String type = getConfig(Config.type);
            if ("deployment".equalsIgnoreCase(type)) {
                log.info("Adding a default Deployment");
                builder.addToDeploymentItems(deployHandler.getDeployment(config, images));
            } else if ("statefulSet".equalsIgnoreCase(type)) {
                log.info("Adding a default StatefulSet");
                builder.addToStatefulSetItems(statefulSetHandler.getStatefulSet(config, images));
            } else if ("daemonSet".equalsIgnoreCase(type)) {
                log.info("Adding a default DaemonSet");
                builder.addToDaemonSetItems(daemonSetHandler.getDaemonSet(config, images));
            } else if ("replicaSet".equalsIgnoreCase(type)) {
                log.info("Adding a default ReplicaSet");
                builder.addToReplicaSetItems(rsHandler.getReplicaSet(config, images));
            } else if ("replicationController".equalsIgnoreCase(type)) {
                log.info("Adding a default ReplicationController");
                builder.addToReplicationControllerItems(rcHandler.getReplicationController(config, images));
            } else if ("job".equalsIgnoreCase(type)) {
                log.info("Adding a default Job");
                builder.addToJobItems(jobHandler.getJob(config, images));
            }
        }
    } else if (KubernetesResourceUtil.checkForKind(builder, "StatefulSet")) {
        final StatefulSetSpec spec = statefulSetHandler.getStatefulSet(config, images).getSpec();
        if (spec != null) {
            builder.accept(new TypedVisitor<StatefulSetBuilder>() {

                @Override
                public void visit(StatefulSetBuilder statefulSetBuilder) {
                    statefulSetBuilder.editOrNewSpec().editOrNewTemplate().editOrNewSpec().endSpec().endTemplate().endSpec();
                    mergeStatefulSetSpec(statefulSetBuilder, spec);
                }
            });
            if (spec.getTemplate() != null && spec.getTemplate().getSpec() != null) {
                final PodSpec podSpec = spec.getTemplate().getSpec();
                builder.accept(new TypedVisitor<PodSpecBuilder>() {

                    @Override
                    public void visit(PodSpecBuilder builder) {
                        KubernetesResourceUtil.mergePodSpec(builder, podSpec, name);
                    }
                });
            }
        }
    } else {
        final DeploymentSpec spec = deployHandler.getDeployment(config, images).getSpec();
        if (spec != null) {
            builder.accept(new TypedVisitor<DeploymentBuilder>() {

                @Override
                public void visit(DeploymentBuilder deploymentBuilder) {
                    deploymentBuilder.editOrNewSpec().editOrNewTemplate().editOrNewSpec().endSpec().endTemplate().endSpec();
                    mergeDeploymentSpec(deploymentBuilder, spec);
                }
            });
            if (spec.getTemplate() != null && spec.getTemplate().getSpec() != null) {
                final PodSpec podSpec = spec.getTemplate().getSpec();
                builder.accept(new TypedVisitor<PodSpecBuilder>() {

                    @Override
                    public void visit(PodSpecBuilder builder) {
                        KubernetesResourceUtil.mergePodSpec(builder, podSpec, name);
                    }
                });
            }
        }
    }
}
Also used : TypedVisitor(io.fabric8.kubernetes.api.builder.TypedVisitor) PodSpecBuilder(io.fabric8.kubernetes.api.model.PodSpecBuilder) StatefulSetSpec(io.fabric8.kubernetes.api.model.extensions.StatefulSetSpec) DeploymentSpec(io.fabric8.kubernetes.api.model.extensions.DeploymentSpec) PodSpec(io.fabric8.kubernetes.api.model.PodSpec) ImageConfiguration(io.fabric8.maven.docker.config.ImageConfiguration) StatefulSetBuilder(io.fabric8.kubernetes.api.model.extensions.StatefulSetBuilder) PodSpecBuilder(io.fabric8.kubernetes.api.model.PodSpecBuilder) KubernetesListBuilder(io.fabric8.kubernetes.api.model.KubernetesListBuilder) DeploymentBuilder(io.fabric8.kubernetes.api.model.extensions.DeploymentBuilder) ResourceConfig(io.fabric8.maven.core.config.ResourceConfig) StatefulSetBuilder(io.fabric8.kubernetes.api.model.extensions.StatefulSetBuilder) DeploymentBuilder(io.fabric8.kubernetes.api.model.extensions.DeploymentBuilder)

Example 2 with ResourceConfig

use of io.fabric8.maven.core.config.ResourceConfig in project fabric8-maven-plugin by fabric8io.

the class ImageEnricher method mergeEnvVariables.

private void mergeEnvVariables(Container container) {
    List<EnvVar> env = container.getEnv();
    if (env == null) {
        env = new LinkedList<>();
        container.setEnv(env);
    }
    ResourceConfig resource = getContext().getResources();
    Map<String, String> userEnv = resource != null ? resource.getEnv() : null;
    if (userEnv != null) {
        for (Map.Entry<String, String> entry : userEnv.entrySet()) {
            EnvVar existingVariable = KubernetesResourceUtil.setEnvVarNoOverride(env, entry.getKey(), entry.getValue());
            if (existingVariable != null) {
                String actualValue = existingVariable.getValue();
                if (actualValue == null) {
                    actualValue = "retrieved using the downward API";
                }
                log.warn("Environment variable %s will not be overridden: trying to set the value %s, but its actual value will be %s", entry.getKey(), entry.getValue(), actualValue);
            }
        }
    }
}
Also used : EnvVar(io.fabric8.kubernetes.api.model.EnvVar) ResourceConfig(io.fabric8.maven.core.config.ResourceConfig) Map(java.util.Map)

Example 3 with ResourceConfig

use of io.fabric8.maven.core.config.ResourceConfig in project fabric8-maven-plugin by fabric8io.

the class ContainerHandlerTest method imagePullPolicyWithoutPolicySetTest.

@Test
public void imagePullPolicyWithoutPolicySetTest() {
    // project with version and ending in SNAPSHOT
    project1.setVersion("3.5-SNAPSHOT");
    // project with version but not ending in SNAPSHOT
    project2.setVersion("3.5-NEW");
    // creating container Handler for two
    ContainerHandler handler1 = new ContainerHandler(project1, envVarHandler, probeHandler);
    ContainerHandler handler2 = new ContainerHandler(project2, envVarHandler, probeHandler);
    // project without version
    ContainerHandler handler3 = new ContainerHandler(project, envVarHandler, probeHandler);
    images.clear();
    images.add(imageConfiguration1);
    // check if policy is not set then both in case of version is set or not
    ResourceConfig config2 = new ResourceConfig.Builder().imagePullPolicy("").build();
    containers = handler1.getContainers(config2, images);
    assertEquals("PullAlways", containers.get(0).getImagePullPolicy());
    containers = handler2.getContainers(config2, images);
    assertEquals("", containers.get(0).getImagePullPolicy());
    containers = handler3.getContainers(config2, images);
    assertEquals("", containers.get(0).getImagePullPolicy());
}
Also used : ResourceConfig(io.fabric8.maven.core.config.ResourceConfig) Test(org.junit.Test)

Example 4 with ResourceConfig

use of io.fabric8.maven.core.config.ResourceConfig in project fabric8-maven-plugin by fabric8io.

the class ContainerHandlerTest method getVolumeMountWithoutMountTest.

@Test
public void getVolumeMountWithoutMountTest() {
    ContainerHandler handler = new ContainerHandler(project, envVarHandler, probeHandler);
    images.clear();
    images.add(imageConfiguration1);
    // volume config without mount
    VolumeConfig volumeConfig1 = new VolumeConfig.Builder().name("first").build();
    volumes1.add(volumeConfig1);
    ResourceConfig config1 = new ResourceConfig.Builder().volumes(volumes1).build();
    containers = handler.getContainers(config1, images);
    assertTrue(containers.get(0).getVolumeMounts().isEmpty());
}
Also used : ResourceConfig(io.fabric8.maven.core.config.ResourceConfig) VolumeConfig(io.fabric8.maven.core.config.VolumeConfig) Test(org.junit.Test)

Example 5 with ResourceConfig

use of io.fabric8.maven.core.config.ResourceConfig in project fabric8-maven-plugin by fabric8io.

the class ContainerHandlerTest method getVolumeMountWithMultipleMountTest.

@Test
public void getVolumeMountWithMultipleMountTest() {
    ContainerHandler handler = new ContainerHandler(project, envVarHandler, probeHandler);
    images.clear();
    images.add(imageConfiguration1);
    List<String> mounts = new ArrayList<>();
    mounts.add("/path/etc");
    // volume config with name and multiple mount
    mounts.add("/path/system");
    mounts.add("/path/sys");
    VolumeConfig volumeConfig4 = new VolumeConfig.Builder().name("test").mounts(mounts).build();
    volumes1.clear();
    volumes1.add(volumeConfig4);
    ResourceConfig config4 = new ResourceConfig.Builder().volumes(volumes1).build();
    containers = handler.getContainers(config4, images);
    assertEquals(3, containers.get(0).getVolumeMounts().size());
    for (int i = 0; i <= 2; i++) assertEquals("test", containers.get(0).getVolumeMounts().get(i).getName());
}
Also used : ArrayList(java.util.ArrayList) ResourceConfig(io.fabric8.maven.core.config.ResourceConfig) VolumeConfig(io.fabric8.maven.core.config.VolumeConfig) Test(org.junit.Test)

Aggregations

ResourceConfig (io.fabric8.maven.core.config.ResourceConfig)31 Test (org.junit.Test)29 VolumeConfig (io.fabric8.maven.core.config.VolumeConfig)9 ArrayList (java.util.ArrayList)6 PodTemplateSpec (io.fabric8.kubernetes.api.model.PodTemplateSpec)5 ImageConfiguration (io.fabric8.maven.docker.config.ImageConfiguration)2 TypedVisitor (io.fabric8.kubernetes.api.builder.TypedVisitor)1 Container (io.fabric8.kubernetes.api.model.Container)1 ContainerBuilder (io.fabric8.kubernetes.api.model.ContainerBuilder)1 EnvVar (io.fabric8.kubernetes.api.model.EnvVar)1 Job (io.fabric8.kubernetes.api.model.Job)1 KubernetesListBuilder (io.fabric8.kubernetes.api.model.KubernetesListBuilder)1 PodSpec (io.fabric8.kubernetes.api.model.PodSpec)1 PodSpecBuilder (io.fabric8.kubernetes.api.model.PodSpecBuilder)1 Probe (io.fabric8.kubernetes.api.model.Probe)1 ReplicationController (io.fabric8.kubernetes.api.model.ReplicationController)1 VolumeMount (io.fabric8.kubernetes.api.model.VolumeMount)1 VolumeMountBuilder (io.fabric8.kubernetes.api.model.VolumeMountBuilder)1 DaemonSet (io.fabric8.kubernetes.api.model.extensions.DaemonSet)1 Deployment (io.fabric8.kubernetes.api.model.extensions.Deployment)1