Search in sources :

Example 71 with Service

use of io.fabric8.patch.Service in project fabric8 by jboss-fuse.

the class HttpGatewayHandler method handle.

@Override
public void handle(HttpServerRequest request) {
    long callStart = System.nanoTime();
    LOG.debug("Proxying request: {} {}", request.method(), request.uri());
    // lets map the request URI to map to the service URI and then the renaming URI
    // using mapping rules...
    Map<String, MappedServices> mappingRules = httpGateway.getMappedServices();
    try {
        if (isMappingIndexRequest(request)) {
            // lets return the JSON of all the results
            doReturnIndex(request, mappingRules);
        } else {
            doRouteRequest(mappingRules, request);
        }
        CallDetailRecord cdr = new CallDetailRecord(System.nanoTime() - callStart, null);
        httpGateway.addCallDetailRecord(cdr);
    } catch (Throwable e) {
        LOG.error("Caught: " + e, e);
        CallDetailRecord cdr = new CallDetailRecord(System.nanoTime() - callStart, new Date() + ":" + e.getMessage());
        httpGateway.addCallDetailRecord(cdr);
        request.response().setStatusCode(404);
        StringWriter buffer = new StringWriter();
        e.printStackTrace(new PrintWriter(buffer));
        request.response().setStatusMessage("Error: " + e + "\nStack Trace: " + buffer);
        request.response().close();
    }
}
Also used : StringWriter(java.io.StringWriter) Date(java.util.Date) CallDetailRecord(io.fabric8.gateway.CallDetailRecord) PrintWriter(java.io.PrintWriter)

Example 72 with Service

use of io.fabric8.patch.Service in project ephemerals by LivePersonInc.

the class KubernetesDeploymentHandler method deploy.

@Override
public DeploymentEndpoints deploy(Deployment deployment) {
    // Create service on KubernetesServiceBuilder cluster
    Service service = kubernetesDeploymentStrategy.service(deployment);
    kubernetesClient.services().inNamespace(kubernetesClient.getNamespace()).create(service);
    // Create pods and replication controller
    ReplicationController replicationController = kubernetesDeploymentStrategy.replicationController(deployment);
    kubernetesClient.replicationControllers().inNamespace(kubernetesClient.getNamespace()).create(replicationController);
    // Wait for deployment to finish by polling KubernetesServiceBuilder and waiting for 'FINISHED' status
    KubernetesDeploymentStatusWaiter deploymentStatusWaiter = new KubernetesDeploymentStatusWaiter(kubernetesClient, deployment, DeploymentStatus.FINISHED);
    try {
        deploymentStatusWaiter.start();
    } catch (TimeoutException e) {
        throw new DeploymentException(e);
    }
    DeploymentEndpoints endpoints = new DeploymentEndpoints();
    for (DeploymentPort deploymentPort : deployment.getDeploymentUnit().getPorts()) {
        KubernetesDeploymentEndpointWaiter kubernetesDeploymentEndpointWaiter = new KubernetesDeploymentEndpointWaiter(kubernetesClient, deployment, deploymentPort);
        try {
            kubernetesDeploymentEndpointWaiter.start();
            endpoints.add(kubernetesDeploymentEndpointWaiter.getEndpoint());
        } catch (TimeoutException e) {
            throw new DeploymentException(e);
        }
    }
    return endpoints;
}
Also used : KubernetesDeploymentStatusWaiter(com.liveperson.ephemerals.provider.kubernetes.wait.KubernetesDeploymentStatusWaiter) ReplicationController(io.fabric8.kubernetes.api.model.ReplicationController) KubernetesDeploymentEndpointWaiter(com.liveperson.ephemerals.provider.kubernetes.wait.KubernetesDeploymentEndpointWaiter) Service(io.fabric8.kubernetes.api.model.Service) TimeoutException(java.util.concurrent.TimeoutException)

Example 73 with Service

use of io.fabric8.patch.Service in project ephemerals by LivePersonInc.

the class KubernetesDeploymentHandler method destroy.

@Override
public void destroy(Deployment deployment) {
    List<ReplicationController> apps = kubernetesClient.replicationControllers().withLabel(DEPLOYMENT_LABEL_KEY, deployment.getId()).list().getItems();
    for (ReplicationController rc : apps) {
        String deploymentId = rc.getMetadata().getName();
        logger.info("Deleting service, replication controller and pods for deployment: {}", deploymentId);
        Service svc = kubernetesClient.services().withName(deploymentId).get();
        if (svc.getSpec().getType().equals(KubernetesServiceType.LOAD_BALANCER.getValue())) {
            KubernetesLoadBalancerWaiter kubernetesLoadBalancerWaiter = new KubernetesLoadBalancerWaiter(kubernetesClient, deployment);
            try {
                kubernetesLoadBalancerWaiter.start();
            } catch (TimeoutException e) {
                throw new DeploymentException(e);
            }
        }
        Boolean svcDeleted = kubernetesClient.services().withName(deploymentId).delete();
        logger.info("Deleted service for deployment: {} {}", deploymentId, svcDeleted);
        Boolean rcDeleted = kubernetesClient.replicationControllers().withName(deploymentId).delete();
        logger.info("Deleted replication controller for deployment: {} {}", deploymentId, rcDeleted);
        Map<String, String> selector = new HashMap<>();
        selector.put(DEPLOYMENT_LABEL_KEY, deploymentId);
        Boolean podDeleted = kubernetesClient.pods().withLabels(selector).delete();
        logger.info("Deleted pods for deployment: {} {}", deploymentId, podDeleted);
    }
}
Also used : HashMap(java.util.HashMap) ReplicationController(io.fabric8.kubernetes.api.model.ReplicationController) Service(io.fabric8.kubernetes.api.model.Service) KubernetesLoadBalancerWaiter(com.liveperson.ephemerals.provider.kubernetes.wait.KubernetesLoadBalancerWaiter) TimeoutException(java.util.concurrent.TimeoutException)

Example 74 with Service

use of io.fabric8.patch.Service in project halyard by spinnaker.

the class LocalDebianServiceProvider method getInstallCommand.

@Override
public String getInstallCommand(DeploymentDetails deploymentDetails, GenerateService.ResolvedConfiguration resolvedConfiguration, Map<String, String> installCommands) {
    Map<String, Object> bindings = new HashMap<>();
    List<SpinnakerService.Type> serviceTypes = new ArrayList<>(installCommands.keySet()).stream().map(SpinnakerService.Type::fromCanonicalName).collect(Collectors.toList());
    List<String> upstartNames = getLocalServices(serviceTypes).stream().filter(i -> resolvedConfiguration.getServiceSettings(i.getService()).getEnabled()).map(i -> ((LocalDebianService) i).getUpstartServiceName()).filter(Objects::nonNull).collect(Collectors.toList());
    List<String> systemdServiceConfigs = upstartNames.stream().map(n -> n + ".service").collect(Collectors.toList());
    List<String> serviceInstalls = serviceTypes.stream().map(t -> installCommands.get(t.getCanonicalName())).collect(Collectors.toList());
    TemplatedResource resource = new StringReplaceJarResource("/debian/init.sh");
    bindings.put("services", Strings.join(upstartNames, " "));
    bindings.put("systemd-service-configs", Strings.join(systemdServiceConfigs, " "));
    String upstartInit = resource.setBindings(bindings).toString();
    BillOfMaterials.ArtifactSources artifactSources = artifactService.getArtifactSources(deploymentDetails.getDeploymentName());
    resource = new StringReplaceJarResource("/debian/install.sh");
    bindings = new HashMap<>();
    bindings.put("prepare-environment", "true");
    bindings.put("install-redis", "true");
    bindings.put("debian-repository", artifactSourcesConfig.mergeWithBomSources(artifactSources).getDebianRepository());
    bindings.put("install-commands", String.join("\n", serviceInstalls));
    bindings.put("service-action", "restart");
    bindings.put("upstart-init", upstartInit);
    return resource.setBindings(bindings).toString();
}
Also used : DeploymentDetails(com.netflix.spinnaker.halyard.deploy.deployment.v1.DeploymentDetails) java.util(java.util) ArtifactSourcesConfig(com.netflix.spinnaker.halyard.config.config.v1.ArtifactSourcesConfig) Autowired(org.springframework.beans.factory.annotation.Autowired) ArtifactService(com.netflix.spinnaker.halyard.deploy.services.v1.ArtifactService) GenerateService(com.netflix.spinnaker.halyard.deploy.services.v1.GenerateService) SpinnakerRuntimeSettings(com.netflix.spinnaker.halyard.deploy.spinnaker.v1.SpinnakerRuntimeSettings) Collectors(java.util.stream.Collectors) Component(org.springframework.stereotype.Component) LocalServiceProvider(com.netflix.spinnaker.halyard.deploy.spinnaker.v1.service.local.LocalServiceProvider) StringReplaceJarResource(com.netflix.spinnaker.halyard.core.resource.v1.StringReplaceJarResource) RemoteAction(com.netflix.spinnaker.halyard.core.RemoteAction) BillOfMaterials(com.netflix.spinnaker.halyard.core.registry.v1.BillOfMaterials) SpinnakerService(com.netflix.spinnaker.halyard.deploy.spinnaker.v1.service.SpinnakerService) Strings(io.fabric8.utils.Strings) TemplatedResource(com.netflix.spinnaker.halyard.core.resource.v1.TemplatedResource) TemplatedResource(com.netflix.spinnaker.halyard.core.resource.v1.TemplatedResource) SpinnakerService(com.netflix.spinnaker.halyard.deploy.spinnaker.v1.service.SpinnakerService) BillOfMaterials(com.netflix.spinnaker.halyard.core.registry.v1.BillOfMaterials) StringReplaceJarResource(com.netflix.spinnaker.halyard.core.resource.v1.StringReplaceJarResource)

Example 75 with Service

use of io.fabric8.patch.Service in project fabric8-maven-plugin by fabric8io.

the class PortForwardServiceTest method testSimpleScenario.

@Test
public void testSimpleScenario() throws Exception {
    // Cannot test more complex scenarios due to errors in mockwebserver
    OpenShiftMockServer mockServer = new OpenShiftMockServer(false);
    Pod pod1 = new PodBuilder().withNewMetadata().withName("mypod").addToLabels("mykey", "myvalue").withResourceVersion("1").endMetadata().withNewStatus().withPhase("run").endStatus().build();
    PodList pods1 = new PodListBuilder().withItems(pod1).withNewMetadata().withResourceVersion("1").endMetadata().build();
    mockServer.expect().get().withPath("/api/v1/namespaces/test/pods?labelSelector=mykey%3Dmyvalue").andReturn(200, pods1).always();
    mockServer.expect().get().withPath("/api/v1/namespaces/test/pods").andReturn(200, pods1).always();
    mockServer.expect().get().withPath("/api/v1/namespaces/test/pods?labelSelector=mykey%3Dmyvalue&watch=true").andUpgradeToWebSocket().open().waitFor(1000).andEmit(new WatchEvent(pod1, "MODIFIED")).done().always();
    mockServer.expect().get().withPath("/api/v1/namespaces/test/pods?resourceVersion=1&watch=true").andUpgradeToWebSocket().open().waitFor(1000).andEmit(new WatchEvent(pod1, "MODIFIED")).done().always();
    OpenShiftClient client = mockServer.createOpenShiftClient();
    PortForwardService service = new PortForwardService(clientToolsService, logger, client) {

        @Override
        public ProcessUtil.ProcessExecutionContext forwardPortAsync(Logger externalProcessLogger, String pod, int remotePort, int localPort) throws Fabric8ServiceException {
            return new ProcessUtil.ProcessExecutionContext(process, Collections.<Thread>emptyList(), logger);
        }
    };
    try (Closeable c = service.forwardPortAsync(logger, new LabelSelectorBuilder().withMatchLabels(Collections.singletonMap("mykey", "myvalue")).build(), 8080, 9000)) {
        Thread.sleep(3000);
    }
}
Also used : LabelSelectorBuilder(io.fabric8.kubernetes.api.model.LabelSelectorBuilder) PodList(io.fabric8.kubernetes.api.model.PodList) Pod(io.fabric8.kubernetes.api.model.Pod) PodBuilder(io.fabric8.kubernetes.api.model.PodBuilder) Closeable(java.io.Closeable) Logger(io.fabric8.maven.docker.util.Logger) ProcessUtil(io.fabric8.maven.core.util.ProcessUtil) PodListBuilder(io.fabric8.kubernetes.api.model.PodListBuilder) OpenShiftMockServer(io.fabric8.openshift.client.server.mock.OpenShiftMockServer) OpenShiftClient(io.fabric8.openshift.client.OpenShiftClient) WatchEvent(io.fabric8.kubernetes.api.model.WatchEvent) Test(org.junit.Test)

Aggregations

Service (io.fabric8.kubernetes.api.model.Service)100 Test (org.junit.Test)78 IOException (java.io.IOException)35 ArrayList (java.util.ArrayList)35 HashMap (java.util.HashMap)33 File (java.io.File)28 OpenShiftClient (io.fabric8.openshift.client.OpenShiftClient)26 ServiceBuilder (io.fabric8.kubernetes.api.model.ServiceBuilder)24 Deployment (io.fabric8.kubernetes.api.model.extensions.Deployment)22 Map (java.util.Map)19 Pod (io.fabric8.kubernetes.api.model.Pod)18 ServicePort (io.fabric8.kubernetes.api.model.ServicePort)18 KubernetesClientException (io.fabric8.kubernetes.client.KubernetesClientException)18 ConfigMap (io.fabric8.kubernetes.api.model.ConfigMap)17 HasMetadata (io.fabric8.kubernetes.api.model.HasMetadata)17 List (java.util.List)17 KubernetesClient (io.fabric8.kubernetes.client.KubernetesClient)15 Reconciliation (io.strimzi.controller.cluster.Reconciliation)15 ConfigMapOperator (io.strimzi.controller.cluster.operator.resource.ConfigMapOperator)15 ServiceOperator (io.strimzi.controller.cluster.operator.resource.ServiceOperator)15