io.fabric8.kubernetes.api.model.EnvFromSource Java Examples

The following examples show how to use io.fabric8.kubernetes.api.model.EnvFromSource. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: PodTemplateUtilsTest.java    From kubernetes-plugin with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldCombineAllEnvFromSourcesWithoutChangingOrder() {
    EnvFromSource configMap1 = new EnvFromSource(new ConfigMapEnvSource("config-map-1", false), null, null);
    EnvFromSource secret1 = new EnvFromSource(null, null, new SecretEnvSource("secret-1", false));
    EnvFromSource configMap2 = new EnvFromSource(new ConfigMapEnvSource("config-map-2", true), null, null);
    EnvFromSource secret2 = new EnvFromSource(null, null, new SecretEnvSource("secret-2", true));

    Container container1 = new Container();
    container1.setEnvFrom(asList(configMap1, secret1));

    Container container2 = new Container();
    container2.setEnvFrom(asList(configMap2, secret2));

    Container result = combine(container1, container2);

    // Config maps and secrets could potentially overwrite each other's variables. We should preserve their order.
    assertThat(result.getEnvFrom(), contains(configMap1, secret1, configMap2, secret2));
}
 
Example #2
Source File: DeploymentPropertiesResolver.java    From spring-cloud-deployer-kubernetes with Apache License 2.0 6 votes vote down vote up
List<EnvFromSource> getConfigMapRefs(Map<String, String> deploymentProperties) {
	List<EnvFromSource> configMapRefs = new ArrayList<>();
	KubernetesDeployerProperties deployerProperties = bindProperties(deploymentProperties,
			this.propertyPrefix + ".configMapRefs", "configMapRefs");

	deployerProperties.getConfigMapRefs().forEach(configMapRef ->
			configMapRefs.add(buildConfigMapRefEnvVar(configMapRef)));

	if (deployerProperties.getConfigMapRefs().isEmpty()) {
		properties.getConfigMapRefs().stream()
				.filter(configMapRef -> configMapRefs.stream()
						.noneMatch(existing -> existing.getConfigMapRef().getName().equals(configMapRef)))
				.collect(Collectors.toList())
				.forEach(configMapRef -> configMapRefs.add(buildConfigMapRefEnvVar(configMapRef)));
	}

	return configMapRefs;
}
 
Example #3
Source File: DeploymentPropertiesResolver.java    From spring-cloud-deployer-kubernetes with Apache License 2.0 6 votes vote down vote up
List<EnvFromSource> getSecretRefs(Map<String, String> deploymentProperties) {
	List<EnvFromSource> secretRefs = new ArrayList<>();
	KubernetesDeployerProperties deployerProperties = bindProperties(deploymentProperties,
			this.propertyPrefix + ".secretRefs", "secretRefs");

	deployerProperties.getSecretRefs().forEach(secretRef ->
			secretRefs.add(buildSecretRefEnvVar(secretRef)));

	if (deployerProperties.getSecretRefs().isEmpty()) {
		properties.getSecretRefs().stream()
				.filter(secretRef -> secretRefs.stream()
						.noneMatch(existing -> existing.getSecretRef().getName().equals(secretRef)))
				.collect(Collectors.toList())
				.forEach(secretRef -> secretRefs.add(buildSecretRefEnvVar(secretRef)));
	}

	return secretRefs;
}
 
Example #4
Source File: UniqueNamesProvisionerTest.java    From che with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void doesNotRewritePodConfigMapEnvFromWhenNoConfigMap() throws Exception {
  when(runtimeIdentity.getWorkspaceId()).thenReturn(WORKSPACE_ID);

  EnvFromSource envFrom =
      new EnvFromSourceBuilder()
          .withNewConfigMapRef()
          .withName(CONFIGMAP_NAME)
          .endConfigMapRef()
          .build();
  Container container = new ContainerBuilder().withEnvFrom(envFrom).build();
  Pod pod = newPod();
  pod.getSpec().setContainers(ImmutableList.of(container));
  PodData podData = new PodData(pod.getSpec(), pod.getMetadata());
  doReturn(ImmutableMap.of(POD_NAME, podData)).when(k8sEnv).getPodsData();

  uniqueNamesProvisioner.provision(k8sEnv, runtimeIdentity);

  EnvFromSource newEnvFromSource = container.getEnvFrom().iterator().next();
  assertEquals(newEnvFromSource.getConfigMapRef().getName(), CONFIGMAP_NAME);
}
 
Example #5
Source File: UniqueNamesProvisionerTest.java    From che with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void rewritePodConfigMapEnvFrom() throws Exception {
  when(runtimeIdentity.getWorkspaceId()).thenReturn(WORKSPACE_ID);

  ConfigMap configMap = newConfigMap();
  doReturn(ImmutableMap.of(CONFIGMAP_NAME, configMap)).when(k8sEnv).getConfigMaps();

  EnvFromSource envFrom =
      new EnvFromSourceBuilder()
          .withNewConfigMapRef()
          .withName(CONFIGMAP_NAME)
          .endConfigMapRef()
          .build();
  Container container = new ContainerBuilder().withEnvFrom(envFrom).build();
  Pod pod = newPod();
  pod.getSpec().setContainers(ImmutableList.of(container));
  PodData podData = new PodData(pod.getSpec(), pod.getMetadata());
  doReturn(ImmutableMap.of(POD_NAME, podData)).when(k8sEnv).getPodsData();

  uniqueNamesProvisioner.provision(k8sEnv, runtimeIdentity);

  String newConfigMapName = configMap.getMetadata().getName();
  EnvFromSource newEnvFromSource = container.getEnvFrom().iterator().next();
  assertEquals(newEnvFromSource.getConfigMapRef().getName(), newConfigMapName);
}
 
Example #6
Source File: PodTemplateUtilsTest.java    From kubernetes-plugin with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldFilterOutEnvFromSourcesWithNullOrEmptyKey() {
    EnvFromSource noSource = new EnvFromSource(null, null, null);
    EnvFromSource noConfigMapKey = new EnvFromSource(new ConfigMapEnvSource(null, false), null, null);
    EnvFromSource emptyConfigMapKey = new EnvFromSource(new ConfigMapEnvSource("", false), null, null);
    EnvFromSource noSecretKey = new EnvFromSource(null, null, new SecretEnvSource(null, false));
    EnvFromSource emptySecretKey = new EnvFromSource(null, null, new SecretEnvSource("", false));

    Container container = new Container();
    container.setEnvFrom(asList(noSource, noConfigMapKey, emptyConfigMapKey, noSecretKey, emptySecretKey));

    Container result = combine(container, new Container());

    assertEquals(0, result.getEnvFrom().size());
}
 
Example #7
Source File: KubernetesWithOldStyleEnvTest.java    From quarkus with Apache License 2.0 5 votes vote down vote up
@Test
public void assertGeneratedResources() throws IOException {
    Path kubernetesDir = prodModeTestResults.getBuildDir().resolve("kubernetes");
    assertThat(kubernetesDir)
            .isDirectoryContaining(p -> p.getFileName().endsWith("kubernetes.json"))
            .isDirectoryContaining(p -> p.getFileName().endsWith("kubernetes.yml"));
    List<HasMetadata> kubernetesList = DeserializationUtil
            .deserializeAsList(kubernetesDir.resolve("kubernetes.yml"));
    assertThat(kubernetesList.get(0)).isInstanceOfSatisfying(Deployment.class, d -> {
        assertThat(d.getMetadata()).satisfies(m -> {
            assertThat(m.getName()).isEqualTo(APPLICATION_NAME);
        });

        assertThat(d.getSpec()).satisfies(deploymentSpec -> {
            assertThat(deploymentSpec.getTemplate()).satisfies(t -> {
                assertThat(t.getSpec()).satisfies(podSpec -> {
                    assertThat(podSpec.getContainers()).hasOnlyOneElementSatisfying(container -> {
                        assertThat(container.getEnv())
                                .filteredOn(env -> "FROMFIELD".equals(env.getName()))
                                .hasOnlyOneElementSatisfying(
                                        env -> assertThat(env.getValueFrom().getFieldRef().getFieldPath())
                                                .isEqualTo("metadata.name"));
                        assertThat(container.getEnv())
                                .filteredOn(env -> "ENVVAR".equals(env.getName()))
                                .hasOnlyOneElementSatisfying(env -> assertThat(env.getValue()).isEqualTo("value"));
                        final List<EnvFromSource> envFrom = container.getEnvFrom();
                        assertThat(envFrom).hasSize(1);
                        assertThat(envFrom)
                                .filteredOn(e -> e.getSecretRef() != null)
                                .hasOnlyOneElementSatisfying(
                                        e -> assertThat(e.getSecretRef().getName()).isEqualTo("secretName"));
                    });
                });
            });
        });
    });
}
 
Example #8
Source File: DeploymentPropertiesResolver.java    From spring-cloud-deployer-kubernetes with Apache License 2.0 5 votes vote down vote up
private EnvFromSource buildConfigMapRefEnvVar(String configMapRefName) {
	ConfigMapEnvSource configMapEnvSource = new ConfigMapEnvSource();
	configMapEnvSource.setName(configMapRefName);

	EnvFromSource envFromSource = new EnvFromSource();
	envFromSource.setConfigMapRef(configMapEnvSource);

	return envFromSource;
}
 
Example #9
Source File: DeploymentPropertiesResolver.java    From spring-cloud-deployer-kubernetes with Apache License 2.0 5 votes vote down vote up
private EnvFromSource buildSecretRefEnvVar(String secretRefName) {
	SecretEnvSource secretEnvSource = new SecretEnvSource();
	secretEnvSource.setName(secretRefName);

	EnvFromSource envFromSource = new EnvFromSource();
	envFromSource.setSecretRef(secretEnvSource);

	return envFromSource;
}
 
Example #10
Source File: KubernetesAppDeployerIntegrationTests.java    From spring-cloud-deployer-kubernetes with Apache License 2.0 4 votes vote down vote up
@Test
public void testSecretRefFromPropertyMultiple() throws InterruptedException {
	log.info("Testing {}...", "SecretRefFromPropertyMultiple");

	Secret secret1 = randomSecret();
	Secret secret2 = randomSecret();

	List<String> secrets = new ArrayList<>();
	secrets.add(secret1.getMetadata().getName());
	secrets.add(secret2.getMetadata().getName());

	KubernetesDeployerProperties kubernetesDeployerProperties = new KubernetesDeployerProperties();
	kubernetesDeployerProperties.setSecretRefs(secrets);

	KubernetesAppDeployer kubernetesAppDeployer = new KubernetesAppDeployer(kubernetesDeployerProperties,
			kubernetesClient);

	AppDefinition definition = new AppDefinition(randomName(), null);
	Resource resource = testApplication();
	AppDeploymentRequest request = new AppDeploymentRequest(definition, resource, null);

	log.info("Deploying {}...", request.getDefinition().getName());
	String deploymentId = kubernetesAppDeployer.deploy(request);
	Timeout timeout = deploymentTimeout();
	assertThat(deploymentId, eventually(hasStatusThat(
			Matchers.hasProperty("state", is(deployed))), timeout.maxAttempts, timeout.pause));

	Container container = kubernetesClient.apps().deployments().withName(deploymentId).get().getSpec()
			.getTemplate().getSpec().getContainers().get(0);

	List<EnvFromSource> envFromSources = container.getEnvFrom();

	assertThat(envFromSources, is(notNullValue()));
	assertThat(envFromSources.size(), is(2));

	String podEnvironment = getPodEnvironment(deploymentId);

	EnvFromSource envFromSource1 = envFromSources.get(0);
	assertEquals(envFromSource1.getSecretRef().getName(), secret1.getMetadata().getName());

	EnvFromSource envFromSource2 = envFromSources.get(1);
	assertEquals(envFromSource2.getSecretRef().getName(), secret2.getMetadata().getName());

	Map<String, String> mergedSecretData = new HashMap<>();
	mergedSecretData.putAll(secret1.getData());
	mergedSecretData.putAll(secret2.getData());

	assertEquals(4, mergedSecretData.size());

	for(Map.Entry<String, String> secretData : secret1.getData().entrySet()) {
		String decodedValue = new String(Base64.getDecoder().decode(secretData.getValue()));
		assertTrue(podEnvironment.contains(secretData.getKey() + "=" + decodedValue));
	}

	log.info("Undeploying {}...", deploymentId);
	timeout = undeploymentTimeout();
	kubernetesAppDeployer.undeploy(deploymentId);
	assertThat(deploymentId, eventually(hasStatusThat(
			Matchers.hasProperty("state", is(unknown))), timeout.maxAttempts, timeout.pause));

	kubernetesClient.secrets().delete(secret1);
	kubernetesClient.secrets().delete(secret2);
}
 
Example #11
Source File: KubernetesAppDeployerIntegrationTests.java    From spring-cloud-deployer-kubernetes with Apache License 2.0 4 votes vote down vote up
@Test
public void testConfigMapRefFromDeploymentPropertyMultiple() throws InterruptedException {
	log.info("Testing {}...", "ConfigMapRefFromDeploymentPropertyMultiple");

	ConfigMap configMap1 = randomConfigMap();
	ConfigMap configMap2 = randomConfigMap();

	Map<String, String> props = new HashMap<>();
	props.put("spring.cloud.deployer.kubernetes.configMapRefs", "[" + configMap1.getMetadata().getName() + "," +
			configMap2.getMetadata().getName() + "]");

	KubernetesAppDeployer kubernetesAppDeployer = new KubernetesAppDeployer(new KubernetesDeployerProperties(),
			kubernetesClient);

	AppDefinition definition = new AppDefinition(randomName(), null);
	Resource resource = testApplication();
	AppDeploymentRequest request = new AppDeploymentRequest(definition, resource, props);

	log.info("Deploying {}...", request.getDefinition().getName());
	String deploymentId = kubernetesAppDeployer.deploy(request);
	Timeout timeout = deploymentTimeout();
	assertThat(deploymentId, eventually(hasStatusThat(
			Matchers.hasProperty("state", is(deployed))), timeout.maxAttempts, timeout.pause));

	Container container = kubernetesClient.apps().deployments().withName(deploymentId).get().getSpec()
			.getTemplate().getSpec().getContainers().get(0);

	List<EnvFromSource> envFromSources = container.getEnvFrom();

	assertThat(envFromSources, is(notNullValue()));
	assertThat(envFromSources.size(), is(2));

	String podEnvironment = getPodEnvironment(deploymentId);

	EnvFromSource envFromSource1 = envFromSources.get(0);
	assertEquals(envFromSource1.getConfigMapRef().getName(), configMap1.getMetadata().getName());

	EnvFromSource envFromSource2 = envFromSources.get(1);
	assertEquals(envFromSource2.getConfigMapRef().getName(), configMap2.getMetadata().getName());

	Map<String, String> mergedConfigMapData = new HashMap<>();
	mergedConfigMapData.putAll(configMap1.getData());
	mergedConfigMapData.putAll(configMap2.getData());

	assertEquals(4, mergedConfigMapData.size());

	for(Map.Entry<String, String> configMapData : configMap1.getData().entrySet()) {
		assertTrue(podEnvironment.contains(configMapData.getKey() + "=" + configMapData.getValue()));
	}

	log.info("Undeploying {}...", deploymentId);
	timeout = undeploymentTimeout();
	kubernetesAppDeployer.undeploy(deploymentId);
	assertThat(deploymentId, eventually(hasStatusThat(
			Matchers.hasProperty("state", is(unknown))), timeout.maxAttempts, timeout.pause));

	kubernetesClient.configMaps().delete(configMap1);
	kubernetesClient.configMaps().delete(configMap2);
}
 
Example #12
Source File: KubernetesAppDeployerIntegrationTests.java    From spring-cloud-deployer-kubernetes with Apache License 2.0 4 votes vote down vote up
@Test
public void testConfigMapRefFromPropertyMultiple() throws InterruptedException {
	log.info("Testing {}...", "ConfigMapRefFromPropertyMultiple");

	ConfigMap configMap1 = randomConfigMap();
	ConfigMap configMap2 = randomConfigMap();

	List<String> configMaps = new ArrayList<>();
	configMaps.add(configMap1.getMetadata().getName());
	configMaps.add(configMap2.getMetadata().getName());

	KubernetesDeployerProperties kubernetesDeployerProperties = new KubernetesDeployerProperties();
	kubernetesDeployerProperties.setConfigMapRefs(configMaps);

	KubernetesAppDeployer kubernetesAppDeployer = new KubernetesAppDeployer(kubernetesDeployerProperties,
			kubernetesClient);

	AppDefinition definition = new AppDefinition(randomName(), null);
	Resource resource = testApplication();
	AppDeploymentRequest request = new AppDeploymentRequest(definition, resource, null);

	log.info("Deploying {}...", request.getDefinition().getName());
	String deploymentId = kubernetesAppDeployer.deploy(request);
	Timeout timeout = deploymentTimeout();
	assertThat(deploymentId, eventually(hasStatusThat(
			Matchers.hasProperty("state", is(deployed))), timeout.maxAttempts, timeout.pause));

	Container container = kubernetesClient.apps().deployments().withName(deploymentId).get().getSpec()
			.getTemplate().getSpec().getContainers().get(0);

	List<EnvFromSource> envFromSources = container.getEnvFrom();

	assertThat(envFromSources, is(notNullValue()));
	assertThat(envFromSources.size(), is(2));

	String podEnvironment = getPodEnvironment(deploymentId);

	EnvFromSource envFromSource1 = envFromSources.get(0);
	assertEquals(envFromSource1.getConfigMapRef().getName(), configMap1.getMetadata().getName());

	EnvFromSource envFromSource2 = envFromSources.get(1);
	assertEquals(envFromSource2.getConfigMapRef().getName(), configMap2.getMetadata().getName());

	Map<String, String> mergedConfigMapData = new HashMap<>();
	mergedConfigMapData.putAll(configMap1.getData());
	mergedConfigMapData.putAll(configMap2.getData());

	assertEquals(4, mergedConfigMapData.size());

	for(Map.Entry<String, String> configMapData : configMap1.getData().entrySet()) {
		assertTrue(podEnvironment.contains(configMapData.getKey() + "=" + configMapData.getValue()));
	}

	log.info("Undeploying {}...", deploymentId);
	timeout = undeploymentTimeout();
	kubernetesAppDeployer.undeploy(deploymentId);
	assertThat(deploymentId, eventually(hasStatusThat(
			Matchers.hasProperty("state", is(unknown))), timeout.maxAttempts, timeout.pause));

	kubernetesClient.configMaps().delete(configMap1);
	kubernetesClient.configMaps().delete(configMap2);
}
 
Example #13
Source File: KubernetesAppDeployerIntegrationTests.java    From spring-cloud-deployer-kubernetes with Apache License 2.0 4 votes vote down vote up
@Test
public void testConfigMapRefFromDeployerPropertyOverride() throws IOException, InterruptedException {
	log.info("Testing {}...", "ConfigMapRefFromDeployerPropertyOverride");

	ConfigMap propertyConfigMap = randomConfigMap();
	ConfigMap deployerPropertyConfigMap = randomConfigMap();

	KubernetesDeployerProperties kubernetesDeployerProperties = new KubernetesDeployerProperties();
	kubernetesDeployerProperties.setConfigMapRefs(Collections.singletonList(propertyConfigMap.getMetadata().getName()));

	KubernetesAppDeployer kubernetesAppDeployer = new KubernetesAppDeployer(kubernetesDeployerProperties,
			kubernetesClient);

	Map<String, String> props = new HashMap<>();
	props.put("spring.cloud.deployer.kubernetes.configMapRefs", deployerPropertyConfigMap.getMetadata().getName());

	AppDefinition definition = new AppDefinition(randomName(), null);
	Resource resource = testApplication();
	AppDeploymentRequest request = new AppDeploymentRequest(definition, resource, props);

	log.info("Deploying {}...", request.getDefinition().getName());
	String deploymentId = kubernetesAppDeployer.deploy(request);
	Timeout timeout = deploymentTimeout();
	assertThat(deploymentId, eventually(hasStatusThat(
			Matchers.hasProperty("state", is(deployed))), timeout.maxAttempts, timeout.pause));

	Container container = kubernetesClient.apps().deployments().withName(deploymentId).get().getSpec()
			.getTemplate().getSpec().getContainers().get(0);

	List<EnvFromSource> envFromSources = container.getEnvFrom();

	assertThat(envFromSources, is(notNullValue()));
	assertThat(envFromSources.size(), is(1));

	EnvFromSource envFromSource = envFromSources.get(0);
	assertEquals(envFromSource.getConfigMapRef().getName(), deployerPropertyConfigMap.getMetadata().getName());

	String podEnvironment = getPodEnvironment(deploymentId);

	for(Map.Entry<String, String> deployerPropertyConfigMapData : deployerPropertyConfigMap.getData().entrySet()) {
		assertTrue(podEnvironment.contains(deployerPropertyConfigMapData.getKey() + "="
				+ deployerPropertyConfigMapData.getValue()));
	}

	for(Map.Entry<String, String> propertyConfigMapData : propertyConfigMap.getData().entrySet()) {
		assertFalse(podEnvironment.contains(propertyConfigMapData.getKey() + "=" + propertyConfigMapData.getValue()));
	}

	log.info("Undeploying {}...", deploymentId);
	timeout = undeploymentTimeout();
	kubernetesAppDeployer.undeploy(deploymentId);
	assertThat(deploymentId, eventually(hasStatusThat(
			Matchers.hasProperty("state", is(unknown))), timeout.maxAttempts, timeout.pause));

	kubernetesClient.configMaps().delete(propertyConfigMap);
	kubernetesClient.configMaps().delete(deployerPropertyConfigMap);
}
 
Example #14
Source File: KubernetesAppDeployerIntegrationTests.java    From spring-cloud-deployer-kubernetes with Apache License 2.0 4 votes vote down vote up
@Test
public void testConfigMapRefFromDeployerProperty() throws InterruptedException {
	log.info("Testing {}...", "ConfigMapRefFromDeployerProperty");

	ConfigMap configMap = randomConfigMap();

	KubernetesAppDeployer kubernetesAppDeployer = new KubernetesAppDeployer(new KubernetesDeployerProperties(),
			kubernetesClient);

	Map<String, String> props = new HashMap<>();
	props.put("spring.cloud.deployer.kubernetes.configMapRefs", configMap.getMetadata().getName());

	AppDefinition definition = new AppDefinition(randomName(), null);
	Resource resource = testApplication();
	AppDeploymentRequest request = new AppDeploymentRequest(definition, resource, props);

	log.info("Deploying {}...", request.getDefinition().getName());
	String deploymentId = kubernetesAppDeployer.deploy(request);
	Timeout timeout = deploymentTimeout();
	assertThat(deploymentId, eventually(hasStatusThat(
			Matchers.hasProperty("state", is(deployed))), timeout.maxAttempts, timeout.pause));

	Container container = kubernetesClient.apps().deployments().withName(deploymentId).get().getSpec()
			.getTemplate().getSpec().getContainers().get(0);

	List<EnvFromSource> envFromSources = container.getEnvFrom();

	assertThat(envFromSources, is(notNullValue()));
	assertThat(envFromSources.size(), is(1));

	EnvFromSource envFromSource = envFromSources.get(0);
	assertEquals(envFromSource.getConfigMapRef().getName(), configMap.getMetadata().getName());

	String podEnvironment = getPodEnvironment(deploymentId);

	assertEquals(configMap.getData().size(), 2);

	for(Map.Entry<String, String> configMapData : configMap.getData().entrySet()) {
		assertTrue(podEnvironment.contains(configMapData.getKey() + "=" + configMapData.getValue()));
	}

	log.info("Undeploying {}...", deploymentId);
	timeout = undeploymentTimeout();
	kubernetesAppDeployer.undeploy(deploymentId);
	assertThat(deploymentId, eventually(hasStatusThat(
			Matchers.hasProperty("state", is(unknown))), timeout.maxAttempts, timeout.pause));

	kubernetesClient.configMaps().delete(configMap);
}
 
Example #15
Source File: KubernetesAppDeployerIntegrationTests.java    From spring-cloud-deployer-kubernetes with Apache License 2.0 4 votes vote down vote up
@Test
public void testConfigMapRef() throws InterruptedException {
	log.info("Testing {}...", "ConfigMapRef");

	ConfigMap configMap = randomConfigMap();

	KubernetesDeployerProperties kubernetesDeployerProperties = new KubernetesDeployerProperties();
	kubernetesDeployerProperties.setConfigMapRefs(Collections.singletonList(configMap.getMetadata().getName()));

	KubernetesAppDeployer kubernetesAppDeployer = new KubernetesAppDeployer(kubernetesDeployerProperties, kubernetesClient);

	AppDefinition definition = new AppDefinition(randomName(), null);
	Resource resource = testApplication();
	AppDeploymentRequest request = new AppDeploymentRequest(definition, resource);

	log.info("Deploying {}...", request.getDefinition().getName());
	String deploymentId = kubernetesAppDeployer.deploy(request);
	Timeout timeout = deploymentTimeout();
	assertThat(deploymentId, eventually(hasStatusThat(
			Matchers.hasProperty("state", is(deployed))), timeout.maxAttempts, timeout.pause));

	Container container = kubernetesClient.apps().deployments().withName(deploymentId).get().getSpec()
			.getTemplate().getSpec().getContainers().get(0);

	List<EnvFromSource> envFromSources = container.getEnvFrom();

	assertThat(envFromSources, is(notNullValue()));
	assertThat(envFromSources.size(), is(1));

	EnvFromSource envFromSource = envFromSources.get(0);
	assertEquals(envFromSource.getConfigMapRef().getName(), configMap.getMetadata().getName());

	String podEnvironment = getPodEnvironment(deploymentId);

	assertEquals(configMap.getData().size(), 2);

	for(Map.Entry<String, String> configMapData : configMap.getData().entrySet()) {
		assertTrue(podEnvironment.contains(configMapData.getKey() + "=" + configMapData.getValue()));
	}

	log.info("Undeploying {}...", deploymentId);
	timeout = undeploymentTimeout();
	kubernetesAppDeployer.undeploy(deploymentId);
	assertThat(deploymentId, eventually(hasStatusThat(
			Matchers.hasProperty("state", is(unknown))), timeout.maxAttempts, timeout.pause));

	kubernetesClient.configMaps().delete(configMap);
}
 
Example #16
Source File: KubernetesAppDeployerIntegrationTests.java    From spring-cloud-deployer-kubernetes with Apache License 2.0 4 votes vote down vote up
@Test
public void testSecretRefFromDeploymentPropertyMultiple() throws InterruptedException {
	log.info("Testing {}...", "SecretRefFromDeploymentPropertyMultiple");

	Secret secret1 = randomSecret();
	Secret secret2 = randomSecret();

	Map<String, String> props = new HashMap<>();
	props.put("spring.cloud.deployer.kubernetes.secretRefs", "[" + secret1.getMetadata().getName() + "," +
			secret2.getMetadata().getName() + "]");

	KubernetesAppDeployer kubernetesAppDeployer = new KubernetesAppDeployer(new KubernetesDeployerProperties(),
			kubernetesClient);

	AppDefinition definition = new AppDefinition(randomName(), null);
	Resource resource = testApplication();
	AppDeploymentRequest request = new AppDeploymentRequest(definition, resource, props);

	log.info("Deploying {}...", request.getDefinition().getName());
	String deploymentId = kubernetesAppDeployer.deploy(request);
	Timeout timeout = deploymentTimeout();
	assertThat(deploymentId, eventually(hasStatusThat(
			Matchers.hasProperty("state", is(deployed))), timeout.maxAttempts, timeout.pause));

	Container container = kubernetesClient.apps().deployments().withName(deploymentId).get().getSpec()
			.getTemplate().getSpec().getContainers().get(0);

	List<EnvFromSource> envFromSources = container.getEnvFrom();

	assertThat(envFromSources, is(notNullValue()));
	assertThat(envFromSources.size(), is(2));

	String podEnvironment = getPodEnvironment(deploymentId);

	EnvFromSource envFromSource1 = envFromSources.get(0);
	assertEquals(envFromSource1.getSecretRef().getName(), secret1.getMetadata().getName());

	EnvFromSource envFromSource2 = envFromSources.get(1);
	assertEquals(envFromSource2.getSecretRef().getName(), secret2.getMetadata().getName());

	Map<String, String> mergedSecretData = new HashMap<>();
	mergedSecretData.putAll(secret1.getData());
	mergedSecretData.putAll(secret2.getData());

	assertEquals(4, mergedSecretData.size());

	for(Map.Entry<String, String> secretData : secret1.getData().entrySet()) {
		String decodedValue = new String(Base64.getDecoder().decode(secretData.getValue()));
		assertTrue(podEnvironment.contains(secretData.getKey() + "=" + decodedValue));
	}

	log.info("Undeploying {}...", deploymentId);
	timeout = undeploymentTimeout();
	kubernetesAppDeployer.undeploy(deploymentId);
	assertThat(deploymentId, eventually(hasStatusThat(
			Matchers.hasProperty("state", is(unknown))), timeout.maxAttempts, timeout.pause));

	kubernetesClient.secrets().delete(secret1);
	kubernetesClient.secrets().delete(secret2);
}
 
Example #17
Source File: KubernetesWithWarningsEnvTest.java    From quarkus with Apache License 2.0 4 votes vote down vote up
@Test
public void ensureProperQuarkusPropertiesLogged() throws IOException {
    Path kubernetesDir = prodModeTestResults.getBuildDir().resolve("kubernetes");
    assertThat(kubernetesDir)
            .isDirectoryContaining(p -> p.getFileName().endsWith("kubernetes.json"))
            .isDirectoryContaining(p -> p.getFileName().endsWith("kubernetes.yml"));
    List<HasMetadata> kubernetesList = DeserializationUtil
            .deserializeAsList(kubernetesDir.resolve("kubernetes.yml"));
    assertThat(kubernetesList.get(0)).isInstanceOfSatisfying(Deployment.class, d -> {
        assertThat(d.getMetadata()).satisfies(m -> {
            assertThat(m.getName()).isEqualTo(APPLICATION_NAME);
        });

        assertThat(d.getSpec()).satisfies(deploymentSpec -> {
            assertThat(deploymentSpec.getTemplate()).satisfies(t -> {
                assertThat(t.getSpec()).satisfies(podSpec -> {
                    assertThat(podSpec.getContainers()).hasOnlyOneElementSatisfying(container -> {
                        assertThat(container.getEnv())
                                .filteredOn(env -> "MY_FIELD".equals(env.getName()))
                                .hasOnlyOneElementSatisfying(
                                        env -> assertThat(env.getValueFrom().getFieldRef().getFieldPath())
                                                .isEqualTo("newField"));
                        assertThat(container.getEnv())
                                .filteredOn(env -> "MY_VAR".equals(env.getName()))
                                .hasOnlyOneElementSatisfying(env -> assertThat(env.getValue()).isEqualTo("newVariable"));
                        final List<EnvFromSource> envFrom = container.getEnvFrom();
                        assertThat(envFrom).hasSize(2);
                        assertThat(envFrom)
                                .filteredOn(e -> e.getSecretRef() != null)
                                .hasOnlyOneElementSatisfying(
                                        e -> assertThat(e.getSecretRef().getName()).isEqualTo("secret"));
                        assertThat(envFrom)
                                .filteredOn(e -> e.getConfigMapRef() != null)
                                .hasOnlyOneElementSatisfying(
                                        e -> assertThat(e.getConfigMapRef().getName()).isEqualTo("configMap"));
                    });
                });
            });
        });
    });

    List<LogRecord> buildLogRecords = prodModeTestResults.getRetainedBuildLogRecords();
    buildLogRecords.forEach(l -> System.out.println("l = " + l.getMessage()));
    assertThat(buildLogRecords).hasSize(4);
    assertThat(buildLogRecords)
            .filteredOn(r -> r.getMessage().contains("my-field"))
            .hasOnlyOneElementSatisfying(r -> assertThat(r.getMessage()).contains("newField"));
    assertThat(buildLogRecords)
            .filteredOn(r -> r.getMessage().contains("my-var"))
            .hasOnlyOneElementSatisfying(r -> assertThat(r.getMessage()).contains("newVariable"));
    assertThat(buildLogRecords)
            .filteredOn(r -> r.getMessage().contains("configMap"))
            .hasOnlyOneElementSatisfying(
                    r -> assertThat(r.getMessage().contains("'xxx'") && r.getMessage().contains("'secret'")));
    assertThat(buildLogRecords)
            .filteredOn(r -> r.getMessage().contains("secret"))
            .hasSize(2);
}
 
Example #18
Source File: KubernetesAppDeployerIntegrationTests.java    From spring-cloud-deployer-kubernetes with Apache License 2.0 4 votes vote down vote up
@Test
public void testSecretRefFromDeployerProperty() throws InterruptedException {
	log.info("Testing {}...", "SecretRefFromDeployerProperty");

	Secret secret = randomSecret();

	KubernetesAppDeployer kubernetesAppDeployer = new KubernetesAppDeployer(new KubernetesDeployerProperties(),
			kubernetesClient);

	Map<String, String> props = new HashMap<>();
	props.put("spring.cloud.deployer.kubernetes.secretRefs", secret.getMetadata().getName());

	AppDefinition definition = new AppDefinition(randomName(), null);
	Resource resource = testApplication();
	AppDeploymentRequest request = new AppDeploymentRequest(definition, resource, props);

	log.info("Deploying {}...", request.getDefinition().getName());
	String deploymentId = kubernetesAppDeployer.deploy(request);
	Timeout timeout = deploymentTimeout();
	assertThat(deploymentId, eventually(hasStatusThat(
			Matchers.hasProperty("state", is(deployed))), timeout.maxAttempts, timeout.pause));

	Container container = kubernetesClient.apps().deployments().withName(deploymentId).get().getSpec()
			.getTemplate().getSpec().getContainers().get(0);

	List<EnvFromSource> envFromSources = container.getEnvFrom();

	assertThat(envFromSources, is(notNullValue()));
	assertThat(envFromSources.size(), is(1));

	EnvFromSource envFromSource = envFromSources.get(0);
	assertEquals(envFromSource.getSecretRef().getName(), secret.getMetadata().getName());

	String podEnvironment = getPodEnvironment(deploymentId);

	assertEquals(secret.getData().size(), 2);

	for(Map.Entry<String, String> secretData : secret.getData().entrySet()) {
		String decodedValue = new String(Base64.getDecoder().decode(secretData.getValue()));
		assertTrue(podEnvironment.contains(secretData.getKey() + "=" + decodedValue));
	}

	log.info("Undeploying {}...", deploymentId);
	timeout = undeploymentTimeout();
	kubernetesAppDeployer.undeploy(deploymentId);
	assertThat(deploymentId, eventually(hasStatusThat(
			Matchers.hasProperty("state", is(unknown))), timeout.maxAttempts, timeout.pause));

	kubernetesClient.secrets().delete(secret);
}
 
Example #19
Source File: KubernetesAppDeployerIntegrationTests.java    From spring-cloud-deployer-kubernetes with Apache License 2.0 4 votes vote down vote up
@Test
public void testSecretRef() throws InterruptedException {
	log.info("Testing {}...", "SecretRef");

	Secret secret = randomSecret();

	KubernetesDeployerProperties kubernetesDeployerProperties = new KubernetesDeployerProperties();
	kubernetesDeployerProperties.setSecretRefs(Collections.singletonList(secret.getMetadata().getName()));

	KubernetesAppDeployer kubernetesAppDeployer = new KubernetesAppDeployer(kubernetesDeployerProperties, kubernetesClient);

	AppDefinition definition = new AppDefinition(randomName(), null);
	Resource resource = testApplication();
	AppDeploymentRequest request = new AppDeploymentRequest(definition, resource);

	log.info("Deploying {}...", request.getDefinition().getName());
	String deploymentId = kubernetesAppDeployer.deploy(request);
	Timeout timeout = deploymentTimeout();
	assertThat(deploymentId, eventually(hasStatusThat(
			Matchers.hasProperty("state", is(deployed))), timeout.maxAttempts, timeout.pause));

	Container container = kubernetesClient.apps().deployments().withName(deploymentId).get().getSpec()
			.getTemplate().getSpec().getContainers().get(0);

	List<EnvFromSource> envFromSources = container.getEnvFrom();

	assertThat(envFromSources, is(notNullValue()));
	assertThat(envFromSources.size(), is(1));

	EnvFromSource envFromSource = envFromSources.get(0);
	assertEquals(envFromSource.getSecretRef().getName(), secret.getMetadata().getName());

	String podEnvironment = getPodEnvironment(deploymentId);

	assertEquals(secret.getData().size(), 2);

	for(Map.Entry<String, String> secretData : secret.getData().entrySet()) {
		String decodedValue = new String(Base64.getDecoder().decode(secretData.getValue()));
		assertTrue(podEnvironment.contains(secretData.getKey() + "=" + decodedValue));
	}

	log.info("Undeploying {}...", deploymentId);
	timeout = undeploymentTimeout();
	kubernetesAppDeployer.undeploy(deploymentId);
	assertThat(deploymentId, eventually(hasStatusThat(
			Matchers.hasProperty("state", is(unknown))), timeout.maxAttempts, timeout.pause));

	kubernetesClient.secrets().delete(secret);
}
 
Example #20
Source File: KubernetesWithNewStyleEnvTest.java    From quarkus with Apache License 2.0 4 votes vote down vote up
@Test
public void assertGeneratedResources() throws IOException {
    Path kubernetesDir = prodModeTestResults.getBuildDir().resolve("kubernetes");
    assertThat(kubernetesDir)
            .isDirectoryContaining(p -> p.getFileName().endsWith("kubernetes.json"))
            .isDirectoryContaining(p -> p.getFileName().endsWith("kubernetes.yml"));
    List<HasMetadata> kubernetesList = DeserializationUtil
            .deserializeAsList(kubernetesDir.resolve("kubernetes.yml"));
    assertThat(kubernetesList.get(0)).isInstanceOfSatisfying(Deployment.class, d -> {
        assertThat(d.getMetadata()).satisfies(m -> {
            assertThat(m.getName()).isEqualTo(APPLICATION_NAME);
        });

        assertThat(d.getSpec()).satisfies(deploymentSpec -> {
            assertThat(deploymentSpec.getTemplate()).satisfies(t -> {
                assertThat(t.getSpec()).satisfies(podSpec -> {
                    assertThat(podSpec.getContainers()).hasOnlyOneElementSatisfying(container -> {
                        assertThat(container.getEnv())
                                .filteredOn(env -> "FROMFIELD".equals(env.getName()))
                                .hasOnlyOneElementSatisfying(
                                        env -> assertThat(env.getValueFrom().getFieldRef().getFieldPath())
                                                .isEqualTo("metadata.name"));
                        assertThat(container.getEnv())
                                .filteredOn(env -> "ENVVAR".equals(env.getName()))
                                .hasOnlyOneElementSatisfying(env -> assertThat(env.getValue()).isEqualTo("value"));
                        final List<EnvFromSource> envFrom = container.getEnvFrom();
                        assertThat(envFrom).hasSize(2);
                        assertThat(envFrom)
                                .filteredOn(e -> e.getSecretRef() != null)
                                .hasOnlyOneElementSatisfying(
                                        e -> assertThat(e.getSecretRef().getName()).isEqualTo("secretName"));
                        assertThat(envFrom)
                                .filteredOn(e -> e.getConfigMapRef() != null)
                                .hasOnlyOneElementSatisfying(
                                        e -> assertThat(e.getConfigMapRef().getName()).isEqualTo("configName"));
                    });
                });
            });
        });
    });
}
 
Example #21
Source File: KubernetesWithMixedStyleEnvTest.java    From quarkus with Apache License 2.0 4 votes vote down vote up
@Test
public void assertGeneratedResources() throws IOException {
    Path kubernetesDir = prodModeTestResults.getBuildDir().resolve("kubernetes");
    assertThat(kubernetesDir)
            .isDirectoryContaining(p -> p.getFileName().endsWith("kubernetes.json"))
            .isDirectoryContaining(p -> p.getFileName().endsWith("kubernetes.yml"));
    List<HasMetadata> kubernetesList = DeserializationUtil
            .deserializeAsList(kubernetesDir.resolve("kubernetes.yml"));
    assertThat(kubernetesList.get(0)).isInstanceOfSatisfying(Deployment.class, d -> {
        assertThat(d.getMetadata()).satisfies(m -> {
            assertThat(m.getName()).isEqualTo(APPLICATION_NAME);
        });

        assertThat(d.getSpec()).satisfies(deploymentSpec -> {
            assertThat(deploymentSpec.getTemplate()).satisfies(t -> {
                assertThat(t.getSpec()).satisfies(podSpec -> {
                    assertThat(podSpec.getContainers()).hasOnlyOneElementSatisfying(container -> {
                        assertThat(container.getEnv())
                                .filteredOn(env -> "FROMFIELD".equals(env.getName()))
                                .hasOnlyOneElementSatisfying(
                                        env -> assertThat(env.getValueFrom().getFieldRef().getFieldPath())
                                                .isEqualTo("metadata.name"));
                        assertThat(container.getEnv())
                                .filteredOn(env -> "ENVVAR".equals(env.getName()))
                                .hasOnlyOneElementSatisfying(env -> assertThat(env.getValue()).isEqualTo("value"));
                        final List<EnvFromSource> envFrom = container.getEnvFrom();
                        assertThat(envFrom).hasSize(2);
                        assertThat(envFrom)
                                .filteredOn(e -> e.getSecretRef() != null)
                                .hasOnlyOneElementSatisfying(
                                        e -> assertThat(e.getSecretRef().getName()).isEqualTo("secretName"));
                        assertThat(envFrom)
                                .filteredOn(e -> e.getConfigMapRef() != null)
                                .hasOnlyOneElementSatisfying(
                                        e -> assertThat(e.getConfigMapRef().getName()).isEqualTo("configName"));
                    });
                });
            });
        });
    });
}