Skip to content

Commit

Permalink
Merge pull request #1070 from garden-io/fix-cleanup-script
Browse files Browse the repository at this point in the history
fix: cleanup-cluster-registry script fails to run
  • Loading branch information
10ko authored Aug 6, 2019
2 parents 117efe3 + b0e8095 commit 068eb25
Showing 1 changed file with 21 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
*/

import chalk from "chalk"
import { chunk } from "lodash"
import pluralize = require("pluralize")
import { PluginCommand } from "../../../types/plugin/command"
import { KubernetesPluginContext, KubernetesProvider } from "../config"
import { KubeApi } from "../api"
Expand All @@ -23,7 +25,7 @@ import { PluginError } from "../../../exceptions"
import { apply, kubectl } from "../kubectl"
import { waitForResources } from "../status/status"
import { execInDeployment } from "../container/run"
import { dedent } from "../../../util/string"
import { dedent, deline } from "../../../util/string"
import { execInBuilder, getBuilderPodName, BuilderExecParams, buildSyncDeploymentName } from "../container/build"
import { getPods } from "../util"

Expand Down Expand Up @@ -137,9 +139,9 @@ async function getImagesInRegistry(ctx: KubernetesPluginContext, log: LogEntry)

while (nextUrl) {
const res = await queryRegistry(ctx, log, nextUrl)

images.push(...res.data.tags.map((tag: string) => `${repo}:${tag}`))

if (res.data.tags) {
images.push(...res.data.tags.map((tag: string) => `${repo}:${tag}`))
}
// Paginate
const linkHeader = res.headers["link"]
if (linkHeader) {
Expand Down Expand Up @@ -292,9 +294,21 @@ async function deleteImagesFromDaemon(provider: KubernetesProvider, log: LogEntr
if (imagesToDelete.length === 0) {
log.info(`Nothing to clean up.`)
} else {
log.info(`Cleaning up ${imagesToDelete.length} images...`)
const args = ["docker", "rmi", ...imagesToDelete]
await execInBuilder({ provider, log, args, podName, timeout: 300 })
const batchSize = 100
const imagesBatches: string[][] = chunk(imagesToDelete, batchSize)

let counter = imagesBatches.length
log.info(dedent
`Cleaning up ${counter} batches of images (total of ${imagesToDelete.length} images)...`)

await Bluebird.map(imagesBatches, async (images) => {
const args = ["docker", "rmi", ...images]
await execInBuilder({ provider, log, args, podName, timeout: 300 })
log.setState(deline`
Deleting images:
${pluralize("batch", counter, true)} of ${imagesBatches.length} left...`)
counter -= 1
}, { concurrency: 25 })
}

// Run a prune operation
Expand Down

0 comments on commit 068eb25

Please sign in to comment.