[BAEL-4945] Using Namespaces and Selectors With the Kubernetes Java API (#10745)

* [BAEL-4863] Inicial code

* [BAEL-4863] Article code

* [BAEL-4945]
This commit is contained in:
psevestre 2021-05-08 22:33:27 -03:00 committed by GitHub
parent 6e02759f10
commit 99213ea74c
6 changed files with 328 additions and 0 deletions

View File

@ -0,0 +1,58 @@
apiVersion: v1
kind: Namespace
metadata:
name: ns1
---
apiVersion: v1
kind: Namespace
metadata:
name: ns2
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: httpd
namespace: ns1
labels:
app: httpd
version: "1"
spec:
replicas: 3
selector:
matchLabels:
app: httpd
template:
metadata:
labels:
app: httpd
spec:
containers:
- name: main
image: httpd:alpine
ports:
- containerPort: 80
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: httpd
namespace: ns2
labels:
app: httpd
version: "2"
foo: bar
spec:
replicas: 3
selector:
matchLabels:
app: httpd
template:
metadata:
labels:
app: httpd
spec:
containers:
- name: main
image: httpd:alpine
ports:
- containerPort: 80

View File

@ -0,0 +1,68 @@
/**
*
*/
package com.baeldung.kubernetes.intro;
import java.util.concurrent.TimeUnit;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import io.kubernetes.client.openapi.ApiClient;
import io.kubernetes.client.openapi.apis.CoreV1Api;
import io.kubernetes.client.openapi.models.V1NodeList;
import io.kubernetes.client.openapi.models.V1PodList;
import io.kubernetes.client.util.Config;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
/**
* @author Philippe
*
*/
public class ListPodsWithFieldSelectors {
private static final Logger log = LoggerFactory.getLogger(ListPodsWithFieldSelectors.class);
/**
* @param args
*/
public static void main(String[] args) throws Exception {
ApiClient client = Config.defaultClient();
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor(message -> log.info(message));
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient newClient = client.getHttpClient()
.newBuilder()
.addInterceptor(interceptor)
.readTimeout(0, TimeUnit.SECONDS)
.build();
client.setHttpClient(newClient);
CoreV1Api api = new CoreV1Api(client);
String fs = createSelector(args);
V1PodList items = api.listPodForAllNamespaces(null, null, fs, null, null, null, null, null, 10, false);
items.getItems()
.stream()
.map((pod) -> pod.getMetadata().getName() )
.forEach((name) -> System.out.println("name=" + name));
}
private static String createSelector(String[] args) {
StringBuilder b = new StringBuilder();
for( int i = 0 ; i < args.length; i++ ) {
if( b.length() > 0 ) {
b.append(',');
}
b.append(args[i]);
}
return b.toString();
}
}

View File

@ -0,0 +1,68 @@
/**
*
*/
package com.baeldung.kubernetes.intro;
import java.util.concurrent.TimeUnit;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import io.kubernetes.client.openapi.ApiClient;
import io.kubernetes.client.openapi.apis.CoreV1Api;
import io.kubernetes.client.openapi.models.V1NodeList;
import io.kubernetes.client.openapi.models.V1PodList;
import io.kubernetes.client.util.Config;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
/**
* @author Philippe
*
*/
public class ListPodsWithLabelSelectors {
private static final Logger log = LoggerFactory.getLogger(ListPodsWithLabelSelectors.class);
/**
* @param args
*/
public static void main(String[] args) throws Exception {
ApiClient client = Config.defaultClient();
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor(message -> log.info(message));
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient newClient = client.getHttpClient()
.newBuilder()
.addInterceptor(interceptor)
.readTimeout(0, TimeUnit.SECONDS)
.build();
client.setHttpClient(newClient);
CoreV1Api api = new CoreV1Api(client);
String selector = createSelector(args);
V1PodList items = api.listPodForAllNamespaces(null, null, null, selector, null, null, null, null, 10, false);
items.getItems()
.stream()
.map((pod) -> pod.getMetadata().getName() )
.forEach((name) -> System.out.println("name=" + name));
}
private static String createSelector(String[] args) {
StringBuilder b = new StringBuilder();
for( int i = 0 ; i < args.length; i++ ) {
if( b.length() > 0 ) {
b.append(',');
}
b.append(args[i]);
}
return b.toString();
}
}

View File

@ -0,0 +1,52 @@
/**
*
*/
package com.baeldung.kubernetes.intro;
import java.util.concurrent.TimeUnit;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import io.kubernetes.client.openapi.ApiClient;
import io.kubernetes.client.openapi.apis.CoreV1Api;
import io.kubernetes.client.openapi.models.V1NodeList;
import io.kubernetes.client.openapi.models.V1PodList;
import io.kubernetes.client.util.Config;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
/**
* @author Philippe
*
*/
public class ListPodsWithNamespaces {
private static final Logger log = LoggerFactory.getLogger(ListPodsWithNamespaces.class);
/**
* @param args
*/
public static void main(String[] args) throws Exception {
ApiClient client = Config.defaultClient();
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor(message -> log.info(message));
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient newClient = client.getHttpClient()
.newBuilder()
.addInterceptor(interceptor)
.readTimeout(0, TimeUnit.SECONDS)
.build();
client.setHttpClient(newClient);
CoreV1Api api = new CoreV1Api(client);
String namespace = "ns1";
V1PodList items = api.listNamespacedPod(namespace,null, null, null, null, null, null, null, null, 10, false);
items.getItems()
.stream()
.map((pod) -> pod.getMetadata().getName() )
.forEach((name) -> System.out.println("name=" + name));
}
}

View File

@ -0,0 +1,27 @@
package com.baeldung.kubernetes.intro;
import org.junit.jupiter.api.Test;
class ListPodsWithFieldSelectorsLiveTest {
@Test
void givenEqualitySelector_whenListPodsWithFieldSelectors_thenSuccess() throws Exception {
ListPodsWithFieldSelectors.main(new String[] {
"metadata.namespace=ns1"
});
}
@Test
void givenInequalitySelector_whenListPodsWithFieldSelectors_thenSuccess() throws Exception {
ListPodsWithFieldSelectors.main(new String[] {
"metadata.namespace!=ns1"
});
}
@Test
void givenChainedSelector_whenListPodsWithFieldSelectors_thenSuccess() throws Exception {
ListPodsWithFieldSelectors.main(new String[] {
"metadata.namespace=ns1",
"status.phase=Running"
});
}
}

View File

@ -0,0 +1,55 @@
package com.baeldung.kubernetes.intro;
import org.junit.jupiter.api.Test;
class ListPodsWithLabelSelectorsLiveTest {
@Test
void givenEqualitySelector_whenListPodsWithLabelSelectors_thenSuccess() throws Exception {
ListPodsWithLabelSelectors.main(new String[] {
"app=httpd"
});
}
@Test
void givenInqualitySelector_whenListPodsWithLabelSelectors_thenSuccess() throws Exception {
ListPodsWithLabelSelectors.main(new String[] {
"app!=httpd"
});
}
@Test
void givenInSetSelector_whenListPodsWithLabelSelectors_thenSuccess() throws Exception {
ListPodsWithLabelSelectors.main(new String[] {
"app in (httpd,test)"
});
}
@Test
void givenNotInSetSelector_whenListPodsWithLabelSelectors_thenSuccess() throws Exception {
ListPodsWithLabelSelectors.main(new String[] {
"app notin (httpd)"
});
}
@Test
void givenLabelPresentSelector_whenListPodsWithLabelSelectors_thenSuccess() throws Exception {
ListPodsWithLabelSelectors.main(new String[] {
"app"
});
}
@Test
void givenLabelNotPresentSelector_whenListPodsWithLabelSelectors_thenSuccess() throws Exception {
ListPodsWithLabelSelectors.main(new String[] {
"!app"
});
}
@Test
void givenChainedSelector_whenListPodsWithLabelSelectors_thenSuccess() throws Exception {
ListPodsWithLabelSelectors.main(new String[] {
"app=httpd",
"!foo"
});
}
}