-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from eidam/e/kv-gc
feat: move gc monitors from cron schedule to deploy postCommands
- Loading branch information
Showing
7 changed files
with
105 additions
and
35 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 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
const yaml = require('yaml-loader') | ||
const fetch = require('node-fetch') | ||
const fs = require('fs') | ||
|
||
const accountId = process.env.CF_ACCOUNT_ID | ||
const namespaceId = process.env.KV_NAMESPACE_ID | ||
const apiToken = process.env.CF_API_TOKEN | ||
|
||
const kvPrefix = 's_' | ||
|
||
if (!accountId || !namespaceId || !apiToken) { | ||
console.error("Missing required environment variables: CF_ACCOUNT_ID, KV_NAMESPACE_ID, CF_API_TOKEN") | ||
process.exit(0) | ||
} | ||
|
||
async function getKvMonitors(kvPrefix) { | ||
const init = { | ||
headers: { | ||
'Content-Type': 'application/json', | ||
Authorization: `Bearer ${apiToken}`, | ||
}, | ||
} | ||
|
||
const res = await fetch( | ||
`https://api.cloudflare.com/client/v4/accounts/${accountId}/storage/kv/namespaces/${namespaceId}/keys?limit=100&prefix=${kvPrefix}`, | ||
init, | ||
) | ||
const json = await res.json() | ||
return json.result | ||
} | ||
|
||
async function deleteKvBulk(keys) { | ||
const init = { | ||
headers: { | ||
'Content-Type': 'application/json', | ||
Authorization: `Bearer ${apiToken}`, | ||
}, | ||
method: 'DELETE', | ||
body: JSON.stringify(keys), | ||
} | ||
|
||
return await fetch( | ||
`https://api.cloudflare.com/client/v4/accounts/${accountId}/storage/kv/namespaces/${namespaceId}/bulk`, | ||
init, | ||
) | ||
} | ||
|
||
function loadConfig() { | ||
const configFile = fs.readFileSync('./config.yaml', 'utf8') | ||
const config = yaml(configFile) | ||
return JSON.parse(config) | ||
} | ||
|
||
getKvMonitors(kvPrefix).then(async kvMonitors => { | ||
const config = loadConfig() | ||
const monitors = config.monitors.map(key => { | ||
return key.id | ||
}) | ||
const kvState = kvMonitors.map(key => { | ||
return key.name | ||
}) | ||
const keysForRemoval = kvState.filter( | ||
x => !monitors.includes(x.replace(kvPrefix, '')), | ||
) | ||
|
||
if (keysForRemoval.length > 0) { | ||
console.log( | ||
`Removing following keys from KV storage as they are no longer in the config: ${keysForRemoval.join( | ||
', ', | ||
)}`, | ||
) | ||
await deleteKvBulk(keysForRemoval) | ||
} | ||
}).catch(e => console.log(e)) |
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 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