Skip to content

Commit

Permalink
Merge pull request #1453 from iocanel/svc-forward
Browse files Browse the repository at this point in the history
  • Loading branch information
fusesource-ci authored Mar 27, 2019
2 parents a2b88d2 + 95218af commit 53d1b16
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
Improvements

* Fix #1425: Preserve labels and fields when using CRD's withResourceVersion()
* Service DSL now includes methods for port forwarding
* Introduce file and dir read / copy from pods

Dependency Upgrade
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,15 @@
*/
package io.fabric8.kubernetes.client.dsl;

public interface ServiceResource<T, D> extends Resource<T, D> {
import io.fabric8.kubernetes.client.LocalPortForward;
import io.fabric8.kubernetes.client.PortForward;

import java.nio.channels.ReadableByteChannel;
import java.nio.channels.WritableByteChannel;

public interface ServiceResource<T, D> extends Resource<T, D> ,
PortForwardable<PortForward, LocalPortForward, ReadableByteChannel, WritableByteChannel> {

String getURL(String portName);

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,12 @@
import io.fabric8.kubernetes.client.dsl.base.HasMetadataOperation;
import io.fabric8.kubernetes.client.dsl.base.OperationContext;
import io.fabric8.kubernetes.client.utils.URLUtils;
import io.fabric8.kubernetes.client.utils.Utils;
import okhttp3.OkHttpClient;


import java.nio.channels.ReadableByteChannel;
import java.nio.channels.WritableByteChannel;
import java.util.*;
import java.util.concurrent.TimeUnit;

Expand Down Expand Up @@ -125,6 +128,33 @@ private String getUrlHelper(String portName) {
return null;
}


private Pod matchingPod() {
Service item = fromServer().get();
Map<String, String> labels = item.getSpec().getSelector();
PodList list = new PodOperationsImpl(client, config).withLabels(labels).list();
return list.getItems().stream().findFirst().orElseThrow(() -> new IllegalStateException("Could not find matching pod for service:" + item + "."));
}

@Override
public PortForward portForward(int port, ReadableByteChannel in, WritableByteChannel out) {
Pod m = matchingPod();
return new PodOperationsImpl(client, config).inNamespace(m.getMetadata().getNamespace()).withName(m.getMetadata().getName()).portForward(port, in, out);
}


@Override
public LocalPortForward portForward(int port, int localPort) {
Pod m = matchingPod();
return new PodOperationsImpl(client, config).inNamespace(m.getMetadata().getNamespace()).withName(m.getMetadata().getName()).portForward(port, localPort);
}

@Override
public LocalPortForward portForward(int port) {
Pod m = matchingPod();
return new PodOperationsImpl(client, config).inNamespace(m.getMetadata().getNamespace()).withName(m.getMetadata().getName()).portForward(port);
}

public class ServiceToUrlSortComparator implements Comparator<ServiceToURLProvider> {
public int compare(ServiceToURLProvider first, ServiceToURLProvider second) {
return first.getPriority() - second.getPriority();
Expand Down

0 comments on commit 53d1b16

Please sign in to comment.