Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

deploy: Stop unnecessarily authorizing pulling older images #2709

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 0 additions & 54 deletions lib/utils/compose.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down Expand Up @@ -183,69 +182,16 @@ export const tagServiceImages = (
}),
);

export const getPreviousRepos = (
sdk: SDK.BalenaSDK,
logger: Logger,
appID: number,
): Promise<string[]> =>
sdk.pine
.get<SDK.Release>({
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<string> {
if (!Array.isArray(images)) {
images = [images];
}

images.push(...previousRepos);
return sdk.request
.send({
baseUrl: tokenAuthEndpoint,
Expand Down
14 changes: 3 additions & 11 deletions lib/utils/compose_ts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string> {
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;
}
Expand Down Expand Up @@ -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,
Expand Down
Loading