Skip to content

Commit 2fe7f34

Browse files
committed
add listWithFields
1 parent 81f1beb commit 2fe7f34

5 files changed

Lines changed: 100 additions & 8 deletions

File tree

pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@
55

66
<groupId>io.github.kubesys</groupId>
77
<artifactId>kubernetes-client</artifactId>
8-
<version>2.2.0</version>
8+
<version>2.2.1</version>
99
<packaging>jar</packaging>
1010

1111
<name>kubernetes-client</name>
1212
<url>http://maven.apache.org</url>
1313

1414
<properties>
15-
<jackson.version>2.13.4</jackson.version>
15+
<jackson.version>2.14.0-rc2</jackson.version>
1616
<httpclient.version>5.1.3</httpclient.version>
1717
<junit.version>4.13.2</junit.version>
1818
</properties>

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

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
import io.github.kubesys.client.core.KubernetesRuleBase;
6464
import io.github.kubesys.client.utils.ReqUtil;
6565
import io.github.kubesys.client.utils.SSLUtil;
66+
import io.github.kubesys.client.utils.URLUtil;
6667

6768
/**
6869
* This class is used for creating a connection between users' application and Kubernetes server.
@@ -356,6 +357,28 @@ public boolean hasResource(String kind, String namespace, String name) throws Ex
356357
public JsonNode listResources(String kind) throws Exception {
357358
return listResources(kind, KubernetesConstants.VALUE_ALL_NAMESPACES, null, null, 0, null);
358359
}
360+
361+
/**
362+
* list all Kubernetes resources using kind
363+
*
364+
* @param kind kind
365+
* @return json json
366+
* @throws Exception Kubernetes cannot parsing this jsonStr
367+
*/
368+
public JsonNode listResourcesWithField(String kind, Map<String, String> fields) throws Exception {
369+
return listResources(kind, KubernetesConstants.VALUE_ALL_NAMESPACES, URLUtil.fromMap(fields), null, 0, null);
370+
}
371+
372+
/**
373+
* list all Kubernetes resources using kind
374+
*
375+
* @param kind kind
376+
* @return json json
377+
* @throws Exception Kubernetes cannot parsing this jsonStr
378+
*/
379+
public JsonNode listResourcesWithLabel(String kind, Map<String, String> labels) throws Exception {
380+
return listResources(kind, KubernetesConstants.VALUE_ALL_NAMESPACES, null, URLUtil.fromMap(labels), 0, null);
381+
}
359382

360383
/**
361384
* list all Kubernetes resources using kind and namespace
@@ -368,6 +391,29 @@ public JsonNode listResources(String kind) throws Exception {
368391
public JsonNode listResources(String kind, String namespace) throws Exception {
369392
return listResources(kind, namespace, null, null, 0, null);
370393
}
394+
395+
/**
396+
* list all Kubernetes resources using kind
397+
*
398+
* @param kind kind
399+
* @return json json
400+
* @throws Exception Kubernetes cannot parsing this jsonStr
401+
*/
402+
public JsonNode listResourcesWithField(String kind, String namespace, Map<String, String> fields) throws Exception {
403+
return listResources(kind, namespace, URLUtil.fromMap(fields), null, 0, null);
404+
}
405+
406+
/**
407+
* list all Kubernetes resources using kind and namespace
408+
*
409+
* @param kind kind
410+
* @param namespace namespace
411+
* @return json json
412+
* @throws Exception Kubernetes cannot parsing this jsonStr
413+
*/
414+
public JsonNode listResourcesWithLabel(String kind, String namespace, Map<String, String> labels) throws Exception {
415+
return listResources(kind, namespace, null, URLUtil.fromMap(labels), 0, null);
416+
}
371417

372418
/**
373419
* list all Kubernetes resources using kind, namespace, fieldSelector and

src/main/java/io/github/kubesys/client/utils/URLUtil.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import java.net.MalformedURLException;
77
import java.net.URL;
8+
import java.util.Map;
89
import java.util.logging.Logger;
910

1011
import io.github.kubesys.client.KubernetesConstants;
@@ -77,4 +78,19 @@ public static String namespacePath(boolean namespaced, String namespace) {
7778
? KubernetesConstants.KUBEAPI_NAMESPACES_PATTERN + namespace
7879
: KubernetesConstants.VALUE_ALL_NAMESPACES;
7980
}
81+
82+
public static String fromMap(Map<String, String> map) {
83+
//%3D
84+
if (map == null || map.size() == 0) {
85+
return null;
86+
}
87+
88+
89+
StringBuilder sb = new StringBuilder();
90+
for (String key : map.keySet()) {
91+
sb.append(key + "%3D" + map.get(key) + ",");
92+
}
93+
94+
return sb.substring(0, sb.length() - 1);
95+
}
8096
}

src/test/java/io/github/kubesys/client/AbstractKubernetesClientTest.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,8 @@
1111
*/
1212
public abstract class AbstractKubernetesClientTest {
1313

14-
static String URL = "https://120.46.180.58:6443/";
15-
16-
// kubectl -n kube-system get secret $(kubectl -n kube-system get secret | grep kuboard-user | awk '{print $1}') -o go-template='{{.data.token}}' | base64 -d
17-
static String TOKEN = "";
18-
1914
public static KubernetesClient createClient1(KubernetesAnalyzer ana) throws Exception {
20-
return (ana == null) ? new KubernetesClient(URL, TOKEN) : new KubernetesClient(URL, TOKEN, ana);
15+
return (ana == null) ? new KubernetesClient(new File(".token")) : new KubernetesClient(new File(".token"), ana);
2116
}
2217

2318
public static KubernetesClient createClient2(KubernetesAnalyzer ana) throws Exception {
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* Copyright (2020, ) Institute of Software, Chinese Academy of Sciences
3+
*/
4+
package io.github.kubesys.client.testcases;
5+
6+
import java.util.HashMap;
7+
import java.util.Map;
8+
9+
import io.github.kubesys.client.AbstractKubernetesClientTest;
10+
import io.github.kubesys.client.KubernetesClient;
11+
12+
/**
13+
* @author wuheng09@gmail.com
14+
*
15+
*/
16+
public class ListNodesTest extends AbstractKubernetesClientTest {
17+
18+
19+
/**
20+
* see command `kubectl api-resources`
21+
*
22+
* @param args
23+
* @throws Exception
24+
*/
25+
public static void main(String[] args) throws Exception {
26+
KubernetesClient client = createClient1(null);
27+
Map<String, String> map= new HashMap<>();
28+
map.put("host", "vm.node133");
29+
// Just kind
30+
System.out.println(client.listResources("Node").get("items").size());
31+
System.out.println(client.listResourcesWithLabel("Node", map).get("items").size());
32+
33+
}
34+
35+
}

0 commit comments

Comments
 (0)