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-7239 - write an configmap at an namespace (#10)
- Loading branch information
Showing
3 changed files
with
140 additions
and
4 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
59 changes: 59 additions & 0 deletions
59
src/main/java/de/svs/doido/mongo/kubernetes/configmap/Write.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,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; | ||
} | ||
} | ||
} |
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