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

The following examples show how to use io.fabric8.kubernetes.api.model.PodSpec. 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: KubernetesAppDeployerTests.java    From spring-cloud-deployer-kubernetes with Apache License 2.0 6 votes vote down vote up
@Test
public void deployWithDeploymentServiceAccountNameDeploymentPropertyOverride() {
	AppDefinition definition = new AppDefinition("app-test", null);

	Map<String, String> props = new HashMap<>();
	props.put("spring.cloud.deployer.kubernetes.deploymentServiceAccountName", "overridesan");

	AppDeploymentRequest appDeploymentRequest = new AppDeploymentRequest(definition, getResource(), props);

	KubernetesDeployerProperties kubernetesDeployerProperties = new KubernetesDeployerProperties();
	kubernetesDeployerProperties.setDeploymentServiceAccountName("defaultsan");

	deployer = new KubernetesAppDeployer(kubernetesDeployerProperties, null);
	PodSpec podSpec = deployer.createPodSpec(appDeploymentRequest);

	assertNotNull(podSpec.getServiceAccountName());
	assertThat(podSpec.getServiceAccountName().equals("overridesan"));
}
 
Example #2
Source File: KubernetesAppDeployerTests.java    From spring-cloud-deployer-kubernetes with Apache License 2.0 6 votes vote down vote up
@Test
public void testPodSecurityContextGlobalProperty() {
	AppDefinition definition = new AppDefinition("app-test", null);
	AppDeploymentRequest appDeploymentRequest = new AppDeploymentRequest(definition, getResource(), null);

	KubernetesDeployerProperties kubernetesDeployerProperties = new KubernetesDeployerProperties();

	KubernetesDeployerProperties.PodSecurityContext securityContext = new KubernetesDeployerProperties.PodSecurityContext();
	securityContext.setFsGroup(65534L);
	securityContext.setRunAsUser(65534L);

	kubernetesDeployerProperties.setPodSecurityContext(securityContext);

	deployer = new KubernetesAppDeployer(kubernetesDeployerProperties, null);
	PodSpec podSpec = deployer.createPodSpec(appDeploymentRequest);

	PodSecurityContext podSecurityContext = podSpec.getSecurityContext();

	assertNotNull("Pod security context should not be null", podSecurityContext);

	assertEquals("Unexpected run as user", Long.valueOf("65534"), podSecurityContext.getRunAsUser());
	assertEquals("Unexpected fs group", Long.valueOf("65534"), podSecurityContext.getFsGroup());
}
 
Example #3
Source File: DebugMojo.java    From jkube with Eclipse Public License 2.0 6 votes vote down vote up
private boolean podHasEnvVarValue(Pod pod, String envVarName, String envVarValue) {
    PodSpec spec = pod.getSpec();
    if (spec != null) {
        List<Container> containers = spec.getContainers();
        if (containers != null && !containers.isEmpty()) {
            Container container = containers.get(0);
            List<EnvVar> env = container.getEnv();
            if (env != null) {
                for (EnvVar envVar : env) {
                    if (Objects.equal(envVar.getName(), envVarName) && Objects.equal(envVar.getValue(), envVarValue)) {
                        return true;
                    }
                }
            }
        }
    }
    return false;
}
 
Example #4
Source File: PodMergerTest.java    From che with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void shouldAssignServiceAccountNameSharedByPods() throws Exception {
  // given
  PodSpec podSpec1 = new PodSpecBuilder().withServiceAccountName("sa").build();
  podSpec1.setAdditionalProperty("add1", 1L);
  PodData podData1 = new PodData(podSpec1, new ObjectMetaBuilder().build());

  PodSpec podSpec2 = new PodSpecBuilder().withServiceAccountName("sa").build();
  podSpec2.setAdditionalProperty("add2", 2L);
  PodData podData2 = new PodData(podSpec2, new ObjectMetaBuilder().build());

  // when
  Deployment merged = podMerger.merge(Arrays.asList(podData1, podData2));

  // then
  PodTemplateSpec podTemplate = merged.getSpec().getTemplate();
  String sa = podTemplate.getSpec().getServiceAccountName();
  assertEquals(sa, "sa");
}
 
Example #5
Source File: KubernetesAppDeployerTests.java    From spring-cloud-deployer-kubernetes with Apache License 2.0 6 votes vote down vote up
@Test
public void testPodSecurityContextPropertyOverrideGlobal() {
	Map<String, String> props = new HashMap<>();
	props.put("spring.cloud.deployer.kubernetes.podSecurityContext", "{runAsUser: 65534, fsGroup: 65534}");

	AppDefinition definition = new AppDefinition("app-test", null);
	AppDeploymentRequest appDeploymentRequest = new AppDeploymentRequest(definition, getResource(), props);

	KubernetesDeployerProperties kubernetesDeployerProperties = new KubernetesDeployerProperties();

	KubernetesDeployerProperties.PodSecurityContext securityContext = new KubernetesDeployerProperties.PodSecurityContext();
	securityContext.setFsGroup(1000L);
	securityContext.setRunAsUser(1000L);

	kubernetesDeployerProperties.setPodSecurityContext(securityContext);

	deployer = new KubernetesAppDeployer(kubernetesDeployerProperties, null);
	PodSpec podSpec = deployer.createPodSpec(appDeploymentRequest);

	PodSecurityContext podSecurityContext = podSpec.getSecurityContext();

	assertNotNull("Pod security context should not be null", podSecurityContext);

	assertEquals("Unexpected run as user", Long.valueOf("65534"), podSecurityContext.getRunAsUser());
	assertEquals("Unexpected fs group", Long.valueOf("65534"), podSecurityContext.getFsGroup());
}
 
Example #6
Source File: DockerImageWatcher.java    From jkube with Eclipse Public License 2.0 6 votes vote down vote up
private boolean updateImageName(HasMetadata entity, PodTemplateSpec template, String imagePrefix, String imageName) {
    boolean answer = false;
    PodSpec spec = template.getSpec();
    if (spec != null) {
        List<Container> containers = spec.getContainers();
        if (containers != null) {
            for (Container container : containers) {
                String image = container.getImage();
                if (image != null && image.startsWith(imagePrefix)) {
                    container.setImage(imageName);
                    log.info("Updating " + KubernetesHelper.getKind(entity) + " " + KubernetesHelper.getName(entity) + " to use image: " + imageName);
                    answer = true;
                }
            }
        }
    }
    return answer;
}
 
Example #7
Source File: KubernetesResourceUtil.java    From jkube with Eclipse Public License 2.0 6 votes vote down vote up
public static boolean podHasContainerImage(Pod pod, String imageName) {
    if (pod != null) {
        PodSpec spec = pod.getSpec();
        if (spec != null) {
            List<Container> containers = spec.getContainers();
            if (containers != null) {
                for (Container container : containers) {
                    if (Objects.equals(imageName, container.getImage())) {
                        return true;
                    }
                }
            }
        }
    }
    return false;
}
 
Example #8
Source File: KubernetesAppDeployerTests.java    From spring-cloud-deployer-kubernetes with Apache License 2.0 6 votes vote down vote up
@Test
public void testPodSecurityContextFsGroupOnly() {
	Map<String, String> props = new HashMap<>();
	props.put("spring.cloud.deployer.kubernetes.podSecurityContext", "{fsGroup: 65534}");

	AppDefinition definition = new AppDefinition("app-test", null);
	AppDeploymentRequest appDeploymentRequest = new AppDeploymentRequest(definition, getResource(), props);

	deployer = new KubernetesAppDeployer(new KubernetesDeployerProperties(), null);
	PodSpec podSpec = deployer.createPodSpec(appDeploymentRequest);

	PodSecurityContext podSecurityContext = podSpec.getSecurityContext();

	assertNotNull("Pod security context should not be null", podSecurityContext);

	assertNull("Unexpected run as user", podSecurityContext.getRunAsUser());
	assertEquals("Unexpected fs group", Long.valueOf("65534"), podSecurityContext.getFsGroup());
}
 
Example #9
Source File: KubernetesAppDeployerTests.java    From spring-cloud-deployer-kubernetes with Apache License 2.0 6 votes vote down vote up
@Test
public void testPodSecurityContextUIDOnly() {
	Map<String, String> props = new HashMap<>();
	props.put("spring.cloud.deployer.kubernetes.podSecurityContext", "{runAsUser: 65534}");

	AppDefinition definition = new AppDefinition("app-test", null);
	AppDeploymentRequest appDeploymentRequest = new AppDeploymentRequest(definition, getResource(), props);

	deployer = new KubernetesAppDeployer(new KubernetesDeployerProperties(), null);
	PodSpec podSpec = deployer.createPodSpec(appDeploymentRequest);

	PodSecurityContext podSecurityContext = podSpec.getSecurityContext();

	assertNotNull("Pod security context should not be null", podSecurityContext);

	assertEquals("Unexpected run as user", Long.valueOf("65534"), podSecurityContext.getRunAsUser());
	assertNull("Unexpected fs group", podSecurityContext.getFsGroup());
}
 
Example #10
Source File: PodMergerTest.java    From che with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void shouldNotAddImagePullPolicyTwice() throws Exception {
  // given
  PodSpec podSpec1 =
      new PodSpecBuilder()
          .withImagePullSecrets(new LocalObjectReferenceBuilder().withName("secret").build())
          .build();
  podSpec1.setAdditionalProperty("add1", 1L);
  PodData podData1 = new PodData(podSpec1, new ObjectMetaBuilder().build());

  PodSpec podSpec2 =
      new PodSpecBuilder()
          .withImagePullSecrets(new LocalObjectReferenceBuilder().withName("secret").build())
          .build();
  podSpec2.setAdditionalProperty("add2", 2L);
  PodData podData2 = new PodData(podSpec2, new ObjectMetaBuilder().build());

  // when
  Deployment merged = podMerger.merge(Arrays.asList(podData1, podData2));

  // then
  PodTemplateSpec podTemplate = merged.getSpec().getTemplate();
  List<LocalObjectReference> imagePullSecrets = podTemplate.getSpec().getImagePullSecrets();
  assertEquals(imagePullSecrets.size(), 1);
  assertEquals(imagePullSecrets.get(0).getName(), "secret");
}
 
Example #11
Source File: PodMergerTest.java    From che with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void shouldAssignSecurityContextSharedByPods() throws Exception {
  // given
  PodSpec podSpec1 =
      new PodSpecBuilder()
          .withSecurityContext(new PodSecurityContextBuilder().withRunAsUser(42L).build())
          .build();
  podSpec1.setAdditionalProperty("add1", 1L);
  PodData podData1 = new PodData(podSpec1, new ObjectMetaBuilder().build());

  PodSpec podSpec2 =
      new PodSpecBuilder()
          .withSecurityContext(new PodSecurityContextBuilder().withRunAsUser(42L).build())
          .build();
  podSpec2.setAdditionalProperty("add2", 2L);
  PodData podData2 = new PodData(podSpec2, new ObjectMetaBuilder().build());

  // when
  Deployment merged = podMerger.merge(Arrays.asList(podData1, podData2));

  // then
  PodTemplateSpec podTemplate = merged.getSpec().getTemplate();
  PodSecurityContext sc = podTemplate.getSpec().getSecurityContext();
  assertEquals(sc.getRunAsUser(), (Long) 42L);
}
 
Example #12
Source File: KubernetesAppDeployerTests.java    From spring-cloud-deployer-kubernetes with Apache License 2.0 6 votes vote down vote up
@Test
public void testPodSecurityContextProperty() {
	Map<String, String> props = new HashMap<>();
	props.put("spring.cloud.deployer.kubernetes.podSecurityContext", "{runAsUser: 65534, fsGroup: 65534}");

	AppDefinition definition = new AppDefinition("app-test", null);
	AppDeploymentRequest appDeploymentRequest = new AppDeploymentRequest(definition, getResource(), props);

	deployer = new KubernetesAppDeployer(new KubernetesDeployerProperties(), null);
	PodSpec podSpec = deployer.createPodSpec(appDeploymentRequest);

	PodSecurityContext podSecurityContext = podSpec.getSecurityContext();

	assertNotNull("Pod security context should not be null", podSecurityContext);

	assertEquals("Unexpected run as user", Long.valueOf("65534"), podSecurityContext.getRunAsUser());
	assertEquals("Unexpected fs group", Long.valueOf("65534"), podSecurityContext.getFsGroup());
}
 
Example #13
Source File: PodMergerTest.java    From che with Eclipse Public License 2.0 6 votes vote down vote up
@Test(
    expectedExceptions = ValidationException.class,
    expectedExceptionsMessageRegExp =
        "Pods have to have volumes with unique names but there are multiple `volume` volumes")
public void shouldThrownAnExceptionIfVolumeNameCollisionHappened() throws Exception {
  // given
  PodSpec podSpec1 =
      new PodSpecBuilder().withVolumes(new VolumeBuilder().withName("volume").build()).build();
  podSpec1.setAdditionalProperty("add1", 1L);
  PodData podData1 = new PodData(podSpec1, new ObjectMetaBuilder().build());

  PodSpec podSpec2 =
      new PodSpecBuilder().withVolumes(new VolumeBuilder().withName("volume").build()).build();
  podSpec2.setAdditionalProperty("add2", 2L);
  PodData podData2 = new PodData(podSpec2, new ObjectMetaBuilder().build());

  // when
  podMerger.merge(Arrays.asList(podData1, podData2));
}
 
Example #14
Source File: PodMergerTest.java    From che with Eclipse Public License 2.0 6 votes vote down vote up
@Test(expectedExceptions = ValidationException.class)
public void shouldFailServiceAccountDiffersInPods() throws Exception {
  // given
  PodSpec podSpec1 = new PodSpecBuilder().withServiceAccount("sa").build();
  podSpec1.setAdditionalProperty("add1", 1L);
  PodData podData1 = new PodData(podSpec1, new ObjectMetaBuilder().build());

  PodSpec podSpec2 = new PodSpecBuilder().withServiceAccount("sb").build();
  podSpec2.setAdditionalProperty("add2", 2L);
  PodData podData2 = new PodData(podSpec2, new ObjectMetaBuilder().build());

  // when
  Deployment merged = podMerger.merge(Arrays.asList(podData1, podData2));

  // then
  // exception is thrown
}
 
Example #15
Source File: ContainerResourceProvisionerTest.java    From che with Eclipse Public License 2.0 6 votes vote down vote up
@BeforeMethod
public void setup() {
  resourceProvisioner = new ContainerResourceProvisioner(1024, 512, "500m", "100m");
  container = new Container();
  container.setName(CONTAINER_NAME);
  when(k8sEnv.getMachines()).thenReturn(of(MACHINE_NAME, internalMachineConfig));
  when(internalMachineConfig.getAttributes())
      .thenReturn(
          of(
              MEMORY_LIMIT_ATTRIBUTE,
              RAM_LIMIT_VALUE,
              MEMORY_REQUEST_ATTRIBUTE,
              RAM_REQUEST_VALUE,
              CPU_LIMIT_ATTRIBUTE,
              CPU_LIMIT_VALUE,
              CPU_REQUEST_ATTRIBUTE,
              CPU_REQUEST_VALUE));
  final ObjectMeta podMetadata = mock(ObjectMeta.class);
  when(podMetadata.getName()).thenReturn(POD_NAME);
  final PodSpec podSpec = mock(PodSpec.class);
  when(podSpec.getContainers()).thenReturn(Collections.singletonList(container));
  when(k8sEnv.getPodsData()).thenReturn(of(POD_NAME, new PodData(podSpec, podMetadata)));
}
 
Example #16
Source File: PodMergerTest.java    From che with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void shouldGenerateContainerNamesIfCollisionHappened() throws Exception {
  // given
  PodSpec podSpec1 =
      new PodSpecBuilder().withContainers(new ContainerBuilder().withName("c").build()).build();
  PodData podData1 = new PodData(podSpec1, new ObjectMetaBuilder().build());

  PodSpec podSpec2 =
      new PodSpecBuilder().withContainers(new ContainerBuilder().withName("c").build()).build();
  PodData podData2 = new PodData(podSpec2, new ObjectMetaBuilder().build());

  // when
  Deployment merged = podMerger.merge(Arrays.asList(podData1, podData2));

  // then
  PodTemplateSpec podTemplate = merged.getSpec().getTemplate();
  List<Container> containers = podTemplate.getSpec().getContainers();
  assertEquals(containers.size(), 2);
  Container container1 = containers.get(0);
  assertEquals(container1.getName(), "c");
  Container container2 = containers.get(1);
  assertNotEquals(container2.getName(), "c");
  assertTrue(container2.getName().startsWith("c"));
}
 
Example #17
Source File: KubernetesAppDeployerTests.java    From spring-cloud-deployer-kubernetes with Apache License 2.0 6 votes vote down vote up
@Test
public void testSecretKeyRefGlobalFromYaml() throws Exception {
	AppDefinition definition = new AppDefinition("app-test", null);
	AppDeploymentRequest appDeploymentRequest = new AppDeploymentRequest(definition, getResource(), null);

	deployer = new KubernetesAppDeployer(bindDeployerProperties(), null);
	PodSpec podSpec = deployer.createPodSpec(appDeploymentRequest);

	List<EnvVar> envVars = podSpec.getContainers().get(0).getEnv();

	assertEquals("Invalid number of env vars", 3, envVars.size());

	EnvVar secretKeyRefEnvVar = envVars.get(0);
	assertEquals("Unexpected env var name", "SECRET_PASSWORD", secretKeyRefEnvVar.getName());
	SecretKeySelector secretKeySelector = secretKeyRefEnvVar.getValueFrom().getSecretKeyRef();
	assertEquals("Unexpected secret name", "mySecret", secretKeySelector.getName());
	assertEquals("Unexpected secret data key", "myPassword", secretKeySelector.getKey());
}
 
Example #18
Source File: OpenShiftEnvironmentFactoryTest.java    From che with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void addPodsWhenRecipeContainsThem() throws Exception {
  // given
  Pod pod =
      new PodBuilder()
          .withNewMetadata()
          .withName("pod")
          .endMetadata()
          .withSpec(new PodSpec())
          .build();
  when(k8sRecipeParser.parse(any(InternalRecipe.class))).thenReturn(singletonList(pod));

  // when
  KubernetesEnvironment osEnv = osEnvFactory.doCreate(internalRecipe, emptyMap(), emptyList());

  // then
  assertEquals(osEnv.getPodsCopy().size(), 1);
  assertEquals(osEnv.getPodsCopy().get("pod"), pod);

  assertEquals(osEnv.getPodsData().size(), 1);
  assertEquals(osEnv.getPodsData().get("pod").getMetadata(), pod.getMetadata());
  assertEquals(osEnv.getPodsData().get("pod").getSpec(), pod.getSpec());
}
 
Example #19
Source File: KubernetesScheduler.java    From spring-cloud-deployer-kubernetes with Apache License 2.0 6 votes vote down vote up
protected CronJob createCronJob(ScheduleRequest scheduleRequest) {
	Map<String, String> labels = Collections.singletonMap(SPRING_CRONJOB_ID_KEY,
			scheduleRequest.getDefinition().getName());

	Map<String, String> schedulerProperties = scheduleRequest.getSchedulerProperties();
	String schedule = schedulerProperties.get(SchedulerPropertyKeys.CRON_EXPRESSION);
	Assert.hasText(schedule, "The property: " + SchedulerPropertyKeys.CRON_EXPRESSION + " must be defined");

	PodSpec podSpec = createPodSpec(scheduleRequest);
	String taskServiceAccountName = this.deploymentPropertiesResolver.getTaskServiceAccountName(scheduleRequest.getSchedulerProperties());
	if (StringUtils.hasText(taskServiceAccountName)) {
		podSpec.setServiceAccountName(taskServiceAccountName);
	}

	CronJob cronJob = new CronJobBuilder().withNewMetadata().withName(scheduleRequest.getScheduleName())
			.withLabels(labels).endMetadata().withNewSpec().withSchedule(schedule).withNewJobTemplate()
			.withNewSpec().withNewTemplate().withSpec(podSpec).endTemplate().endSpec()
			.endJobTemplate().endSpec().build();

	setImagePullSecret(scheduleRequest, cronJob);

	return this.client.batch().cronjobs().create(cronJob);
}
 
Example #20
Source File: KubernetesAppDeployerTests.java    From spring-cloud-deployer-kubernetes with Apache License 2.0 6 votes vote down vote up
@Test
public void testSecretKeyRefGlobal() {
	AppDefinition definition = new AppDefinition("app-test", null);
	AppDeploymentRequest appDeploymentRequest = new AppDeploymentRequest(definition, getResource(), null);

	KubernetesDeployerProperties kubernetesDeployerProperties = new KubernetesDeployerProperties();
	KubernetesDeployerProperties.SecretKeyRef secretKeyRef = new KubernetesDeployerProperties.SecretKeyRef();
	secretKeyRef.setEnvVarName("SECRET_PASSWORD_GLOBAL");
	secretKeyRef.setSecretName("mySecretGlobal");
	secretKeyRef.setDataKey("passwordGlobal");
	kubernetesDeployerProperties.setSecretKeyRefs(Collections.singletonList(secretKeyRef));

	deployer = new KubernetesAppDeployer(kubernetesDeployerProperties, null);
	PodSpec podSpec = deployer.createPodSpec(appDeploymentRequest);

	List<EnvVar> envVars = podSpec.getContainers().get(0).getEnv();

	assertEquals("Invalid number of env vars", 2, envVars.size());

	EnvVar secretKeyRefEnvVar = envVars.get(0);
	assertEquals("Unexpected env var name", "SECRET_PASSWORD_GLOBAL", secretKeyRefEnvVar.getName());
	SecretKeySelector secretKeySelector = secretKeyRefEnvVar.getValueFrom().getSecretKeyRef();
	assertEquals("Unexpected secret name", "mySecretGlobal", secretKeySelector.getName());
	assertEquals("Unexpected secret data key", "passwordGlobal", secretKeySelector.getKey());
}
 
Example #21
Source File: KubernetesAppDeployerTests.java    From spring-cloud-deployer-kubernetes with Apache License 2.0 6 votes vote down vote up
@Test
public void testSecretKeyRef() {
	Map<String, String> props = new HashMap<>();
	props.put("spring.cloud.deployer.kubernetes.secretKeyRefs",
			"[{envVarName: 'SECRET_PASSWORD', secretName: 'mySecret', dataKey: 'password'}]");

	AppDefinition definition = new AppDefinition("app-test", null);
	AppDeploymentRequest appDeploymentRequest = new AppDeploymentRequest(definition, getResource(), props);

	deployer = new KubernetesAppDeployer(new KubernetesDeployerProperties(), null);
	PodSpec podSpec = deployer.createPodSpec(appDeploymentRequest);

	List<EnvVar> envVars = podSpec.getContainers().get(0).getEnv();

	assertEquals("Invalid number of env vars", 2, envVars.size());

	EnvVar secretKeyRefEnvVar = envVars.get(0);
	assertEquals("Unexpected env var name", "SECRET_PASSWORD", secretKeyRefEnvVar.getName());
	SecretKeySelector secretKeySelector = secretKeyRefEnvVar.getValueFrom().getSecretKeyRef();
	assertEquals("Unexpected secret name", "mySecret", secretKeySelector.getName());
	assertEquals("Unexpected secret data key", "password", secretKeySelector.getKey());
}
 
Example #22
Source File: PodMergerTest.java    From che with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void shouldAssignServiceAccountSharedByPods() throws Exception {
  // given
  PodSpec podSpec1 = new PodSpecBuilder().withServiceAccount("sa").build();
  podSpec1.setAdditionalProperty("add1", 1L);
  PodData podData1 = new PodData(podSpec1, new ObjectMetaBuilder().build());

  PodSpec podSpec2 = new PodSpecBuilder().withServiceAccount("sa").build();
  podSpec2.setAdditionalProperty("add2", 2L);
  PodData podData2 = new PodData(podSpec2, new ObjectMetaBuilder().build());

  // when
  Deployment merged = podMerger.merge(Arrays.asList(podData1, podData2));

  // then
  PodTemplateSpec podTemplate = merged.getSpec().getTemplate();
  String sa = podTemplate.getSpec().getServiceAccount();
  assertEquals(sa, "sa");
}
 
Example #23
Source File: KubernetesIntegrationTest.java    From wildfly-camel with Apache License 2.0 6 votes vote down vote up
private PodSpec createPodSpec() throws IOException {
    PodSpec podSpec = new PodSpec();
    podSpec.setHostname("localhost");

    Container container = new Container();
    container.setImage("docker.io/wildflyext/wildfly-camel:latest");
    container.setName("wildfly-camel-test");

    ContainerPort port = new ContainerPort();
    port.setHostIP("0.0.0.0");
    port.setContainerPort(8080);

    List<ContainerPort> ports = new ArrayList<>();
    ports.add(port);
    container.setPorts(ports);

    List<Container> containers = new ArrayList<>();
    containers.add(container);

    podSpec.setContainers(containers);

    return podSpec;
}
 
Example #24
Source File: KubernetesAppDeployerTests.java    From spring-cloud-deployer-kubernetes with Apache License 2.0 6 votes vote down vote up
@Test
public void deployWithEnvironmentWithCommaDelimitedValue() throws Exception {
	AppDefinition definition = new AppDefinition("app-test", null);
	Map<String, String> props = new HashMap<>();
	props.put("spring.cloud.deployer.kubernetes.environmentVariables",
			"JAVA_TOOL_OPTIONS='thing1,thing2',foo='bar,baz',car=caz,boo='zoo,gnu',doo=dar,OPTS='thing1'");

	AppDeploymentRequest appDeploymentRequest = new AppDeploymentRequest(definition, getResource(), props);

	deployer = new KubernetesAppDeployer(bindDeployerProperties(), null);
	PodSpec podSpec = deployer.createPodSpec(appDeploymentRequest);

	assertThat(podSpec.getContainers().get(0).getEnv())
		.contains(
			new EnvVar("foo", "bar,baz", null),
			new EnvVar("car", "caz", null),
			new EnvVar("boo", "zoo,gnu", null),
			new EnvVar("doo", "dar", null),
			new EnvVar("JAVA_TOOL_OPTIONS", "thing1,thing2", null),
			new EnvVar("OPTS", "thing1", null));
}
 
Example #25
Source File: PodTemplateUtilsTest.java    From kubernetes-plugin with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldCombineAllTolerations() {
    PodSpec podSpec1 = new PodSpec();
    Pod pod1 = new Pod();
    Toleration toleration1 = new Toleration("effect1", "key1", "oper1", Long.parseLong("1"), "val1");
    Toleration toleration2 = new Toleration("effect2", "key2", "oper2", Long.parseLong("2"), "val2");
    podSpec1.setTolerations(asList(toleration1, toleration2));
    pod1.setSpec(podSpec1);
    pod1.setMetadata(new ObjectMeta());

    PodSpec podSpec2 = new PodSpec();
    Pod pod2 = new Pod();
    Toleration toleration3 = new Toleration("effect3", "key3", "oper3", Long.parseLong("3"), "val3");
    Toleration toleration4 = new Toleration("effect4", "key4", "oper4", Long.parseLong("4"), "val4");
    podSpec2.setTolerations(asList(toleration3, toleration4));
    pod2.setSpec(podSpec2);
    pod2.setMetadata(new ObjectMeta());

    Pod result = combine(pod1, pod2);
    assertThat(result.getSpec().getTolerations(), containsInAnyOrder(toleration1, toleration2, toleration3, toleration4));
}
 
Example #26
Source File: PodMergerTest.java    From che with Eclipse Public License 2.0 6 votes vote down vote up
@Test(expectedExceptions = ValidationException.class)
public void shouldFailServiceAccountNameDiffersInPods() throws Exception {
  // given
  PodSpec podSpec1 = new PodSpecBuilder().withServiceAccountName("sa").build();
  podSpec1.setAdditionalProperty("add1", 1L);
  PodData podData1 = new PodData(podSpec1, new ObjectMetaBuilder().build());

  PodSpec podSpec2 = new PodSpecBuilder().withServiceAccountName("sb").build();
  podSpec2.setAdditionalProperty("add2", 2L);
  PodData podData2 = new PodData(podSpec2, new ObjectMetaBuilder().build());

  // when
  Deployment merged = podMerger.merge(Arrays.asList(podData1, podData2));

  // then
  // exception is thrown
}
 
Example #27
Source File: SubPathPrefixesTest.java    From che with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void shouldPrefixVolumeMountsSubpathsAndUsePvcNameAsVolumeName() {
  // when
  subpathPrefixes.prefixVolumeMountsSubpaths(k8sEnv, WORKSPACE_ID);

  // then
  PodSpec podSpec = k8sEnv.getPodsData().get(POD_1_NAME).getSpec();

  io.fabric8.kubernetes.api.model.Volume userPodVolume = podSpec.getVolumes().get(0);
  assertEquals(userPodVolume.getPersistentVolumeClaim().getClaimName(), USER_DATA_PVC_NAME);
  assertEquals(
      podSpec.getVolumes().get(0).getPersistentVolumeClaim().getClaimName(), USER_DATA_PVC_NAME);

  Container initContainer = podSpec.getInitContainers().get(0);
  VolumeMount initVolumeMount = initContainer.getVolumeMounts().get(0);
  assertEquals(
      initVolumeMount.getSubPath(),
      WORKSPACE_ID + "/" + USER_DATA_PVC_NAME + "/tmp/init/userData");
  assertEquals(initVolumeMount.getName(), userPodVolume.getName());

  Container container = podSpec.getContainers().get(0);
  VolumeMount volumeMount = container.getVolumeMounts().get(0);
  assertEquals(
      volumeMount.getSubPath(), WORKSPACE_ID + "/" + USER_DATA_PVC_NAME + "/home/user/data");
  assertEquals(volumeMount.getName(), userPodVolume.getName());
}
 
Example #28
Source File: KubernetesAppDeployerTests.java    From spring-cloud-deployer-kubernetes with Apache License 2.0 6 votes vote down vote up
@Test
public void deployWithGlobalTolerations() {
	AppDefinition definition = new AppDefinition("app-test", null);

	Map<String, String> props = new HashMap<>();
	props.put("spring.cloud.deployer.kubernetes.tolerations",
			"[{key: 'test', value: 'true', operator: 'Equal', effect: 'NoSchedule', tolerationSeconds: 5}, "
					+ "{key: 'test2', value: 'false', operator: 'Equal', effect: 'NoSchedule', tolerationSeconds: 5}]");

	AppDeploymentRequest appDeploymentRequest = new AppDeploymentRequest(definition, getResource(), props);

	deployer = new KubernetesAppDeployer(new KubernetesDeployerProperties(), null);
	PodSpec podSpec = deployer.createPodSpec(appDeploymentRequest);

	assertNotNull(podSpec.getTolerations());
	assertThat(podSpec.getTolerations().size() == 2);
	assertThat(podSpec.getTolerations().contains(new Toleration("NoSchedule", "test", "Equal", 5L, "true")));
	assertThat(podSpec.getTolerations().contains(new Toleration("NoSchedule", "test2", "Equal", 5L, "false")));
}
 
Example #29
Source File: PodMergerTest.java    From che with Eclipse Public License 2.0 6 votes vote down vote up
@Test(expectedExceptions = ValidationException.class)
public void shouldFailIfSecurityContextDiffersInPods() throws Exception {
  // given
  PodSpec podSpec1 =
      new PodSpecBuilder()
          .withSecurityContext(new PodSecurityContextBuilder().withRunAsUser(42L).build())
          .build();
  podSpec1.setAdditionalProperty("add1", 1L);
  PodData podData1 = new PodData(podSpec1, new ObjectMetaBuilder().build());

  PodSpec podSpec2 =
      new PodSpecBuilder()
          .withSecurityContext(new PodSecurityContextBuilder().withRunAsUser(43L).build())
          .build();
  podSpec2.setAdditionalProperty("add2", 2L);
  PodData podData2 = new PodData(podSpec2, new ObjectMetaBuilder().build());

  // when
  Deployment merged = podMerger.merge(Arrays.asList(podData1, podData2));

  // then
  // exception is thrown
}
 
Example #30
Source File: KubernetesAppDeployerTests.java    From spring-cloud-deployer-kubernetes with Apache License 2.0 5 votes vote down vote up
@Test
public void testPodAffinityFromYaml() throws Exception {
	AppDefinition definition = new AppDefinition("app-test", null);
	AppDeploymentRequest appDeploymentRequest = new AppDeploymentRequest(definition, getResource(), null);

	deployer = new KubernetesAppDeployer(bindDeployerProperties(), null);
	PodSpec podSpec = deployer.createPodSpec(appDeploymentRequest);

	PodAffinity podAffinity = podSpec.getAffinity().getPodAffinity();
	assertNotNull("Pod affinity should not be null", podAffinity);
	assertNotNull("RequiredDuringSchedulingIgnoredDuringExecution should not be null", podAffinity.getRequiredDuringSchedulingIgnoredDuringExecution());
	assertEquals("PreferredDuringSchedulingIgnoredDuringExecution should have one element", 1, podAffinity.getPreferredDuringSchedulingIgnoredDuringExecution().size());
}