Skip to content

Commit

Permalink
Don't delete images created less than a day ago
Browse files Browse the repository at this point in the history
Prevent races
  • Loading branch information
amezin committed Nov 26, 2023
1 parent 3b6cbfd commit 8c153e9
Showing 1 changed file with 27 additions and 11 deletions.
38 changes: 27 additions & 11 deletions cleanup.js
Original file line number Diff line number Diff line change
Expand Up @@ -229,29 +229,45 @@ async function main() {
'application/vnd.oci.image.config.v1+json'
];

const getRefName = async version => {
octokit.log.debug(`Getting revision for ${version.image}`, version.manifestUrl);
const fetchDockerImageConfig = async version => {
octokit.log.debug(`Getting image config for ${version.image}`, version.manifestUrl);

const manifest = await dockerRegistryFetch(version.manifestUrl, dockerManifestTypes);
const digest = manifest?.config?.digest;
if (!digest) {
octokit.log.warn(`Can't get digest for ${version.image} from manifest`, manifest);
octokit.log.warn(`Can't get digest for ${version.image} config from manifest`, manifest);
return null;
}

version.configUrl = new url.URL(`./${digest}`, version.blobBaseUrl).toString();
const config = await dockerRegistryFetch(version.configUrl, dockerConfigTypes);
const labels = config?.config?.Labels;
const refName = labels ? labels['org.opencontainers.image.version'] : null;
octokit.log.debug(`Version of ${version.image}: ${refName}`);
return refName;
};
return await dockerRegistryFetch(version.configUrl, dockerConfigTypes);
}

const minAge = new Date();
minAge.setDate(minAge.getDate() - 1);

const toDelete = versions.filter(
async version => {
octokit.log.debug(`Processing ${version.displayImage}`);
const ref = await getRefName(version);
return ref && !refs.includes(ref);

const config = await fetchDockerImageConfig(version);

const created = Date.parse(config?.created);
if (isNaN(created)) {
octokit.log.warn(`No created date in ${version.image} config`, config);
return false;
}

if (created > minAge) {
octokit.log.info(`Image ${version.image} is too new`, created);
return false;
}

const labels = config?.config?.Labels;
const refName = labels ? labels['org.opencontainers.image.version'] : null;
octokit.log.debug(`Version of ${version.image}: ${refName}`);

return refName && !refs.includes(refName);
},
concurrencyOptions,
);
Expand Down

0 comments on commit 8c153e9

Please sign in to comment.