🌳
pt0/deployF/hostnameF/cleanupExplicitWcDnsAI.mts
5export const cleanupExplicitWcDns = async (hostname: string) => {
6 const {cfApiKeySecretName} = getKlusterCtx()
8 const headers = getCfHeaders({cfApiKeySecretName, required: false})
9 if (!headers) return
11 const zoneName = hostname.split('.').slice(-2).join('.')
12 const zones = await cfFetch('/zones', headers)
13 const zone = zones.find((z: any) => z.name === zoneName)
14 if (!zone) return
16 const allRecords = await cfFetch(`/zones/${zone.id}/dns_records?per_page=1000`, headers)
17 const matchNames = [hostname, `a-${hostname}`, `cname-${hostname}`]
18 const txtOwnerRecords = allRecords.filter((r: any) =>
19 r.type === 'TXT' && r.content?.includes('heritage=external-dns') && matchNames.includes(r.name)
20 )
21 const aRecords = allRecords.filter((r: any) =>
22 (r.type === 'A' || r.type === 'CNAME') && r.name === hostname
23 )
24 const toDelete = [...txtOwnerRecords, ...aRecords]
26 if (!toDelete.length) return
28 for (const record of toDelete) {
29 await cfFetch(`/zones/${zone.id}/dns_records/${record.id}`, headers, {method: 'DELETE'})
30 }
31 betLog({cleanupExplicitWcDns: hostname, deletedRecords: toDelete.length})