Search in sources :

Example 36 with ImageConfiguration

use of org.eclipse.jkube.kit.config.image.ImageConfiguration in project jkube by eclipse.

the class WebAppGeneratorTest method customize_withFromTargetDirConfiguredAndNoCmd_shouldNotInitializeInvalidCmdArgument.

@Test
public void customize_withFromTargetDirConfiguredAndNoCmd_shouldNotInitializeInvalidCmdArgument() throws IOException {
    // Given
    final List<ImageConfiguration> originalImageConfigurations = new ArrayList<>();
    final File buildDirectory = temporaryFolder.newFolder("build");
    final File artifactFile = new File(buildDirectory, "artifact.war");
    assertTrue(artifactFile.createNewFile());
    final Properties projectProperties = new Properties();
    projectProperties.put("jkube.generator.webapp.targetDir", "/usr/local/tomcat/webapps");
    projectProperties.put("jkube.generator.webapp.from", "tomcat:jdk11-openjdk-slim");
    when(generatorContext.getProject().getBuildDirectory()).thenReturn(buildDirectory);
    when(generatorContext.getProject().getBuildFinalName()).thenReturn("artifact");
    when(generatorContext.getProject().getPackaging()).thenReturn("war");
    when(generatorContext.getProject().getVersion()).thenReturn("1.33.7-SNAPSHOT");
    when(generatorContext.getProject().getProperties()).thenReturn(projectProperties);
    // When
    final List<ImageConfiguration> result = new WebAppGenerator(generatorContext).customize(originalImageConfigurations, false);
    // Then
    assertThat(result).isSameAs(originalImageConfigurations).hasSize(1).first().hasFieldOrPropertyWithValue("name", "%g/%a:%l").extracting(ImageConfiguration::getBuildConfiguration).hasFieldOrPropertyWithValue("from", "tomcat:jdk11-openjdk-slim").hasFieldOrPropertyWithValue("env", Collections.singletonMap("DEPLOY_DIR", "/usr/local/tomcat/webapps")).hasFieldOrPropertyWithValue("cmd", null);
}
Also used : ImageConfiguration(org.eclipse.jkube.kit.config.image.ImageConfiguration) ArrayList(java.util.ArrayList) Properties(java.util.Properties) File(java.io.File) Test(org.junit.Test)

Example 37 with ImageConfiguration

use of org.eclipse.jkube.kit.config.image.ImageConfiguration in project jkube by eclipse.

the class OpenShiftResourceMojoTest method executeInternal_resolvesGroupInImageNameToNamespaceSetViaConfiguration_whenNoNamespaceDetected.

@Test
public void executeInternal_resolvesGroupInImageNameToNamespaceSetViaConfiguration_whenNoNamespaceDetected() throws MojoExecutionException, MojoFailureException {
    // Given
    ImageConfiguration imageConfiguration = ImageConfiguration.builder().name("%g/%a").build(BuildConfiguration.builder().from("test-base-image:latest").build()).build();
    when(mockedImageConfigResolver.resolve(eq(imageConfiguration), any())).thenReturn(Collections.singletonList(imageConfiguration));
    this.openShiftResourceMojo.images = Collections.singletonList(imageConfiguration);
    this.openShiftResourceMojo.namespace = "namespace-configured-via-plugin";
    // When
    openShiftResourceMojo.executeInternal();
    // Then
    assertEquals(1, openShiftResourceMojo.resolvedImages.size());
    assertEquals("namespace-configured-via-plugin/test-project", openShiftResourceMojo.resolvedImages.get(0).getName());
}
Also used : ImageConfiguration(org.eclipse.jkube.kit.config.image.ImageConfiguration) Test(org.junit.Test)

Example 38 with ImageConfiguration

use of org.eclipse.jkube.kit.config.image.ImageConfiguration in project jkube by eclipse.

the class OpenShiftResourceMojoTest method executeInternal_resolvesGroupInImageNameToClusterAccessNamespace_whenNamespaceDetected.

@Test
public void executeInternal_resolvesGroupInImageNameToClusterAccessNamespace_whenNamespaceDetected() throws MojoExecutionException, MojoFailureException {
    // Given
    ImageConfiguration imageConfiguration = ImageConfiguration.builder().name("%g/%a").build(BuildConfiguration.builder().from("test-base-image:latest").build()).build();
    when(mockedClusterAccess.getNamespace()).thenReturn("test-custom-namespace");
    when(mockedImageConfigResolver.resolve(eq(imageConfiguration), any())).thenReturn(Collections.singletonList(imageConfiguration));
    this.openShiftResourceMojo.images = Collections.singletonList(imageConfiguration);
    // When
    openShiftResourceMojo.executeInternal();
    // Then
    assertEquals(1, openShiftResourceMojo.resolvedImages.size());
    assertEquals("test-custom-namespace/test-project", openShiftResourceMojo.resolvedImages.get(0).getName());
}
Also used : ImageConfiguration(org.eclipse.jkube.kit.config.image.ImageConfiguration) Test(org.junit.Test)

Example 39 with ImageConfiguration

use of org.eclipse.jkube.kit.config.image.ImageConfiguration in project jkube by eclipse.

the class ConfigHelper method validateExternalPropertyActivation.

public static void validateExternalPropertyActivation(JavaProject project, List<ImageConfiguration> images) {
    String prop = getExternalConfigActivationProperty(project);
    if (prop == null) {
        return;
    }
    if (images.size() == 1) {
        return;
    }
    // With more than one image, externally activating propertyConfig get's tricky. We can only allow it to affect
    // one single image. Go through each image and check if they will be controlled by default properties.
    // If more than one image matches, fail.
    int imagesWithoutExternalConfig = 0;
    for (ImageConfiguration image : images) {
        if (PropertyConfigHandler.canCoexistWithOtherPropertyConfiguredImages(image.getExternalConfig())) {
            continue;
        }
        // else, it will be affected by the external property.
        imagesWithoutExternalConfig++;
    }
    if (imagesWithoutExternalConfig > 1) {
        throw new IllegalStateException("Configuration error: Cannot use property " + EXTERNALCONFIG_ACTIVATION_PROPERTY + " on projects with multiple images without explicit image external configuration.");
    }
}
Also used : ImageConfiguration(org.eclipse.jkube.kit.config.image.ImageConfiguration)

Example 40 with ImageConfiguration

use of org.eclipse.jkube.kit.config.image.ImageConfiguration in project jkube by eclipse.

the class ConfigHelper method initImageConfiguration.

public static List<ImageConfiguration> initImageConfiguration(String apiVersion, Date buildTimeStamp, List<ImageConfiguration> images, ImageConfigResolver imageConfigResolver, KitLogger log, String filter, ConfigHelper.Customizer customizer, JKubeConfiguration jKubeConfiguration) {
    List<ImageConfiguration> resolvedImages;
    ImageNameFormatter imageNameFormatter = new ImageNameFormatter(jKubeConfiguration.getProject(), buildTimeStamp);
    // Resolve images
    resolvedImages = ConfigHelper.resolveImages(log, // Unresolved images
    images, (ImageConfiguration image) -> imageConfigResolver.resolve(image, jKubeConfiguration.getProject()), // A filter which image to process
    filter, // customizer (can be overwritten by a subclass)
    customizer);
    // Check for simple Dockerfile mode
    if (isSimpleDockerFileMode(jKubeConfiguration.getBasedir())) {
        File topDockerfile = getTopLevelDockerfile(jKubeConfiguration.getBasedir());
        String defaultImageName = imageNameFormatter.format(getValueFromProperties(jKubeConfiguration.getProject().getProperties(), "jkube.image.name", "jkube.generator.name"));
        if (resolvedImages.isEmpty()) {
            resolvedImages.add(createSimpleDockerfileConfig(topDockerfile, defaultImageName));
        } else if (resolvedImages.size() == 1 && resolvedImages.get(0).getBuildConfiguration() == null) {
            resolvedImages.set(0, addSimpleDockerfileConfig(resolvedImages.get(0), topDockerfile));
        }
    }
    // Initialize configuration and detect minimal API version
    ConfigHelper.initAndValidate(resolvedImages, apiVersion, imageNameFormatter);
    for (ImageConfiguration image : resolvedImages) {
        BuildConfiguration buildConfiguration = image.getBuildConfiguration();
        if (buildConfiguration != null && buildConfiguration.isDockerFileMode()) {
            log.info("Using Dockerfile: %s", buildConfiguration.getDockerFile().getAbsolutePath());
            log.info("Using Docker Context Directory: %s", buildConfiguration.getAbsoluteContextDirPath(jKubeConfiguration.getSourceDirectory(), jKubeConfiguration.getBasedir().getAbsolutePath()));
        }
    }
    return resolvedImages;
}
Also used : BuildConfiguration(org.eclipse.jkube.kit.config.image.build.BuildConfiguration) ImageConfiguration(org.eclipse.jkube.kit.config.image.ImageConfiguration) File(java.io.File)

Aggregations

ImageConfiguration (org.eclipse.jkube.kit.config.image.ImageConfiguration)109 Test (org.junit.Test)75 BuildConfiguration (org.eclipse.jkube.kit.config.image.build.BuildConfiguration)29 RunImageConfiguration (org.eclipse.jkube.kit.config.image.RunImageConfiguration)28 File (java.io.File)26 ArrayList (java.util.ArrayList)19 Expectations (mockit.Expectations)15 GroupArtifactVersion (org.eclipse.jkube.kit.config.resource.GroupArtifactVersion)11 Before (org.junit.Before)11 IOException (java.io.IOException)9 Properties (java.util.Properties)8 KitLogger (org.eclipse.jkube.kit.common.KitLogger)8 KubernetesListBuilder (io.fabric8.kubernetes.api.model.KubernetesListBuilder)7 JKubeServiceException (org.eclipse.jkube.kit.config.service.JKubeServiceException)7 WatchImageConfiguration (org.eclipse.jkube.kit.config.image.WatchImageConfiguration)6 ResourceConfig (org.eclipse.jkube.kit.config.resource.ResourceConfig)6 List (java.util.List)5 JKubeConfiguration (org.eclipse.jkube.kit.common.JKubeConfiguration)5 VolumeConfig (org.eclipse.jkube.kit.config.resource.VolumeConfig)5 Container (io.fabric8.kubernetes.api.model.Container)4