Skip to content

Commit

Permalink
feat: DNS switcher
Browse files Browse the repository at this point in the history
DNS switcher switches the external DNS to
internal DNS that is organized per cluster
which allows for HA. This task includes
waiting for the DNS propagation (locally)
to finish
  • Loading branch information
kirinnee committed Oct 18, 2024
1 parent 2e197f3 commit 098bb43
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 0 deletions.
1 change: 1 addition & 0 deletions nix/env.nix
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ with packages;
terraform
bun
curl
dogdns
];

lint = [
Expand Down
1 change: 1 addition & 0 deletions nix/packages.nix
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ let
{
inherit
coreutils
dogdns
sd
curl
bash
Expand Down
3 changes: 3 additions & 0 deletions src/init/tasks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ import { SulfoxideBoronWaiter } from '../tasks/sulfoxide-boron-waiter.ts';
import { SERVICE_TREE } from '../lib/service-tree.ts';
import { SulfoxideXenonWaiter } from '../tasks/sulfoxide-xenon-waiter.ts';
import { SulfoxideFluorineCreator } from '../tasks/sulfoxide-fluorine-creator.ts';
import { LoadBalancerDNSSwitcher } from "../tasks/lb-dns-switcher.ts";

interface TaskGenerator {
lbDNSSwitcher: LoadBalancerDNSSwitcher;
nitrosoWaiter: NitrosoWaiter;
sulfoxideHeliumWaiter: SulfoxideHeliumWaiter;
sulfoxideBoronWaiter: SulfoxideBoronWaiter;
Expand All @@ -17,6 +19,7 @@ interface TaskGenerator {
function initTasks(d: Dependencies): TaskGenerator {
const services = SERVICE_TREE.sulfoxide.services;
return {
lbDNSSwitcher: new LoadBalancerDNSSwitcher("cluster.atomi.cloud","lb.atomi.cloud"),
nitrosoWaiter: new NitrosoWaiter(d.kubectl, d.httpUtil),
sulfoxideHeliumWaiter: new SulfoxideHeliumWaiter(d.kubectl, services.argocd),
sulfoxideBoronWaiter: new SulfoxideBoronWaiter(d.kubectl, services.internal_ingress),
Expand Down
43 changes: 43 additions & 0 deletions src/tasks/lb-dns-switcher.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import type { Task } from "./tasks.ts";
import type { CloudTreeClusterPrincipal } from "../lib/service-tree-def.ts";
import { $ } from "bun";

class LoadBalancerDNSSwitcher {
constructor(
private external: string,
private internal: string
) {
}

name: string = "Switching DNS for Load Balancing";

task(from: CloudTreeClusterPrincipal, target: CloudTreeClusterPrincipal): Task {
return [
this.name,
async () => {
console.log(`🚧 Switching DNS from ${from.name} to ${target.name}`);
const path = `./platforms/sulfoxide/tofu`;
await $`nix develop -c pls setup`.cwd(path);
await $`nix develop -c pls arceus:apply -- -var="target_cluster=${{ raw: target.slug }}" -auto-approve `.cwd(path);
console.log(`✅ DNS now points to ${target.name}`);
await $`sleep 120`;
console.log("🔍 Checking if DNS has propagated...");
const query = async (): Promise<string> => {
const result = await $`dog pichu.${this.external} -J`.cwd(path).json();
const answers: { name: string, type: string, data: { domain: string } }[] = result.responses[0].answers;
return answers.find(x => x.type === "CNAME")?.data.domain ?? "";
};
const t = `${target.slug}.${this.internal}`;
let domain = await query();
while (domain != t) {
console.log(`⏳ Waiting for DNS to propagate...`);
await $`sleep 5`;
domain = await query();
}
console.log(`✅ DNS propagated!`);
}
];
}
}

export { LoadBalancerDNSSwitcher };

0 comments on commit 098bb43

Please sign in to comment.