use of io.fabric8.kubernetes.api.model.ContainerPort in project camel by apache.
the class KubernetesPodsConsumerTest method createAndDeletePod.
@Test
public void createAndDeletePod() throws Exception {
if (ObjectHelper.isEmpty(authToken)) {
return;
}
mockResultEndpoint.expectedMessageCount(3);
mockResultEndpoint.expectedHeaderValuesReceivedInAnyOrder(KubernetesConstants.KUBERNETES_EVENT_ACTION, "ADDED", "MODIFIED", "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.ContainerPort in project kubernetes by ballerinax.
the class DeploymentHandler method generate.
/**
* Generate kubernetes deployment definition from annotation.
*
* @return Generated kubernetes @{@link Deployment} definition
* @throws KubernetesPluginException If an error occurs while generating artifact.
*/
public String generate() throws KubernetesPluginException {
List<ContainerPort> containerPorts = null;
if (deploymentModel.getPorts() != null) {
containerPorts = populatePorts(deploymentModel.getPorts());
}
Container container = generateContainer(deploymentModel, containerPorts);
Deployment deployment = new DeploymentBuilder().withNewMetadata().withName(deploymentModel.getName()).withNamespace(deploymentModel.getNamespace()).withLabels(deploymentModel.getLabels()).endMetadata().withNewSpec().withReplicas(deploymentModel.getReplicas()).withNewTemplate().withNewMetadata().addToLabels(deploymentModel.getLabels()).endMetadata().withNewSpec().withContainers(container).withVolumes(populateVolume(deploymentModel)).endSpec().endTemplate().endSpec().build();
try {
return SerializationUtils.dumpWithoutRuntimeStateAsYaml(deployment);
} catch (JsonProcessingException e) {
String errorMessage = "Error while parsing yaml file for deployment: " + deploymentModel.getName();
throw new KubernetesPluginException(errorMessage, e);
}
}
use of io.fabric8.kubernetes.api.model.ContainerPort in project fabric8-maven-plugin by fabric8io.
the class DebugEnricher method enableDebugging.
private boolean enableDebugging(HasMetadata entity, PodTemplateSpec template) {
if (template != null) {
PodSpec podSpec = template.getSpec();
if (podSpec != null) {
List<Container> containers = podSpec.getContainers();
if (containers.size() > 0) {
Container container = containers.get(0);
List<EnvVar> env = container.getEnv();
if (env == null) {
env = new ArrayList<>();
}
String remoteDebugPort = KubernetesResourceUtil.getEnvVar(env, ENV_VAR_JAVA_DEBUG_PORT, ENV_VAR_JAVA_DEBUG_PORT_DEFAULT);
boolean enabled = false;
if (KubernetesResourceUtil.setEnvVar(env, ENV_VAR_JAVA_DEBUG, "true")) {
container.setEnv(env);
enabled = true;
}
List<ContainerPort> ports = container.getPorts();
if (ports == null) {
ports = new ArrayList<>();
}
if (KubernetesResourceUtil.addPort(ports, remoteDebugPort, "debug", log)) {
container.setPorts(ports);
enabled = true;
}
if (enabled) {
log.info("Enabling debug on " + getKind(entity) + " " + KubernetesHelper.getName(entity) + " due to the property: " + ENABLE_DEBUG_MAVEN_PROPERTY);
return true;
}
}
}
}
return false;
}
use of io.fabric8.kubernetes.api.model.ContainerPort in project fabric8-maven-plugin by fabric8io.
the class KubernetesResourceUtil method ensureHasPort.
private static void ensureHasPort(Container container, ContainerPort port) {
List<ContainerPort> ports = container.getPorts();
if (ports == null) {
ports = new ArrayList<>();
container.setPorts(ports);
}
for (ContainerPort cp : ports) {
String n1 = cp.getName();
String n2 = port.getName();
if (n1 != null && n2 != null && n1.equals(n2)) {
return;
}
Integer p1 = cp.getContainerPort();
Integer p2 = port.getContainerPort();
if (p1 != null && p2 != null && p1.intValue() == p2.intValue()) {
return;
}
}
ports.add(port);
}
use of io.fabric8.kubernetes.api.model.ContainerPort in project fabric8-maven-plugin by fabric8io.
the class SpringBootWatcher method getPortForwardUrl.
private String getPortForwardUrl(final Set<HasMetadata> resources) throws Exception {
LabelSelector selector = KubernetesResourceUtil.getPodLabelSelector(resources);
if (selector == null) {
log.warn("Unable to determine a selector for application pods");
return null;
}
Properties properties = SpringBootUtil.getSpringBootApplicationProperties(getContext().getProject());
SpringBootConfigurationHelper propertyHelper = new SpringBootConfigurationHelper(SpringBootUtil.getSpringBootVersion(getContext().getProject()));
PortForwardService portForwardService = getContext().getFabric8ServiceHub().getPortForwardService();
int port = IoUtil.getFreeRandomPort();
int containerPort = findSpringBootWebPort(propertyHelper, properties);
portForwardService.forwardPortAsync(getContext().getLogger(), selector, containerPort, port);
return createForwardUrl(propertyHelper, properties, port);
}
Aggregations