Search in sources :

Example 21 with ServiceBuilder

use of io.fabric8.knative.serving.v1.ServiceBuilder in project carbon-apimgt by wso2.

the class ServiceDiscovererKubernetesTestCase method init.

/**
 *  ServiceName  Namespace   Criteria  Type          Ports  LoadBalancer  ExternalIP
 *
 *  service0     dev         app=web   ClusterIP     http       -         included
 *  service1     dev         -         ExternalName  http       -             -
 *  service2     dev         -         LoadBalancer  https      IP            -
 *  service3     prod        app=web   ClusterIP     http       -             -
 *  service4     prod        -         LoadBalancer  http     hostname        -
 */
@BeforeTest(description = "Create a list of services with all combinations included")
void init() {
    Map<String, String> oneLabel = createOneLabelHashMap();
    ServicePortBuilder httpPortBuilder = new ServicePortBuilder().withName("http").withPort(80);
    ServicePort httpPort = httpPortBuilder.build();
    ServicePort nodePort2 = new ServicePortBuilder().withName("https").withPort(443).withNodePort(30002).build();
    ServicePort nodePort4 = httpPortBuilder.withNodePort(30004).build();
    List<LoadBalancerIngress> ipIngresses = new ArrayList<>();
    LoadBalancerIngress ingressWithIP = new LoadBalancerIngressBuilder().withIp("100.1.1.2").build();
    ipIngresses.add(ingressWithIP);
    List<LoadBalancerIngress> hostnameIngresses = new ArrayList<>();
    LoadBalancerIngress ingressWithHostname = new LoadBalancerIngressBuilder().withHostname("abc.com").build();
    hostnameIngresses.add(ingressWithHostname);
    Service service0 = new ServiceBuilder().withNewMetadata().withName("service0").withNamespace("dev").withLabels(oneLabel).and().withNewSpec().withType("ClusterIP").withClusterIP("1.1.1.0").withPorts(httpPort).withExternalIPs("100.2.1.0").and().build();
    Service service1 = new ServiceBuilder().withNewMetadata().withName("service1").withNamespace("dev").and().withNewSpec().withType("ExternalName").withExternalName("aaa.com").withPorts(httpPort).and().build();
    Service service2 = new ServiceBuilder().withNewMetadata().withName("service2").withNamespace("dev").and().withNewSpec().withType("LoadBalancer").withClusterIP("1.1.1.2").withPorts(nodePort2).and().withNewStatus().withNewLoadBalancer().withIngress(ipIngresses).endLoadBalancer().and().build();
    Service service3 = new ServiceBuilder().withNewMetadata().withName("service3").withNamespace("prod").withLabels(oneLabel).and().withNewSpec().withType("ClusterIP").withClusterIP("1.1.1.3").withPorts(httpPort).and().build();
    Service service4 = new ServiceBuilder().withNewMetadata().withName("service4").withNamespace("prod").and().withNewSpec().withType("LoadBalancer").withClusterIP("1.1.1.4").withPorts(nodePort4).and().withNewStatus().withNewLoadBalancer().withIngress(hostnameIngresses).endLoadBalancer().and().build();
    List<Service> servicesList = new ArrayList<>();
    servicesList.add(service0);
    servicesList.add(service1);
    servicesList.add(service2);
    servicesList.add(service3);
    servicesList.add(service4);
    this.listOfServices = servicesList;
}
Also used : ServicePort(io.fabric8.kubernetes.api.model.ServicePort) LoadBalancerIngress(io.fabric8.kubernetes.api.model.LoadBalancerIngress) ServicePortBuilder(io.fabric8.kubernetes.api.model.ServicePortBuilder) ArrayList(java.util.ArrayList) LoadBalancerIngressBuilder(io.fabric8.kubernetes.api.model.LoadBalancerIngressBuilder) Service(io.fabric8.kubernetes.api.model.Service) ServiceBuilder(io.fabric8.kubernetes.api.model.ServiceBuilder) BeforeTest(org.testng.annotations.BeforeTest)

Example 22 with ServiceBuilder

use of io.fabric8.knative.serving.v1.ServiceBuilder in project strimzi by strimzi.

the class AbstractModel method createService.

protected Service createService(String type, List<ServicePort> ports) {
    Service service = new ServiceBuilder().withNewMetadata().withName(name).withLabels(getLabelsWithName()).withNamespace(namespace).endMetadata().withNewSpec().withType(type).withSelector(getLabelsWithName()).withPorts(ports).endSpec().build();
    log.trace("Created service {}", service);
    return service;
}
Also used : Service(io.fabric8.kubernetes.api.model.Service) ServiceBuilder(io.fabric8.kubernetes.api.model.ServiceBuilder)

Example 23 with ServiceBuilder

use of io.fabric8.knative.serving.v1.ServiceBuilder in project fabric8-maven-plugin by fabric8io.

the class OpenShiftRouteEnricher method createRoutePort.

private RoutePort createRoutePort(ServiceBuilder serviceBuilder) {
    RoutePort routePort = null;
    ServiceSpec spec = serviceBuilder.getSpec();
    if (spec != null) {
        List<ServicePort> ports = spec.getPorts();
        if (ports != null && ports.size() > 0) {
            ServicePort servicePort = ports.get(0);
            if (servicePort != null) {
                IntOrString targetPort = servicePort.getTargetPort();
                if (targetPort != null) {
                    routePort = new RoutePort();
                    routePort.setTargetPort(targetPort);
                }
            }
        }
    }
    return routePort;
}
Also used : ServicePort(io.fabric8.kubernetes.api.model.ServicePort) RoutePort(io.fabric8.openshift.api.model.RoutePort) IntOrString(io.fabric8.kubernetes.api.model.IntOrString) ServiceSpec(io.fabric8.kubernetes.api.model.ServiceSpec)

Example 24 with ServiceBuilder

use of io.fabric8.knative.serving.v1.ServiceBuilder in project halyard by spinnaker.

the class KubernetesV1DistributedService method ensureRunning.

default void ensureRunning(AccountDeploymentDetails<KubernetesAccount> details, GenerateService.ResolvedConfiguration resolvedConfiguration, List<ConfigSource> configSources, boolean recreate) {
    ServiceSettings settings = resolvedConfiguration.getServiceSettings(getService());
    SpinnakerRuntimeSettings runtimeSettings = resolvedConfiguration.getRuntimeSettings();
    String namespace = getNamespace(settings);
    String serviceName = getServiceName();
    String replicaSetName = serviceName + "-v000";
    int port = settings.getPort();
    SpinnakerMonitoringDaemonService monitoringService = getMonitoringDaemonService();
    ServiceSettings monitoringSettings = runtimeSettings.getServiceSettings(monitoringService);
    KubernetesClient client = KubernetesV1ProviderUtils.getClient(details);
    KubernetesV1ProviderUtils.createNamespace(details, namespace);
    Map<String, String> serviceSelector = new HashMap<>();
    serviceSelector.put("load-balancer-" + serviceName, "true");
    Map<String, String> replicaSetSelector = new HashMap<>();
    replicaSetSelector.put("replication-controller", replicaSetName);
    Map<String, String> podLabels = new HashMap<>();
    podLabels.putAll(replicaSetSelector);
    podLabels.putAll(serviceSelector);
    Map<String, String> serviceLabels = new HashMap<>();
    serviceLabels.put("app", "spin");
    serviceLabels.put("stack", getCanonicalName());
    ServiceBuilder serviceBuilder = new ServiceBuilder();
    serviceBuilder = serviceBuilder.withNewMetadata().withName(serviceName).withNamespace(namespace).withLabels(serviceLabels).endMetadata().withNewSpec().withSelector(serviceSelector).withPorts(new ServicePortBuilder().withPort(port).withName("http").build(), new ServicePortBuilder().withPort(monitoringSettings.getPort()).withName("monitoring").build()).endSpec();
    boolean create = true;
    if (client.services().inNamespace(namespace).withName(serviceName).get() != null) {
        if (recreate) {
            client.services().inNamespace(namespace).withName(serviceName).delete();
        } else {
            create = false;
        }
    }
    if (create) {
        client.services().inNamespace(namespace).create(serviceBuilder.build());
    }
    List<Container> containers = new ArrayList<>();
    DeploymentEnvironment deploymentEnvironment = details.getDeploymentConfiguration().getDeploymentEnvironment();
    containers.add(ResourceBuilder.buildContainer(serviceName, settings, configSources, deploymentEnvironment));
    for (SidecarService sidecarService : getSidecars(runtimeSettings)) {
        String sidecarName = sidecarService.getService().getServiceName();
        ServiceSettings sidecarSettings = resolvedConfiguration.getServiceSettings(sidecarService.getService());
        containers.add(ResourceBuilder.buildContainer(sidecarName, sidecarSettings, configSources, deploymentEnvironment));
    }
    List<Volume> volumes = configSources.stream().map(c -> {
        return new VolumeBuilder().withName(c.getId()).withSecret(new SecretVolumeSourceBuilder().withSecretName(c.getId()).build()).build();
    }).collect(Collectors.toList());
    ReplicaSetBuilder replicaSetBuilder = new ReplicaSetBuilder();
    List<LocalObjectReference> imagePullSecrets = getImagePullSecrets(settings);
    Map componentSizing = deploymentEnvironment.getCustomSizing().get(serviceName);
    replicaSetBuilder = replicaSetBuilder.withNewMetadata().withName(replicaSetName).withNamespace(namespace).endMetadata().withNewSpec().withReplicas(retrieveKubernetesTargetSize(componentSizing)).withNewSelector().withMatchLabels(replicaSetSelector).endSelector().withNewTemplate().withNewMetadata().withAnnotations(settings.getKubernetes().getPodAnnotations()).withLabels(podLabels).endMetadata().withNewSpec().withContainers(containers).withTerminationGracePeriodSeconds(5L).withVolumes(volumes).withImagePullSecrets(imagePullSecrets).endSpec().endTemplate().endSpec();
    create = true;
    if (client.extensions().replicaSets().inNamespace(namespace).withName(replicaSetName).get() != null) {
        if (recreate) {
            client.extensions().replicaSets().inNamespace(namespace).withName(replicaSetName).delete();
            RunningServiceDetails runningServiceDetails = getRunningServiceDetails(details, runtimeSettings);
            while (runningServiceDetails.getLatestEnabledVersion() != null) {
                DaemonTaskHandler.safeSleep(TimeUnit.SECONDS.toMillis(5));
                runningServiceDetails = getRunningServiceDetails(details, runtimeSettings);
            }
        } else {
            create = false;
        }
    }
    if (create) {
        client.extensions().replicaSets().inNamespace(namespace).create(replicaSetBuilder.build());
    }
    RunningServiceDetails runningServiceDetails = getRunningServiceDetails(details, runtimeSettings);
    Integer version = runningServiceDetails.getLatestEnabledVersion();
    while (version == null || runningServiceDetails.getInstances().get(version).stream().anyMatch(i -> !(i.isHealthy() && i.isRunning()))) {
        DaemonTaskHandler.safeSleep(TimeUnit.SECONDS.toMillis(5));
        runningServiceDetails = getRunningServiceDetails(details, runtimeSettings);
        version = runningServiceDetails.getLatestEnabledVersion();
    }
}
Also used : KubernetesResourceDescription(com.netflix.spinnaker.clouddriver.kubernetes.v1.deploy.description.servergroup.KubernetesResourceDescription) KubernetesUtil(com.netflix.spinnaker.clouddriver.kubernetes.v1.deploy.KubernetesUtil) SpinnakerMonitoringDaemonService(com.netflix.spinnaker.halyard.deploy.spinnaker.v1.service.SpinnakerMonitoringDaemonService) KubernetesAccount(com.netflix.spinnaker.halyard.config.model.v1.providers.kubernetes.KubernetesAccount) LogCollector(com.netflix.spinnaker.halyard.deploy.spinnaker.v1.service.LogCollector) HalException(com.netflix.spinnaker.halyard.core.error.v1.HalException) DaemonTaskHandler(com.netflix.spinnaker.halyard.core.tasks.v1.DaemonTaskHandler) SidecarService(com.netflix.spinnaker.halyard.deploy.spinnaker.v1.service.distributed.SidecarService) Pair(org.apache.commons.lang3.tuple.Pair) Names(com.netflix.frigga.Names) Map(java.util.Map) ContainerStatus(io.fabric8.kubernetes.api.model.ContainerStatus) Provider(com.netflix.spinnaker.halyard.config.model.v1.node.Provider) TypeReference(com.fasterxml.jackson.core.type.TypeReference) SpinnakerService(com.netflix.spinnaker.halyard.deploy.spinnaker.v1.service.SpinnakerService) ServiceBuilder(io.fabric8.kubernetes.api.model.ServiceBuilder) Strings(io.fabric8.utils.Strings) DeploymentEnvironment(com.netflix.spinnaker.halyard.config.model.v1.node.DeploymentEnvironment) SecretVolumeSourceBuilder(io.fabric8.kubernetes.api.model.SecretVolumeSourceBuilder) ServiceInterfaceFactory(com.netflix.spinnaker.halyard.deploy.spinnaker.v1.service.ServiceInterfaceFactory) KubernetesVolumeMount(com.netflix.spinnaker.clouddriver.kubernetes.v1.deploy.description.servergroup.KubernetesVolumeMount) Set(java.util.Set) KubernetesProbe(com.netflix.spinnaker.clouddriver.kubernetes.v1.deploy.description.servergroup.KubernetesProbe) GenerateService(com.netflix.spinnaker.halyard.deploy.services.v1.GenerateService) VolumeBuilder(io.fabric8.kubernetes.api.model.VolumeBuilder) Collectors(java.util.stream.Collectors) List(java.util.List) KubernetesVolumeSource(com.netflix.spinnaker.clouddriver.kubernetes.v1.deploy.description.servergroup.KubernetesVolumeSource) SocketUtils(org.springframework.util.SocketUtils) Problem(com.netflix.spinnaker.halyard.core.problem.v1.Problem) KubernetesContainerDescription(com.netflix.spinnaker.clouddriver.kubernetes.v1.deploy.description.servergroup.KubernetesContainerDescription) ServiceSettings(com.netflix.spinnaker.halyard.deploy.spinnaker.v1.service.ServiceSettings) JobStatus(com.netflix.spinnaker.halyard.core.job.v1.JobStatus) Container(io.fabric8.kubernetes.api.model.Container) DeployKubernetesAtomicOperationDescription(com.netflix.spinnaker.clouddriver.kubernetes.v1.deploy.description.servergroup.DeployKubernetesAtomicOperationDescription) CustomSizing(com.netflix.spinnaker.halyard.config.model.v1.node.CustomSizing) ServicePortBuilder(io.fabric8.kubernetes.api.model.ServicePortBuilder) HashMap(java.util.HashMap) ArtifactService(com.netflix.spinnaker.halyard.deploy.services.v1.ArtifactService) LocalObjectReference(io.fabric8.kubernetes.api.model.LocalObjectReference) RunningServiceDetails(com.netflix.spinnaker.halyard.deploy.spinnaker.v1.RunningServiceDetails) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) KubernetesHandler(com.netflix.spinnaker.clouddriver.kubernetes.v1.deploy.description.servergroup.KubernetesHandler) AccountDeploymentDetails(com.netflix.spinnaker.halyard.deploy.deployment.v1.AccountDeploymentDetails) ReplicaSet(io.fabric8.kubernetes.api.model.extensions.ReplicaSet) JobExecutor(com.netflix.spinnaker.halyard.core.job.v1.JobExecutor) KubernetesImageDescription(com.netflix.spinnaker.clouddriver.kubernetes.v1.deploy.description.servergroup.KubernetesImageDescription) KubernetesContainerPort(com.netflix.spinnaker.clouddriver.kubernetes.v1.deploy.description.servergroup.KubernetesContainerPort) Instance(com.netflix.spinnaker.halyard.deploy.spinnaker.v1.RunningServiceDetails.Instance) KubernetesNamedServicePort(com.netflix.spinnaker.clouddriver.kubernetes.v1.deploy.description.loadbalancer.KubernetesNamedServicePort) Volume(io.fabric8.kubernetes.api.model.Volume) ReplicaSetBuilder(io.fabric8.kubernetes.api.model.extensions.ReplicaSetBuilder) KubernetesLoadBalancerDescription(com.netflix.spinnaker.clouddriver.kubernetes.v1.deploy.description.loadbalancer.KubernetesLoadBalancerDescription) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) KubernetesEnvVar(com.netflix.spinnaker.clouddriver.kubernetes.v1.deploy.description.servergroup.KubernetesEnvVar) KubernetesSecretVolumeSource(com.netflix.spinnaker.clouddriver.kubernetes.v1.deploy.description.servergroup.KubernetesSecretVolumeSource) JobRequest(com.netflix.spinnaker.halyard.core.job.v1.JobRequest) Pod(io.fabric8.kubernetes.api.model.Pod) SpinnakerRuntimeSettings(com.netflix.spinnaker.halyard.deploy.spinnaker.v1.SpinnakerRuntimeSettings) ImmutablePair(org.apache.commons.lang3.tuple.ImmutablePair) File(java.io.File) TimeUnit(java.util.concurrent.TimeUnit) Profile(com.netflix.spinnaker.halyard.deploy.spinnaker.v1.profile.Profile) Paths(java.nio.file.Paths) KubernetesHttpGetAction(com.netflix.spinnaker.clouddriver.kubernetes.v1.deploy.description.servergroup.KubernetesHttpGetAction) KubernetesVolumeSourceType(com.netflix.spinnaker.clouddriver.kubernetes.v1.deploy.description.servergroup.KubernetesVolumeSourceType) KubernetesTcpSocketAction(com.netflix.spinnaker.clouddriver.kubernetes.v1.deploy.description.servergroup.KubernetesTcpSocketAction) DistributedService(com.netflix.spinnaker.halyard.deploy.spinnaker.v1.service.distributed.DistributedService) KubernetesClient(io.fabric8.kubernetes.client.KubernetesClient) ConfigSource(com.netflix.spinnaker.halyard.deploy.spinnaker.v1.service.ConfigSource) Collections(java.util.Collections) KubernetesHandlerType(com.netflix.spinnaker.clouddriver.kubernetes.v1.deploy.description.servergroup.KubernetesHandlerType) DeploymentEnvironment(com.netflix.spinnaker.halyard.config.model.v1.node.DeploymentEnvironment) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) SpinnakerRuntimeSettings(com.netflix.spinnaker.halyard.deploy.spinnaker.v1.SpinnakerRuntimeSettings) VolumeBuilder(io.fabric8.kubernetes.api.model.VolumeBuilder) ServiceBuilder(io.fabric8.kubernetes.api.model.ServiceBuilder) Container(io.fabric8.kubernetes.api.model.Container) RunningServiceDetails(com.netflix.spinnaker.halyard.deploy.spinnaker.v1.RunningServiceDetails) ReplicaSetBuilder(io.fabric8.kubernetes.api.model.extensions.ReplicaSetBuilder) KubernetesClient(io.fabric8.kubernetes.client.KubernetesClient) SecretVolumeSourceBuilder(io.fabric8.kubernetes.api.model.SecretVolumeSourceBuilder) SidecarService(com.netflix.spinnaker.halyard.deploy.spinnaker.v1.service.distributed.SidecarService) ServiceSettings(com.netflix.spinnaker.halyard.deploy.spinnaker.v1.service.ServiceSettings) ServicePortBuilder(io.fabric8.kubernetes.api.model.ServicePortBuilder) Volume(io.fabric8.kubernetes.api.model.Volume) LocalObjectReference(io.fabric8.kubernetes.api.model.LocalObjectReference) SpinnakerMonitoringDaemonService(com.netflix.spinnaker.halyard.deploy.spinnaker.v1.service.SpinnakerMonitoringDaemonService) Map(java.util.Map) HashMap(java.util.HashMap)

Example 25 with ServiceBuilder

use of io.fabric8.knative.serving.v1.ServiceBuilder in project kubernetes by ballerinax.

the class ServiceHandler method generate.

/**
 * Generate kubernetes service definition from annotation.
 *
 * @return Generated kubernetes service yaml as a string
 * @throws KubernetesPluginException If an error occurs while generating artifact.
 */
public String generate() throws KubernetesPluginException {
    Service service = new ServiceBuilder().withNewMetadata().withName(serviceModel.getName()).addToLabels(serviceModel.getLabels()).endMetadata().withNewSpec().addNewPort().withProtocol(KubernetesConstants.KUBERNETES_SVC_PROTOCOL).withPort(serviceModel.getPort()).withNewTargetPort(serviceModel.getPort()).endPort().addToSelector(KubernetesConstants.KUBERNETES_SELECTOR_KEY, serviceModel.getSelector()).withType(serviceModel.getServiceType()).endSpec().build();
    String serviceYAML;
    try {
        serviceYAML = SerializationUtils.dumpWithoutRuntimeStateAsYaml(service);
    } catch (IOException e) {
        String errorMessage = "Error while generating yaml file for service: " + serviceModel.getName();
        throw new KubernetesPluginException(errorMessage, e);
    }
    return serviceYAML;
}
Also used : Service(io.fabric8.kubernetes.api.model.Service) IOException(java.io.IOException) KubernetesPluginException(org.ballerinax.kubernetes.exceptions.KubernetesPluginException) ServiceBuilder(io.fabric8.kubernetes.api.model.ServiceBuilder)

Aggregations

ServiceBuilder (io.fabric8.kubernetes.api.model.ServiceBuilder)39 Service (io.fabric8.kubernetes.api.model.Service)32 Test (org.junit.Test)14 ServicePortBuilder (io.fabric8.kubernetes.api.model.ServicePortBuilder)11 ServicePort (io.fabric8.kubernetes.api.model.ServicePort)7 ArrayList (java.util.ArrayList)7 ServiceListBuilder (io.fabric8.kubernetes.api.model.ServiceListBuilder)5 EndpointsListBuilder (io.fabric8.kubernetes.api.model.EndpointsListBuilder)4 Pod (io.fabric8.kubernetes.api.model.Pod)4 ServiceSpec (io.fabric8.kubernetes.api.model.ServiceSpec)4 VolumeBuilder (io.fabric8.kubernetes.api.model.VolumeBuilder)4 KubernetesClient (io.fabric8.kubernetes.client.KubernetesClient)4 RouteBuilder (io.fabric8.openshift.api.model.RouteBuilder)4 Map (java.util.Map)4 Container (io.fabric8.kubernetes.api.model.Container)3 IOException (java.io.IOException)3 KubernetesPluginException (org.ballerinax.kubernetes.exceptions.KubernetesPluginException)3 Alias (io.fabric8.annotations.Alias)2 External (io.fabric8.annotations.External)2 PortName (io.fabric8.annotations.PortName)2