use of io.fabric8.maven.core.service.PortForwardService 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);
}
}
use of io.fabric8.maven.core.service.PortForwardService in project fabric8-maven-plugin by fabric8io.
the class SpringBootWatcher method getPortForwardUrl.
private String getPortForwardUrl(final Set<HasMetadata> resources) throws Exception {
LabelSelector selector = KubernetesResourceUtil.getPodLabelSelector(resources);
if (selector == null) {
log.warn("Unable to determine a selector for application pods");
return null;
}
Properties properties = SpringBootUtil.getSpringBootApplicationProperties(getContext().getProject());
SpringBootConfigurationHelper propertyHelper = new SpringBootConfigurationHelper(SpringBootUtil.getSpringBootVersion(getContext().getProject()));
PortForwardService portForwardService = getContext().getFabric8ServiceHub().getPortForwardService();
int port = IoUtil.getFreeRandomPort();
int containerPort = findSpringBootWebPort(propertyHelper, properties);
portForwardService.forwardPortAsync(getContext().getLogger(), selector, containerPort, port);
return createForwardUrl(propertyHelper, properties, port);
}
use of io.fabric8.maven.core.service.PortForwardService in project fabric8-maven-plugin by fabric8io.
the class Fabric8ServiceHub method init.
private void init() {
Objects.requireNonNull(clusterAccess, "clusterAccess");
Objects.requireNonNull(log, "log");
this.resolvedMode = clusterAccess.resolvePlatformMode(platformMode, log);
if (resolvedMode != PlatformMode.kubernetes && resolvedMode != PlatformMode.openshift) {
throw new IllegalArgumentException("Unknown platform mode " + platformMode + " resolved as " + resolvedMode);
}
this.client = clusterAccess.createDefaultClient(log);
if (this.controller == null) {
this.controller = new Controller(this.client);
this.controller.setThrowExceptionOnError(true);
}
// Lazily building services
this.services.putIfAbsent(ClientToolsService.class, new LazyBuilder<ClientToolsService>() {
@Override
protected ClientToolsService build() {
return new ClientToolsService(controller, log);
}
});
this.services.putIfAbsent(PortForwardService.class, new LazyBuilder<PortForwardService>() {
@Override
protected PortForwardService build() {
return new PortForwardService(getClientToolsService(), log, client);
}
});
this.services.putIfAbsent(BuildService.class, new LazyBuilder<BuildService>() {
@Override
protected BuildService build() {
BuildService buildService;
// Creating platform-dependent services
if (resolvedMode == PlatformMode.openshift) {
// Openshift services
buildService = new OpenshiftBuildService((OpenShiftClient) client, log, dockerServiceHub, buildServiceConfig);
} else {
// Kubernetes services
buildService = new DockerBuildService(dockerServiceHub, buildServiceConfig);
}
return buildService;
}
});
this.services.putIfAbsent(ArtifactResolverService.class, new LazyBuilder<ArtifactResolverService>() {
@Override
protected ArtifactResolverService build() {
return new ArtifactResolverServiceMavenImpl(repositorySystem, mavenProject);
}
});
}
Aggregations