Skip to content

Commit 9ee0219

Browse files
committed
support watchPodsOnLocalNode
1 parent 0695f4d commit 9ee0219

5 files changed

Lines changed: 41 additions & 3 deletions

File tree

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
<groupId>io.github.kubesys</groupId>
77
<artifactId>client-java</artifactId>
8-
<version>0.9.3</version>
8+
<version>0.9.4</version>
99
<packaging>jar</packaging>
1010

1111
<name>client-java</name>

src/main/java/io/github/kubesys/client/KubernetesClient.java

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import java.io.InputStream;
1212
import java.io.InputStreamReader;
1313
import java.math.BigInteger;
14+
import java.net.InetAddress;
1415
import java.security.KeyFactory;
1516
import java.security.KeyStore;
1617
import java.security.KeyStoreException;
@@ -66,8 +67,10 @@
6667
import io.github.kubesys.client.utils.URLUtil;
6768

6869
/**
69-
* This class is used for creating a connection between users' application and Kubernetes server.
70-
* It provides an easy-to-use way to Create, Update, Delete, Get, List and Watch all Kubernetes resources.
70+
* Kubernetes的客户端,根据https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.27/的
71+
* URL规则生产URL
72+
*
73+
* 对于JSON参数,可参见https://kubernetes.io/docs/reference/kubernetes-api/
7174
*
7275
* @author wuheng@iscas.ac.cn
7376
* @since 2.0.0
@@ -676,6 +679,7 @@ public Thread watchResources(String kind, String namespace, KubernetesWatcher wa
676679
+ (namespace == null || "".equals(namespace) ? "all-namespaces" : namespace);
677680
return watchResources(watchName, kind, namespace, watcher);
678681
}
682+
679683

680684
/**
681685
* watch a Kubernetes resources using kind, namespace, and WebSocketListener
@@ -694,6 +698,23 @@ public Thread watchResources(String watchName, String kind, String namespace, Ku
694698
thread.start();
695699
return thread;
696700
}
701+
702+
/**
703+
* watch a Kubernetes resources using kind, namespace, and WebSocketListener
704+
*
705+
* @param watcher watcher
706+
* @return thread thread
707+
* @throws Exception Kubernetes cannot parsing this jsonStr
708+
*/
709+
public Thread watchPodsOnLocalNode(KubernetesWatcher watcher)
710+
throws Exception {
711+
String hostname = InetAddress.getLocalHost().getHostName().toLowerCase();
712+
watcher.setRequest(ReqUtil.get(requester, analyzer.getConvertor()
713+
.watchAllUrlWithFieldSelector("Pod", "", "spec.nodeName=" + hostname)));
714+
Thread thread = new Thread(watcher, "watch-pods-by-hostname");
715+
thread.start();
716+
return thread;
717+
}
697718

698719
/**
699720
* for Scheduler

src/main/java/io/github/kubesys/client/KubernetesConstants.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
/**
77
* @author wuheng@iscas.ac.cn
88
*
9+
*
910
*/
1011
public final class KubernetesConstants {
1112

src/main/java/io/github/kubesys/client/core/KubernetesConvertor.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,19 @@ public String watchAllUrl(String kind, String ns) throws NullPointerException {
162162
KubernetesConstants.HTTP_QUERY_WATCHER_ENABLE);
163163
}
164164

165+
/**
166+
* @param kind kind
167+
* @param ns ns
168+
* @return Url
169+
* @throws NullPointerException exception
170+
*/
171+
public String watchAllUrlWithFieldSelector(String kind, String ns, String fieldSelector) throws NullPointerException {
172+
String fullKind = fullkind(kind);
173+
return URLUtil.join(baseUrl(fullKind, ns, true),
174+
KubernetesConstants.HTTP_QUERY_WATCHER_ENABLE)
175+
+ "&fieldSelector=" + fieldSelector;
176+
}
177+
165178

166179
/*******************************************
167180
*

src/test/java/io/github/kubesys/client/core/ConvertorTest.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,4 +214,7 @@ void testValidWatchAllUrlURL() {
214214
assertEquals("https://39.100.71.73:6443/apis/networking.k8s.io/v1/watch/ingressclasses?watch=true&timeoutSeconds=315360000", convertor.watchAllUrl("networking.k8s.io.IngressClass", ""));
215215
}
216216

217+
public static void main() {
218+
219+
}
217220
}

0 commit comments

Comments
 (0)