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);
}
}
}
}
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!");
}
}
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();
}
}
});
}
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());
}
}
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());
}
}
Aggregations