use of io.fabric8.arquillian.kubernetes.log.Logger in project fabric8-maven-plugin by fabric8io.
the class KubernetesResourceUtil method mergeResources.
/**
* Merges the given resources together into a single resource.
*
* If switchOnLocalCustomisation is false then the overrides from item2 are merged into item1
*
* @return the newly merged resources
*/
public static HasMetadata mergeResources(HasMetadata item1, HasMetadata item2, Logger log, boolean switchOnLocalCustomisation) {
if (item1 instanceof Deployment && item2 instanceof Deployment) {
return mergeDeployments((Deployment) item1, (Deployment) item2, log, switchOnLocalCustomisation);
}
if (item1 instanceof ConfigMap && item2 instanceof ConfigMap) {
ConfigMap cm1 = (ConfigMap) item1;
ConfigMap cm2 = (ConfigMap) item2;
return mergeConfigMaps(cm1, cm2, log, switchOnLocalCustomisation);
}
mergeMetadata(item1, item2);
return item1;
}
use of io.fabric8.arquillian.kubernetes.log.Logger in project fabric8-maven-plugin by fabric8io.
the class KubernetesResourceUtil method mergeConfigMaps.
protected static HasMetadata mergeConfigMaps(ConfigMap cm1, ConfigMap cm2, Logger log, boolean switchOnLocalCustomisation) {
ConfigMap cm1OrCopy = cm1;
if (!switchOnLocalCustomisation) {
// lets copy the original to avoid modifying it
cm1OrCopy = new ConfigMapBuilder(cm1OrCopy).build();
}
log.info("Merging 2 resources for " + getKind(cm1OrCopy) + " " + getName(cm1OrCopy) + " from " + getSourceUrlAnnotation(cm1OrCopy) + " and " + getSourceUrlAnnotation(cm2) + " and removing " + getSourceUrlAnnotation(cm1OrCopy));
cm1OrCopy.setData(mergeMapsAndRemoveEmptyStrings(cm2.getData(), cm1OrCopy.getData()));
mergeMetadata(cm1OrCopy, cm2);
return cm1OrCopy;
}
use of io.fabric8.arquillian.kubernetes.log.Logger in project fabric8-maven-plugin by fabric8io.
the class KubernetesResourceUtil method addPort.
public static boolean addPort(List<ContainerPort> ports, String portNumberText, String portName, Logger log) {
if (Strings.isNullOrBlank(portNumberText)) {
return false;
}
int portValue;
try {
portValue = Integer.parseInt(portNumberText);
} catch (NumberFormatException e) {
log.warn("Could not parse remote debugging port %s as an integer: %s", portNumberText, e);
return false;
}
for (ContainerPort port : ports) {
String name = port.getName();
Integer containerPort = port.getContainerPort();
if (containerPort != null && containerPort.intValue() == portValue) {
return false;
}
}
ports.add(new ContainerPortBuilder().withName(portName).withContainerPort(portValue).build());
return true;
}
use of io.fabric8.arquillian.kubernetes.log.Logger 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.arquillian.kubernetes.log.Logger in project fabric8-maven-plugin by fabric8io.
the class Fabric8ServiceHubTest method testObtainBuildService.
@Test
public void testObtainBuildService() {
Fabric8ServiceHub hub = new Fabric8ServiceHub.Builder().clusterAccess(clusterAccess).log(logger).platformMode(PlatformMode.kubernetes).dockerServiceHub(dockerServiceHub).buildServiceConfig(buildServiceConfig).build();
BuildService buildService = hub.getBuildService();
assertNotNull(buildService);
assertTrue(buildService instanceof DockerBuildService);
}
Aggregations