Java Code Examples for io.fabric8.kubernetes.api.model.Container#setImage()

The following examples show how to use io.fabric8.kubernetes.api.model.Container#setImage() . 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: 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 2
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 3
Source File: ReplicaSetOperationsImpl.java    From kubernetes-client with Apache License 2.0 6 votes vote down vote up
@Override
public ReplicaSet updateImage(Map<String, String> containerToImageMap) {
  ReplicaSet replicationController = get();
  if (replicationController == null) {
    throw new KubernetesClientException("Existing replica set doesn't exist");
  }
  if (replicationController.getSpec().getTemplate().getSpec().getContainers().isEmpty()) {
    throw new KubernetesClientException("Pod has no containers!");
  }

  List<Container> containers = replicationController.getSpec().getTemplate().getSpec().getContainers();
  for (Container container : containers) {
    if (containerToImageMap.containsKey(container.getName())) {
      container.setImage(containerToImageMap.get(container.getName()));
    }
  }
  replicationController.getSpec().getTemplate().getSpec().setContainers(containers);
  return sendPatchedObject(get(), replicationController);
}
 
Example 4
Source File: StatefulSetOperationsImpl.java    From kubernetes-client with Apache License 2.0 6 votes vote down vote up
@Override
public StatefulSet updateImage(Map<String, String> containerToImageMap) {
  StatefulSet statefulSet = get();
  if (statefulSet == null) {
    throw new KubernetesClientException("Existing replica set doesn't exist");
  }
  if (statefulSet.getSpec().getTemplate().getSpec().getContainers().isEmpty()) {
    throw new KubernetesClientException("Pod has no containers!");
  }

  List<Container> containers = statefulSet.getSpec().getTemplate().getSpec().getContainers();
  for (Container container : containers) {
    if (containerToImageMap.containsKey(container.getName())) {
      container.setImage(containerToImageMap.get(container.getName()));
    }
  }
  statefulSet.getSpec().getTemplate().getSpec().setContainers(containers);
  return sendPatchedObject(get(), statefulSet);
}
 
Example 5
Source File: ReplicationControllerOperationsImpl.java    From kubernetes-client with Apache License 2.0 6 votes vote down vote up
@Override
public ReplicationController updateImage(Map<String, String> containerToImageMap) {
  ReplicationController replicationController = get();
  if (replicationController == null) {
    throw new KubernetesClientException("Existing replica set doesn't exist");
  }
  if (replicationController.getSpec().getTemplate().getSpec().getContainers().isEmpty()) {
    throw new KubernetesClientException("Pod has no containers!");
  }

  List<Container> containers = replicationController.getSpec().getTemplate().getSpec().getContainers();
  for (Container container : containers) {
    if (containerToImageMap.containsKey(container.getName())) {
      container.setImage(containerToImageMap.get(container.getName()));
    }
  }
  replicationController.getSpec().getTemplate().getSpec().setContainers(containers);
  return sendPatchedObject(get(), replicationController);
}
 
Example 6
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 7
Source File: ImageEnricher.java    From jkube with Eclipse Public License 2.0 5 votes vote down vote up
private void mergeImage(ImageConfiguration imageConfiguration, Container container) {
    if (StringUtils.isBlank(container.getImage())) {
        String prefix = "";
        if (StringUtils.isNotBlank(imageConfiguration.getRegistry())) {
            log.verbose("Using registry %s for the image", imageConfiguration.getRegistry());
            prefix = imageConfiguration.getRegistry() + "/";
        }
        String imageFullName = prefix + imageConfiguration.getName();
        log.verbose("Setting image %s", imageFullName);
        container.setImage(imageFullName);
    }
}
 
Example 8
Source File: KubernetesResource.java    From camel-quarkus with Apache License 2.0 5 votes vote down vote up
private PodSpec createPodSpec(String containerName) {
    PodSpec podSpec = new PodSpec();

    Container container = new Container();
    container.setImage("docker.io/busybox:latest");
    container.setName(containerName);

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

    podSpec.setContainers(containers);

    return podSpec;
}
 
Example 9
Source File: ApplyStepExecution.java    From kubernetes-pipeline-plugin with Apache License 2.0 5 votes vote down vote up
private void prefixRegistryIfNotPresent(List<Container> containers, String registry) {
    for (Container container : containers) {
        if (!hasRegistry(container.getImage())){
            container.setImage(registry+"/"+container.getImage());
        }
    }
}
 
Example 10
Source File: KubernetesTest.java    From camel-quarkus with Apache License 2.0 4 votes vote down vote up
@Test
public void testKubernetesComponent() {
    Container container = new Container();
    container.setImage("docker.io/busybox:latest");
    container.setName("camel-pod");

    Pod pod = new PodBuilder()
            .withNewMetadata()
            .withName("camel-pod")
            .withNamespace("test")
            .and()
            .withNewSpec()
            .withContainers(container)
            .and()
            .build();

    mockServer.expect()
            .post()
            .withPath("/api/v1/namespaces/test/pods")
            .andReturn(201, pod)
            .once();

    mockServer.expect()
            .get()
            .withPath("/api/v1/namespaces/test/pods/camel-pod")
            .andReturn(200, pod)
            .always();

    mockServer.expect()
            .delete()
            .withPath("/api/v1/namespaces/test/pods/camel-pod")
            .andReturn(200, "{}")
            .once();

    RestAssured.when()
            .post("/kubernetes/pod/test/camel-pod")
            .then()
            .statusCode(201);

    RestAssured.when()
            .get("/kubernetes/pod/test/camel-pod")
            .then()
            .statusCode(200)
            .body(is("camel-pod"));

    RestAssured.when()
            .delete("/kubernetes/pod/test/camel-pod")
            .then()
            .statusCode(204);
}
 
Example 11
Source File: KubernetesSchedulerTests.java    From spring-cloud-deployer-kubernetes with Apache License 2.0 4 votes vote down vote up
@Test
public void listScheduleWithExternalCronJobs() {
	CronJobList cronJobList = new CronJobList();
	CronJobSpec cronJobSpec = new CronJobSpec();
	JobTemplateSpec jobTemplateSpec = new JobTemplateSpec();
	JobSpec jobSpec = new JobSpec();
	PodTemplateSpec podTemplateSpec = new PodTemplateSpec();
	PodSpec podSpec = new PodSpec();
	Container container = new Container();
	container.setName("test");
	container.setImage("busybox");
	podSpec.setContainers(Arrays.asList(container));
	podSpec.setRestartPolicy("OnFailure");
	podTemplateSpec.setSpec(podSpec);
	jobSpec.setTemplate(podTemplateSpec);
	jobTemplateSpec.setSpec(jobSpec);
	cronJobSpec.setJobTemplate(jobTemplateSpec);
	cronJobSpec.setSchedule("0/10 * * * *");

	CronJob cronJob1 = new CronJob();
	ObjectMeta objectMeta1 = new ObjectMeta();
	Map<String, String> labels = new HashMap<>();
	labels.put("spring-cronjob-id", "test");
	objectMeta1.setLabels(labels);
	objectMeta1.setName("job1");
	cronJob1.setMetadata(objectMeta1);
	cronJob1.setSpec(cronJobSpec);
	ObjectMeta objectMeta2 = new ObjectMeta();
	objectMeta2.setName("job2");
	CronJob cronJob2 = new CronJob();
	cronJob2.setSpec(cronJobSpec);
	cronJob2.setMetadata(objectMeta2);
	ObjectMeta objectMeta3 = new ObjectMeta();
	objectMeta3.setName("job3");
	CronJob cronJob3 = new CronJob();
	cronJob3.setSpec(cronJobSpec);
	cronJob3.setMetadata(objectMeta3);
	cronJobList.setItems(Arrays.asList(cronJob1, cronJob2, cronJob3));
	this.kubernetesClient.batch().cronjobs().create(cronJob1);
	this.kubernetesClient.batch().cronjobs().create(cronJob2);
	this.kubernetesClient.batch().cronjobs().create(cronJob3);
	List<ScheduleInfo> scheduleInfos = this.scheduler.list();
	assertThat(scheduleInfos.size() == 1);
	assertThat(scheduleInfos.get(0).getScheduleName().equals("job1"));
}