io.fabric8.kubernetes.api.model.apps.Deployment Java Examples

The following examples show how to use io.fabric8.kubernetes.api.model.apps.Deployment. 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: KubernetesComponentToWorkspaceApplier.java    From che with Eclipse Public License 2.0 6 votes vote down vote up
private List<PodData> getPodDatas(List<HasMetadata> componentsObjects) {
  List<PodData> podsData = new ArrayList<>();

  componentsObjects
      .stream()
      .filter(hasMetadata -> hasMetadata instanceof Pod)
      .map(hasMetadata -> (Pod) hasMetadata)
      .forEach(p -> podsData.add(new PodData(p)));

  componentsObjects
      .stream()
      .filter(hasMetadata -> hasMetadata instanceof Deployment)
      .map(hasMetadata -> (Deployment) hasMetadata)
      .forEach(d -> podsData.add(new PodData(d)));
  return podsData;
}
 
Example #2
Source File: DeploymentTest.java    From kubernetes-client with Apache License 2.0 6 votes vote down vote up
@Test
@DisplayName("Should restart rollout")
void testRolloutRestart() throws InterruptedException {
  // Given
  server.expect().get().withPath("/apis/apps/v1/namespaces/ns1/deployments/deploy1")
    .andReturn(HttpURLConnection.HTTP_OK, getDeploymentBuilder().build()).times(3);
  server.expect().patch().withPath("/apis/apps/v1/namespaces/ns1/deployments/deploy1")
    .andReturn(HttpURLConnection.HTTP_OK, getDeploymentBuilder().build()).once();
  KubernetesClient client = server.getClient();

  // When
  Deployment deployment = client.apps().deployments().inNamespace("ns1").withName("deploy1")
    .rolling().restart();

  // Then
  RecordedRequest recordedRequest = server.getLastRequest();
  assertNotNull(deployment);
  assertEquals("PATCH", recordedRequest.getMethod());
  assertTrue(recordedRequest.getBody().readUtf8().contains("kubectl.kubernetes.io/restartedAt"));
}
 
Example #3
Source File: KafkaMirrorMakerClusterTest.java    From strimzi-kafka-operator with Apache License 2.0 6 votes vote down vote up
@Test
public void testImagePullSecrets() {
    LocalObjectReference secret1 = new LocalObjectReference("some-pull-secret");
    LocalObjectReference secret2 = new LocalObjectReference("some-other-pull-secret");

    KafkaMirrorMaker resource = new KafkaMirrorMakerBuilder(this.resource)
            .editSpec()
                .withNewTemplate()
                    .withNewPod()
                        .withImagePullSecrets(secret1, secret2)
                    .endPod()
                .endTemplate()
            .endSpec()
            .build();
    KafkaMirrorMakerCluster mmc = KafkaMirrorMakerCluster.fromCrd(resource, VERSIONS);

    Deployment dep = mmc.generateDeployment(emptyMap(), true, null, null);
    assertThat(dep.getSpec().getTemplate().getSpec().getImagePullSecrets().size(), is(2));
    assertThat(dep.getSpec().getTemplate().getSpec().getImagePullSecrets().contains(secret1), is(true));
    assertThat(dep.getSpec().getTemplate().getSpec().getImagePullSecrets().contains(secret2), is(true));
}
 
Example #4
Source File: Sample14Test.java    From module-ballerina-kubernetes with Apache License 2.0 6 votes vote down vote up
@BeforeClass
public void compileSample() throws IOException, InterruptedException {
    Assert.assertEquals(KubernetesTestUtils.compileBallerinaFile(SOURCE_DIR_PATH, "hello_world_k8s_namespace.bal"),
            0);
    File yamlFile = KUBERNETES_TARGET_PATH.resolve("hello_world_k8s_namespace.yaml").toFile();
    Assert.assertTrue(yamlFile.exists());
    List<HasMetadata> k8sItems = KubernetesTestUtils.loadYaml(yamlFile);
    for (HasMetadata data : k8sItems) {
        switch (data.getKind()) {
            case "Deployment":
                deployment = (Deployment) data;
                break;
            case "Service":
                service = (Service) data;
                break;
            case "Ingress":
                ingress = (Ingress) data;
                break;
            default:
                break;
        }
    }
}
 
Example #5
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 #6
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 #7
Source File: KafkaConnectClusterTest.java    From strimzi-kafka-operator with Apache License 2.0 6 votes vote down vote up
@Test
public void testTracing() {
    KafkaConnect resource = new KafkaConnectBuilder(this.resource)
            .editSpec()
                .withNewJaegerTracing()
                .endJaegerTracing()
            .endSpec()
            .build();
    KafkaConnectCluster kc = KafkaConnectCluster.fromCrd(resource, VERSIONS);

    Deployment dep = kc.generateDeployment(Collections.EMPTY_MAP, true, null, null);
    Container cont = dep.getSpec().getTemplate().getSpec().getContainers().get(0);
    assertThat(cont.getEnv().stream().filter(env -> KafkaConnectCluster.ENV_VAR_STRIMZI_TRACING.equals(env.getName())).map(EnvVar::getValue).findFirst().orElse("").equals("jaeger"), is(true));
    assertThat(cont.getEnv().stream().filter(env -> KafkaConnectCluster.ENV_VAR_KAFKA_CONNECT_CONFIGURATION.equals(env.getName())).map(EnvVar::getValue).findFirst().orElse("").contains("consumer.interceptor.classes=io.opentracing.contrib.kafka.TracingConsumerInterceptor"), is(true));
    assertThat(cont.getEnv().stream().filter(env -> KafkaConnectCluster.ENV_VAR_KAFKA_CONNECT_CONFIGURATION.equals(env.getName())).map(EnvVar::getValue).findFirst().orElse("").contains("producer.interceptor.classes=io.opentracing.contrib.kafka.TracingProducerInterceptor"), is(true));
}
 
Example #8
Source File: Issue421Test.java    From dekorate with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldHavePort() {
  KubernetesList list = Serialization.unmarshalAsList(Issue421Test.class.getClassLoader().getResourceAsStream("META-INF/dekorate/kubernetes.yml"));
  assertNotNull(list);
  Deployment d = findFirst(list, Deployment.class).orElseThrow(() -> new IllegalStateException());
  assertNotNull(d);
  Container c = d.getSpec().getTemplate().getSpec().getContainers().get(0);
  assertNotNull(c);

  List<ContainerPort> ports = c.getPorts();
  assertNotNull(ports);
  assertEquals(1, ports.size());

  ContainerPort port = ports.get(0);
  assertEquals("HTTP", port.getName());
  assertEquals(8080, port.getContainerPort());
}
 
Example #9
Source File: DeploymentTest.java    From kubernetes-client with Apache License 2.0 6 votes vote down vote up
@Test
@DisplayName("Should pause resource")
void testRolloutPause() throws InterruptedException {
  // Given
  server.expect().get().withPath("/apis/apps/v1/namespaces/ns1/deployments/deploy1")
    .andReturn(HttpURLConnection.HTTP_OK, getDeploymentBuilder().build()).times(3);
  server.expect().patch().withPath("/apis/apps/v1/namespaces/ns1/deployments/deploy1")
    .andReturn(HttpURLConnection.HTTP_OK, getDeploymentBuilder().build()).once();
  KubernetesClient client = server.getClient();

  // When
  Deployment deployment = client.apps().deployments().inNamespace("ns1").withName("deploy1")
    .rolling().pause();

  // Then
  RecordedRequest recordedRequest = server.getLastRequest();
  assertNotNull(deployment);
  assertEquals("PATCH", recordedRequest.getMethod());
  assertEquals("{\"spec\":{\"paused\":true}}", recordedRequest.getBody().readUtf8());
}
 
Example #10
Source File: KafkaMirrorMaker2ClusterTest.java    From strimzi-kafka-operator with Apache License 2.0 6 votes vote down vote up
@Test
public void testNoExternalConfigurationEnvs() {
    ExternalConfigurationEnv env = new ExternalConfigurationEnvBuilder()
            .withName("MY_ENV_VAR")
            .withNewValueFrom()
            .endValueFrom()
            .build();

    KafkaMirrorMaker2 resource = new KafkaMirrorMaker2Builder(this.resource)
            .editSpec()
            .withNewExternalConfiguration()
            .withEnv(env)
            .endExternalConfiguration()
            .endSpec()
            .build();
    KafkaMirrorMaker2Cluster kmm2 = KafkaMirrorMaker2Cluster.fromCrd(resource, VERSIONS);

    // Check Deployment
    Deployment dep = kmm2.generateDeployment(emptyMap(), true, null, null);
    List<EnvVar> envs = getContainer(dep).getEnv();
    List<EnvVar> selected = envs.stream().filter(var -> var.getName().equals("MY_ENV_VAR")).collect(Collectors.toList());
    assertThat(selected.size(), is(0));
}
 
Example #11
Source File: KafkaBridgeClusterTest.java    From strimzi-kafka-operator with Apache License 2.0 6 votes vote down vote up
@Test
public void testGenerateDeploymentWithTls() {
    KafkaBridge resource = new KafkaBridgeBuilder(this.resource)
            .editSpec()
                .editOrNewTls()
                    .addToTrustedCertificates(new CertSecretSourceBuilder().withSecretName("my-secret").withCertificate("cert.crt").build())
                    .addToTrustedCertificates(new CertSecretSourceBuilder().withSecretName("my-secret").withCertificate("new-cert.crt").build())
                    .addToTrustedCertificates(new CertSecretSourceBuilder().withSecretName("my-another-secret").withCertificate("another-cert.crt").build())
                .endTls()
            .endSpec()
            .build();
    KafkaBridgeCluster kbc = KafkaBridgeCluster.fromCrd(resource, VERSIONS);
    Deployment dep = kbc.generateDeployment(emptyMap(), true, null, null);

    assertThat(dep.getSpec().getTemplate().getSpec().getVolumes().get(1).getName(), is("my-secret"));
    assertThat(dep.getSpec().getTemplate().getSpec().getVolumes().get(2).getName(), is("my-another-secret"));

    List<Container> containers = dep.getSpec().getTemplate().getSpec().getContainers();

    assertThat(containers.get(0).getVolumeMounts().get(1).getMountPath(), is(KafkaBridgeCluster.TLS_CERTS_BASE_VOLUME_MOUNT + "my-secret"));
    assertThat(containers.get(0).getVolumeMounts().get(2).getMountPath(), is(KafkaBridgeCluster.TLS_CERTS_BASE_VOLUME_MOUNT + "my-another-secret"));

    assertThat(AbstractModel.containerEnvVars(containers.get(0)).get(KafkaBridgeCluster.ENV_VAR_KAFKA_BRIDGE_TRUSTED_CERTS), is("my-secret/cert.crt;my-secret/new-cert.crt;my-another-secret/another-cert.crt"));
    assertThat(AbstractModel.containerEnvVars(containers.get(0)).get(KafkaBridgeCluster.ENV_VAR_KAFKA_BRIDGE_TLS), is("true"));
}
 
Example #12
Source File: KubernetesWithQuarkusAppNameTest.java    From quarkus with Apache License 2.0 6 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("foo");
            assertThat(m.getLabels()).contains(entry("app.kubernetes.io/name", "foo"),
                    entry("app.kubernetes.io/version", "1.0-kube"));
        });
    });

    List<HasMetadata> openshiftList = DeserializationUtil
            .deserializeAsList(kubernetesDir.resolve("openshift.yml"));
    assertThat(openshiftList).allSatisfy(h -> {
        assertThat(h.getMetadata().getName()).isIn("ofoo", "s2ifoo", "s2i-java");
        assertThat(h.getMetadata().getLabels()).contains(entry("app.kubernetes.io/name", "ofoo"),
                entry("app.kubernetes.io/version", "1.0-openshift"));
    });
}
 
Example #13
Source File: KubernetesEnvironment.java    From che with Eclipse Public License 2.0 6 votes vote down vote up
protected KubernetesEnvironment(
    InternalRecipe internalRecipe,
    Map<String, InternalMachineConfig> machines,
    List<Warning> warnings,
    Map<String, Pod> pods,
    Map<String, Deployment> deployments,
    Map<String, Service> services,
    Map<String, Ingress> ingresses,
    Map<String, PersistentVolumeClaim> persistentVolumeClaims,
    Map<String, Secret> secrets,
    Map<String, ConfigMap> configMaps) {
  super(internalRecipe, machines, warnings);
  setType(TYPE);
  this.pods = pods;
  this.deployments = deployments;
  this.services = services;
  this.ingresses = ingresses;
  this.persistentVolumeClaims = persistentVolumeClaims;
  this.secrets = secrets;
  this.configMaps = configMaps;
  this.podData = new HashMap<>();
  this.injectablePods = new HashMap<>();
  pods.forEach((name, pod) -> podData.put(name, new PodData(pod)));
  deployments.forEach((name, deployment) -> podData.put(name, new PodData(deployment)));
}
 
Example #14
Source File: EntityOperatorTest.java    From strimzi-kafka-operator with Apache License 2.0 6 votes vote down vote up
@Test
public void testGracePeriod() {
    Kafka resource = new KafkaBuilder(ResourceUtils.createKafkaCluster(namespace, cluster, replicas, image, healthDelay, healthTimeout))
            .editSpec()
                .withNewEntityOperator()
                .withTopicOperator(entityTopicOperatorSpec)
                .withUserOperator(entityUserOperatorSpec)
                .withNewTemplate()
                    .withNewPod()
                        .withTerminationGracePeriodSeconds(123)
                    .endPod()
                .endTemplate()
                .endEntityOperator()
            .endSpec()
            .build();
    EntityOperator eo = EntityOperator.fromCrd(resource, VERSIONS);

    Deployment dep = eo.generateDeployment(true, Collections.EMPTY_MAP, null, null);
    assertThat(dep.getSpec().getTemplate().getSpec().getTerminationGracePeriodSeconds(), is(Long.valueOf(123)));
    assertThat(dep.getSpec().getTemplate().getSpec().getContainers().get(2).getLifecycle(), is(notNullValue()));
    assertThat(dep.getSpec().getTemplate().getSpec().getContainers().get(2).getLifecycle().getPreStop().getExec().getCommand().contains("/opt/stunnel/entity_operator_stunnel_pre_stop.sh"), is(true));
}
 
Example #15
Source File: DeploymentOperationsImpl.java    From kubernetes-client with Apache License 2.0 6 votes vote down vote up
@Override
public Deployment updateImage(Map<String, String> containerToImageMap) {
  Deployment deployment = get();
  if (deployment == null) {
    throw new KubernetesClientException("Existing replica set doesn't exist");
  }
  if (deployment.getSpec().getTemplate().getSpec().getContainers().isEmpty()) {
    throw new KubernetesClientException("Pod has no containers!");
  }

  List<Container> containers = deployment.getSpec().getTemplate().getSpec().getContainers();
  for (Container container : containers) {
    if (containerToImageMap.containsKey(container.getName())) {
      container.setImage(containerToImageMap.get(container.getName()));
    }
  }
  deployment.getSpec().getTemplate().getSpec().setContainers(containers);
  return sendPatchedObject(get(), deployment);
}
 
Example #16
Source File: KafkaMirrorMaker2ClusterTest.java    From strimzi-kafka-operator with Apache License 2.0 6 votes vote down vote up
@Test
public void testResources() {
    Map<String, Quantity> requests = new HashMap<>(2);
    requests.put("cpu", new Quantity("250m"));
    requests.put("memory", new Quantity("512Mi"));

    Map<String, Quantity> limits = new HashMap<>(2);
    limits.put("cpu", new Quantity("500m"));
    limits.put("memory", new Quantity("1024Mi"));

    KafkaMirrorMaker2 resource = new KafkaMirrorMaker2Builder(this.resource)
            .editSpec()
                .withResources(new ResourceRequirementsBuilder().withLimits(limits).withRequests(requests).build())
            .endSpec()
            .build();
    KafkaMirrorMaker2Cluster kmm2 = KafkaMirrorMaker2Cluster.fromCrd(resource, VERSIONS);

    Deployment dep = kmm2.generateDeployment(Collections.EMPTY_MAP, true, null, null);
    Container cont = getContainer(dep);
    assertThat(cont.getResources().getLimits(), is(limits));
    assertThat(cont.getResources().getRequests(), is(requests));
}
 
Example #17
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 #18
Source File: KafkaMirrorMaker2ClusterTest.java    From strimzi-kafka-operator with Apache License 2.0 6 votes vote down vote up
@Test
public void testImagePullSecretsBoth() {
    LocalObjectReference secret1 = new LocalObjectReference("some-pull-secret");
    LocalObjectReference secret2 = new LocalObjectReference("some-other-pull-secret");

    KafkaMirrorMaker2 resource = new KafkaMirrorMaker2Builder(this.resource)
            .editSpec()
                .withNewTemplate()
                    .withNewPod()
                        .withImagePullSecrets(secret2)
                    .endPod()
                .endTemplate()
            .endSpec()
            .build();
    KafkaMirrorMaker2Cluster kmm2 = KafkaMirrorMaker2Cluster.fromCrd(resource, VERSIONS);

    Deployment dep = kmm2.generateDeployment(emptyMap(), true, null, singletonList(secret1));
    assertThat(dep.getSpec().getTemplate().getSpec().getImagePullSecrets().size(), is(1));
    assertThat(dep.getSpec().getTemplate().getSpec().getImagePullSecrets().contains(secret1), is(false));
    assertThat(dep.getSpec().getTemplate().getSpec().getImagePullSecrets().contains(secret2), is(true));
}
 
Example #19
Source File: KafkaMirrorMakerClusterTest.java    From strimzi-kafka-operator with Apache License 2.0 6 votes vote down vote up
@Test
public void testJvmOptions() {
    Map<String, String> xx = new HashMap<>(2);
    xx.put("UseG1GC", "true");
    xx.put("MaxGCPauseMillis", "20");

    KafkaMirrorMaker resource = new KafkaMirrorMakerBuilder(this.resource)
            .editSpec()
                .withNewJvmOptions()
                    .withNewXms("512m")
                    .withNewXmx("1024m")
                    .withNewServer(true)
                    .withXx(xx)
                .endJvmOptions()
            .endSpec()
            .build();
    KafkaMirrorMakerCluster mmc = KafkaMirrorMakerCluster.fromCrd(resource, VERSIONS);

    Deployment dep = mmc.generateDeployment(Collections.EMPTY_MAP, true, null, null);
    Container cont = dep.getSpec().getTemplate().getSpec().getContainers().get(0);
    assertThat(cont.getEnv().stream().filter(env -> "KAFKA_JVM_PERFORMANCE_OPTS".equals(env.getName())).map(EnvVar::getValue).findFirst().orElse("").contains("-server"), is(true));
    assertThat(cont.getEnv().stream().filter(env -> "KAFKA_JVM_PERFORMANCE_OPTS".equals(env.getName())).map(EnvVar::getValue).findFirst().orElse("").contains("-XX:+UseG1GC"), is(true));
    assertThat(cont.getEnv().stream().filter(env -> "KAFKA_JVM_PERFORMANCE_OPTS".equals(env.getName())).map(EnvVar::getValue).findFirst().orElse("").contains("-XX:MaxGCPauseMillis=20"), is(true));
    assertThat(cont.getEnv().stream().filter(env -> "KAFKA_HEAP_OPTS".equals(env.getName())).map(EnvVar::getValue).findFirst().orElse("").contains("-Xmx1024m"), is(true));
    assertThat(cont.getEnv().stream().filter(env -> "KAFKA_HEAP_OPTS".equals(env.getName())).map(EnvVar::getValue).findFirst().orElse("").contains("-Xms512m"), is(true));
}
 
Example #20
Source File: KafkaConnectClusterTest.java    From strimzi-kafka-operator with Apache License 2.0 6 votes vote down vote up
@Test
public void testResources() {
    Map<String, Quantity> requests = new HashMap<>(2);
    requests.put("cpu", new Quantity("250m"));
    requests.put("memory", new Quantity("512Mi"));

    Map<String, Quantity> limits = new HashMap<>(2);
    limits.put("cpu", new Quantity("500m"));
    limits.put("memory", new Quantity("1024Mi"));

    KafkaConnect resource = new KafkaConnectBuilder(this.resource)
            .editSpec()
                .withResources(new ResourceRequirementsBuilder().withLimits(limits).withRequests(requests).build())
            .endSpec()
            .build();
    KafkaConnectCluster kc = KafkaConnectCluster.fromCrd(resource, VERSIONS);

    Deployment dep = kc.generateDeployment(Collections.EMPTY_MAP, true, null, null);
    Container cont = dep.getSpec().getTemplate().getSpec().getContainers().get(0);
    assertThat(cont.getResources().getLimits(), is(limits));
    assertThat(cont.getResources().getRequests(), is(requests));
}
 
Example #21
Source File: KafkaConnectS2IClusterTest.java    From strimzi-kafka-operator with Apache License 2.0 6 votes vote down vote up
@Test
public void testImagePullSecretsBoth() {
    LocalObjectReference secret1 = new LocalObjectReference("some-pull-secret");
    LocalObjectReference secret2 = new LocalObjectReference("some-other-pull-secret");

    KafkaConnectS2I resource = new KafkaConnectS2IBuilder(this.resource)
            .editSpec()
                .withNewTemplate()
                    .withNewPod()
                        .withImagePullSecrets(secret2)
                    .endPod()
                .endTemplate()
            .endSpec()
            .build();
    KafkaConnectS2ICluster kc = KafkaConnectS2ICluster.fromCrd(resource, VERSIONS);

    Deployment dep = kc.generateDeployment(emptyMap(), true, null, singletonList(secret1));
    assertThat(dep.getSpec().getTemplate().getSpec().getImagePullSecrets().size(), is(1));
    assertThat(dep.getSpec().getTemplate().getSpec().getImagePullSecrets().contains(secret1), is(false));
    assertThat(dep.getSpec().getTemplate().getSpec().getImagePullSecrets().contains(secret2), is(true));
}
 
Example #22
Source File: ReadinessProbeTest.java    From module-ballerina-kubernetes with Apache License 2.0 6 votes vote down vote up
/**
 * Build bal file with deployment having readiness disabled.
 *
 * @throws IOException               Error when loading the generated yaml.
 * @throws InterruptedException      Error when compiling the ballerina file.
 * @throws KubernetesPluginException Error when deleting the generated artifacts folder.
 */
@Test
public void disabledTest() throws IOException, InterruptedException, KubernetesPluginException,
        DockerTestException {
    Assert.assertEquals(KubernetesTestUtils.compileBallerinaFile(BAL_DIRECTORY, "disabled.bal"), 0);

    // Check if docker image exists and correct
    validateDockerfile();
    validateDockerImage();

    // Validate deployment yaml
    File deploymentYAML = KUBERNETES_TARGET_PATH.resolve("disabled_deployment.yaml").toFile();
    Assert.assertTrue(deploymentYAML.exists());
    Deployment deployment = KubernetesTestUtils.loadYaml(deploymentYAML);
    Assert.assertNotNull(deployment.getSpec());
    Assert.assertNotNull(deployment.getSpec().getTemplate());
    Assert.assertNotNull(deployment.getSpec().getTemplate().getSpec());
    Assert.assertTrue(deployment.getSpec().getTemplate().getSpec().getContainers().size() > 0);
    Assert.assertNull(deployment.getSpec().getTemplate().getSpec().getContainers().get(0).getReadinessProbe());

    KubernetesUtils.deleteDirectory(KUBERNETES_TARGET_PATH);
    KubernetesUtils.deleteDirectory(DOCKER_TARGET_PATH);
    KubernetesTestUtils.deleteDockerImage(DOCKER_IMAGE);
}
 
Example #23
Source File: Readiness.java    From kubernetes-client with Apache License 2.0 6 votes vote down vote up
public static boolean isReady(HasMetadata item) {
  if (item instanceof Deployment) {
    return isDeploymentReady((Deployment) item);
  } else if (item instanceof ReplicaSet) {
    return isReplicaSetReady((ReplicaSet) item);
  } else if (item instanceof Pod) {
    return isPodReady((Pod) item);
  } else if (item instanceof DeploymentConfig) {
    return isDeploymentConfigReady((DeploymentConfig) item);
  } else if (item instanceof ReplicationController) {
    return isReplicationControllerReady((ReplicationController) item);
  } else if (item instanceof Endpoints) {
    return isEndpointsReady((Endpoints) item);
  } else if (item instanceof Node) {
    return isNodeReady((Node) item);
  } else if (item instanceof StatefulSet) {
    return isStatefulSetReady((StatefulSet) item);
  } else {
    throw new IllegalArgumentException("Item needs to be one of [Node, Deployment, ReplicaSet, StatefulSet, Pod, DeploymentConfig, ReplicationController], but was: [" + (item != null ? item.getKind() : "Unknown (null)") + "]");
  }
}
 
Example #24
Source File: KafkaConnectClusterTest.java    From strimzi-kafka-operator with Apache License 2.0 5 votes vote down vote up
@Test
public void testKafkaConnectContainerSecurityContext() {

    SecurityContext securityContext = new SecurityContextBuilder()
            .withPrivileged(false)
            .withNewReadOnlyRootFilesystem(false)
            .withAllowPrivilegeEscalation(false)
            .withRunAsNonRoot(true)
            .withNewCapabilities()
                .addNewDrop("ALL")
            .endCapabilities()
            .build();

    KafkaConnect resource = new KafkaConnectBuilder(this.resource)
            .editSpec()
                .editOrNewTemplate()
                    .withNewConnectContainer()
                        .withSecurityContext(securityContext)
                    .endConnectContainer()
                .endTemplate()
            .endSpec()
            .build();

    KafkaConnectCluster kcc = KafkaConnectCluster.fromCrd(resource, VERSIONS);
    Deployment deployment = kcc.generateDeployment(null, false, null, null);

    assertThat(deployment.getSpec().getTemplate().getSpec().getContainers(),
            hasItem(allOf(
                    hasProperty("name", equalTo(cluster + "-connect")),
                    hasProperty("securityContext", equalTo(securityContext))
            )));
}
 
Example #25
Source File: ContainerSearch.java    From che with Eclipse Public License 2.0 5 votes vote down vote up
private Stream<Container> findContainers(HasMetadata o) {
  // hopefully, this covers all types of objects that can contain a container
  if (o instanceof Pod) {
    return ((Pod) o).getSpec().getContainers().stream();
  } else if (o instanceof PodTemplate) {
    return ((PodTemplate) o).getTemplate().getSpec().getContainers().stream();
  } else if (o instanceof DaemonSet) {
    return ((DaemonSet) o).getSpec().getTemplate().getSpec().getContainers().stream();
  } else if (o instanceof Deployment) {
    return ((Deployment) o).getSpec().getTemplate().getSpec().getContainers().stream();
  } else if (o instanceof Job) {
    return ((Job) o).getSpec().getTemplate().getSpec().getContainers().stream();
  } else if (o instanceof ReplicaSet) {
    return ((ReplicaSet) o).getSpec().getTemplate().getSpec().getContainers().stream();
  } else if (o instanceof ReplicationController) {
    return ((ReplicationController) o).getSpec().getTemplate().getSpec().getContainers().stream();
  } else if (o instanceof StatefulSet) {
    return ((StatefulSet) o).getSpec().getTemplate().getSpec().getContainers().stream();
  } else if (o instanceof CronJob) {
    return ((CronJob) o)
        .getSpec()
        .getJobTemplate()
        .getSpec()
        .getTemplate()
        .getSpec()
        .getContainers()
        .stream();
  } else if (o instanceof DeploymentConfig) {
    return ((DeploymentConfig) o).getSpec().getTemplate().getSpec().getContainers().stream();
  } else if (o instanceof Template) {
    return ((Template) o).getObjects().stream().flatMap(this::findContainers);
  } else {
    return Stream.empty();
  }
}
 
Example #26
Source File: KafkaConnectClusterTest.java    From strimzi-kafka-operator with Apache License 2.0 5 votes vote down vote up
@Test
public void testDefaultGracePeriod() {
    KafkaConnect resource = new KafkaConnectBuilder(this.resource).build();
    KafkaConnectCluster kc = KafkaConnectCluster.fromCrd(resource, VERSIONS);

    Deployment dep = kc.generateDeployment(emptyMap(), true, null, null);
    assertThat(dep.getSpec().getTemplate().getSpec().getTerminationGracePeriodSeconds(), is(Long.valueOf(30)));
}
 
Example #27
Source File: DeploymentNameTransformer.java    From apollo with Apache License 2.0 5 votes vote down vote up
@Override
public Deployment transform(Deployment deployment,
                            io.logz.apollo.models.Deployment apolloDeployment,
                            Service apolloService,
                            Environment apolloEnvironment,
                            DeployableVersion apolloDeployableVersion,
                            Group apolloGroup) {

    if (apolloGroup != null) {
        deployment.getMetadata().setName(deployment.getMetadata().getName() + "-" + apolloGroup.getName());
    }
    return deployment;
}
 
Example #28
Source File: KafkaMirrorMaker2ClusterTest.java    From strimzi-kafka-operator with Apache License 2.0 5 votes vote down vote up
@Test
public void testGenerateDeploymentWithTlsSameSecret() {
    KafkaMirrorMaker2ClusterSpec targetClusterWithTlsAuth = new KafkaMirrorMaker2ClusterSpecBuilder(this.targetCluster)
            .editOrNewTls()
            .addToTrustedCertificates(new CertSecretSourceBuilder().withSecretName("my-secret").withCertificate("cert.crt").build())
            .endTls()
            .withAuthentication(
                    new KafkaClientAuthenticationTlsBuilder()
                            .withNewCertificateAndKey()
                            .withSecretName("my-secret")
                            .withCertificate("user.crt")
                            .withKey("user.key")
                            .endCertificateAndKey()
                            .build())
            .build();

    KafkaMirrorMaker2 resource = new KafkaMirrorMaker2Builder(this.resource)
            .editSpec()
                .withClusters(targetClusterWithTlsAuth)
            .endSpec()
            .build();
    KafkaMirrorMaker2Cluster kmm2 = KafkaMirrorMaker2Cluster.fromCrd(resource, VERSIONS);
    Deployment dep = kmm2.generateDeployment(emptyMap(), true, null, null);

    // 3 = 1 volume from logging/metrics + 2 from above cert mounted for connect and for connectors
    assertThat(dep.getSpec().getTemplate().getSpec().getVolumes().size(), is(3));
    assertThat(dep.getSpec().getTemplate().getSpec().getVolumes().get(1).getName(), is("my-secret"));
}
 
Example #29
Source File: KafkaConnectClusterTest.java    From strimzi-kafka-operator with Apache License 2.0 5 votes vote down vote up
@Test
public void testImagePullPolicy() {
    KafkaConnectCluster kc = KafkaConnectCluster.fromCrd(resource, VERSIONS);

    Deployment dep = kc.generateDeployment(Collections.EMPTY_MAP, true, ImagePullPolicy.ALWAYS, null);
    assertThat(dep.getSpec().getTemplate().getSpec().getContainers().get(0).getImagePullPolicy(), is(ImagePullPolicy.ALWAYS.toString()));

    dep = kc.generateDeployment(Collections.EMPTY_MAP, true, ImagePullPolicy.IFNOTPRESENT, null);
    assertThat(dep.getSpec().getTemplate().getSpec().getContainers().get(0).getImagePullPolicy(), is(ImagePullPolicy.IFNOTPRESENT.toString()));
}
 
Example #30
Source File: Sample8Test.java    From module-ballerina-kubernetes with Apache License 2.0 5 votes vote down vote up
@BeforeClass
public void compileSample() throws IOException, InterruptedException {
    Assert.assertEquals(KubernetesTestUtils.compileBallerinaFile(SOURCE_DIR_PATH, "hello_world_config_map_k8s.bal")
            , 0);
    File artifactYaml = KUBERNETES_TARGET_PATH.resolve("hello_world_config_map_k8s.yaml").toFile();
    Assert.assertTrue(artifactYaml.exists());
    KubernetesClient client = new DefaultKubernetesClient();
    List<HasMetadata> k8sItems = client.load(new FileInputStream(artifactYaml)).get();
    for (HasMetadata data : k8sItems) {
        switch (data.getKind()) {
            case "Deployment":
                deployment = (Deployment) data;
                break;
            case "ConfigMap":
                switch (data.getMetadata().getName()) {
                    case "helloworld-ballerina-conf-config-map":
                        ballerinaConf = (ConfigMap) data;
                        break;
                    case "helloworld-config-map":
                        dataMap = (ConfigMap) data;
                        break;
                    default:
                        break;
                }
                break;
            case "Service":
            case "Secret":
            case "Ingress":
                break;
            default:
                Assert.fail("Unexpected k8s resource found: " + data.getKind());
                break;
        }
    }
}