Search in sources :

Example 31 with Command

use of io.fabric8.gateway.handlers.detecting.protocol.openwire.command.Command in project fabric8-maven-plugin by fabric8io.

the class ApplyMojo method applyEntities.

protected void applyEntities(Controller controller, KubernetesClient kubernetes, String namespace, String fileName, Set<HasMetadata> entities) throws Exception {
    // Apply all items
    for (HasMetadata entity : entities) {
        if (entity instanceof Pod) {
            Pod pod = (Pod) entity;
            controller.applyPod(pod, fileName);
        } else if (entity instanceof Service) {
            Service service = (Service) entity;
            controller.applyService(service, fileName);
        } else if (entity instanceof ReplicationController) {
            ReplicationController replicationController = (ReplicationController) entity;
            controller.applyReplicationController(replicationController, fileName);
        } else if (entity != null) {
            controller.apply(entity, fileName);
        }
    }
    String command = clusterAccess.isOpenShiftImageStream(log) ? "oc" : "kubectl";
    log.info("[[B]]HINT:[[B]] Use the command `%s get pods -w` to watch your pods start up", command);
    Logger serviceLogger = createExternalProcessLogger("[[G]][SVC][[G]] ");
    long serviceUrlWaitTimeSeconds = this.serviceUrlWaitTimeSeconds;
    for (HasMetadata entity : entities) {
        if (entity instanceof Service) {
            Service service = (Service) entity;
            String name = getName(service);
            Resource<Service, DoneableService> serviceResource = kubernetes.services().inNamespace(namespace).withName(name);
            String url = null;
            // lets wait a little while until there is a service URL in case the exposecontroller is running slow
            for (int i = 0; i < serviceUrlWaitTimeSeconds; i++) {
                if (i > 0) {
                    Thread.sleep(1000);
                }
                Service s = serviceResource.get();
                if (s != null) {
                    url = getExternalServiceURL(s);
                    if (Strings.isNotBlank(url)) {
                        break;
                    }
                }
                if (!isExposeService(service)) {
                    break;
                }
            }
            // lets not wait for other services
            serviceUrlWaitTimeSeconds = 1;
            if (Strings.isNotBlank(url) && url.startsWith("http")) {
                serviceLogger.info("" + name + ": " + url);
            }
        }
    }
}
Also used : HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) Pod(io.fabric8.kubernetes.api.model.Pod) DoneableService(io.fabric8.kubernetes.api.model.DoneableService) ReplicationController(io.fabric8.kubernetes.api.model.ReplicationController) DoneableService(io.fabric8.kubernetes.api.model.DoneableService) Service(io.fabric8.kubernetes.api.model.Service) KubernetesHelper.createIntOrString(io.fabric8.kubernetes.api.KubernetesHelper.createIntOrString) Logger(io.fabric8.maven.docker.util.Logger)

Example 32 with Command

use of io.fabric8.gateway.handlers.detecting.protocol.openwire.command.Command in project fabric8-maven-plugin by fabric8io.

the class PodLogService method tailAppPodsLogs.

public void tailAppPodsLogs(final KubernetesClient kubernetes, final String namespace, final Set<HasMetadata> entities, boolean watchAddedPodsOnly, String onExitOperation, boolean followLog, Date ignorePodsOlderThan, boolean waitInCurrentThread) {
    LabelSelector selector = KubernetesResourceUtil.getPodLabelSelector(entities);
    if (selector != null) {
        String ctrlCMessage = "stop tailing the log";
        if (Strings.isNotBlank(onExitOperation)) {
            final String onExitOperationLower = onExitOperation.toLowerCase().trim();
            if (onExitOperationLower.equals(OPERATION_UNDEPLOY)) {
                ctrlCMessage = "undeploy the app";
            } else if (onExitOperationLower.equals(OPERATION_STOP)) {
                ctrlCMessage = "scale down the app and stop tailing the log";
            } else {
                log.warn("Unknown on-exit command: `%s`", onExitOperationLower);
            }
            resizeApp(kubernetes, namespace, entities, 1, log);
            Runtime.getRuntime().addShutdownHook(new Thread("pod log service shutdown hook") {

                @Override
                public void run() {
                    if (onExitOperationLower.equals(OPERATION_UNDEPLOY)) {
                        log.info("Undeploying the app:");
                        deleteEntities(kubernetes, namespace, entities, context.getS2iBuildNameSuffix(), log);
                    } else if (onExitOperationLower.equals(OPERATION_STOP)) {
                        log.info("Stopping the app:");
                        resizeApp(kubernetes, namespace, entities, 0, log);
                    }
                    if (podWatcher != null) {
                        podWatcher.close();
                    }
                    closeLogWatcher();
                }
            });
        }
        waitAndLogPods(kubernetes, namespace, selector, watchAddedPodsOnly, ctrlCMessage, followLog, ignorePodsOlderThan, waitInCurrentThread);
    } else {
        log.warn("No selector in deployment so cannot watch pods!");
    }
}
Also used : LabelSelector(io.fabric8.kubernetes.api.model.LabelSelector)

Example 33 with Command

use of io.fabric8.gateway.handlers.detecting.protocol.openwire.command.Command in project fabric8-maven-plugin by fabric8io.

the class VolumePermissionEnricher method adapt.

@Override
public void adapt(KubernetesListBuilder builder) {
    builder.accept(new TypedVisitor<PodTemplateSpecBuilder>() {

        @Override
        public void visit(PodTemplateSpecBuilder builder) {
            PodSpec podSpec = builder.buildSpec();
            if (podSpec == null) {
                return;
            }
            if (!checkForPvc(podSpec)) {
                return;
            }
            List<Container> containers = podSpec.getContainers();
            if (containers == null || containers.isEmpty()) {
                return;
            }
            log.verbose("Adding init container for changing persistent volumes access mode to %s", getConfig(Config.permission));
            if (!initContainerHandler.hasInitContainer(builder, ENRICHER_NAME)) {
                initContainerHandler.appendInitContainer(builder, createPvInitContainer(podSpec));
            }
        }

        private boolean checkForPvc(PodSpec podSpec) {
            List<Volume> volumes = podSpec.getVolumes();
            if (volumes != null) {
                for (Volume volume : volumes) {
                    PersistentVolumeClaimVolumeSource persistentVolumeClaim = volume.getPersistentVolumeClaim();
                    if (persistentVolumeClaim != null) {
                        return true;
                    }
                }
            }
            return false;
        }

        private JSONObject createPvInitContainer(PodSpec podSpec) {
            Map<String, String> mountPoints = extractMountPoints(podSpec);
            JSONObject entry = new JSONObject();
            entry.put("name", ENRICHER_NAME);
            entry.put("image", "busybox");
            entry.put("imagePullPolicy", "IfNotPresent");
            entry.put("command", createChmodCommandArray(mountPoints));
            entry.put("volumeMounts", createMounts(mountPoints));
            return entry;
        }

        private JSONArray createChmodCommandArray(Map<String, String> mountPoints) {
            JSONArray ret = new JSONArray();
            ret.put("chmod");
            ret.put(getConfig(Config.permission));
            Set<String> uniqueNames = new LinkedHashSet<>(mountPoints.values());
            for (String name : uniqueNames) {
                ret.put(name);
            }
            return ret;
        }

        private JSONArray createMounts(Map<String, String> mountPoints) {
            JSONArray ret = new JSONArray();
            for (Map.Entry<String, String> entry : mountPoints.entrySet()) {
                JSONObject mount = new JSONObject();
                mount.put("name", entry.getKey());
                mount.put("mountPath", entry.getValue());
                ret.put(mount);
            }
            return ret;
        }

        private Map<String, String> extractMountPoints(PodSpec podSpec) {
            Map<String, String> nameToMount = new LinkedHashMap<>();
            List<Volume> volumes = podSpec.getVolumes();
            if (volumes != null) {
                for (Volume volume : volumes) {
                    PersistentVolumeClaimVolumeSource persistentVolumeClaim = volume.getPersistentVolumeClaim();
                    if (persistentVolumeClaim != null) {
                        String name = volume.getName();
                        String mountPath = getMountPath(podSpec.getContainers(), name);
                        nameToMount.put(name, mountPath);
                    }
                }
            }
            return nameToMount;
        }

        private String getMountPath(List<Container> containers, String name) {
            for (Container container : containers) {
                List<VolumeMount> volumeMounts = container.getVolumeMounts();
                if (volumeMounts != null) {
                    for (VolumeMount volumeMount : volumeMounts) {
                        if (name.equals(volumeMount.getName())) {
                            return volumeMount.getMountPath();
                        }
                    }
                }
            }
            throw new IllegalArgumentException("No matching volume mount found for volume " + name);
        }
    });
    builder.accept(new TypedVisitor<PersistentVolumeClaimBuilder>() {

        @Override
        public void visit(PersistentVolumeClaimBuilder pvcBuilder) {
            // lets ensure we have a default storage class so that PVs will get dynamically created OOTB
            if (pvcBuilder.buildMetadata() == null) {
                pvcBuilder.withNewMetadata().endMetadata();
            }
            String storageClass = getConfig(Config.defaultStorageClass);
            if (Strings.isNotBlank(storageClass) && !pvcBuilder.buildMetadata().getAnnotations().containsKey(VOLUME_STORAGE_CLASS_ANNOTATION)) {
                pvcBuilder.editMetadata().addToAnnotations(VOLUME_STORAGE_CLASS_ANNOTATION, storageClass).endMetadata();
            }
        }
    });
}
Also used : Set(java.util.Set) LinkedHashSet(java.util.LinkedHashSet) PodSpec(io.fabric8.kubernetes.api.model.PodSpec) JSONArray(org.json.JSONArray) PersistentVolumeClaimBuilder(io.fabric8.kubernetes.api.model.PersistentVolumeClaimBuilder) PersistentVolumeClaimVolumeSource(io.fabric8.kubernetes.api.model.PersistentVolumeClaimVolumeSource) Container(io.fabric8.kubernetes.api.model.Container) JSONObject(org.json.JSONObject) Volume(io.fabric8.kubernetes.api.model.Volume) PodTemplateSpecBuilder(io.fabric8.kubernetes.api.model.PodTemplateSpecBuilder) VolumeMount(io.fabric8.kubernetes.api.model.VolumeMount) List(java.util.List) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 34 with Command

use of io.fabric8.gateway.handlers.detecting.protocol.openwire.command.Command in project fabric8-maven-plugin by fabric8io.

the class VolumePermissionEnricherTest method testAdapt.

@Test
public void testAdapt() throws Exception {
    final TestConfig[] data = new TestConfig[] { new TestConfig(null, null), new TestConfig(null, VolumePermissionEnricher.ENRICHER_NAME, "volumeA"), new TestConfig(null, VolumePermissionEnricher.ENRICHER_NAME, "volumeA", "volumeB") };
    for (final TestConfig tc : data) {
        final ProcessorConfig config = new ProcessorConfig(null, null, Collections.singletonMap(VolumePermissionEnricher.ENRICHER_NAME, new TreeMap(Collections.singletonMap(VolumePermissionEnricher.Config.permission.name(), tc.permission))));
        // Setup mock behaviour
        new Expectations() {

            {
                context.getConfig();
                result = config;
            }
        };
        VolumePermissionEnricher enricher = new VolumePermissionEnricher(context);
        PodTemplateBuilder ptb = createEmptyPodTemplate();
        for (String vn : tc.volumeNames) {
            ptb = addVolume(ptb, vn);
        }
        KubernetesListBuilder klb = new KubernetesListBuilder().addToPodTemplateItems(ptb.build());
        enricher.adapt(klb);
        PodTemplate pt = (PodTemplate) klb.buildItem(0);
        String initContainers = pt.getTemplate().getMetadata().getAnnotations().get(InitContainerHandler.INIT_CONTAINER_ANNOTATION);
        boolean shouldHaveInitContainer = tc.volumeNames.length > 0;
        assertEquals(shouldHaveInitContainer, initContainers != null);
        if (!shouldHaveInitContainer) {
            continue;
        }
        JSONArray ja = new JSONArray(initContainers);
        assertEquals(1, ja.length());
        JSONObject jo = ja.getJSONObject(0);
        assertEquals(tc.initContainerName, jo.get("name"));
        String permission = Strings.isNullOrBlank(tc.permission) ? "777" : tc.permission;
        JSONArray chmodCmd = new JSONArray();
        chmodCmd.put("chmod");
        chmodCmd.put(permission);
        for (String vn : tc.volumeNames) {
            chmodCmd.put("/tmp/" + vn);
        }
        assertEquals(chmodCmd.toString(), jo.getJSONArray("command").toString());
    }
}
Also used : Expectations(mockit.Expectations) JSONArray(org.json.JSONArray) TreeMap(java.util.TreeMap) ProcessorConfig(io.fabric8.maven.core.config.ProcessorConfig) JSONObject(org.json.JSONObject) Test(org.junit.Test)

Example 35 with Command

use of io.fabric8.gateway.handlers.detecting.protocol.openwire.command.Command 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)

Aggregations

IOException (java.io.IOException)9 File (java.io.File)6 HashMap (java.util.HashMap)6 Map (java.util.Map)6 Container (io.fabric8.api.Container)5 JMXRequest (io.fabric8.api.commands.JMXRequest)4 KubernetesClient (io.fabric8.kubernetes.client.KubernetesClient)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)3 FabricService (io.fabric8.api.FabricService)3 ContainerCreateConfig (io.fabric8.maven.docker.access.ContainerCreateConfig)3 Arguments (io.fabric8.maven.docker.config.Arguments)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 Properties (java.util.Properties)3 TemplatedResource (com.netflix.spinnaker.halyard.core.resource.v1.TemplatedResource)2 ArtifactService (com.netflix.spinnaker.halyard.deploy.services.v1.ArtifactService)2 GenerateService (com.netflix.spinnaker.halyard.deploy.services.v1.GenerateService)2 SpinnakerRuntimeSettings (com.netflix.spinnaker.halyard.deploy.spinnaker.v1.SpinnakerRuntimeSettings)2 Container (io.fabric8.kubernetes.api.model.Container)2