Skip to content

Commit

Permalink
feat: extract content manifests from layers
Browse files Browse the repository at this point in the history
in rhel image, /root/buildinfo/content_manifests
conatins repositories information for each layer
this info will now be extracted for every image if exists
  • Loading branch information
RotemS committed Jan 25, 2021
1 parent 2b1f49f commit 70a0058
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 0 deletions.
6 changes: 6 additions & 0 deletions lib/extractor/types.ts
Expand Up @@ -76,3 +76,9 @@ export interface DetectedImageLayers {
packages;
layers;
}

export interface RedHatRepos {
[imageLayerIndex: string]: RedHatRepo[];
}

export type RedHatRepo = string;
36 changes: 36 additions & 0 deletions lib/inputs/rhel/static.ts
@@ -0,0 +1,36 @@
import {
ExtractAction,
ExtractedLayers,
RedHatRepo,
RedHatRepos,
} from "../../extractor/types";
import { streamToJson } from "../../stream-utils";

export const getRedHatReposContentAction: ExtractAction = {
actionName: "redhat-content-manifests",
filePathMatches: isRedHatContentManifest,
callback: streamToJson,
};

export function getRedHatReposFromExtractedLayers(
extractedLayers: ExtractedLayers,
): RedHatRepos {
const reposByLayer = {};
for (const filePath in extractedLayers) {
if (isRedHatContentManifest(filePath)) {
const content = extractedLayers[filePath][
"redhat-content-manifests"
] as any;
const layerIndex = content?.metadata?.image_layer_index;
const repos = content?.content_sets;
if (layerIndex && repos) {
reposByLayer[layerIndex] = repos as RedHatRepo;
}
}
}
return reposByLayer as RedHatRepos;
}

function isRedHatContentManifest(filePath: string): boolean {
return filePath.startsWith("/root/buildinfo/content_manifests/");
}
Binary file not shown.
17 changes: 17 additions & 0 deletions test/fixtures/extracted-layers/with-content-manifests.json
@@ -0,0 +1,17 @@
{
"/root/buildinfo/content_manifests/jenkins-agent-maven-35-rhel7-container-v3.11.346-2.json": {
"redhat-content-manifests": {
"metadata": {
"icm_version": 1,
"icm_spec": "https://raw.githubusercontent.com/containerbuildsystem/atomic-reactor/master/atomic_reactor/schemas/content_manifest.json",
"image_layer_index": 6
},
"content_sets": [
"rhel-7-server-ose-3.11-rpms",
"rhel-server-rhscl-7-rpms",
"rhel-7-server-rpms"
],
"image_contents": []
}
}
}
37 changes: 37 additions & 0 deletions test/lib/extractor/extractor.spec.ts
@@ -0,0 +1,37 @@
import { extractImageContent } from "../../../lib/extractor";
import { getRedHatReposContentAction } from "../../../lib/inputs/rhel/static";
import { ImageType } from "../../../lib/types";
import { getFixture } from "../../util/index";

describe("extractImageContent", () => {
it("extracts red hat repositories information from layers", async () => {
const extractedContent = await extractImageContent(
ImageType.DockerArchive,
getFixture("docker-archives/docker-save/nginx-with-buildinfo.tar"),
[getRedHatReposContentAction],
);

const numOfFoundFiles = Object.keys(extractedContent.extractedLayers)
.length;
expect(numOfFoundFiles).toBe(1);

expect(
extractedContent.extractedLayers[
"/root/buildinfo/content_manifests/jenkins-agent-maven-35-rhel7-container-v3.11.346-2.json"
]["redhat-content-manifests"],
).toMatchObject({
metadata: {
icm_version: 1,
icm_spec:
"https://raw.githubusercontent.com/containerbuildsystem/atomic-reactor/master/atomic_reactor/schemas/content_manifest.json",
image_layer_index: 6,
},
content_sets: [
"rhel-7-server-ose-3.11-rpms",
"rhel-server-rhscl-7-rpms",
"rhel-7-server-rpms",
],
image_contents: [],
});
});
});
19 changes: 19 additions & 0 deletions test/lib/inputs/rhel/static.spec.ts
@@ -0,0 +1,19 @@
import { getRedHatReposFromExtractedLayers } from "../../../../lib/inputs/rhel/static";
import { getObjFromFixture } from "../../../util";

describe("getRedHatReposFromExtractedLayers()", () => {
it("correctly gets list of repos and image layer index", () => {
const extractedLayers = getObjFromFixture(
"extracted-layers/with-content-manifests.json",
);

const repos = getRedHatReposFromExtractedLayers(extractedLayers);
expect(repos).toMatchObject({
"6": [
"rhel-7-server-ose-3.11-rpms",
"rhel-server-rhscl-7-rpms",
"rhel-7-server-rpms",
],
});
});
});

0 comments on commit 70a0058

Please sign in to comment.