Skip to content

Commit

Permalink
BC-7239 - write an configmap at an namespace (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
mamutmk5 authored Jun 25, 2024
1 parent 9b29d79 commit 38632b4
Show file tree
Hide file tree
Showing 3 changed files with 140 additions and 4 deletions.
26 changes: 23 additions & 3 deletions src/main/java/de/svs/doido/mongo/ConfigmapApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,42 @@

import jakarta.ws.rs.Path;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.POST;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.sse.OutboundSseEvent;
import jakarta.ws.rs.ForbiddenException;
import jakarta.inject.Inject;
import svs.doido.mongo.dto.Configmap;
import svs.doido.mongo.kubernetes.configmap.Read;
import svs.doido.mongo.kubernetes.configmap.Write;

@Path("/configmap")
public class ConfigmapApi {

@Inject
Read cfg;
Read cfgRead;

@Inject
Write cfgWrite;

@GET
@Path("/{namespace}/{configmapName}")
@Produces(MediaType.APPLICATION_JSON)
public Configmap configmaps(String namespace, String configmapName) {
return cfg.readConfigmap(namespace,configmapName);
public Configmap configmapsRead(String namespace, String configmapName) {
return cfgRead.readConfigmap(namespace,configmapName);
}

@POST
@Path("/{namespace}/{configmapName}")
@Produces(MediaType.APPLICATION_JSON)
public void configmapsWrite(String namespace, String configmapName) {
Configmap cfg = new Configmap();
cfg.setName(configmapName);
cfg.setUri("http://www.test.org");
if( !cfgWrite.writeConfigmap(namespace,cfg) ) {
throw new ForbiddenException();
}

}
}
59 changes: 59 additions & 0 deletions src/main/java/de/svs/doido/mongo/kubernetes/configmap/Write.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
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.ConfigMapBuilder;
import io.fabric8.kubernetes.api.model.ObjectMeta;
import io.fabric8.kubernetes.client.KubernetesClient;
import jakarta.inject.Inject;
import jakarta.enterprise.context.RequestScoped;

import java.util.Map;
import java.util.HashMap;

@RequestScoped
public class Write {

private static final String NAME_LABEL = "app.kubernetes.io/name";
private static final String NAME_LABEL_VALUE = "doido-mongo";

@Inject
KubernetesClient client;

public boolean writeConfigmap(String namespace, Configmap cfg) {
ConfigMap configmap = client.configMaps().inNamespace(namespace).withName(cfg.getName()).get();
Map<String,String> labels;
Map<String,String> data;
ObjectMeta meta;

if(configmap != null) {
labels = configmap.getMetadata().getLabels();
if(
labels.containsKey(NAME_LABEL) &&
labels.get(NAME_LABEL).equals(NAME_LABEL_VALUE)
) {
data = configmap.getData();
data.put("uri", cfg.getUri());
configmap.setData(data);
client.configMaps().createOrReplace(configmap);
return true;
}
else {
return false;
}
}
else {
labels = new HashMap<>();
data = new HashMap<>();
labels.put(NAME_LABEL, NAME_LABEL_VALUE);
data.put("uri", cfg.getUri());
configmap = new ConfigMapBuilder().withNewMetadata().withName(cfg.getName()).withNamespace(namespace).and().build();
configmap.setData(data);
meta = configmap.getMetadata();
meta.setLabels(labels);
configmap.setMetadata(meta);
client.configMaps().create(configmap);
return true;
}
}
}
59 changes: 58 additions & 1 deletion src/test/java/de/svs/doido/mongo/ConfigmapApiTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
import io.fabric8.kubernetes.api.model.NamespaceBuilder;
import io.fabric8.kubernetes.api.model.ConfigMap;
import io.fabric8.kubernetes.api.model.ConfigMapBuilder;
import io.fabric8.kubernetes.api.model.ObjectMeta;

import java.util.Map;
import java.util.HashMap;

import static io.restassured.RestAssured.given;
Expand Down Expand Up @@ -49,18 +51,73 @@ public void before() {
public void after() {
final Namespace namespace1 = new NamespaceBuilder().withNewMetadata().withName("namespaceA").and().build();

for ( ConfigMap cfg : client.configMaps().inNamespace("namespaceA").list().getItems()) {
client.configMaps().resource(cfg).delete();
}
// Set up Kubernetes so that our "pretend" namespaces are deleted
client.namespaces().resource(namespace1).delete();
}


@Test
void testInteractionWithAPIServerForConfigmaps() {
void testInteractionWithGetConfigmaps() {
given()
.when().get("/configmap/namespaceA/test")
.then()
.statusCode(200)
.body("size()", is(2));
}


@Test
void testInteractionWithPostConfigmaps1() {
given()
.when().post("/configmap/namespaceA/tes2t")
.then()
.statusCode(204);
}


@Test
void testInteractionWithPostConfigmapsExistAndNoLabels() {
given()
.when().post("/configmap/namespaceA/test")
.then()
.statusCode(403);
}


@Test
void testInteractionWithPostConfigmapsExistAndLabels() {
ConfigMap configmap = new ConfigMapBuilder().withNewMetadata().withName("test3").withNamespace("namespaceA").and().build();
Map<String,String> labels = new HashMap<>();
ObjectMeta meta;
labels.put("app.kubernetes.io/name", "doido-mongo");
meta = configmap.getMetadata();
meta.setLabels(labels);
configmap.setMetadata(meta);
client.configMaps().create(configmap);
given()
.when().post("/configmap/namespaceA/test3")
.then()
.statusCode(204);
}


@Test
void testInteractionWithPostConfigmapsExistAndLabelsWrong() {
ConfigMap configmap = new ConfigMapBuilder().withNewMetadata().withName("test3").withNamespace("namespaceA").and().build();
Map<String,String> labels = new HashMap<>();
ObjectMeta meta;
labels.put("app.kubernetes.io/name", "doido-mongoB");
meta = configmap.getMetadata();
meta.setLabels(labels);
configmap.setMetadata(meta);
client.configMaps().create(configmap);
given()
.when().post("/configmap/namespaceA/test3")
.then()
.statusCode(403);
}

}

0 comments on commit 38632b4

Please sign in to comment.