use of io.fabric8.kubernetes.api.model.LabelSelector in project fabric8 by fabric8io.
the class KubernetesConfigAdminBridgeTest method testOr.
@Test
public void testOr() {
System.setProperty("fabric8.pid.filters", "appName=A;B");
KubernetesMockServer plainServer = new KubernetesMockServer(false);
plainServer.expect().get().withPath("/api/v1/namespaces/test/configmaps?labelSelector=karaf.pid,appName%20in%20(A,B)&watch=true").andReturnChunked(200).always();
plainServer.expect().get().withPath("/api/v1/namespaces/test/configmaps?labelSelector=karaf.pid,appName%20in%20(A,B)").andReturn(200, cmEmptyList).once();
KubernetesConfigAdminBridge kcab = new KubernetesConfigAdminBridge();
kcab.bindConfigAdmin(caService);
kcab.bindKubernetesClient(plainServer.createClient());
kcab.activate();
}
use of io.fabric8.kubernetes.api.model.LabelSelector in project fabric8-maven-plugin by fabric8io.
the class DebugMojo method waitForRunningPodWithEnvVar.
private String waitForRunningPodWithEnvVar(final KubernetesClient kubernetes, final String namespace, LabelSelector selector, final Map<String, String> envVars) throws MojoExecutionException {
// wait for the newest pod to be ready with the given env var
FilterWatchListDeletable<Pod, PodList, Boolean, Watch, Watcher<Pod>> pods = withSelector(kubernetes.pods().inNamespace(namespace), selector, log);
log.info("Waiting for debug pod with selector " + selector + " and environment variables " + envVars);
podWaitLog = createExternalProcessLogger("[[Y]][W][[Y]] ");
PodList list = pods.list();
if (list != null) {
Pod latestPod = KubernetesResourceUtil.getNewestPod(list.getItems());
if (latestPod != null && podHasEnvVars(latestPod, envVars)) {
return getName(latestPod);
}
}
podWatcher = pods.watch(new Watcher<Pod>() {
@Override
public void eventReceived(Watcher.Action action, Pod pod) {
podWaitLog.info(getName(pod) + " status: " + getPodStatusDescription(pod) + getPodStatusMessagePostfix(action));
if (isAddOrModified(action) && isPodRunning(pod) && isPodReady(pod) && podHasEnvVars(pod, envVars)) {
foundPod = pod;
terminateLatch.countDown();
}
}
@Override
public void onClose(KubernetesClientException e) {
// ignore
}
});
// now lets wait forever?
while (terminateLatch.getCount() > 0) {
try {
terminateLatch.await();
} catch (InterruptedException e) {
// ignore
}
if (foundPod != null) {
return getName(foundPod);
}
}
throw new MojoExecutionException("Could not find a running pod with environment variables " + envVars);
}
use of io.fabric8.kubernetes.api.model.LabelSelector in project fabric8-maven-plugin by fabric8io.
the class KubernetesResourceUtil method getPodLabelSelector.
public static LabelSelector getPodLabelSelector(HasMetadata entity) {
LabelSelector selector = null;
if (entity instanceof Deployment) {
Deployment resource = (Deployment) entity;
DeploymentSpec spec = resource.getSpec();
if (spec != null) {
selector = spec.getSelector();
}
} else if (entity instanceof ReplicaSet) {
ReplicaSet resource = (ReplicaSet) entity;
ReplicaSetSpec spec = resource.getSpec();
if (spec != null) {
selector = spec.getSelector();
}
} else if (entity instanceof DeploymentConfig) {
DeploymentConfig resource = (DeploymentConfig) entity;
DeploymentConfigSpec spec = resource.getSpec();
if (spec != null) {
selector = toLabelSelector(spec.getSelector());
}
} else if (entity instanceof ReplicationController) {
ReplicationController resource = (ReplicationController) entity;
ReplicationControllerSpec spec = resource.getSpec();
if (spec != null) {
selector = toLabelSelector(spec.getSelector());
}
} else if (entity instanceof DaemonSet) {
DaemonSet resource = (DaemonSet) entity;
DaemonSetSpec spec = resource.getSpec();
if (spec != null) {
selector = spec.getSelector();
}
} else if (entity instanceof StatefulSet) {
StatefulSet resource = (StatefulSet) entity;
StatefulSetSpec spec = resource.getSpec();
if (spec != null) {
selector = spec.getSelector();
}
} else if (entity instanceof Job) {
Job resource = (Job) entity;
JobSpec spec = resource.getSpec();
if (spec != null) {
selector = spec.getSelector();
}
}
return selector;
}
use of io.fabric8.kubernetes.api.model.LabelSelector 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.kubernetes.api.model.LabelSelector 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!");
}
}
Aggregations