use of io.fabric8.maven.docker.util.Logger in project fabric8-maven-plugin by fabric8io.
the class PortForwardService method forwardPortAsync.
/**
* Forwards a port to the newest pod matching the given selector.
* If another pod is created, it forwards connections to the new pod once it's ready.
*/
public Closeable forwardPortAsync(final Logger externalProcessLogger, final LabelSelector podSelector, final int remotePort, final int localPort) throws Fabric8ServiceException {
final Lock monitor = new ReentrantLock(true);
final Condition podChanged = monitor.newCondition();
final Pod[] nextForwardedPod = new Pod[1];
final Thread forwarderThread = new Thread() {
@Override
public void run() {
Pod currentPod = null;
Closeable currentPortForward = null;
try {
monitor.lock();
while (true) {
if (podEquals(currentPod, nextForwardedPod[0])) {
podChanged.await();
} else {
// may be null
Pod nextPod = nextForwardedPod[0];
try {
monitor.unlock();
if (currentPortForward != null) {
log.info("Closing port-forward from pod %s", KubernetesHelper.getName(currentPod));
currentPortForward.close();
currentPortForward = null;
}
if (nextPod != null) {
log.info("Starting port-forward to pod %s", KubernetesHelper.getName(nextPod));
currentPortForward = forwardPortAsync(externalProcessLogger, KubernetesHelper.getName(nextPod), remotePort, localPort);
} else {
log.info("Waiting for a pod to become ready before starting port-forward");
}
currentPod = nextPod;
} finally {
monitor.lock();
}
}
}
} catch (InterruptedException e) {
log.debug("Port-forwarding thread interrupted", e);
Thread.currentThread().interrupt();
} catch (Exception e) {
log.warn("Error while port-forwarding to pod", e);
} finally {
monitor.unlock();
if (currentPortForward != null) {
try {
currentPortForward.close();
} catch (Exception e) {
}
}
}
}
};
// Switching forward to the current pod if present
Pod newPod = getNewestPod(podSelector);
nextForwardedPod[0] = newPod;
final Watch watch = KubernetesClientUtil.withSelector(kubernetes.pods(), podSelector, log).watch(new Watcher<Pod>() {
@Override
public void eventReceived(Action action, Pod pod) {
monitor.lock();
try {
List<Pod> candidatePods;
if (nextForwardedPod[0] != null) {
candidatePods = new LinkedList<>();
candidatePods.add(nextForwardedPod[0]);
candidatePods.add(pod);
} else {
candidatePods = Collections.singletonList(pod);
}
// may be null
Pod newPod = getNewestPod(candidatePods);
if (!podEquals(nextForwardedPod[0], newPod)) {
nextForwardedPod[0] = newPod;
podChanged.signal();
}
} finally {
monitor.unlock();
}
}
@Override
public void onClose(KubernetesClientException e) {
// don't care
}
});
forwarderThread.start();
final Closeable handle = new Closeable() {
@Override
public void close() throws IOException {
try {
watch.close();
} catch (Exception e) {
}
try {
forwarderThread.interrupt();
forwarderThread.join(15000);
} catch (Exception e) {
}
}
};
Runtime.getRuntime().addShutdownHook(new Thread() {
@Override
public void run() {
try {
handle.close();
} catch (Exception e) {
// suppress
}
}
});
return handle;
}
use of io.fabric8.maven.docker.util.Logger in project fabric8-maven-plugin by fabric8io.
the class Fabric8ServiceHubTest method testObtainBuildService.
@Test
public void testObtainBuildService() {
Fabric8ServiceHub hub = new Fabric8ServiceHub.Builder().clusterAccess(clusterAccess).log(logger).platformMode(PlatformMode.kubernetes).dockerServiceHub(dockerServiceHub).buildServiceConfig(buildServiceConfig).build();
BuildService buildService = hub.getBuildService();
assertNotNull(buildService);
assertTrue(buildService instanceof DockerBuildService);
}
use of io.fabric8.maven.docker.util.Logger in project fabric8-maven-plugin by fabric8io.
the class Fabric8ServiceHubTest method testObtainOpenshiftBuildService.
@Test
public void testObtainOpenshiftBuildService() {
Fabric8ServiceHub hub = new Fabric8ServiceHub.Builder().clusterAccess(clusterAccess).log(logger).platformMode(PlatformMode.openshift).dockerServiceHub(dockerServiceHub).buildServiceConfig(buildServiceConfig).build();
BuildService buildService = hub.getBuildService();
assertNotNull(buildService);
assertTrue(buildService instanceof OpenshiftBuildService);
}
use of io.fabric8.maven.docker.util.Logger in project fabric8-maven-plugin by fabric8io.
the class ClusterAccessTest method createClientTestKubernetes.
@Test
public void createClientTestKubernetes() throws Exception {
RootPaths rootpaths = new RootPaths();
rootpaths.setPaths(paths);
mockServer.expect().get().withPath("/").andReturn(200, rootpaths).always();
ClusterAccess clusterAccess = new ClusterAccess(null, client);
Client outputClient = clusterAccess.createDefaultClient(logger);
assertTrue(outputClient instanceof KubernetesClient);
}
use of io.fabric8.maven.docker.util.Logger in project fabric8-maven-plugin by fabric8io.
the class ClusterAccessTest method kubernetesPlatformModeTest.
@Test
public void kubernetesPlatformModeTest() throws Exception {
RootPaths rootpaths = new RootPaths();
rootpaths.setPaths(paths);
mockServer.expect().get().withPath("/").andReturn(200, rootpaths).always();
ClusterAccess clusterAccess = new ClusterAccess(null, client);
mode = clusterAccess.resolvePlatformMode(PlatformMode.kubernetes, logger);
assertEquals(PlatformMode.kubernetes, mode);
mode = clusterAccess.resolvePlatformMode(PlatformMode.DEFAULT, logger);
assertEquals(PlatformMode.kubernetes, mode);
mode = clusterAccess.resolvePlatformMode(null, logger);
assertEquals(PlatformMode.kubernetes, mode);
}
Aggregations