Search in sources :

Example 6 with Watch

use of io.fabric8.kubernetes.client.Watch in project fabric8-maven-plugin by fabric8io.

the class PortForwardService method getNewestPod.

private Pod getNewestPod(LabelSelector selector) {
    FilterWatchListDeletable<Pod, PodList, Boolean, Watch, Watcher<Pod>> pods = KubernetesClientUtil.withSelector(kubernetes.pods(), selector, log);
    PodList list = pods.list();
    if (list != null) {
        List<Pod> items = list.getItems();
        return getNewestPod(items);
    }
    return null;
}
Also used : PodList(io.fabric8.kubernetes.api.model.PodList) Pod(io.fabric8.kubernetes.api.model.Pod) Watch(io.fabric8.kubernetes.client.Watch) Watcher(io.fabric8.kubernetes.client.Watcher)

Example 7 with Watch

use of io.fabric8.kubernetes.client.Watch in project fabric8-maven-plugin by fabric8io.

the class OpenshiftBuildService method waitForOpenShiftBuildToComplete.

private void waitForOpenShiftBuildToComplete(OpenShiftClient client, Build build) throws MojoExecutionException, InterruptedException {
    final CountDownLatch latch = new CountDownLatch(1);
    final CountDownLatch logTerminateLatch = new CountDownLatch(1);
    final String buildName = KubernetesHelper.getName(build);
    final AtomicReference<Build> buildHolder = new AtomicReference<>();
    // Don't query for logs directly, Watch over the build pod:
    waitUntilPodIsReady(buildName + "-build", 20, log);
    log.info("Waiting for build " + buildName + " to complete...");
    try (LogWatch logWatch = client.pods().withName(buildName + "-build").watchLog()) {
        KubernetesClientUtil.printLogsAsync(logWatch, "Failed to tail build log", logTerminateLatch, log);
        Watcher<Build> buildWatcher = getBuildWatcher(latch, buildName, buildHolder);
        try (Watch watcher = client.builds().withName(buildName).watch(buildWatcher)) {
            // Check if the build is already finished to avoid waiting indefinitely
            Build lastBuild = client.builds().withName(buildName).get();
            String lastStatus = KubernetesResourceUtil.getBuildStatusPhase(lastBuild);
            if (Builds.isFinished(lastStatus)) {
                log.debug("Build %s is already finished", buildName);
                buildHolder.set(lastBuild);
                latch.countDown();
            }
            waitUntilBuildFinished(latch);
            logTerminateLatch.countDown();
            build = buildHolder.get();
            String status = KubernetesResourceUtil.getBuildStatusPhase(build);
            if (Builds.isFailed(status) || Builds.isCancelled(status)) {
                throw new MojoExecutionException("OpenShift Build " + buildName + ": " + KubernetesResourceUtil.getBuildStatusReason(build));
            }
            log.info("Build " + buildName + " " + status);
        }
    }
}
Also used : LogWatch(io.fabric8.kubernetes.client.dsl.LogWatch) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) Build(io.fabric8.openshift.api.model.Build) Watch(io.fabric8.kubernetes.client.Watch) LogWatch(io.fabric8.kubernetes.client.dsl.LogWatch) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 8 with Watch

use of io.fabric8.kubernetes.client.Watch in project fabric8-maven-plugin by fabric8io.

the class WatcherManager method watch.

public static void watch(List<ImageConfiguration> ret, Set<HasMetadata> resources, WatcherContext watcherCtx) throws Exception {
    PluginServiceFactory<WatcherContext> pluginFactory = watcherCtx.isUseProjectClasspath() ? new PluginServiceFactory<>(watcherCtx, ClassUtil.createProjectClassLoader(watcherCtx.getProject(), watcherCtx.getLogger())) : new PluginServiceFactory<>(watcherCtx);
    boolean isOpenshift = KubernetesHelper.isOpenShift(watcherCtx.getKubernetesClient());
    PlatformMode mode = isOpenshift ? PlatformMode.openshift : PlatformMode.kubernetes;
    List<Watcher> watchers = pluginFactory.createServiceObjects("META-INF/fabric8/watcher-default", "META-INF/fabric8/fabric8-watcher-default", "META-INF/fabric8/watcher", "META-INF/fabric8-watcher");
    ProcessorConfig config = watcherCtx.getConfig();
    Logger log = watcherCtx.getLogger();
    List<Watcher> usableWatchers = config.prepareProcessors(watchers, "watcher");
    log.verbose("Watchers:");
    Watcher chosen = null;
    for (Watcher watcher : usableWatchers) {
        if (watcher.isApplicable(ret, resources, mode)) {
            if (chosen == null) {
                log.verbose(" - %s [selected]", watcher.getName());
                chosen = watcher;
            } else {
                log.verbose(" - %s", watcher.getName());
            }
        } else {
            log.verbose(" - %s [not applicable]", watcher.getName());
        }
    }
    if (chosen == null) {
        throw new IllegalStateException("No watchers can be used for the current project");
    }
    log.info("Running watcher %s", chosen.getName());
    chosen.watch(ret, resources, mode);
}
Also used : WatcherContext(io.fabric8.maven.watcher.api.WatcherContext) Watcher(io.fabric8.maven.watcher.api.Watcher) PlatformMode(io.fabric8.maven.core.config.PlatformMode) Logger(io.fabric8.maven.docker.util.Logger) ProcessorConfig(io.fabric8.maven.core.config.ProcessorConfig)

Example 9 with Watch

use of io.fabric8.kubernetes.client.Watch in project fabric8-maven-plugin by fabric8io.

the class WatchMojo method customizeConfig.

@Override
public List<ImageConfiguration> customizeConfig(List<ImageConfiguration> configs) {
    try {
        Fabric8ServiceHub serviceHub = getFabric8ServiceHub();
        GeneratorContext ctx = new GeneratorContext.Builder().config(extractGeneratorConfig()).project(project).session(session).goalFinder(goalFinder).goalName("fabric8:watch").logger(log).mode(mode).strategy(buildStrategy).useProjectClasspath(useProjectClasspath).artifactResolver(serviceHub.getArtifactResolverService()).build();
        return GeneratorManager.generate(configs, ctx, false);
    } catch (MojoExecutionException e) {
        throw new IllegalArgumentException("Cannot extract generator config: " + e, e);
    }
}
Also used : MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) Fabric8ServiceHub(io.fabric8.maven.core.service.Fabric8ServiceHub) GeneratorContext(io.fabric8.maven.generator.api.GeneratorContext)

Example 10 with Watch

use of io.fabric8.kubernetes.client.Watch in project fabric8-maven-plugin by fabric8io.

the class SpringBootWatcher method watch.

@Override
public void watch(List<ImageConfiguration> configs, Set<HasMetadata> resources, PlatformMode mode) throws Exception {
    KubernetesClient kubernetes = getContext().getKubernetesClient();
    PodLogService.PodLogServiceContext logContext = new PodLogService.PodLogServiceContext.Builder().log(log).newPodLog(getContext().getNewPodLogger()).oldPodLog(getContext().getOldPodLogger()).build();
    new PodLogService(logContext).tailAppPodsLogs(kubernetes, getContext().getNamespace(), resources, false, null, true, null, false);
    String url = getServiceExposeUrl(kubernetes, resources);
    if (url == null) {
        url = getPortForwardUrl(resources);
    }
    if (url != null) {
        runRemoteSpringApplication(url);
    } else {
        throw new IllegalStateException("Unable to open a channel to the remote pod.");
    }
}
Also used : KubernetesClient(io.fabric8.kubernetes.client.KubernetesClient) PodLogService(io.fabric8.maven.core.service.PodLogService)

Aggregations

KubernetesClientException (io.fabric8.kubernetes.client.KubernetesClientException)13 Watch (io.fabric8.kubernetes.client.Watch)12 Pod (io.fabric8.kubernetes.api.model.Pod)10 Watcher (io.fabric8.kubernetes.client.Watcher)10 KubernetesClient (io.fabric8.kubernetes.client.KubernetesClient)8 PodList (io.fabric8.kubernetes.api.model.PodList)7 ConfigMap (io.fabric8.kubernetes.api.model.ConfigMap)6 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)6 ArrayList (java.util.ArrayList)5 Test (org.junit.Test)5 Logger (org.slf4j.Logger)5 LoggerFactory (org.slf4j.LoggerFactory)5 HasMetadata (io.fabric8.kubernetes.api.model.HasMetadata)4 Logger (io.fabric8.maven.docker.util.Logger)4 IOException (java.io.IOException)4 DoneablePod (io.fabric8.kubernetes.api.model.DoneablePod)3 MixedOperation (io.fabric8.kubernetes.client.dsl.MixedOperation)3 Build (io.fabric8.openshift.api.model.Build)3 Map (java.util.Map)3 ConfigMapList (io.fabric8.kubernetes.api.model.ConfigMapList)2