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");
}
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);
}
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);
}
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);
}
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();
}
Aggregations