Skip to content

Commit

Permalink
- Initial API server based implementation.
Browse files Browse the repository at this point in the history
  • Loading branch information
jjzazuet committed Aug 25, 2024
1 parent 8946bbb commit f153f89
Show file tree
Hide file tree
Showing 17 changed files with 825 additions and 396 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,7 @@ public class BlHeadless {

private static final Logger log = LoggerFactory.getLogger(BlHeadless.class);

public interface BlUnsafe {
void run() throws Exception;
}

public static void runOnDesktop(BlUnsafe block) {
public static void runLocal(Runnable block) {
if (!GraphicsEnvironment.isHeadless()) {
try {
block.run();
Expand Down
4 changes: 1 addition & 3 deletions bl-k8s-rt/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@ val api by configurations
val k8s129 = ":bl-k8s129"

dependencies {
api("org.buildobjects:jproc:2.8.2")
api("io.github.java-diff-utils:java-diff-utils:4.12")
api(project(":bl-codegen"))
compileOnly(project(k8s129))
testImplementation(project(k8s129))
}

// LELZ test?
80 changes: 0 additions & 80 deletions bl-k8s-rt/src/main/java/io/vacco/beleth/rt/BlKubeCtl.java

This file was deleted.

46 changes: 0 additions & 46 deletions bl-k8s-rt/src/main/java/io/vacco/beleth/rt/BlKubeRes.java

This file was deleted.

108 changes: 44 additions & 64 deletions bl-k8s-rt/src/main/java/io/vacco/beleth/rt/BlKubeRt.java
Original file line number Diff line number Diff line change
@@ -1,88 +1,68 @@
package io.vacco.beleth.rt;

import io.k8s.api.core.v1.Namespace;
import io.vacco.beleth.rt.impl.BlKubeApi;
import io.vacco.beleth.rt.schema.*;
import io.vacco.beleth.xform.BlDocumentContext;
import java.util.*;
import java.util.function.*;

import static io.vacco.beleth.rt.BlKubeUtil.initTx;
import static java.util.Objects.requireNonNull;
import static io.vacco.beleth.rt.impl.BlKubeIo.pointerOnObject;
import static io.vacco.beleth.rt.schema.BlKubeResources.*;
import static io.vacco.beleth.rt.schema.BlKubeResourceTypes.*;

public class BlKubeRt {

public static int ApiServerTimeoutMs = 5000;

private final BlKubeApi kubeApi;
private final BlDocumentContext ctx = new BlDocumentContext();
private final List<Object> manifests = new ArrayList<>();
private final BlKubeCtl ctl = new BlKubeCtl();

public BlKubeRt add(Object manifest) {
this.manifests.add(Objects.requireNonNull(manifest));
return this;
public BlKubeRt(String apiServerUrl, String apiToken) {
this.kubeApi = new BlKubeApi(apiServerUrl, apiToken, ApiServerTimeoutMs);
}

private void delete(Map<String, List<BlKubeRes>> resourceIdx, String packageName, Predicate<BlKubeRes> condition) {
for (var e : resourceIdx.entrySet()) {
for (var res : e.getValue()) {
if (res.blId.startsWith(packageName) && condition.test(res)) {
ctl.delete(res);
}
}
public Set<BlKubeResource> buildResourceSet(String packageName) {
if (!isValidPackageName(packageName)) {
throw new IllegalArgumentException("Invalid package name: " + packageName);
}
var idx = new LinkedHashSet<BlKubeResource>();
for (var manifest : manifests) {
var objCopy = ctx.fromJson(ctx.toJson(manifest), manifest.getClass());
var blId = String.format("%s.%s", packageName, Integer.toHexString(ctx.toJson(objCopy).hashCode()));
var kind = requireNonNull(pointerOnObject(manifest, "/kind")).toString().toLowerCase() + "s";
var rType = BlKubeResourceType.of("", kind, false);
injectMeta(objCopy, kBl, Boolean.TRUE.toString());
injectMeta(objCopy, kBlId, blId);
idx.add(resourceOf(blId, objCopy).withType(rType));
}
return idx;
}

private void delete(Map<String, List<BlKubeRes>> resourceIdx, String packageName) {
delete(resourceIdx, packageName, res ->
!(res.type.equals("persistentvolumeclaims")
|| res.type.equals("persistentvolumes")
|| res.type.equals("namespaces")
)
);
delete(resourceIdx, packageName, res ->
!(res.type.equals("persistentvolumes")
|| res.type.equals("namespaces"))
);
delete(resourceIdx, packageName, res ->
!(res.type.equals("namespaces"))
);
delete(resourceIdx, packageName, res -> // pesky bee...
res.type.equals("namespaces")
);
}

private void commit(BlKubeRes res, String packageName) {
if (!ctl.check(res).synced) {
ctl.apply(res.manifest, packageName);
}
public BlKubeRt add(Object manifest) {
this.manifests.add(Objects.requireNonNull(manifest));
return this;
}

public void commit(String packageName) {
var txIdx = new LinkedHashMap<String, BlKubeRes>();
var diff = new BlResourceDiff().update(
buildResourceSet(packageName),
this.kubeApi.resourceSet(packageName)
);

diff.toAdd.sort(Comparator.comparingInt(r -> priorityOf(r.type)));
diff.toUpdate.sort(Comparator.comparingInt(r -> priorityOf(r.type)));
diff.toDelete.sort(Comparator.comparingInt(r -> -priorityOf(r.type)));

for (Object manifest : manifests) {
var tx = initTx(manifest, ctl.ctx, packageName);
if (txIdx.containsKey(tx.blId)) {
throw new IllegalStateException("Duplicate resource definition - " + tx);
} else {
txIdx.put(tx.blId, tx);
}
for (var res : diff.toAdd) {
kubeApi.create(res);
}
for (var res : txIdx.values()) {
if (res.manifest instanceof Namespace) {
commit(res, packageName);
}
for (var res : diff.toUpdate) {
System.out.println("lol?");
}
for (var res : txIdx.values()) {
if (!(res.manifest instanceof Namespace)) {
commit(res, packageName);
}
for (var res : diff.toDelete) {
kubeApi.delete(res);
}

var resIdx = ctl.resourceIndex(true);
resIdx.putAll(ctl.resourceIndex(false));
resIdx.keySet().removeAll(txIdx.keySet());
delete(resIdx, packageName);
}

public void deleteAll(String packageName) {
var resIdx = ctl.resourceIndex(true);
resIdx.putAll(ctl.resourceIndex(false));
delete(resIdx, packageName);
}

}
Loading

0 comments on commit f153f89

Please sign in to comment.