Skip to content

Commit

Permalink
fix: image names include the recipe id not only the container name
Browse files Browse the repository at this point in the history
Fixes #283

Signed-off-by: Jeff MAURY <[email protected]>
  • Loading branch information
jeffmaury committed Mar 6, 2024
1 parent bc03f30 commit d7f583d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 9 deletions.
21 changes: 14 additions & 7 deletions packages/backend/src/managers/applicationManager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ describe('pullApplication', () => {
mocks.buildImageMock.mockResolvedValue(undefined);
mocks.listImagesMock.mockResolvedValue([
{
RepoTags: ['container1:latest'],
RepoTags: ['recipe1-container1:latest'],
engineId: 'engine',
Id: 'id1',
},
Expand Down Expand Up @@ -296,7 +296,7 @@ describe('pullApplication', () => {
expect.anything(),
{
containerFile: 'Containerfile',
tag: 'container1:latest',
tag: 'recipe1-container1:latest',
labels: {
[LABEL_RECIPE_ID]: 'recipe1',
},
Expand Down Expand Up @@ -737,6 +737,9 @@ describe('getRandomName', () => {
});

describe('buildImages', () => {
const recipe = {
id: 'recipe1',
} as Recipe;
const containers: ContainerConfig[] = [
{
name: 'container1',
Expand All @@ -762,13 +765,15 @@ describe('buildImages', () => {
test('setTaskState should be called with error if context does not exist', async () => {
vi.spyOn(fs, 'existsSync').mockReturnValue(false);
mocks.listImagesMock.mockRejectedValue([]);
await expect(manager.buildImages(containers, 'config')).rejects.toThrow('Context configured does not exist.');
await expect(manager.buildImages(recipe, containers, 'config')).rejects.toThrow(
'Context configured does not exist.',
);
});
test('setTaskState should be called with error if buildImage executon fails', async () => {
vi.spyOn(fs, 'existsSync').mockReturnValue(true);
mocks.buildImageMock.mockRejectedValue('error');
mocks.listImagesMock.mockRejectedValue([]);
await expect(manager.buildImages(containers, 'config')).rejects.toThrow(
await expect(manager.buildImages(recipe, containers, 'config')).rejects.toThrow(
'Something went wrong while building the image: error',
);
expect(mocks.updateTaskMock).toBeCalledWith({
Expand All @@ -783,7 +788,9 @@ describe('buildImages', () => {
vi.spyOn(fs, 'existsSync').mockReturnValue(true);
mocks.buildImageMock.mockResolvedValue({});
mocks.listImagesMock.mockResolvedValue([]);
await expect(manager.buildImages(containers, 'config')).rejects.toThrow('no image found for container1:latest');
await expect(manager.buildImages(recipe, containers, 'config')).rejects.toThrow(
'no image found for container1:latest',
);
expect(mocks.updateTaskMock).toBeCalledWith({
error: 'no image found for container1:latest',
name: 'Building container1',
Expand All @@ -797,12 +804,12 @@ describe('buildImages', () => {
mocks.buildImageMock.mockResolvedValue({});
mocks.listImagesMock.mockResolvedValue([
{
RepoTags: ['container1:latest'],
RepoTags: ['recipe1-container1:latest'],
engineId: 'engine',
Id: 'id1',
},
]);
const imageInfoList = await manager.buildImages(containers, 'config');
const imageInfoList = await manager.buildImages(recipe, containers, 'config');
expect(mocks.updateTaskMock).toBeCalledWith({
name: 'Building container1',
id: expect.any(String),
Expand Down
12 changes: 10 additions & 2 deletions packages/backend/src/managers/applicationManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ export class ApplicationManager {

// build all images, one per container (for a basic sample we should have 2 containers = sample app + model service)
const images = await this.buildImages(
recipe,
configAndFilteredContainers.containers,
configAndFilteredContainers.aiConfigFile.path,
{
Expand Down Expand Up @@ -371,6 +372,7 @@ export class ApplicationManager {
}

async buildImages(
recipe: Recipe,
containers: ContainerConfig[],
configPath: string,
labels?: { [key: string]: string },
Expand Down Expand Up @@ -400,9 +402,10 @@ export class ApplicationManager {
throw new Error('Context configured does not exist.');
}

const imageTag = this.getImageTag(recipe, container);
const buildOptions = {
containerFile: container.containerfile,
tag: `${container.name}:latest`,
tag: imageTag,
labels: {
[LABEL_RECIPE_ID]: labels !== undefined && 'recipe-id' in labels ? labels['recipe-id'] : undefined,
},
Expand Down Expand Up @@ -434,9 +437,10 @@ export class ApplicationManager {
await Promise.all(
containers.map(async container => {
const task = containerTasks[container.name];
const imageTag = this.getImageTag(recipe, container);

const image = images.find(im => {
return im.RepoTags?.some(tag => tag.endsWith(`${container.name}:latest`));
return im.RepoTags?.some(tag => tag.endsWith(imageTag));
});

if (!image) {
Expand All @@ -460,6 +464,10 @@ export class ApplicationManager {
return imageInfoList;
}

private getImageTag(recipe: Recipe, container: ContainerConfig) {
return `${recipe.id}-${container.name}:latest`;
}

getConfigAndFilterContainers(
recipeConfig: string,
localFolder: string,
Expand Down

0 comments on commit d7f583d

Please sign in to comment.