Search in sources :

Example 31 with Watch

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

the class DockerImageWatcher method watch.

@Override
public void watch(List<ImageConfiguration> configs, final Set<HasMetadata> resources, PlatformMode mode) {
    BuildService.BuildContext buildContext = getContext().getBuildContext();
    WatchService.WatchContext watchContext = getContext().getWatchContext();
    // add a image customizer
    watchContext = new WatchService.WatchContext.Builder(watchContext).imageCustomizer(new Task<ImageConfiguration>() {

        @Override
        public void execute(ImageConfiguration imageConfiguration) throws DockerAccessException, MojoExecutionException, MojoFailureException {
            buildImage(imageConfiguration);
        }
    }).containerRestarter(new Task<WatchService.ImageWatcher>() {

        @Override
        public void execute(WatchService.ImageWatcher imageWatcher) throws DockerAccessException, MojoExecutionException, MojoFailureException {
            restartContainer(imageWatcher, resources);
        }
    }).build();
    ServiceHub hub = getContext().getServiceHub();
    try {
        hub.getWatchService().watch(watchContext, buildContext, configs);
    } catch (Exception ex) {
        throw new RuntimeException("Error while watching", ex);
    }
}
Also used : Task(io.fabric8.maven.docker.util.Task) ServiceHub(io.fabric8.maven.docker.service.ServiceHub) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) BuildService(io.fabric8.maven.docker.service.BuildService) MojoFailureException(org.apache.maven.plugin.MojoFailureException) DockerAccessException(io.fabric8.maven.docker.access.DockerAccessException) KubernetesClientException(io.fabric8.kubernetes.client.KubernetesClientException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) MojoFailureException(org.apache.maven.plugin.MojoFailureException) ImageConfiguration(io.fabric8.maven.docker.config.ImageConfiguration) DockerAccessException(io.fabric8.maven.docker.access.DockerAccessException) WatchService(io.fabric8.maven.docker.service.WatchService)

Example 32 with Watch

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

the class SpringBootWatcher method runRemoteSpringApplication.

private void runRemoteSpringApplication(String url) {
    log.info("Running RemoteSpringApplication against endpoint: " + url);
    Properties properties = SpringBootUtil.getSpringBootApplicationProperties(getContext().getProject());
    String remoteSecret = properties.getProperty(DEV_TOOLS_REMOTE_SECRET, System.getProperty(DEV_TOOLS_REMOTE_SECRET));
    if (Strings.isNullOrBlank(remoteSecret)) {
        log.warn("There is no `%s` property defined in your src/main/resources/application.properties. Please add one!", DEV_TOOLS_REMOTE_SECRET);
        throw new IllegalStateException("No " + DEV_TOOLS_REMOTE_SECRET + " property defined in application.properties or system properties");
    }
    ClassLoader classLoader = getClass().getClassLoader();
    if (classLoader instanceof URLClassLoader) {
        URLClassLoader pluginClassLoader = (URLClassLoader) classLoader;
        URLClassLoader projectClassLoader = ClassUtil.createProjectClassLoader(getContext().getProject(), log);
        URLClassLoader[] classLoaders = { projectClassLoader, pluginClassLoader };
        StringBuilder buffer = new StringBuilder("java -cp ");
        int count = 0;
        for (URLClassLoader urlClassLoader : classLoaders) {
            URL[] urLs = urlClassLoader.getURLs();
            for (URL u : urLs) {
                if (count++ > 0) {
                    buffer.append(File.pathSeparator);
                }
                try {
                    URI uri = u.toURI();
                    File file = new File(uri);
                    buffer.append(file.getCanonicalPath());
                } catch (Exception e) {
                    throw new IllegalStateException("Failed to create classpath: " + e, e);
                }
            }
        }
        // Add dev tools to the classpath (the main class is not read from BOOT-INF/lib)
        try {
            File devtools = getSpringBootDevToolsJar(getContext().getProject());
            buffer.append(File.pathSeparator);
            buffer.append(devtools.getCanonicalPath());
        } catch (Exception e) {
            throw new IllegalStateException("Failed to include devtools in the classpath: " + e, e);
        }
        buffer.append(" -Dspring.devtools.remote.secret=");
        buffer.append(remoteSecret);
        buffer.append(" org.springframework.boot.devtools.RemoteSpringApplication ");
        buffer.append(url);
        try {
            String command = buffer.toString();
            log.debug("Running: " + command);
            final Process process = Runtime.getRuntime().exec(command);
            final AtomicBoolean outputEnabled = new AtomicBoolean(true);
            Runtime.getRuntime().addShutdownHook(new Thread("fabric8:watch [spring-boot] shutdown hook") {

                @Override
                public void run() {
                    log.info("Terminating the Spring remote client...");
                    outputEnabled.set(false);
                    process.destroy();
                }
            });
            Logger logger = new PrefixedLogger("Spring-Remote", log);
            Thread stdOutPrinter = startOutputProcessor(logger, process.getInputStream(), false, outputEnabled);
            Thread stdErrPrinter = startOutputProcessor(logger, process.getErrorStream(), true, outputEnabled);
            int status = process.waitFor();
            stdOutPrinter.join();
            stdErrPrinter.join();
            if (status != 0) {
                log.warn("Process returned status: %s", status);
            }
        } catch (Exception e) {
            throw new RuntimeException("Failed to run RemoteSpringApplication: " + e, e);
        }
    } else {
        throw new IllegalStateException("ClassLoader must be a URLClassLoader but it is: " + classLoader.getClass().getName());
    }
}
Also used : Properties(java.util.Properties) Logger(io.fabric8.maven.docker.util.Logger) PrefixedLogger(io.fabric8.maven.core.util.PrefixedLogger) URI(java.net.URI) URL(java.net.URL) IOException(java.io.IOException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) PrefixedLogger(io.fabric8.maven.core.util.PrefixedLogger) URLClassLoader(java.net.URLClassLoader) URLClassLoader(java.net.URLClassLoader) File(java.io.File)

Example 33 with Watch

use of io.fabric8.kubernetes.client.Watch in project watchdog by isdream.

the class KubernetesListener method start.

@SuppressWarnings({ "rawtypes", "unchecked" })
@Override
public void start(Object client, Configure config) throws Exception {
    // System.out.println(config.getApiVersion());
    if (ObjectUtils.isNull(client) && ObjectUtils.isNull(config)) {
        throw new Exception("Invalid paremeters");
    }
    // it may throw Exception if wrong PROPERTY_KIND values
    Object model = getModelParamtersGenerator().getKindModel(client, config.getKind());
    if (model instanceof Namespaceable || model instanceof AnyNamespaceable) {
        String namespace = config.getProperties().getOrDefault(PROPERTY_NAMESPACE, ALL_NAMESPACE);
        if (ALL_NAMESPACE.equals(namespace)) {
            model = ((AnyNamespaceable) model).inAnyNamespace();
        } else {
            model = ((Namespaceable) model).inNamespace(namespace);
        }
    }
    List<String> handlers = config.getHandlers();
    if (handlers.isEmpty()) {
        throw new Exception("No handlers");
    }
    Handler handler = (Handler) Class.forName(handlers.remove(0)).newInstance();
    Handler thisHandler = handler;
    for (String name : handlers) {
        Handler nextHandler = (Handler) Class.forName(name).newInstance();
        thisHandler.setNextHandler(nextHandler);
        thisHandler = nextHandler;
    }
    System.out.println(model.getClass());
    ((Watchable) model).watch(new KubernetesWatcher(handler));
}
Also used : Watchable(io.fabric8.kubernetes.client.dsl.Watchable) Handler(com.github.isdream.cwatcher.Handler) Namespaceable(io.fabric8.kubernetes.client.dsl.Namespaceable) AnyNamespaceable(io.fabric8.kubernetes.client.dsl.AnyNamespaceable) AnyNamespaceable(io.fabric8.kubernetes.client.dsl.AnyNamespaceable) KubernetesClientException(io.fabric8.kubernetes.client.KubernetesClientException)

Example 34 with Watch

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

the class KubernetesConfigAdminBridgeTest method testAndOr.

@Test
public void testAndOr() {
    System.setProperty("fabric8.pid.filters", "appName=A;B,database.name=my.oracle.datasource");
    KubernetesMockServer plainServer = new KubernetesMockServer(false);
    plainServer.expect().get().withPath("/api/v1/namespaces/test/configmaps?labelSelector=karaf.pid,appName%20in%20(A,B),database.name%20in%20(my.oracle.datasource)&watch=true").andReturnChunked(200).always();
    plainServer.expect().get().withPath("/api/v1/namespaces/test/configmaps?labelSelector=karaf.pid,appName%20in%20(A,B),database.name%20in%20(my.oracle.datasource)").andReturn(200, cmEmptyList).once();
    KubernetesConfigAdminBridge kcab = new KubernetesConfigAdminBridge();
    kcab.bindConfigAdmin(caService);
    kcab.bindKubernetesClient(plainServer.createClient());
    kcab.activate();
}
Also used : KubernetesMockServer(io.fabric8.kubernetes.client.server.mock.KubernetesMockServer) Test(org.junit.Test)

Example 35 with Watch

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

the class KubernetesConfigAdminBridgeTest method testAand.

@Test
public void testAand() {
    System.setProperty("fabric8.pid.filters", "appName=A,database.name=my.oracle.datasource");
    KubernetesMockServer plainServer = new KubernetesMockServer(false);
    plainServer.expect().get().withPath("/api/v1/namespaces/test/configmaps?labelSelector=karaf.pid,appName%20in%20(A),database.name%20in%20(my.oracle.datasource)&watch=true").andReturnChunked(200).always();
    plainServer.expect().get().withPath("/api/v1/namespaces/test/configmaps?labelSelector=karaf.pid,appName%20in%20(A),database.name%20in%20(my.oracle.datasource)").andReturn(200, cmEmptyList).once();
    KubernetesConfigAdminBridge kcab = new KubernetesConfigAdminBridge();
    kcab.bindConfigAdmin(caService);
    kcab.bindKubernetesClient(plainServer.createClient());
    kcab.activate();
}
Also used : KubernetesMockServer(io.fabric8.kubernetes.client.server.mock.KubernetesMockServer) Test(org.junit.Test)

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