Skip to content

Commit 7b527a8

Browse files
committed
Build temporary catalog for FBC operators
The temporary catalog is build in hosted pipeline and is used for preflight testing. The catalog contains just one operator and it is generated using basic template. JIRA: ISV-4990 Signed-off-by: Ales Raszka <[email protected]>
1 parent 461ed64 commit 7b527a8

File tree

11 files changed

+610
-141
lines changed

11 files changed

+610
-141
lines changed

ansible/roles/operator-pipeline/templates/openshift/pipelines/operator-hosted-pipeline.yml

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -844,7 +844,14 @@ spec:
844844
- name: add-bundle-to-index
845845
runAfter:
846846
- add-fbc-fragments-to-index
847-
when: *operatorOrBundleChange
847+
when:
848+
# Run only when a bundle is added and it doesn't use FBC
849+
- input: "$(tasks.detect-changes.results.affected_catalogs)"
850+
operator: in
851+
values: [""]
852+
- input: "$(tasks.read-config.results.fbc-enabled)"
853+
operator: notin
854+
values: ["true"]
848855
taskRef:
849856
name: add-bundle-to-index
850857
params:
@@ -875,9 +882,50 @@ spec:
875882
workspace: results
876883
subPath: paths
877884

885+
- name: build-fbc-scratch-catalog
886+
runAfter:
887+
- add-fbc-fragments-to-index
888+
when:
889+
# Run only when a bundle is added and it uses FBC
890+
- input: "$(tasks.detect-changes.results.affected_catalogs)"
891+
operator: in
892+
values: [""]
893+
- input: "$(tasks.read-config.results.fbc-enabled)"
894+
operator: in
895+
values: ["true"]
896+
taskRef:
897+
name: build-fbc-scratch-catalog
898+
params:
899+
- name: pipeline_image
900+
value: "$(params.pipeline_image)"
901+
- name: fbc_enabled
902+
value: "$(tasks.read-config.results.fbc-enabled)"
903+
- name: ocp_version
904+
value: "$(tasks.get-supported-versions.results.max_supported_ocp_version)"
905+
- name: bundle_pullspec
906+
value: *bundleImage
907+
- name: operator_name
908+
value: "$(tasks.detect-changes.results.added_operator)"
909+
- name: bundle_version
910+
value: "$(tasks.detect-changes.results.added_bundle)"
911+
- name: index_image_destination
912+
value: *tempIndexRepo
913+
- name: index_image_destination_tag_suffix
914+
value: "-$(params.git_commit)"
915+
workspaces:
916+
- name: source
917+
workspace: repository
918+
subPath: src
919+
- name: credentials
920+
workspace: registry-credentials
921+
- name: output
922+
workspace: results
923+
subPath: paths
924+
878925
- name: make-index-repo-public
879926
runAfter:
880927
- add-bundle-to-index
928+
- build-fbc-scratch-catalog
881929
when: *operatorOrBundleChange
882930
taskRef:
883931
name: set-quay-repo-visibility
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
---
2+
apiVersion: tekton.dev/v1
3+
kind: Task
4+
metadata:
5+
name: build-fbc-scratch-catalog
6+
spec:
7+
description: |
8+
The purpose of this task is to build a new index/catalog image with a single
9+
operator bundle. The catalog is used only for testing purposes to test if
10+
a bundle can be installed. After the tests are executed the catalog is
11+
no longer used.
12+
13+
The catalog is build using FBC mechanism:
14+
- create an empty catalog
15+
- create a basic FBC template and insert the bundle
16+
- render a template to FBC
17+
- build and push a catalog to registry given by arguments
18+
params:
19+
- name: pipeline_image
20+
21+
- name: catalog_repository
22+
default: "."
23+
description: A git repository where catalog is pushed
24+
25+
- name: ocp_version
26+
description: A version of OCP that is going to be used as a target for a catalog
27+
28+
- name: bundle_pullspec
29+
description: Bundle pullspec
30+
31+
- name: operator_name
32+
description: A name of an operator given by the directory name in git repository.
33+
34+
- name: bundle_version
35+
description: A version of a bundle that will be added to the catalog
36+
37+
- name: index_image_destination
38+
default: ""
39+
description: "The destination registry and repository where index images will be pushed"
40+
41+
- name: index_image_destination_tag_suffix
42+
default: ""
43+
description: "A tag suffix that is added to an image tag in destination registry"
44+
45+
workspaces:
46+
- name: source
47+
- name: output
48+
- name: credentials
49+
optional: true
50+
description: A workspace with credentials for registry push
51+
steps:
52+
- name: build-fbc-catalog
53+
image: "$(params.pipeline_image)"
54+
workingDir: $(workspaces.source.path)
55+
securityContext:
56+
privileged: true
57+
script: |
58+
#! /usr/bin/env bash
59+
set -xe
60+
61+
EXTRA_ARGS=""
62+
if [[ "$(workspaces.credentials.bound)" == "true" ]]; then
63+
EXTRA_ARGS+=" --authfile $(workspaces.credentials.path)/.dockerconfigjson"
64+
fi
65+
66+
# The registry pull-spec is in following format:
67+
# quay.io/{namespace}/{repository}:{ocp-version}{suffix}
68+
# Example: quay.io/namespace/repo-name:v4.15-suffix
69+
CATALOG_REPO="$(params.index_image_destination):v$(params.ocp_version)$(params.index_image_destination_tag_suffix)"
70+
71+
build-scratch-catalog \
72+
--repo-path "$(params.catalog_repository)" \
73+
--operator-name "$(params.operator_name)" \
74+
--bundle-version "$(params.bundle_version)" \
75+
--bundle-pullspec "$(params.bundle_pullspec)" \
76+
--repository-destination $CATALOG_REPO \
77+
--verbose $EXTRA_ARGS
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
"""Module for building and pushing images using buildah."""
2+
3+
import logging
4+
import os
5+
from typing import Any
6+
7+
from operatorcert.utils import run_command
8+
9+
LOGGER = logging.getLogger("operator-cert")
10+
11+
12+
def build_image(dockerfile_path: str, context: str, output_image: str) -> Any:
13+
"""
14+
Build an image using buildah using given dockerfile and context.
15+
16+
Args:
17+
dockerfile_path (str): Path to a dockerfile
18+
context (str): Build context directory
19+
output_image (str): A name of the output image
20+
21+
Returns:
22+
Any: Command output
23+
"""
24+
cmd = [
25+
"buildah",
26+
"bud",
27+
"--format",
28+
"docker",
29+
"-f",
30+
dockerfile_path,
31+
"-t",
32+
output_image,
33+
context,
34+
]
35+
LOGGER.info("Building image: %s", output_image)
36+
return run_command(cmd)
37+
38+
39+
def push_image(image: str, authfile: str) -> Any:
40+
"""
41+
Push an image to a registry using buildah.
42+
43+
Args:
44+
image (str): A name of the image to push
45+
authfile (str): A path to the authentication file
46+
47+
Returns:
48+
Any: Command output
49+
"""
50+
authfile = os.path.expanduser(authfile)
51+
cmd = [
52+
"buildah",
53+
"push",
54+
"--authfile",
55+
authfile,
56+
image,
57+
f"docker://{image}",
58+
]
59+
60+
LOGGER.info("Pushing image: %s", image)
61+
return run_command(cmd)

operator-pipeline-images/operatorcert/entrypoints/build_fragment_images.py

Lines changed: 5 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,10 @@
33
import argparse
44
import json
55
import logging
6-
import os
7-
from typing import Any
86

7+
from operatorcert import buildah, opm
98
from operatorcert.logger import setup_logger
10-
from operatorcert.utils import SplitArgs, run_command
9+
from operatorcert.utils import SplitArgs
1110

1211
LOGGER = logging.getLogger("operator-cert")
1312

@@ -52,84 +51,6 @@ def setup_argparser() -> argparse.ArgumentParser:
5251
return parser
5352

5453

55-
def create_dockerfile(catalog_path: str, catalog_name: str) -> str:
56-
"""
57-
Generate a Dockerfile using opm for a given catalog.
58-
59-
Args:
60-
catalog_path (str): Path to a catalogs direcotory
61-
catalog_name (str): Name of the catalog in the catalogs directory
62-
63-
Returns:
64-
str: A Dockerfile path for the given catalog
65-
"""
66-
dockerfile_path = f"{catalog_path}/{catalog_name}.Dockerfile"
67-
if os.path.exists(dockerfile_path):
68-
LOGGER.warning("Dockerfile already exists: %s. Removing...", dockerfile_path)
69-
os.remove(dockerfile_path)
70-
cmd = [
71-
"opm",
72-
"generate",
73-
"dockerfile",
74-
f"{catalog_path}/{catalog_name}",
75-
]
76-
LOGGER.debug("Creating dockerfile: %s", catalog_name)
77-
run_command(cmd)
78-
return f"{catalog_path}/{catalog_name}.Dockerfile"
79-
80-
81-
def build_fragment_image(dockerfile_path: str, context: str, output_image: str) -> Any:
82-
"""
83-
Build a fragment image using buildah using given dockerfile and context.
84-
85-
Args:
86-
dockerfile_path (str): Path to a dockerfile
87-
context (str): Build context directory
88-
output_image (str): A name of the output image
89-
90-
Returns:
91-
Any: Command output
92-
"""
93-
cmd = [
94-
"buildah",
95-
"bud",
96-
"--format",
97-
"docker",
98-
"-f",
99-
dockerfile_path,
100-
"-t",
101-
output_image,
102-
context,
103-
]
104-
LOGGER.info("Building fragment image: %s", output_image)
105-
return run_command(cmd)
106-
107-
108-
def push_fragment_image(image: str, authfile: str) -> Any:
109-
"""
110-
Push a fragment image to a registry using buildah.
111-
112-
Args:
113-
image (str): A name of the image to push
114-
authfile (str): A path to the authentication file
115-
116-
Returns:
117-
Any: Command output
118-
"""
119-
authfile = os.path.expanduser(authfile)
120-
cmd = [
121-
"buildah",
122-
"push",
123-
"--authfile",
124-
authfile,
125-
image,
126-
f"docker://{image}",
127-
]
128-
129-
LOGGER.info("Pushing fragment image: %s", image)
130-
return run_command(cmd)
131-
132-
13354
def create_fragment_image(
13455
catalog_path: str, catalog_name: str, args: argparse.Namespace
13556
) -> str:
@@ -145,19 +66,19 @@ def create_fragment_image(
14566
Returns:
14667
str: A pullspec of the pushed fragment image
14768
"""
148-
dockerfile_path = create_dockerfile(catalog_path, catalog_name)
69+
dockerfile_path = opm.create_catalog_dockerfile(catalog_path, catalog_name)
14970

15071
# Add '-<tag_suffix>' to the repository destination if tag_suffix is set
15172
suffix = f"-{args.tag_suffix}" if args.tag_suffix else ""
15273
repository_destination = f"{args.repository_destination}:{catalog_name}{suffix}"
15374

15475
# Build and push the fragment image
155-
build_fragment_image(
76+
buildah.build_image(
15677
dockerfile_path,
15778
catalog_path,
15879
repository_destination,
15980
)
160-
push_fragment_image(repository_destination, args.authfile)
81+
buildah.push_image(repository_destination, args.authfile)
16182

16283
return repository_destination
16384

0 commit comments

Comments
 (0)