use of io.fabric8.knative.serving.v1.Service in project che by eclipse.
the class OpenShiftConnector method createContainer.
/**
* @param createContainerParams
* @return
* @throws IOException
*/
@Override
public ContainerCreated createContainer(CreateContainerParams createContainerParams) throws IOException {
String containerName = KubernetesStringUtils.convertToContainerName(createContainerParams.getContainerName());
String workspaceID = getCheWorkspaceId(createContainerParams);
// Generate workspaceID if CHE_WORKSPACE_ID env var does not exist
workspaceID = workspaceID.isEmpty() ? KubernetesStringUtils.generateWorkspaceID() : workspaceID;
// imageForDocker is the docker version of the image repository. It's needed for other
// OpenShiftConnector API methods, but is not acceptable as an OpenShift name
String imageForDocker = createContainerParams.getContainerConfig().getImage();
// imageStreamTagName is imageForDocker converted into a form that can be used
// in OpenShift
String imageStreamTagName = KubernetesStringUtils.convertPullSpecToTagName(imageForDocker);
// imageStreamTagName is not enough to fill out a pull spec; it is only the tag, so we
// have to get the ImageStreamTag from the tag, and then get the full ImageStreamTag name
// from that tag. This works because the tags used in Che are unique.
ImageStreamTag imageStreamTag = getImageStreamTagFromRepo(imageStreamTagName);
String imageStreamTagPullSpec = imageStreamTag.getMetadata().getName();
// Next we need to get the address of the registry where the ImageStreamTag is stored
String imageStreamName = KubernetesStringUtils.getImageStreamNameFromPullSpec(imageStreamTagPullSpec);
ImageStream imageStream = openShiftClient.imageStreams().inNamespace(openShiftCheProjectName).withName(imageStreamName).get();
if (imageStream == null) {
throw new OpenShiftException("ImageStream not found");
}
String registryAddress = imageStream.getStatus().getDockerImageRepository().split("/")[0];
// The above needs to be combined to form a pull spec that will work when defining a container.
String dockerPullSpec = String.format("%s/%s/%s", registryAddress, openShiftCheProjectName, imageStreamTagPullSpec);
Set<String> containerExposedPorts = createContainerParams.getContainerConfig().getExposedPorts().keySet();
Set<String> imageExposedPorts = inspectImage(InspectImageParams.create(imageForDocker)).getConfig().getExposedPorts().keySet();
Set<String> exposedPorts = getExposedPorts(containerExposedPorts, imageExposedPorts);
boolean runContainerAsRoot = runContainerAsRoot(imageForDocker);
String[] envVariables = createContainerParams.getContainerConfig().getEnv();
String[] volumes = createContainerParams.getContainerConfig().getHostConfig().getBinds();
Map<String, String> additionalLabels = createContainerParams.getContainerConfig().getLabels();
String containerID;
try {
createOpenShiftService(workspaceID, exposedPorts, additionalLabels);
String deploymentName = createOpenShiftDeployment(workspaceID, dockerPullSpec, containerName, exposedPorts, envVariables, volumes, runContainerAsRoot);
containerID = waitAndRetrieveContainerID(deploymentName);
if (containerID == null) {
throw new OpenShiftException("Failed to get the ID of the container running in the OpenShift pod");
}
} catch (IOException e) {
// Make sure we clean up deployment and service in case of an error -- otherwise Che can end up
// in an inconsistent state.
LOG.info("Error while creating Pod, removing deployment");
String deploymentName = CHE_OPENSHIFT_RESOURCES_PREFIX + workspaceID;
cleanUpWorkspaceResources(deploymentName);
openShiftClient.resource(imageStreamTag).delete();
throw e;
}
return new ContainerCreated(containerID, null);
}
use of io.fabric8.knative.serving.v1.Service in project che by eclipse.
the class OpenShiftConnector method getCheServiceBySelector.
private Service getCheServiceBySelector(String selectorKey, String selectorValue) {
ServiceList svcs = openShiftClient.services().inNamespace(this.openShiftCheProjectName).list();
Service svc = svcs.getItems().stream().filter(s -> s.getSpec().getSelector().containsKey(selectorKey)).filter(s -> s.getSpec().getSelector().get(selectorKey).equals(selectorValue)).findAny().orElse(null);
if (svc == null) {
LOG.warn("No Service with selector {}={} could be found", selectorKey, selectorValue);
}
return svc;
}
use of io.fabric8.knative.serving.v1.Service in project che by eclipse.
the class OpenShiftConnector method cleanUpWorkspaceResources.
private void cleanUpWorkspaceResources(String deploymentName) throws IOException {
Deployment deployment = getDeploymentByName(deploymentName);
Service service = getCheServiceBySelector(OPENSHIFT_DEPLOYMENT_LABEL, deploymentName);
if (service != null) {
LOG.info("Removing OpenShift Service {}", service.getMetadata().getName());
openShiftClient.resource(service).delete();
}
if (deployment != null) {
LOG.info("Removing OpenShift Deployment {}", deployment.getMetadata().getName());
openShiftClient.resource(deployment).delete();
}
// Wait for all pods to terminate before returning.
try {
for (int waitCount = 0; waitCount < OPENSHIFT_WAIT_POD_TIMEOUT; waitCount++) {
List<Pod> pods = openShiftClient.pods().inNamespace(openShiftCheProjectName).withLabel(OPENSHIFT_DEPLOYMENT_LABEL, deploymentName).list().getItems();
if (pods.size() == 0) {
return;
}
Thread.sleep(OPENSHIFT_WAIT_POD_DELAY);
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
LOG.info("Thread interrupted while cleaning up workspace");
}
throw new OpenShiftException("Timeout while waiting for pods to terminate");
}
use of io.fabric8.knative.serving.v1.Service in project camel by apache.
the class KubernetesServicesProducerTest method getServiceTest.
@Test
public void getServiceTest() throws Exception {
if (ObjectHelper.isEmpty(authToken)) {
return;
}
Exchange ex = template.request("direct:getServices", new Processor() {
@Override
public void process(Exchange exchange) throws Exception {
exchange.getIn().setHeader(KubernetesConstants.KUBERNETES_NAMESPACE_NAME, "default");
exchange.getIn().setHeader(KubernetesConstants.KUBERNETES_SERVICE_NAME, "elasticsearch");
}
});
Service result = ex.getOut().getBody(Service.class);
assertNull(result);
}
use of io.fabric8.knative.serving.v1.Service in project camel by apache.
the class KubernetesServicesProducerTest method listTest.
@Test
public void listTest() throws Exception {
if (ObjectHelper.isEmpty(authToken)) {
return;
}
List<Service> result = template.requestBody("direct:list", "", List.class);
boolean fabric8Exists = false;
Iterator<Service> it = result.iterator();
while (it.hasNext()) {
Service service = it.next();
if ("fabric8".equalsIgnoreCase(service.getMetadata().getName())) {
fabric8Exists = true;
}
}
assertTrue(fabric8Exists);
}
Aggregations