linting
This commit is contained in:
@@ -1,12 +1,19 @@
|
||||
import { CustomResource, NotReadyError, ResourceReference, Secret, type CustomResourceOptions } from "@morten-olsen/box-k8s";
|
||||
import { cloudflareAccountSchema } from "./account.schemas.js";
|
||||
import { API_VERSION } from "@morten-olsen/box-utils/consts";
|
||||
import {
|
||||
CustomResource,
|
||||
NotReadyError,
|
||||
ResourceReference,
|
||||
Secret,
|
||||
type CustomResourceOptions,
|
||||
} from '@morten-olsen/box-k8s';
|
||||
import { API_VERSION } from '@morten-olsen/box-utils/consts';
|
||||
|
||||
import { cloudflareAccountSchema } from './account.schemas.js';
|
||||
|
||||
class CloudflareAccountResource extends CustomResource<typeof cloudflareAccountSchema> {
|
||||
public static readonly apiVersion = API_VERSION;
|
||||
public static readonly kind = "CloudflareAccount";
|
||||
public static readonly kind = 'CloudflareAccount';
|
||||
public static readonly spec = cloudflareAccountSchema;
|
||||
public static readonly scope = "Cluster";
|
||||
public static readonly scope = 'Cluster';
|
||||
public static readonly dependsOn = [Secret];
|
||||
|
||||
#secret: ResourceReference<typeof Secret>;
|
||||
@@ -30,19 +37,15 @@ class CloudflareAccountResource extends CustomResource<typeof cloudflareAccountS
|
||||
if (!spec) {
|
||||
return;
|
||||
}
|
||||
return this.resources.get(
|
||||
Secret,
|
||||
spec.token.secret,
|
||||
spec.token.namespace,
|
||||
)
|
||||
}
|
||||
return this.resources.get(Secret, spec.token.secret, spec.token.namespace);
|
||||
};
|
||||
|
||||
public reconcile = async () => {
|
||||
this.#secret.current = this.#getSecret();
|
||||
if (!this.token) {
|
||||
throw new NotReadyError('Token not found');
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
export { CloudflareAccountResource };
|
||||
|
||||
@@ -1,20 +1,20 @@
|
||||
import { CustomResource, NotReadyError } from '@morten-olsen/box-k8s';
|
||||
import { API_VERSION } from '@morten-olsen/box-utils/consts';
|
||||
|
||||
import { CloudflareService } from '../../services/cloudflare/cloudflare.js';
|
||||
// import { CloudflareAccountResource } from '../account/account.js';
|
||||
|
||||
import { cloudflareDnsRecordSchema } from './dns-record.schemas.js';
|
||||
import { API_VERSION } from '@morten-olsen/box-utils/consts';
|
||||
import { CloudflareAccountResource } from '../account/account.js';
|
||||
|
||||
class CloudflareDnsRecordResource extends CustomResource<typeof cloudflareDnsRecordSchema> {
|
||||
public static readonly apiVersion = API_VERSION;
|
||||
public static readonly kind = "CloudflareDnsRecord";
|
||||
public static readonly kind = 'CloudflareDnsRecord';
|
||||
public static readonly spec = cloudflareDnsRecordSchema;
|
||||
public static readonly scope = "Namespaced";
|
||||
public static readonly dependsOn = [CloudflareAccountResource];
|
||||
public static readonly scope = 'Namespaced';
|
||||
// public static readonly dependsOn = [CloudflareAccountResource];
|
||||
|
||||
public get dnsId() {
|
||||
return `homelab|${this.namespace}|${this.name}`
|
||||
return `homelab|${this.namespace}|${this.name}`;
|
||||
}
|
||||
|
||||
public reconcile = async () => {
|
||||
@@ -22,19 +22,18 @@ class CloudflareDnsRecordResource extends CustomResource<typeof cloudflareDnsRec
|
||||
throw new NotReadyError('Missing spec');
|
||||
}
|
||||
const service = this.services.get(CloudflareService);
|
||||
const { getDnsRecord, ensrureDnsRecord } = service.getAccount(this.spec.account);
|
||||
const { ensrureDnsRecord } = service.getAccount(this.spec.account);
|
||||
await ensrureDnsRecord(this.dnsId, this.spec);
|
||||
};
|
||||
|
||||
public destroy = async () => {
|
||||
|
||||
if (!this.spec) {
|
||||
throw new NotReadyError('Missing spec');
|
||||
}
|
||||
const service = this.services.get(CloudflareService);
|
||||
const { removeDnsRecord } = service.getAccount(this.spec.account);
|
||||
await removeDnsRecord(this.dnsId, this.spec.domain);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
export { CloudflareDnsRecordResource, cloudflareDnsRecordSchema };
|
||||
|
||||
@@ -1,19 +1,20 @@
|
||||
import type { Services } from "@morten-olsen/box-utils/services";
|
||||
import type { CloudflareAccountResource } from "../../resources/account/account.js"
|
||||
import type { Services } from '@morten-olsen/box-utils/services';
|
||||
import API from 'cloudflare';
|
||||
import type { Zone } from "cloudflare/resources/zones/zones.mjs";
|
||||
import type { z } from "@morten-olsen/box-k8s";
|
||||
import type { cloudflareDnsRecordSchema } from "../../resources/dns-record/dns-record.schemas.js";
|
||||
import type { Zone } from 'cloudflare/resources/zones/zones.mjs';
|
||||
import type { z } from '@morten-olsen/box-k8s';
|
||||
|
||||
import type { CloudflareAccountResource } from '../../resources/account/account.js';
|
||||
import type { cloudflareDnsRecordSchema } from '../../resources/dns-record/dns-record.schemas.js';
|
||||
|
||||
type CloudflareAccountOptions = {
|
||||
services: Services;
|
||||
resource: CloudflareAccountResource;
|
||||
}
|
||||
};
|
||||
|
||||
class CloudflareAccount {
|
||||
#options: CloudflareAccountOptions;
|
||||
#api?: API;
|
||||
#zones: Map<string, Promise<Zone | undefined>>
|
||||
#zones: Map<string, Promise<Zone | undefined>>;
|
||||
|
||||
constructor(options: CloudflareAccountOptions) {
|
||||
this.#zones = new Map();
|
||||
@@ -31,7 +32,7 @@ class CloudflareAccount {
|
||||
if (!this.#api) {
|
||||
this.#api = new API({
|
||||
apiToken: this.token,
|
||||
})
|
||||
});
|
||||
}
|
||||
return this.#api;
|
||||
}
|
||||
@@ -39,10 +40,10 @@ class CloudflareAccount {
|
||||
#getZone = async (name: string) => {
|
||||
const zones = await this.api.zones.list({
|
||||
name,
|
||||
})
|
||||
});
|
||||
const [zone] = zones.result;
|
||||
return zone;
|
||||
}
|
||||
};
|
||||
|
||||
public getZone = async (name: string) => {
|
||||
if (!this.#zones.has(name)) {
|
||||
@@ -50,7 +51,7 @@ class CloudflareAccount {
|
||||
}
|
||||
const current = this.#zones.get(name);
|
||||
return await current;
|
||||
}
|
||||
};
|
||||
|
||||
public getDnsRecord = async (id: string, domain: string) => {
|
||||
const zone = await this.getZone(domain);
|
||||
@@ -64,11 +65,11 @@ class CloudflareAccount {
|
||||
comment: {
|
||||
exact: id,
|
||||
},
|
||||
})
|
||||
});
|
||||
const [dnsRecord] = dnsRecords.result;
|
||||
|
||||
return dnsRecord;
|
||||
}
|
||||
};
|
||||
|
||||
public removeDnsRecord = async (id: string, domain: string) => {
|
||||
const zone = await this.getZone(domain);
|
||||
@@ -83,7 +84,7 @@ class CloudflareAccount {
|
||||
await this.api.dns.records.delete(record.id, {
|
||||
zone_id: zone.id,
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
public ensrureDnsRecord = async (id: string, options: z.infer<typeof cloudflareDnsRecordSchema>) => {
|
||||
const { domain, subdomain, value, type, ttl = 1, proxy } = options;
|
||||
@@ -102,9 +103,8 @@ class CloudflareAccount {
|
||||
comment: id,
|
||||
ttl,
|
||||
proxied: proxy,
|
||||
})
|
||||
});
|
||||
} else {
|
||||
|
||||
await this.api.dns.records.update(current.id, {
|
||||
zone_id: zone.id,
|
||||
type,
|
||||
@@ -113,9 +113,9 @@ class CloudflareAccount {
|
||||
comment: id,
|
||||
ttl,
|
||||
proxied: proxy,
|
||||
})
|
||||
});
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
export { CloudflareAccount };
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
import type { Services } from "@morten-olsen/box-utils/services";
|
||||
import { CloudflareAccount } from "./cloudflare.account.js";
|
||||
import { ResourceService } from "@morten-olsen/box-k8s";
|
||||
import { CloudflareAccountResource } from "../../resources/account/account.js";
|
||||
import type { Services } from '@morten-olsen/box-utils/services';
|
||||
import { ResourceService } from '@morten-olsen/box-k8s';
|
||||
|
||||
import { CloudflareAccountResource } from '../../resources/account/account.js';
|
||||
|
||||
import { CloudflareAccount } from './cloudflare.account.js';
|
||||
|
||||
class CloudflareService {
|
||||
#services: Services;
|
||||
@@ -16,17 +18,20 @@ class CloudflareService {
|
||||
if (!this.#instances.has(name)) {
|
||||
const resourceService = this.#services.get(ResourceService);
|
||||
const resource = resourceService.get(CloudflareAccountResource, name);
|
||||
this.#instances.set(name, new CloudflareAccount({
|
||||
resource,
|
||||
services: this.#services,
|
||||
}))
|
||||
this.#instances.set(
|
||||
name,
|
||||
new CloudflareAccount({
|
||||
resource,
|
||||
services: this.#services,
|
||||
}),
|
||||
);
|
||||
}
|
||||
const current = this.#instances.get(name);
|
||||
if (!current) {
|
||||
throw new Error('Could not get cloudflare account');
|
||||
}
|
||||
return current;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
export { CloudflareService };
|
||||
|
||||
@@ -1,13 +1,8 @@
|
||||
import { K8sOperator } from '@morten-olsen/box-k8s';
|
||||
|
||||
import { CloudflareAccountResource } from './resources/account/account.js';
|
||||
import { CloudflareDnsRecordResource } from './resources/dns-record/dns-record.js';
|
||||
|
||||
const operator = new K8sOperator();
|
||||
await operator.resources.install(
|
||||
CloudflareAccountResource,
|
||||
CloudflareDnsRecordResource,
|
||||
)
|
||||
await operator.resources.register(
|
||||
CloudflareAccountResource,
|
||||
CloudflareDnsRecordResource,
|
||||
)
|
||||
await operator.resources.install(CloudflareAccountResource, CloudflareDnsRecordResource);
|
||||
await operator.resources.register(CloudflareAccountResource, CloudflareDnsRecordResource);
|
||||
|
||||
Reference in New Issue
Block a user