use of io.fabric8.maven.core.service.Fabric8ServiceException in project fabric8-maven-plugin by fabric8io.
the class DebugMojo method portForward.
private void portForward(Controller controller, String podName) throws MojoExecutionException {
try {
getFabric8ServiceHub(controller).getPortForwardService().forwardPort(createExternalProcessLogger("[[B]]port-forward[[B]] "), podName, portToInt(remoteDebugPort, "remoteDebugPort"), portToInt(localDebugPort, "localDebugPort"));
log.info("");
log.info("Now you can start a Remote debug execution in your IDE by using localhost and the debug port " + localDebugPort);
log.info("");
} catch (Fabric8ServiceException e) {
throw new MojoExecutionException("Failed to start port forwarding" + e, e);
}
}
use of io.fabric8.maven.core.service.Fabric8ServiceException 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.core.service.Fabric8ServiceException in project fabric8-maven-plugin by fabric8io.
the class DockerBuildService method build.
@Override
public void build(ImageConfiguration imageConfig) throws Fabric8ServiceException {
io.fabric8.maven.docker.service.BuildService dockerBuildService = dockerServiceHub.getBuildService();
io.fabric8.maven.docker.service.BuildService.BuildContext dockerBuildContext = config.getDockerBuildContext();
try {
dockerBuildService.buildImage(imageConfig, dockerBuildContext);
// Assume we always want to tag
dockerBuildService.tagImage(imageConfig.getName(), imageConfig);
} catch (Exception ex) {
throw new Fabric8ServiceException("Error while trying to build the image", ex);
}
}
use of io.fabric8.maven.core.service.Fabric8ServiceException in project fabric8-maven-plugin by fabric8io.
the class OpenshiftBuildServiceTest method checkTarPackage.
@Test
public void checkTarPackage() throws Exception {
int nTries = 0;
boolean bTestComplete = false;
do {
try {
nTries++;
BuildService.BuildServiceConfig config = defaultConfig.build();
WebServerEventCollector<OpenShiftMockServer> collector = createMockServer(config, true, 50, true, true);
OpenShiftMockServer mockServer = collector.getMockServer();
OpenShiftClient client = mockServer.createOpenShiftClient();
final OpenshiftBuildService service = new OpenshiftBuildService(client, logger, dockerServiceHub, config);
ImageConfiguration imageWithEnv = new ImageConfiguration.Builder(image).buildConfig(new BuildImageConfiguration.Builder(image.getBuildConfiguration()).env(Collections.singletonMap("FOO", "BAR")).build()).build();
service.createBuildArchive(imageWithEnv);
final List<ArchiverCustomizer> customizer = new LinkedList<>();
new Verifications() {
{
archiveService.createDockerBuildArchive(withInstanceOf(ImageConfiguration.class), withInstanceOf(MojoParameters.class), withCapture(customizer));
assertTrue(customizer.size() == 1);
}
};
customizer.get(0).customize(tarArchiver);
final List<File> file = new LinkedList<>();
new Verifications() {
{
String path;
tarArchiver.addFile(withCapture(file), path = withCapture());
assertEquals(".s2i/environment", path);
}
};
assertEquals(1, file.size());
List<String> lines;
try (FileReader reader = new FileReader(file.get(0))) {
lines = IOUtils.readLines(reader);
}
assertTrue(lines.contains("FOO=BAR"));
bTestComplete = true;
} catch (Fabric8ServiceException exception) {
Throwable rootCause = getRootCause(exception);
logger.warn("A problem encountered while running test {}, retrying..", exception.getMessage());
// Let's wait for a while, and then retry again
if (rootCause != null && rootCause instanceof IOException) {
continue;
}
}
} while (nTries < MAX_TIMEOUT_RETRIES && !bTestComplete);
}
use of io.fabric8.maven.core.service.Fabric8ServiceException in project fabric8-maven-plugin by fabric8io.
the class OpenshiftBuildServiceTest method testSuccessfulBuild.
@Test
public void testSuccessfulBuild() throws Exception {
int nTries = 0;
boolean bTestComplete = false;
do {
try {
nTries++;
BuildService.BuildServiceConfig config = defaultConfig.build();
WebServerEventCollector<OpenShiftMockServer> collector = createMockServer(config, true, 50, false, false);
OpenShiftMockServer mockServer = collector.getMockServer();
DefaultOpenShiftClient client = (DefaultOpenShiftClient) mockServer.createOpenShiftClient();
LOG.info("Current write timeout is : {}", client.getHttpClient().writeTimeoutMillis());
LOG.info("Current read timeout is : {}", client.getHttpClient().readTimeoutMillis());
LOG.info("Retry on failure : {}", client.getHttpClient().retryOnConnectionFailure());
OpenshiftBuildService service = new OpenshiftBuildService(client, logger, dockerServiceHub, config);
service.build(image);
// we should Foadd a better way to assert that a certain call has been made
assertTrue(mockServer.getRequestCount() > 8);
collector.assertEventsRecordedInOrder("build-config-check", "new-build-config", "pushed");
collector.assertEventsNotRecorded("patch-build-config");
bTestComplete = true;
} catch (Fabric8ServiceException exception) {
Throwable rootCause = getRootCause(exception);
logger.warn("A problem encountered while running test {}, retrying..", exception.getMessage());
// Let's wait for a while, and then retry again
if (rootCause != null && rootCause instanceof IOException) {
continue;
}
}
} while (nTries < MAX_TIMEOUT_RETRIES && !bTestComplete);
}
Aggregations