-
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.
- Initial API server based implementation.
- Loading branch information
Showing
17 changed files
with
825 additions
and
396 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
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
108 changes: 44 additions & 64 deletions
108
bl-k8s-rt/src/main/java/io/vacco/beleth/rt/BlKubeRt.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 |
---|---|---|
@@ -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); | ||
} | ||
|
||
} |
Oops, something went wrong.