This repository was archived by the owner on Jan 31, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
BC-7234 - read configmap for understanding the how to interact with t…
…he k8s API (#9)
- Loading branch information
Showing
8 changed files
with
201 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
config.stopBubbling = true | ||
lombok.addLombokGeneratedAnnotation = true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package svs.doido.mongo; | ||
|
||
import jakarta.ws.rs.Path; | ||
import jakarta.ws.rs.GET; | ||
import jakarta.ws.rs.Produces; | ||
import jakarta.ws.rs.core.MediaType; | ||
import jakarta.inject.Inject; | ||
import svs.doido.mongo.dto.Configmap; | ||
import svs.doido.mongo.kubernetes.configmap.Read; | ||
|
||
@Path("/configmap") | ||
public class ConfigmapApi { | ||
|
||
@Inject | ||
Read cfg; | ||
|
||
@GET | ||
@Path("/{namespace}/{configmapName}") | ||
@Produces(MediaType.APPLICATION_JSON) | ||
public Configmap configmaps(String namespace, String configmapName) { | ||
return cfg.readConfigmap(namespace,configmapName); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package svs.doido.mongo.dto; | ||
|
||
import lombok.Getter; | ||
import lombok.Setter; | ||
import lombok.ToString; | ||
import lombok.EqualsAndHashCode; | ||
import org.apache.commons.validator.routines.UrlValidator; | ||
import jakarta.enterprise.context.RequestScoped; | ||
|
||
|
||
@ToString | ||
@EqualsAndHashCode | ||
@RequestScoped | ||
public class Configmap { | ||
@Getter | ||
@Setter | ||
private String name = ""; | ||
|
||
@Getter | ||
private String uri = ""; | ||
|
||
public void setUri(String uriIn) { | ||
UrlValidator urlValidator = new UrlValidator( UrlValidator.ALLOW_ALL_SCHEMES|UrlValidator.ALLOW_LOCAL_URLS ); | ||
if (urlValidator.isValid( uriIn )) { | ||
uri = uriIn; | ||
} | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
src/main/java/de/svs/doido/mongo/kubernetes/configmap/Read.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package svs.doido.mongo.kubernetes.configmap; | ||
|
||
import svs.doido.mongo.dto.Configmap; | ||
import io.fabric8.kubernetes.api.model.ConfigMap; | ||
import io.fabric8.kubernetes.api.model.ObjectMeta; | ||
import io.fabric8.kubernetes.client.KubernetesClient; | ||
import jakarta.inject.Inject; | ||
import jakarta.enterprise.context.RequestScoped; | ||
|
||
|
||
@RequestScoped | ||
public class Read { | ||
|
||
@Inject | ||
Configmap cfg; | ||
|
||
@Inject | ||
KubernetesClient client; | ||
|
||
public Configmap readConfigmap(String namespace, String name) { | ||
ConfigMap configmap = client.configMaps().inNamespace(namespace).withName(name).get(); | ||
cfg.setName(configmap.getMetadata().getName()); | ||
cfg.setUri(configmap.getData().get("uri")); | ||
return cfg; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package svs.doido.mongo; | ||
|
||
import io.quarkus.test.junit.QuarkusTest; | ||
import io.quarkus.test.kubernetes.client.KubernetesTestServer; | ||
import io.quarkus.test.kubernetes.client.WithKubernetesTestServer; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.AfterEach; | ||
import jakarta.inject.Inject; | ||
|
||
import io.fabric8.kubernetes.client.KubernetesClient; | ||
import io.fabric8.kubernetes.client.server.mock.KubernetesServer; | ||
import io.fabric8.kubernetes.api.model.Namespace; | ||
import io.fabric8.kubernetes.api.model.NamespaceBuilder; | ||
import io.fabric8.kubernetes.api.model.ConfigMap; | ||
import io.fabric8.kubernetes.api.model.ConfigMapBuilder; | ||
|
||
import java.util.HashMap; | ||
|
||
import static io.restassured.RestAssured.given; | ||
import static org.hamcrest.CoreMatchers.is; | ||
|
||
|
||
|
||
|
||
@WithKubernetesTestServer | ||
@QuarkusTest | ||
class ConfigmapApiTest { | ||
|
||
@KubernetesTestServer | ||
KubernetesServer mockServer; | ||
@Inject | ||
KubernetesClient client; | ||
|
||
@BeforeEach | ||
public void before() { | ||
final Namespace namespace1 = new NamespaceBuilder().withNewMetadata().withName("namespaceA").and().build(); | ||
final ConfigMap cfgTest = new ConfigMapBuilder().withNewMetadata().withName("test").withNamespace("namespaceA").and().build(); | ||
final HashMap<String,String> data = new HashMap<String,String>(); | ||
data.put("uri","http://www.test.de"); | ||
cfgTest.setData(data); | ||
|
||
// Set up Kubernetes so that our "pretend" pods namespaces created | ||
client.namespaces().resource(namespace1).create(); | ||
client.configMaps().resource(cfgTest).create(); | ||
} | ||
|
||
@AfterEach | ||
public void after() { | ||
final Namespace namespace1 = new NamespaceBuilder().withNewMetadata().withName("namespaceA").and().build(); | ||
|
||
// Set up Kubernetes so that our "pretend" namespaces are deleted | ||
client.namespaces().resource(namespace1).delete(); | ||
} | ||
|
||
|
||
@Test | ||
void testInteractionWithAPIServerForConfigmaps() { | ||
given() | ||
.when().get("/configmap/namespaceA/test") | ||
.then() | ||
.statusCode(200) | ||
.body("size()", is(2)); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package svs.doido.mongo.dto; | ||
|
||
import io.quarkus.test.junit.QuarkusTest; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
@QuarkusTest | ||
class ConfigmapTest { | ||
|
||
private Configmap c; | ||
|
||
@BeforeEach | ||
public void before() { | ||
c = new Configmap(); | ||
} | ||
|
||
@Test | ||
void testGetterSetterName() { | ||
String name = "hausboot"; | ||
c.setName(name); | ||
assertEquals(name,c.getName()); | ||
} | ||
|
||
@Test | ||
void testGetterSetterUri() { | ||
String uri = "proto://foo.bar.com/"; | ||
c.setUri(uri); | ||
assertEquals(uri,c.getUri()); | ||
} | ||
|
||
@Test | ||
void testGetterSetterUriFail() { | ||
String uri = "proto:foo.bar.com/"; | ||
c.setUri(uri); | ||
assertEquals("", c.getUri()); | ||
} | ||
} |