Search in sources :

Example 26 with ServiceBuilder

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

the class MockConfigurer method configure.

public static void configure() {
    MOCK.expect().get().withPath("/api/v1/namespaces/cdi/services/service1").andReturn(200, new ServiceBuilder().withNewMetadata().withName("service1").endMetadata().withNewSpec().addNewPort().withProtocol("TCP").withPort(80).withNewTargetPort(9090).endPort().withClusterIP("172.30.17.2").endSpec().build()).always();
    // Services
    MOCK.expect().get().withPath("/api/v1/namespaces/cdi/services/service2").andReturn(200, new ServiceBuilder().withNewMetadata().withName("service2").endMetadata().withNewSpec().addNewPort().withProtocol("TCP").withPort(80).withNewTargetPort(8080).endPort().withClusterIP("172.30.17.2").endSpec().build()).always();
    MOCK.expect().get().withPath("/api/v1/namespaces/cdi/services/service3").andReturn(200, new ServiceBuilder().withNewMetadata().withName("service3").endMetadata().withNewSpec().addNewPort().withProtocol("TCP").withPort(443).withNewTargetPort(443).endPort().withClusterIP("172.30.17.2").endSpec().build()).always();
    MOCK.expect().get().withPath("/api/v1/namespaces/cdi/services/multiport").andReturn(200, new ServiceBuilder().withNewMetadata().withName("multiport").endMetadata().withNewSpec().addNewPort().withName("port1").withProtocol("TCP").withPort(8081).withNewTargetPort(8081).endPort().addNewPort().withName("port2").withProtocol("TCP").withPort(8082).withNewTargetPort(8082).endPort().addNewPort().withName("port3").withProtocol("TCP").withPort(8083).withNewTargetPort(8083).endPort().withClusterIP("172.30.17.2").endSpec().build()).always();
    // Endpoints
    Endpoints service1Endpoints = new EndpointsBuilder().withNewMetadata().withName("service1").withNamespace("default").endMetadata().addNewSubset().addNewPort().withName("port").withPort(8080).endPort().addNewAddress().withIp("10.0.0.1").endAddress().endSubset().addNewSubset().addNewPort().withName("port").withPort(8080).endPort().addNewAddress().withIp("10.0.0.2").endAddress().endSubset().build();
    Endpoints service2EndpointsA = new EndpointsBuilder().withNewMetadata().withName("service2").withNamespace("default").endMetadata().addNewSubset().addNewPort().withName("port").withPort(8080).endPort().addNewAddress().withIp("10.0.0.1").endAddress().endSubset().addNewSubset().addNewPort().withName("port").withPort(8080).endPort().addNewAddress().withIp("10.0.0.2").endAddress().endSubset().build();
    Endpoints service2EndpointsB = new EndpointsBuilder().withNewMetadata().withName("service2").withNamespace("default").endMetadata().addNewSubset().addNewPort().withName("port").withPort(8080).endPort().addNewAddress().withIp("10.0.0.1").endAddress().endSubset().build();
    Endpoints multiPortEndpoint = new EndpointsBuilder().withNewMetadata().withName("multiport").withNamespace("default").endMetadata().addNewSubset().addNewAddress().withIp("172.30.17.2").endAddress().addNewPort("port1", 8081, "TCP").addNewPort("port2", 8082, "TCP").addNewPort("port3", 8083, "TCP").endSubset().build();
    MOCK.expect().withPath("/api/v1/namespaces/cdi/endpoints/service1").andReturn(200, service1Endpoints).always();
    MOCK.expect().withPath("/api/v1/namespaces/cdi/endpoints/service2").andReturn(200, service2EndpointsA).once();
    MOCK.expect().withPath("/api/v1/namespaces/cdi/endpoints/service2").andReturn(200, service2EndpointsB).always();
    MOCK.expect().withPath("/api/v1/namespaces/cdi/endpoints/multiport").andReturn(200, multiPortEndpoint).always();
    String masterUrl = MOCK.getServer().url("/").toString();
    System.setProperty(Config.KUBERNETES_MASTER_SYSTEM_PROPERTY, masterUrl);
    System.setProperty(Config.KUBERNETES_NAMESPACE_SYSTEM_PROPERTY, "cdi");
}
Also used : Endpoints(io.fabric8.kubernetes.api.model.Endpoints) EndpointsBuilder(io.fabric8.kubernetes.api.model.EndpointsBuilder) ServiceBuilder(io.fabric8.kubernetes.api.model.ServiceBuilder)

Example 27 with ServiceBuilder

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

the class ConfigCompareServiceTest method testServicesEqual.

@Test
public void testServicesEqual() throws Exception {
    Service entity1 = new ServiceBuilder().withNewMetadata().withName("foo").addToLabels("label1", "value1").addToLabels("label2", "value2").endMetadata().withNewSpec().addToSelector("label1", "value1").addToSelector("label2", "value2").addNewPort().withPort(123).withNewTargetPort(456).endPort().endSpec().build();
    Service entity2 = new ServiceBuilder().withNewMetadata().withName("foo").addToLabels("label2", "value2").addToLabels("label1", "value1").endMetadata().withNewSpec().addToSelector("label1", "value1").addToSelector("label2", "value2").addNewPort().withPort(123).withNewTargetPort(456).endPort().endSpec().build();
    assertCompareConfig(entity1, entity2, true);
}
Also used : Service(io.fabric8.kubernetes.api.model.Service) ServiceBuilder(io.fabric8.kubernetes.api.model.ServiceBuilder) Test(org.junit.Test)

Example 28 with ServiceBuilder

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

the class ConfigCompareServiceTest method testServicesLabelsNotEqual.

@Test
public void testServicesLabelsNotEqual() throws Exception {
    Service entity1 = new ServiceBuilder().withNewMetadata().withName("foo").addToLabels("label1", "value1").addToLabels("label2", "value2").endMetadata().withNewSpec().addToSelector("label1", "value1").addToSelector("label2", "value2").addNewPort().withPort(123).withNewTargetPort(456).endPort().endSpec().build();
    Service entity2 = new ServiceBuilder().withNewMetadata().withName("foo").addToLabels("label2", "value2").addToLabels("notSame", "value1").endMetadata().withNewSpec().addToSelector("label1", "value1").addToSelector("label2", "value2").addNewPort().withPort(123).withNewTargetPort(456).endPort().endSpec().build();
    assertCompareConfig(entity1, entity2, false);
}
Also used : Service(io.fabric8.kubernetes.api.model.Service) ServiceBuilder(io.fabric8.kubernetes.api.model.ServiceBuilder) Test(org.junit.Test)

Example 29 with ServiceBuilder

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

the class KubernetesContainerMock method getMockedKubernetesContainerClient.

public static KubernetesContainerClient getMockedKubernetesContainerClient() {
    // Mocking the environment variable to return false for video recording enabled
    Environment environment = mock(Environment.class);
    when(environment.getStringEnvVariable("ZALENIUM_KUBERNETES_CPU_REQUEST", null)).thenReturn("250m");
    when(environment.getStringEnvVariable("ZALENIUM_KUBERNETES_CPU_LIMIT", null)).thenReturn("500m");
    when(environment.getStringEnvVariable("ZALENIUM_KUBERNETES_MEMORY_REQUEST", null)).thenReturn("1Gi");
    when(environment.getStringEnvVariable("ZALENIUM_KUBERNETES_MEMORY_LIMIT", null)).thenReturn("4Gi");
    String hostName;
    try {
        hostName = InetAddress.getLocalHost().getHostName();
    } catch (UnknownHostException e) {
        hostName = "";
    }
    KubernetesServer server = new KubernetesServer();
    server.before();
    Map<String, String> zaleniumPodLabels = new HashMap<>();
    zaleniumPodLabels.put("app", "zalenium");
    zaleniumPodLabels.put("role", "grid");
    Pod zaleniumPod = new PodBuilder().withNewMetadata().withLabels(zaleniumPodLabels).addToLabels(zaleniumPodLabels).withNamespace("test").withGenerateName(hostName).withName(hostName).and().build();
    String videosVolumeName = RandomStringUtils.randomAlphabetic(5).toLowerCase();
    String generalVolumeName = RandomStringUtils.randomAlphabetic(5).toLowerCase();
    VolumeMount volumeMountVideos = new VolumeMountBuilder().withName(videosVolumeName).withMountPath("/tmp/videos").build();
    VolumeMount volumeMountGeneral = new VolumeMountBuilder().withName(generalVolumeName).withMountPath("/tmp/mounted").build();
    Container zaleniumContainer = new ContainerBuilder().withVolumeMounts(volumeMountVideos, volumeMountGeneral).build();
    Volume videosVolume = new VolumeBuilder().withName(videosVolumeName).build();
    Volume generalVolume = new VolumeBuilder().withName(generalVolumeName).build();
    HostAlias hostAlias = new HostAliasBuilder().withHostnames("foo.local").withIp("127.0.0.1").build();
    Map<String, String> nodeSelector = new HashMap<>();
    nodeSelector.put("nodeLabelKey", "nodeLabelValue");
    PodSpec zaleniumPodSpec = new PodSpecBuilder().withContainers(zaleniumContainer).withVolumes(videosVolume, generalVolume).withHostAliases(hostAlias).withNodeSelector(nodeSelector).build();
    zaleniumPod.setSpec(zaleniumPodSpec);
    String podsPath = String.format("/api/v1/namespaces/test/pods/%s", hostName);
    server.expect().withPath(podsPath).andReturn(200, zaleniumPod).once();
    Map<String, String> dockerSeleniumPodLabels = new HashMap<>();
    dockerSeleniumPodLabels.put("createdBy", "zalenium");
    Pod dockerSeleniumPod = new PodBuilder().withNewMetadata().withLabels(dockerSeleniumPodLabels).withNamespace("test").withName(hostName).and().build();
    Container dockerSeleniumContainer = new ContainerBuilder().withEnv(new EnvVarBuilder().withName("NOVNC_PORT").withValue("40000").build()).build();
    PodSpec dockerSeleniumPodSpec = new PodSpecBuilder().withContainers(dockerSeleniumContainer).build();
    dockerSeleniumPod.setSpec(dockerSeleniumPodSpec);
    PodStatus dockerSeleniumPodStatus = new PodStatusBuilder().withPodIP("localhost").build();
    dockerSeleniumPod.setStatus(dockerSeleniumPodStatus);
    server.expect().withPath("/api/v1/namespaces/test/pods?labelSelector=createdBy%3Dzalenium").andReturn(200, new PodListBuilder().withItems(dockerSeleniumPod).build()).always();
    ServiceSpec serviceSpec = new ServiceSpecBuilder().withPorts(new ServicePortBuilder().withNodePort(40000).build()).build();
    Service service = new ServiceBuilder().withSpec(serviceSpec).build();
    server.expect().withPath("/api/v1/namespaces/test/services").andReturn(201, service).always();
    String bashCommand = String.format("/api/v1/namespaces/test/pods/%s/exec?command=bash&command=-c&command=", hostName);
    String tarCommand = String.format("/api/v1/namespaces/test/pods/%s/exec?command=tar&command=-C&command=", hostName);
    String commandSuffix = "&stdout=true&stderr=true";
    String expectedOutput = "test";
    List<String> execPaths = new ArrayList<>();
    execPaths.add(String.format("%stransfer-logs.sh%s", bashCommand, commandSuffix));
    execPaths.add(String.format("%s/var/log/cont/&command=-c&command=.%s", tarCommand, commandSuffix));
    execPaths.add(String.format("%s/videos/&command=-c&command=.%s", tarCommand, commandSuffix));
    execPaths.add(String.format("%s/videos/&command=-C&command=.%s", tarCommand, commandSuffix));
    execPaths.add(String.format("%sstop-video%s", bashCommand, commandSuffix));
    execPaths.add(String.format("%sstart-video%s", bashCommand, commandSuffix));
    execPaths.add(String.format("%stransfer-logs.sh%s", bashCommand, commandSuffix));
    execPaths.add(String.format("%scleanup-container.sh%s", bashCommand, commandSuffix));
    String notifyComplete = bashCommand.concat("notify%20%27Zalenium%27,%20%27TEST%20COMPLETED%27,%20--icon=/home/seluser/images/completed.png").concat(commandSuffix);
    String notifyTimeout = bashCommand.concat("notify%20%27Zalenium%27,%20%27TEST%20TIMED%20OUT%27,%20--icon=/home/seluser/images/timeout.png").concat(commandSuffix);
    execPaths.add(notifyComplete);
    execPaths.add(notifyTimeout);
    for (String execPath : execPaths) {
        server.expect().withPath(execPath).andUpgradeToWebSocket().open(new OutputStreamMessage(expectedOutput)).done().once();
    }
    server.expect().withPath("/api/v1/namespaces/test/pods").andReturn(201, new PodBuilder().build()).always();
    KubernetesClient client = server.getClient();
    return new KubernetesContainerClient(environment, KubernetesContainerClient::createDoneablePodDefaultImpl, client);
}
Also used : HashMap(java.util.HashMap) PodSpec(io.fabric8.kubernetes.api.model.PodSpec) ServiceSpec(io.fabric8.kubernetes.api.model.ServiceSpec) ArrayList(java.util.ArrayList) OutputStreamMessage(io.fabric8.kubernetes.client.server.mock.OutputStreamMessage) PodStatusBuilder(io.fabric8.kubernetes.api.model.PodStatusBuilder) VolumeBuilder(io.fabric8.kubernetes.api.model.VolumeBuilder) EnvVarBuilder(io.fabric8.kubernetes.api.model.EnvVarBuilder) ServiceBuilder(io.fabric8.kubernetes.api.model.ServiceBuilder) PodStatus(io.fabric8.kubernetes.api.model.PodStatus) Container(io.fabric8.kubernetes.api.model.Container) ContainerBuilder(io.fabric8.kubernetes.api.model.ContainerBuilder) PodSpecBuilder(io.fabric8.kubernetes.api.model.PodSpecBuilder) HostAliasBuilder(io.fabric8.kubernetes.api.model.HostAliasBuilder) KubernetesClient(io.fabric8.kubernetes.client.KubernetesClient) UnknownHostException(java.net.UnknownHostException) Pod(io.fabric8.kubernetes.api.model.Pod) PodBuilder(io.fabric8.kubernetes.api.model.PodBuilder) Service(io.fabric8.kubernetes.api.model.Service) KubernetesServer(io.fabric8.kubernetes.client.server.mock.KubernetesServer) VolumeMountBuilder(io.fabric8.kubernetes.api.model.VolumeMountBuilder) KubernetesContainerClient(de.zalando.ep.zalenium.container.kubernetes.KubernetesContainerClient) HostAlias(io.fabric8.kubernetes.api.model.HostAlias) ServiceSpecBuilder(io.fabric8.kubernetes.api.model.ServiceSpecBuilder) PodListBuilder(io.fabric8.kubernetes.api.model.PodListBuilder) Volume(io.fabric8.kubernetes.api.model.Volume) ServicePortBuilder(io.fabric8.kubernetes.api.model.ServicePortBuilder) VolumeMount(io.fabric8.kubernetes.api.model.VolumeMount)

Example 30 with ServiceBuilder

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

the class InternalServiceRegistar method getService.

@Override
public Service getService(String serviceName) {
    Map<String, String> env = System.getenv();
    String prefix = serviceName.toUpperCase();
    String serviceHost = env.get(prefix + HOST_SUFFIX);
    String defaultPortName = prefix + SERVICE_PORT;
    String namedPortPrefix = defaultPortName + "_";
    List<ServicePort> servicePorts = new ArrayList<>();
    for (Map.Entry<String, String> entry : env.entrySet()) {
        String key = entry.getKey();
        if (key.startsWith(namedPortPrefix)) {
            String name = key.substring(namedPortPrefix.length());
            String portValue = entry.getValue();
            String protocolValue = env.get(key + "_" + PROTO_SUFFIX);
            servicePorts.add(new ServicePortBuilder().withName(name.toLowerCase()).withPort(Integer.parseInt(portValue)).withProtocol(protocolValue != null ? protocolValue : "TCP").build());
        }
    }
    // Check if we need to fallback to single port.
    if (servicePorts.isEmpty()) {
        String portValue = env.get(defaultPortName);
        String protocolValue = env.get(defaultPortName + PROTO_SUFFIX);
        servicePorts.add(new ServicePortBuilder().withPort(Integer.parseInt(portValue)).withProtocol(protocolValue != null ? protocolValue : "TCP").build());
    }
    return new ServiceBuilder().withNewMetadata().withName(serviceName).endMetadata().withNewSpec().withClusterIP(serviceHost).withPorts(servicePorts).endSpec().build();
}
Also used : ServicePort(io.fabric8.kubernetes.api.model.ServicePort) ServicePortBuilder(io.fabric8.kubernetes.api.model.ServicePortBuilder) ArrayList(java.util.ArrayList) Map(java.util.Map) 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