use of io.fabric8.kubernetes.api.model.Service in project che by eclipse.
the class OpenShiftConnector method createContainerInfo.
/**
* Collects the relevant information from a Service, an ImageInfo, and a Pod into
* a docker ContainerInfo JSON object. The returned object is what would be returned
* by executing {@code docker inspect <container>}, with fields filled as available.
* @param svc
* @param imageInfo
* @param pod
* @param containerId
* @return
*/
private ContainerInfo createContainerInfo(Service svc, ImageInfo imageInfo, Pod pod, String containerId) {
// In Che on OpenShift, we only have one container per pod.
Container container = pod.getSpec().getContainers().get(0);
ContainerConfig imageContainerConfig = imageInfo.getContainerConfig();
// HostConfig
HostConfig hostConfig = new HostConfig();
hostConfig.setBinds(new String[0]);
hostConfig.setMemory(imageInfo.getConfig().getMemory());
// Env vars
List<String> imageEnv = Arrays.asList(imageContainerConfig.getEnv());
List<String> containerEnv = container.getEnv().stream().map(e -> String.format("%s=%s", e.getName(), e.getValue())).collect(Collectors.toList());
String[] env = Stream.concat(imageEnv.stream(), containerEnv.stream()).toArray(String[]::new);
// Exposed Ports
Map<String, List<PortBinding>> ports = getCheServicePorts(svc);
Map<String, Map<String, String>> exposedPorts = new HashMap<>();
for (String key : ports.keySet()) {
exposedPorts.put(key, Collections.emptyMap());
}
// Labels
Map<String, String> annotations = KubernetesLabelConverter.namesToLabels(svc.getMetadata().getAnnotations());
Map<String, String> containerLabels = imageInfo.getConfig().getLabels();
Map<String, String> labels = Stream.concat(annotations.entrySet().stream(), containerLabels.entrySet().stream()).filter(e -> e.getKey().startsWith(KubernetesLabelConverter.getCheServerLabelPrefix())).collect(Collectors.toMap(e -> e.getKey(), e -> e.getValue()));
// ContainerConfig
ContainerConfig config = imageContainerConfig;
config.setHostname(svc.getMetadata().getName());
config.setEnv(env);
config.setExposedPorts(exposedPorts);
config.setLabels(labels);
config.setImage(container.getImage());
// NetworkSettings
NetworkSettings networkSettings = new NetworkSettings();
networkSettings.setIpAddress(svc.getSpec().getClusterIP());
networkSettings.setGateway(svc.getSpec().getClusterIP());
networkSettings.setPorts(ports);
// Make final ContainerInfo
ContainerInfo info = new ContainerInfo();
info.setId(containerId);
info.setConfig(config);
info.setNetworkSettings(networkSettings);
info.setHostConfig(hostConfig);
info.setImage(imageInfo.getConfig().getImage());
return info;
}
use of io.fabric8.kubernetes.api.model.Service in project che by eclipse.
the class OpenShiftConnector method getCheServicePorts.
private Map<String, List<PortBinding>> getCheServicePorts(Service service) {
Map<String, List<PortBinding>> networkSettingsPorts = new HashMap<>();
List<ServicePort> servicePorts = service.getSpec().getPorts();
LOG.info("Retrieving {} ports exposed by service {}", servicePorts.size(), service.getMetadata().getName());
for (ServicePort servicePort : servicePorts) {
String protocol = servicePort.getProtocol();
String targetPort = String.valueOf(servicePort.getTargetPort().getIntVal());
String nodePort = String.valueOf(servicePort.getNodePort());
String portName = servicePort.getName();
LOG.info("Port: {}{}{} ({})", targetPort, DOCKER_PROTOCOL_PORT_DELIMITER, protocol, portName);
networkSettingsPorts.put(targetPort + DOCKER_PROTOCOL_PORT_DELIMITER + protocol.toLowerCase(), Collections.singletonList(new PortBinding().withHostIp(CHE_DEFAULT_EXTERNAL_ADDRESS).withHostPort(nodePort)));
}
return networkSettingsPorts;
}
use of io.fabric8.kubernetes.api.model.Service in project che by eclipse.
the class OpenShiftConnector method inspectContainer.
@Override
public ContainerInfo inspectContainer(String containerId) throws IOException {
Pod pod = getChePodByContainerId(containerId);
if (pod == null) {
LOG.warn("No Pod found by container ID {}", containerId);
return null;
}
List<Container> podContainers = pod.getSpec().getContainers();
if (podContainers.size() > 1) {
throw new OpenShiftException("Multiple Containers found in Pod.");
} else if (podContainers.size() < 1 || isNullOrEmpty(podContainers.get(0).getImage())) {
throw new OpenShiftException(String.format("Container %s not found", containerId));
}
String podPullSpec = podContainers.get(0).getImage();
String tagName = KubernetesStringUtils.getTagNameFromPullSpec(podPullSpec);
ImageStreamTag tag = getImageStreamTagFromRepo(tagName);
ImageInfo imageInfo = getImageInfoFromTag(tag);
String deploymentName = pod.getMetadata().getLabels().get(OPENSHIFT_DEPLOYMENT_LABEL);
if (deploymentName == null) {
LOG.warn("No label {} found for Pod {}", OPENSHIFT_DEPLOYMENT_LABEL, pod.getMetadata().getName());
return null;
}
Service svc = getCheServiceBySelector(OPENSHIFT_DEPLOYMENT_LABEL, deploymentName);
if (svc == null) {
LOG.warn("No Service found by selector {}={}", OPENSHIFT_DEPLOYMENT_LABEL, deploymentName);
return null;
}
return createContainerInfo(svc, imageInfo, pod, containerId);
}
use of io.fabric8.kubernetes.api.model.Service in project camel by apache.
the class KubernetesServicesProducer method doListServiceByLabels.
protected void doListServiceByLabels(Exchange exchange, String operation) throws Exception {
ServiceList servicesList = null;
Map<String, String> labels = exchange.getIn().getHeader(KubernetesConstants.KUBERNETES_SERVICE_LABELS, Map.class);
String namespaceName = exchange.getIn().getHeader(KubernetesConstants.KUBERNETES_NAMESPACE_NAME, String.class);
if (!ObjectHelper.isEmpty(namespaceName)) {
NonNamespaceOperation<Service, ServiceList, DoneableService, Resource<Service, DoneableService>> services;
services = getEndpoint().getKubernetesClient().services().inNamespace(namespaceName);
for (Map.Entry<String, String> entry : labels.entrySet()) {
services.withLabel(entry.getKey(), entry.getValue());
}
servicesList = services.list();
} else {
MixedOperation<Service, ServiceList, DoneableService, Resource<Service, DoneableService>> services;
services = getEndpoint().getKubernetesClient().services();
for (Map.Entry<String, String> entry : labels.entrySet()) {
services.withLabel(entry.getKey(), entry.getValue());
}
servicesList = services.list();
}
exchange.getOut().setBody(servicesList.getItems());
}
use of io.fabric8.kubernetes.api.model.Service in project camel by apache.
the class KubernetesServicesProducer method doGetService.
protected void doGetService(Exchange exchange, String operation) throws Exception {
Service service = null;
String serviceName = exchange.getIn().getHeader(KubernetesConstants.KUBERNETES_SERVICE_NAME, String.class);
String namespaceName = exchange.getIn().getHeader(KubernetesConstants.KUBERNETES_NAMESPACE_NAME, String.class);
if (ObjectHelper.isEmpty(serviceName)) {
LOG.error("Get a specific service require specify a service name");
throw new IllegalArgumentException("Get a specific service require specify a service name");
}
if (ObjectHelper.isEmpty(namespaceName)) {
LOG.error("Get a specific service require specify a namespace name");
throw new IllegalArgumentException("Get a specific service require specify a namespace name");
}
service = getEndpoint().getKubernetesClient().services().inNamespace(namespaceName).withName(serviceName).get();
exchange.getOut().setBody(service);
}
Aggregations