use of io.fabric8.kubernetes.api.model.PodSpec in project fabric8-maven-plugin by fabric8io.
the class DeploymentOpenShiftConverter method convert.
@Override
public HasMetadata convert(HasMetadata item, boolean trimImageInContainerSpec, boolean enableAutomaticTrigger) {
Deployment resource = (Deployment) item;
DeploymentConfigBuilder builder = new DeploymentConfigBuilder();
builder.withMetadata(resource.getMetadata());
DeploymentSpec spec = resource.getSpec();
if (spec != null) {
DeploymentConfigFluent.SpecNested<DeploymentConfigBuilder> specBuilder = builder.withNewSpec();
Integer replicas = spec.getReplicas();
if (replicas != null) {
specBuilder.withReplicas(replicas);
}
Integer revisionHistoryLimit = spec.getRevisionHistoryLimit();
if (revisionHistoryLimit != null) {
specBuilder.withRevisionHistoryLimit(revisionHistoryLimit);
}
LabelSelector selector = spec.getSelector();
if (selector != null) {
Map<String, String> matchLabels = selector.getMatchLabels();
if (matchLabels != null && !matchLabels.isEmpty()) {
specBuilder.withSelector(matchLabels);
}
}
Map<String, String> containerToImageMap = new HashMap<>();
PodTemplateSpec template = spec.getTemplate();
if (template != null) {
specBuilder.withTemplate(template);
PodSpec podSpec = template.getSpec();
notNull(podSpec, "No PodSpec for PodTemplate:" + template);
List<Container> containers = podSpec.getContainers();
notNull(podSpec, "No containers for PodTemplate.spec: " + template);
for (Container container : containers) {
validateContainer(container);
containerToImageMap.put(container.getName(), container.getImage());
}
}
DeploymentStrategy strategy = spec.getStrategy();
String strategyType = null;
if (strategy != null) {
strategyType = strategy.getType();
}
if (openshiftDeployTimeoutSeconds != null && openshiftDeployTimeoutSeconds > 0) {
if (Strings.isNullOrBlank(strategyType) || "Rolling".equals(strategyType)) {
specBuilder.withNewStrategy().withType("Rolling").withNewRollingParams().withTimeoutSeconds(openshiftDeployTimeoutSeconds).endRollingParams().endStrategy();
} else if ("Recreate".equals(strategyType)) {
specBuilder.withNewStrategy().withType("Recreate").withNewRecreateParams().withTimeoutSeconds(openshiftDeployTimeoutSeconds).endRecreateParams().endStrategy();
} else {
specBuilder.withNewStrategy().withType(strategyType).endStrategy();
}
} else if (Strings.isNotBlank(strategyType)) {
// TODO is there any values we can copy across?
specBuilder.withNewStrategy().withType(strategyType).endStrategy();
}
// lets add a default trigger so that its triggered when we change its config
if (enableAutomaticTrigger) {
specBuilder.addNewTrigger().withType("ConfigChange").endTrigger();
}
// add a new image change trigger for the build stream
if (containerToImageMap.size() != 0) {
if (mode.equals(PlatformMode.openshift)) {
for (Map.Entry<String, String> entry : containerToImageMap.entrySet()) {
String containerName = entry.getKey();
ImageName image = new ImageName(entry.getValue());
String tag = image.getTag() != null ? image.getTag() : "latest";
specBuilder.addNewTrigger().withType("ImageChange").withNewImageChangeParams().withAutomatic(enableAutomaticTrigger).withNewFrom().withKind("ImageStreamTag").withName(image.getSimpleName() + ":" + tag).withNamespace(image.getUser()).endFrom().withContainerNames(containerName).endImageChangeParams().endTrigger();
}
}
if (trimImageInContainerSpec) {
/*
* In Openshift 3.7, update to container image is automatically triggering redeployments
* and those subsequent rollouts lead to RC complaining about a missing image reference.
*
* See this : https://github.com/openshift/origin/issues/18406#issuecomment-364090247
*
* this the time it gets fixed. Do this:
* Since we're using ImageTrigger here, set container image to " ". If there is any config
* change never set to image else than " "; so doing oc apply/rollouts won't be creating
* re-deployments again and again.
*
*/
List<Container> containers = template.getSpec().getContainers();
for (Integer nIndex = 0; nIndex < containers.size(); nIndex++) {
containers.get(nIndex).setImage(" ");
}
template.getSpec().setContainers(containers);
specBuilder.withTemplate(template);
}
}
specBuilder.endSpec();
}
return builder.build();
}
use of io.fabric8.kubernetes.api.model.PodSpec in project fabric8 by fabric8io.
the class Example method listPods.
protected static void listPods(KubernetesClient kube) {
System.out.println("\n\nLooking up pods");
System.out.println("=========================================================================");
PodList pods = kube.pods().list();
// System.out.println("Got pods: " + pods);
List<Pod> items = pods.getItems();
for (Pod item : items) {
System.out.println("Pod " + KubernetesHelper.getName(item) + " with ip: " + item.getStatus().getPodIP() + " created: " + item.getMetadata().getCreationTimestamp());
PodSpec spec = item.getSpec();
if (spec != null) {
List<Container> containers = spec.getContainers();
if (containers != null) {
for (Container container : containers) {
System.out.println("Container " + container.getImage() + " " + container.getCommand() + " ports: " + container.getPorts());
}
}
}
Map<String, ContainerStatus> currentContainers = KubernetesHelper.getCurrentContainers(item);
System.out.println("Has " + currentContainers.size() + " container(s)");
Set<Map.Entry<String, ContainerStatus>> entries = currentContainers.entrySet();
for (Map.Entry<String, ContainerStatus> entry : entries) {
String id = entry.getKey();
ContainerStatus info = entry.getValue();
System.out.println("Current container: " + id + " info: " + info);
}
}
System.out.println();
}
use of io.fabric8.kubernetes.api.model.PodSpec in project fabric8 by fabric8io.
the class Controller method doCreateReplicationController.
protected void doCreateReplicationController(ReplicationController replicationController, String namespace, String sourceName) {
LOG.info("Creating a ReplicationController from " + sourceName + " namespace " + namespace + " name " + getName(replicationController));
try {
// lets check that if secrets are required they exist
ReplicationControllerSpec spec = replicationController.getSpec();
if (spec != null) {
PodTemplateSpec template = spec.getTemplate();
if (template != null) {
PodSpec podSpec = template.getSpec();
validatePodSpec(podSpec, namespace);
}
}
Object answer;
if (Strings.isNotBlank(namespace)) {
answer = kubernetesClient.replicationControllers().inNamespace(namespace).create(replicationController);
} else {
answer = kubernetesClient.replicationControllers().inNamespace(getNamespace()).create(replicationController);
}
logGeneratedEntity("Created ReplicationController: ", namespace, replicationController, answer);
} catch (Exception e) {
onApplyError("Failed to create ReplicationController from " + sourceName + ". " + e + ". " + replicationController, e);
}
}
use of io.fabric8.kubernetes.api.model.PodSpec in project camel by apache.
the class KubernetesNodesConsumerTest method createAndDeletePod.
@Test
public void createAndDeletePod() throws Exception {
if (ObjectHelper.isEmpty(authToken)) {
return;
}
mockResultEndpoint.expectedMessageCount(1);
mockResultEndpoint.expectedHeaderValuesReceivedInAnyOrder(KubernetesConstants.KUBERNETES_EVENT_ACTION, "MODIFIED");
Exchange ex = template.request("direct:createPod", new Processor() {
@Override
public void process(Exchange exchange) throws Exception {
exchange.getIn().setHeader(KubernetesConstants.KUBERNETES_NAMESPACE_NAME, "default");
exchange.getIn().setHeader(KubernetesConstants.KUBERNETES_POD_NAME, "test");
Map<String, String> labels = new HashMap<String, String>();
labels.put("this", "rocks");
exchange.getIn().setHeader(KubernetesConstants.KUBERNETES_PODS_LABELS, labels);
PodSpec podSpec = new PodSpec();
podSpec.setHostname("localhost");
Container cont = new Container();
cont.setImage("docker.io/jboss/wildfly:latest");
cont.setName("pippo");
List<ContainerPort> containerPort = new ArrayList<ContainerPort>();
ContainerPort port = new ContainerPort();
port.setHostIP("0.0.0.0");
port.setHostPort(8080);
port.setContainerPort(8080);
containerPort.add(port);
cont.setPorts(containerPort);
List<Container> list = new ArrayList<Container>();
list.add(cont);
podSpec.setContainers(list);
exchange.getIn().setHeader(KubernetesConstants.KUBERNETES_POD_SPEC, podSpec);
}
});
ex = template.request("direct:deletePod", new Processor() {
@Override
public void process(Exchange exchange) throws Exception {
exchange.getIn().setHeader(KubernetesConstants.KUBERNETES_NAMESPACE_NAME, "default");
exchange.getIn().setHeader(KubernetesConstants.KUBERNETES_POD_NAME, "test");
}
});
boolean podDeleted = ex.getOut().getBody(Boolean.class);
assertTrue(podDeleted);
Thread.sleep(3000);
mockResultEndpoint.assertIsSatisfied();
}
use of io.fabric8.kubernetes.api.model.PodSpec in project camel by apache.
the class KubernetesPodsProducerTest method createAndDeletePod.
@Test
public void createAndDeletePod() throws Exception {
if (ObjectHelper.isEmpty(authToken)) {
return;
}
Exchange ex = template.request("direct:createPod", new Processor() {
@Override
public void process(Exchange exchange) throws Exception {
exchange.getIn().setHeader(KubernetesConstants.KUBERNETES_NAMESPACE_NAME, "default");
exchange.getIn().setHeader(KubernetesConstants.KUBERNETES_POD_NAME, "test");
Map<String, String> labels = new HashMap<String, String>();
labels.put("this", "rocks");
exchange.getIn().setHeader(KubernetesConstants.KUBERNETES_PODS_LABELS, labels);
PodSpec podSpec = new PodSpec();
podSpec.setHostname("localhost");
Container cont = new Container();
cont.setImage("docker.io/jboss/wildfly:latest");
cont.setName("pippo");
List<ContainerPort> containerPort = new ArrayList<ContainerPort>();
ContainerPort port = new ContainerPort();
port.setHostIP("0.0.0.0");
port.setHostPort(8080);
port.setContainerPort(8080);
containerPort.add(port);
cont.setPorts(containerPort);
List<Container> list = new ArrayList<Container>();
list.add(cont);
podSpec.setContainers(list);
exchange.getIn().setHeader(KubernetesConstants.KUBERNETES_POD_SPEC, podSpec);
}
});
ex = template.request("direct:deletePod", new Processor() {
@Override
public void process(Exchange exchange) throws Exception {
exchange.getIn().setHeader(KubernetesConstants.KUBERNETES_NAMESPACE_NAME, "default");
exchange.getIn().setHeader(KubernetesConstants.KUBERNETES_POD_NAME, "test");
}
});
boolean podDeleted = ex.getOut().getBody(Boolean.class);
assertTrue(podDeleted);
}
Aggregations