From 78224d895e57828c536bee8435a9b6bc6cd69be8 Mon Sep 17 00:00:00 2001 From: Thodoris Greasidis Date: Thu, 30 Nov 2023 18:11:31 +0200 Subject: [PATCH] deploy: Stop unnecessarily authorizing pulling older images Change-type: patch See: https://balena.zulipchat.com/#narrow/stream/403752-channel.2Fsupport-help/topic/balena.20deploy.20too.20many.20requests --- lib/utils/compose.ts | 54 ----------------------------------------- lib/utils/compose_ts.ts | 14 +++-------- 2 files changed, 3 insertions(+), 65 deletions(-) diff --git a/lib/utils/compose.ts b/lib/utils/compose.ts index c9c39496e4..d441c8d490 100644 --- a/lib/utils/compose.ts +++ b/lib/utils/compose.ts @@ -28,7 +28,6 @@ import type { TaggedImage, } from './compose-types'; import { getChalk } from './lazy'; -import Logger = require('./logger'); import { ProgressCallback } from 'docker-progress'; export function generateOpts(options: { @@ -183,69 +182,16 @@ export const tagServiceImages = ( }), ); -export const getPreviousRepos = ( - sdk: SDK.BalenaSDK, - logger: Logger, - appID: number, -): Promise => - sdk.pine - .get({ - resource: 'release', - options: { - $select: 'id', - $filter: { - belongs_to__application: appID, - status: 'success', - }, - $expand: { - contains__image: { - $select: 'image', - $expand: { image: { $select: 'is_stored_at__image_location' } }, - }, - }, - $orderby: 'id desc', - $top: 1, - }, - }) - .then(function (release) { - // grab all images from the latest release, return all image locations in the registry - if (release.length > 0) { - const images = release[0].contains__image as Array<{ - image: [SDK.Image]; - }>; - const { getRegistryAndName } = - require('@balena/compose/dist/multibuild') as typeof import('@balena/compose/dist/multibuild'); - return Promise.all( - images.map(function (d) { - const imageName = d.image[0].is_stored_at__image_location || ''; - const registry = getRegistryAndName(imageName); - logger.logDebug( - `Requesting access to previously pushed image repo (${registry.imageName})`, - ); - return registry.imageName; - }), - ); - } else { - return []; - } - }) - .catch((e) => { - logger.logDebug(`Failed to access previously pushed image repo: ${e}`); - return []; - }); - export const authorizePush = function ( sdk: SDK.BalenaSDK, tokenAuthEndpoint: string, registry: string, images: string[], - previousRepos: string[], ): Promise { if (!Array.isArray(images)) { images = [images]; } - images.push(...previousRepos); return sdk.request .send({ baseUrl: tokenAuthEndpoint, diff --git a/lib/utils/compose_ts.ts b/lib/utils/compose_ts.ts index 202d85f1ba..d96f044ef6 100644 --- a/lib/utils/compose_ts.ts +++ b/lib/utils/compose_ts.ts @@ -1215,23 +1215,20 @@ export async function validateProjectDirectory( return result; } -async function getTokenForPreviousRepos( +async function getRegistryToken( logger: Logger, - appId: number, apiEndpoint: string, taggedImages: TaggedImage[], ): Promise { logger.logDebug('Authorizing push...'); - const { authorizePush, getPreviousRepos } = await import('./compose'); + const { authorizePush } = await import('./compose'); const sdk = getBalenaSdk(); - const previousRepos = await getPreviousRepos(sdk, logger, appId); const token = await authorizePush( sdk, apiEndpoint, taggedImages[0].registry, _.map(taggedImages, 'repo'), - previousRepos, ); return token; } @@ -1405,12 +1402,7 @@ export async function deployProject( // awaitInterruptibleTask throws SIGINTError on CTRL-C, // causing the release status to be set to 'failed' await awaitInterruptibleTask(async () => { - const token = await getTokenForPreviousRepos( - logger, - appId, - apiEndpoint, - taggedImages, - ); + const token = await getRegistryToken(logger, apiEndpoint, taggedImages); await pushServiceImages( docker, logger,