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

The following examples show how to use io.fabric8.kubernetes.api.model.PodTemplate. 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: VolumePermissionEnricherTest.java    From jkube with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void alreadyExistingInitContainer(@Mocked final ProcessorConfig config) throws Exception {
    new Expectations() {{
        context.getConfiguration(); result = Configuration.builder().processorConfig(config).build();
    }};

    PodTemplateBuilder ptb = createEmptyPodTemplate();
    addVolume(ptb, "VolumeA");

    Container initContainer = new ContainerBuilder()
            .withName(VolumePermissionEnricher.ENRICHER_NAME)
            .withVolumeMounts(new VolumeMountBuilder().withName("vol-blub").withMountPath("blub").build())
            .build();
    ptb.editTemplate().editSpec().withInitContainers(Collections.singletonList(initContainer)).endSpec().endTemplate();
    KubernetesListBuilder klb = new KubernetesListBuilder().addToPodTemplateItems(ptb.build());

    VolumePermissionEnricher enricher = new VolumePermissionEnricher(context);
    enricher.enrich(PlatformMode.kubernetes,klb);

    List<Container> initS = ((PodTemplate) klb.build().getItems().get(0)).getTemplate().getSpec().getInitContainers();
    assertNotNull(initS);
    assertEquals(1, initS.size());
    Container actualInitContainer = initS.get(0);
    assertEquals("blub", actualInitContainer.getVolumeMounts().get(0).getMountPath());
}
 
Example #2
Source File: PodTemplateTest.java    From kubernetes-client with Apache License 2.0 6 votes vote down vote up
@Test
@DisplayName("Should create a PodTemplate")
public void testCreate() {
  // Given
  PodTemplate pt1 = getPodTemplate();
  server.expect().post().withPath("/api/v1/namespaces/test/podtemplates")
    .andReturn(200, pt1)
    .once();
  KubernetesClient client = server.getClient();

  // When
  PodTemplate result = client.v1().podTemplates().inNamespace("test").create(pt1);

  // Then
  assertEquals("pt1", result.getMetadata().getName());
  assertEquals(1, result.getTemplate().getSpec().getContainers().size());
}
 
Example #3
Source File: PodTemplateTest.java    From kubernetes-client with Apache License 2.0 6 votes vote down vote up
@Test
@DisplayName("Should fetch PodTemplate from apiServer")
public void testGet() {
  // Given
  server.expect().get().withPath("/api/v1/namespaces/test/podtemplates/pt1")
    .andReturn(200, getPodTemplate())
    .once();
  KubernetesClient client = server.getClient();

  // When
  PodTemplate result = client.v1().podTemplates().inNamespace("test").withName("pt1").get();

  // Then
  assertEquals("pt1", result.getMetadata().getName());
  assertEquals(1, result.getTemplate().getSpec().getContainers().size());
}
 
Example #4
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 #5
Source File: PodTemplateTest.java    From kubernetes-client with Apache License 2.0 5 votes vote down vote up
@Test
@DisplayName("Should load a PodTemplate from yaml")
public void testLoad() {
  KubernetesClient client = server.getClient();
  PodTemplate podTemplate = client.v1().podTemplates().load(getClass().getResourceAsStream("/test-podtemplate.yml")).get();
  assertNotNull(podTemplate);
  assertEquals("hello", podTemplate.getMetadata().getName());
  assertEquals(1, podTemplate.getTemplate().getSpec().getContainers().size());
}
 
Example #6
Source File: PodTemplateTest.java    From kubernetes-client with Apache License 2.0 5 votes vote down vote up
@Test
@DisplayName("Should be able to edit a PodTemplate")
public void testEdit() {
  // Given
  PodTemplate updatedPodTemplate = new PodTemplateBuilder()
    .withNewMetadata()
    .withName("pt1")
    .addToLabels("foo", "bar")
    .endMetadata()
    .withNewTemplate()
    .withNewSpec()
    .addNewContainer()
    .withName("foo")
    .withImage("docker.io/matzew/eventing-display@sha256:f1c948343622a75b5f7a9058aacdffd2dc1732e07a339477f7d1d6ef09da872a")
    .endContainer()
    .endSpec()
    .endTemplate()
    .build();
  server.expect().get().withPath("/api/v1/namespaces/test/podtemplates/pt1")
    .andReturn(200, getPodTemplate())
    .times(3);
  server.expect().patch().withPath("/api/v1/namespaces/test/podtemplates/pt1")
    .andReturn(200, updatedPodTemplate)
    .once();
  KubernetesClient client = server.getClient();

  // When
  PodTemplate podTemplate = client.v1().podTemplates().inNamespace("test").withName("pt1").edit().editMetadata().addToLabels("foo", "bar").endMetadata().done();

  // Then
  assertEquals("pt1", podTemplate.getMetadata().getName());
  assertEquals("bar", podTemplate.getMetadata().getLabels().get("foo"));
  assertEquals(1, podTemplate.getTemplate().getSpec().getContainers().size());
}
 
Example #7
Source File: PodTemplateTest.java    From kubernetes-client with Apache License 2.0 5 votes vote down vote up
private PodTemplate getPodTemplate() {
  return new PodTemplateBuilder()
    .withNewMetadata().withName("pt1").endMetadata()
    .withNewTemplate()
    .withNewSpec()
    .addNewContainer()
    .withName("foo")
    .withImage("docker.io/matzew/eventing-display@sha256:f1c948343622a75b5f7a9058aacdffd2dc1732e07a339477f7d1d6ef09da872a")
    .endContainer()
    .endSpec()
    .endTemplate()
    .build();
}
 
Example #8
Source File: AutoTLSEnricherTest.java    From jkube with Eclipse Public License 2.0 4 votes vote down vote up
@Test
public void testAdapt() throws Exception {
    final AdaptTestConfig[] data = new AdaptTestConfig[] {
            new AdaptTestConfig(RuntimeMode.KUBERNETES, null, null, null, null, null, null, null, null),
            new AdaptTestConfig(RuntimeMode.OPENSHIFT, null, "tls-jks-converter", null,
                    "jimmidyson/pemtokeystore:v0.1.0", null, "tls-pem", null, "tls-jks"),
            new AdaptTestConfig(RuntimeMode.OPENSHIFT, null, "tls-jks-converter", null,
                    "jimmidyson/pemtokeystore:v0.1.0", "tls-a", "tls-a", null, "tls-jks"),
            new AdaptTestConfig(RuntimeMode.OPENSHIFT, null, "tls-jks-converter", null,
                    "jimmidyson/pemtokeystore:v0.1.0", null, "tls-pem", "jks-b", "jks-b"),
            new AdaptTestConfig(RuntimeMode.OPENSHIFT, "test-container-name", "test-container-name", "image/123",
                    "image/123", "tls-a", "tls-a", "jks-b", "jks-b") };

    for (final AdaptTestConfig tc : data) {
        TreeMap<String, String> configMap = new TreeMap<>();
        configMap.put(AutoTLSEnricher.Config.pemToJKSInitContainerName.name(), tc.initContainerNameConfig);
        configMap.put(AutoTLSEnricher.Config.pemToJKSInitContainerImage.name(), tc.initContainerImageConfig);
        configMap.put(AutoTLSEnricher.Config.tlsSecretVolumeName.name(), tc.tlsSecretVolumeNameConfig);
        configMap.put(AutoTLSEnricher.Config.jksVolumeName.name(), tc.jksVolumeNameConfig);

        final ProcessorConfig config = new ProcessorConfig(null, null,
                Collections.singletonMap(AutoTLSEnricher.ENRICHER_NAME, configMap));

        final Properties projectProps = new Properties();
        projectProps.put(RuntimeMode.JKUBE_EFFECTIVE_PLATFORM_MODE, tc.mode.name());

        // Setup mock behaviour
        new Expectations() {
            {
                Configuration configuration = Configuration.builder()
                    .properties(projectProps)
                    .processorConfig(config)
                    .build();
                context.getConfiguration();
                result = configuration;
                project.getArtifactId();
                result = "projectA";
                minTimes = 0;
            }
        };

        AutoTLSEnricher enricher = new AutoTLSEnricher(context);
        KubernetesListBuilder klb = new KubernetesListBuilder().addNewPodTemplateItem().withNewMetadata().and()
                .withNewTemplate().withNewMetadata().and().withNewSpec().and().and().and();
        enricher.enrich(PlatformMode.kubernetes, klb);
        PodTemplate pt = (PodTemplate) klb.buildItems().get(0);

        List<Container> initContainers = pt.getTemplate().getSpec().getInitContainers();
        assertEquals(tc.mode == RuntimeMode.OPENSHIFT, !initContainers.isEmpty());

        if (tc.mode == RuntimeMode.KUBERNETES) {
            continue;
        }

        Gson gson = new Gson();
        JsonArray ja = new JsonParser().parse(gson.toJson(initContainers, new TypeToken<Collection<Container>>() {}.getType())).getAsJsonArray();
        assertEquals(1, ja.size());
        JsonObject jo = ja.get(0).getAsJsonObject();
        assertEquals(tc.initContainerName, jo.get("name").getAsString());
        assertEquals(tc.initContainerImage, jo.get("image").getAsString());
        JsonArray mounts = jo.get("volumeMounts").getAsJsonArray();
        assertEquals(2, mounts.size());
        JsonObject mount = mounts.get(0).getAsJsonObject();
        assertEquals(tc.tlsSecretVolumeName, mount.get("name").getAsString());
        mount = mounts.get(1).getAsJsonObject();
        assertEquals(tc.jksVolumeName, mount.get("name").getAsString());
    }
}
 
Example #9
Source File: KubernetesDSL.java    From kubernetes-pipeline-plugin with Apache License 2.0 4 votes vote down vote up
public PlugiWhiteList() throws IOException {
    super(ScriptBytecodeAdapter.class,
            PodTemplate.class, ContainerTemplate.class, PodEnvVar.class, ContainerEnvVar.class,
            ArrayList.class, Collection.class, HashMap.class, HashSet.class, Collections.class,
            Callable.class);
}
 
Example #10
Source File: V1APIGroupClient.java    From kubernetes-client with Apache License 2.0 4 votes vote down vote up
@Override
public MixedOperation<PodTemplate, PodTemplateList, DoneablePodTemplate, Resource<PodTemplate, DoneablePodTemplate>> podTemplates() {
  return new PodTemplateOperationsImpl(httpClient, getConfiguration());
}
 
Example #11
Source File: UtilsTest.java    From kubernetes-client with Apache License 2.0 4 votes vote down vote up
@Test
void testGetPluralFromKind() {
  // Given
  Map<String, Class> pluralToKubernetesResourceMap = new HashMap<>();
  pluralToKubernetesResourceMap.put("bindings", Binding.class);
  pluralToKubernetesResourceMap.put("componentstatuses", ComponentStatus.class);
  pluralToKubernetesResourceMap.put("configmaps", ConfigMap.class);
  pluralToKubernetesResourceMap.put("endpoints", Endpoints.class);
  pluralToKubernetesResourceMap.put("events", Event.class);
  pluralToKubernetesResourceMap.put("limitranges", LimitRange.class);
  pluralToKubernetesResourceMap.put("namespaces", Namespace.class);
  pluralToKubernetesResourceMap.put("nodes", Node.class);
  pluralToKubernetesResourceMap.put("persistentvolumeclaims", PersistentVolumeClaim.class);
  pluralToKubernetesResourceMap.put("persistentvolumes", PersistentVolume.class);
  pluralToKubernetesResourceMap.put("pods", Pod.class);
  pluralToKubernetesResourceMap.put("podtemplates", PodTemplate.class);
  pluralToKubernetesResourceMap.put("replicationcontrollers", ReplicationController.class);
  pluralToKubernetesResourceMap.put("resourcequotas", ResourceQuota.class);
  pluralToKubernetesResourceMap.put("secrets", Secret.class);
  pluralToKubernetesResourceMap.put("serviceaccounts", ServiceAccount.class);
  pluralToKubernetesResourceMap.put("services", Service.class);
  pluralToKubernetesResourceMap.put("mutatingwebhookconfigurations", MutatingWebhookConfiguration.class);
  pluralToKubernetesResourceMap.put("validatingwebhookconfigurations", ValidatingWebhookConfiguration.class);
  pluralToKubernetesResourceMap.put("customresourcedefinitions", CustomResourceDefinition.class);
  pluralToKubernetesResourceMap.put("controllerrevisions", ControllerRevision.class);
  pluralToKubernetesResourceMap.put("daemonsets", DaemonSet.class);
  pluralToKubernetesResourceMap.put("deployments", Deployment.class);
  pluralToKubernetesResourceMap.put("replicasets", ReplicaSet.class);
  pluralToKubernetesResourceMap.put("statefulsets", StatefulSet.class);
  pluralToKubernetesResourceMap.put("tokenreviews", TokenReview.class);
  pluralToKubernetesResourceMap.put("localsubjectaccessreviews", LocalSubjectAccessReview.class);
  pluralToKubernetesResourceMap.put("selfsubjectaccessreviews", SelfSubjectAccessReview.class);
  pluralToKubernetesResourceMap.put("selfsubjectrulesreviews", SelfSubjectRulesReview.class);
  pluralToKubernetesResourceMap.put("subjectaccessreviews", SubjectAccessReview.class);
  pluralToKubernetesResourceMap.put("horizontalpodautoscalers", HorizontalPodAutoscaler.class);
  pluralToKubernetesResourceMap.put("cronjobs", CronJob.class);
  pluralToKubernetesResourceMap.put("jobs", Job.class);
  pluralToKubernetesResourceMap.put("certificatesigningrequests", CertificateSigningRequest.class);
  pluralToKubernetesResourceMap.put("leases", Lease.class);
  pluralToKubernetesResourceMap.put("endpointslices", EndpointSlice.class);
  pluralToKubernetesResourceMap.put("ingresses", Ingress.class);
  pluralToKubernetesResourceMap.put("networkpolicies", NetworkPolicy.class);
  pluralToKubernetesResourceMap.put("poddisruptionbudgets", PodDisruptionBudget.class);
  pluralToKubernetesResourceMap.put("podsecuritypolicies", PodSecurityPolicy.class);
  pluralToKubernetesResourceMap.put("clusterrolebindings", ClusterRoleBinding.class);
  pluralToKubernetesResourceMap.put("clusterroles", ClusterRole.class);
  pluralToKubernetesResourceMap.put("rolebindings", RoleBinding.class);
  pluralToKubernetesResourceMap.put("roles", Role.class);
  pluralToKubernetesResourceMap.put("priorityclasses", PriorityClass.class);
  pluralToKubernetesResourceMap.put("csidrivers", CSIDriver.class);
  pluralToKubernetesResourceMap.put("csinodes", CSINode.class);
  pluralToKubernetesResourceMap.put("storageclasses", StorageClass.class);
  pluralToKubernetesResourceMap.put("volumeattachments", VolumeAttachment.class);

  // When & Then
  pluralToKubernetesResourceMap.forEach((plural, kubernetesResource)
    -> assertEquals(plural, Utils.getPluralFromKind(kubernetesResource.getSimpleName())));
}
 
Example #12
Source File: UtilsTest.java    From kubernetes-client with Apache License 2.0 4 votes vote down vote up
@Test
@DisplayName("Should test whether resource is namespaced or not")
void testWhetherNamespacedOrNot() {
  assertTrue(Utils.isResourceNamespaced(Binding.class));
  assertFalse(Utils.isResourceNamespaced(ComponentStatus.class));
  assertTrue(Utils.isResourceNamespaced(ConfigMap.class));
  assertTrue(Utils.isResourceNamespaced(Endpoints.class));
  assertTrue(Utils.isResourceNamespaced(Event.class));
  assertTrue(Utils.isResourceNamespaced(LimitRange.class));
  assertFalse(Utils.isResourceNamespaced(Namespace.class));
  assertFalse(Utils.isResourceNamespaced(Node.class));
  assertTrue(Utils.isResourceNamespaced(PersistentVolumeClaim.class));
  assertFalse(Utils.isResourceNamespaced(PersistentVolume.class));
  assertTrue(Utils.isResourceNamespaced(Pod.class));
  assertTrue(Utils.isResourceNamespaced(PodTemplate.class));
  assertTrue(Utils.isResourceNamespaced(ReplicationController.class));
  assertTrue(Utils.isResourceNamespaced(ResourceQuota.class));
  assertTrue(Utils.isResourceNamespaced(Secret.class));
  assertTrue(Utils.isResourceNamespaced(ServiceAccount.class));
  assertTrue(Utils.isResourceNamespaced(Service.class));
  assertFalse(Utils.isResourceNamespaced(MutatingWebhookConfiguration.class));
  assertFalse(Utils.isResourceNamespaced(ValidatingWebhookConfiguration.class));
  assertFalse(Utils.isResourceNamespaced(CustomResourceDefinition.class));
  assertTrue(Utils.isResourceNamespaced(ControllerRevision.class));
  assertTrue(Utils.isResourceNamespaced(DaemonSet.class));
  assertTrue(Utils.isResourceNamespaced(Deployment.class));
  assertTrue(Utils.isResourceNamespaced(ReplicaSet.class));
  assertTrue(Utils.isResourceNamespaced(StatefulSet.class));
  assertTrue(Utils.isResourceNamespaced(TokenReview.class));
  assertTrue(Utils.isResourceNamespaced(LocalSubjectAccessReview.class));
  assertTrue(Utils.isResourceNamespaced(SelfSubjectAccessReview.class));
  assertTrue(Utils.isResourceNamespaced(SelfSubjectRulesReview.class));
  assertTrue(Utils.isResourceNamespaced(SubjectAccessReview.class));
  assertTrue(Utils.isResourceNamespaced(HorizontalPodAutoscaler.class));
  assertTrue(Utils.isResourceNamespaced(CronJob.class));
  assertTrue(Utils.isResourceNamespaced(Job.class));
  assertTrue(Utils.isResourceNamespaced(CertificateSigningRequest.class));
  assertTrue(Utils.isResourceNamespaced(Lease.class));
  assertTrue(Utils.isResourceNamespaced(EndpointSlice.class));
  assertTrue(Utils.isResourceNamespaced(Ingress.class));
  assertTrue(Utils.isResourceNamespaced(NetworkPolicy.class));
  assertTrue(Utils.isResourceNamespaced(PodDisruptionBudget.class));
  assertFalse(Utils.isResourceNamespaced(PodSecurityPolicy.class));
  assertFalse(Utils.isResourceNamespaced(ClusterRoleBinding.class));
  assertFalse(Utils.isResourceNamespaced(ClusterRole.class));
  assertTrue(Utils.isResourceNamespaced(RoleBinding.class));
  assertTrue(Utils.isResourceNamespaced(Role.class));
  assertFalse(Utils.isResourceNamespaced(PriorityClass.class));
  assertTrue(Utils.isResourceNamespaced(CSIDriver.class));
  assertTrue(Utils.isResourceNamespaced(CSINode.class));
  assertFalse(Utils.isResourceNamespaced(StorageClass.class));
  assertTrue(Utils.isResourceNamespaced(VolumeAttachment.class));
}
 
Example #13
Source File: V1APIGroupDSL.java    From kubernetes-client with Apache License 2.0 votes vote down vote up
MixedOperation<PodTemplate, PodTemplateList, DoneablePodTemplate, Resource<PodTemplate, DoneablePodTemplate>> podTemplates();