Skip to content

Commit 8ef53c2

Browse files
committed
Migrate to a new basic template schema
A opm introduced a new breaking change and changed a basic template schema. The schema produced by the fbc migration tool was no longer supported and required a change. This commit adapts to a new opm version and produce new basic template schema. It also updates a onboarding doc to reflect a changes and does couple of user improvements. JIRA: ISV-5164 Signed-off-by: Ales Raszka <[email protected]>
1 parent 83d737a commit 8ef53c2

File tree

5 files changed

+68
-59
lines changed

5 files changed

+68
-59
lines changed

docs/img/release-info.png

29.1 KB
Loading

docs/users/fbc_onboarding.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ wget https://raw.githubusercontent.com/redhat-openshift-ecosystem/operator-pipel
3030
Now we can convert existing operator into FBC. The initial run takes a while because
3131
a local cache is generated during a run.
3232

33-
> [!NOTE]
33+
> **Note**
3434
> A user executing the conversion script needs to be authenticated to registries used by OLM catalog.
3535
> Use `podman login` to log in into all registries.
3636
@@ -50,6 +50,7 @@ $ make fbc-onboarding
5050
> variable in the `Makefile` and include only versions supported by an operator.
5151
5252
The Makefile will execute following steps:
53+
5354
- Download dependencies needed for the migration (opm, fbc-onboarding CLI)
5455
- Fetch a list of currently supported OCP catalogs (this might take a while when doing it for the first time)
5556
- Transform existing catalogs into a basic template
@@ -97,7 +98,7 @@ catalogs
9798
Artifacts generated in the previous step need to be added to a git and submitted via pull request. The operator pipeline validates the content of the catalogs and releases changes into ocp catalogs.
9899

99100
```bash
100-
$ git add operators/aqua/{catalog-templates,ci.yaml}
101+
$ git add operators/aqua/{catalog-templates,ci.yaml,Makefile}
101102

102103
$ git add catalogs/{v4.12,v4.13,v4.14,v4.15,v4.16}/aqua
103104

docs/users/fbc_workflow.md

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ fbc:
1515
1616
## FBC templates
1717
File-based catalog templates serve as a simplified view of a catalog that can be updated
18-
by the user. The OPM currently supports 3 types of templates and it is up to the user which
18+
by the user. The OPM currently supports 2 types of templates and it is up to the user which
1919
template the operator will be using.
2020
2121
* Basic template
@@ -90,6 +90,8 @@ in the catalog yet. To add the bundle to the catalog you need to update catalog
9090
and add a bundle pullspec given by pull request comment and open a new pull request with catalog
9191
changes.
9292

93+
![Release info](../img/release-info.png)
94+
9395
#### SemVer
9496
For example if I want to add `v1.1.0` bundle into `Fast` channel of a specific catalog I'll
9597
add it as mentioned in the example below:
@@ -123,7 +125,7 @@ Stable:
123125
Bundles:
124126
- Image: quay.io/foo/olm:testoperator.v1.0.0
125127
```
126-
Also see [opm doc](https://olm.operatorframework.io/docs/advanced-tasks/catalog-update-formulary/#fbc)
128+
Also see [opm doc](https://olm.operatorframework.io/docs/advanced-tasks/catalog-update-formulary/#semver)
127129
for automate-able step.
128130
129131
#### Basic
@@ -135,29 +137,28 @@ add it as mentioned in the example below.
135137

136138
```yaml
137139
---
138-
schema: olm.package
139-
name: example-operator
140-
defaultChannel: stable
141-
142-
---
143-
schema: olm.channel
144-
package: example-operator
145-
name: stable
140+
schema: olm.template.basic
146141
entries:
147-
- name: example-operator.v0.1.0
148-
- name: example-operator.v0.2.0 # <-- Add bundle into channel
149-
replaces: example-operator.v0.1.0
150-
151-
---
152-
schema: olm.bundle
153-
image: docker.io/example/example-operator-bundle:0.1.0
154-
155-
---
156-
schema: olm.bundle # <-- Add new bundle entry
157-
image: docker.io/example-operator-bundle:0.2.0
142+
- schema: olm.package
143+
name: example-operator
144+
defaultChannel: stable
145+
146+
- schema: olm.channel
147+
package: example-operator
148+
name: stable
149+
entries:
150+
- name: example-operator.v0.1.0
151+
- name: example-operator.v0.2.0 # <-- Add bundle into channel
152+
replaces: example-operator.v0.1.0
153+
154+
- schema: olm.bundle
155+
image: docker.io/example/example-operator-bundle:0.1.0
156+
157+
- schema: olm.bundle # <-- Add new bundle entry
158+
image: docker.io/example-operator-bundle:0.2.0
158159
```
159160

160-
Also see [opm doc](https://olm.operatorframework.io/docs/advanced-tasks/catalog-update-formulary/#semver)
161+
Also see [opm doc](https://olm.operatorframework.io/docs/advanced-tasks/catalog-update-formulary/#fbc)
161162
for automate-able step.
162163

163164
### Updating existing catalogs

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

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import logging
55
import os
66
import sys
7-
from typing import Any, List
7+
from typing import Any, Dict, Optional
88

99
import requests
1010
import yaml
@@ -118,7 +118,9 @@ def build_cache(version: str, image: str, cache_dir: str) -> None:
118118
f.write(catalog)
119119

120120

121-
def get_base_template_from_catalog(operator_name: str, catalog: Any) -> List[Any]:
121+
def get_base_template_from_catalog(
122+
operator_name: str, catalog: Any
123+
) -> Optional[Dict[str, Any]]:
122124
"""
123125
Generate a basic template from file based catalog. The function filters
124126
out operator from a catalog and removes unnecessary fields and
@@ -129,33 +131,30 @@ def get_base_template_from_catalog(operator_name: str, catalog: Any) -> List[Any
129131
catalog (Any): A file based catalog with all operators
130132
131133
Returns:
132-
List[Any]: List of basic template items for a given operator
134+
Optional[Dict[str, Any]]: A basic template for the operator.
135+
In case an operator is not present in a catalog, None is returned
133136
"""
134137
# Filter out the operator items from the catalog
135138
# Items can be identified by the name or package field
136-
operator_items = list(
137-
filter(
138-
lambda x: x.get("name") == operator_name
139-
or x.get("package") == operator_name,
140-
catalog,
141-
)
142-
)
143-
144-
# Keep only the non-bundle items - packages, channels, etc.
145-
non_bundles = list(
146-
filter(lambda x: x.get("schema") != "olm.bundle", operator_items)
139+
operator_items = (
140+
item
141+
for item in catalog
142+
if item.get("name") == operator_name or item.get("package") == operator_name
147143
)
148144

149-
bundles = list(filter(lambda x: x.get("schema") == "olm.bundle", operator_items))
145+
entries = []
150146

151-
# Keep only image and schema fields - rest can be extracted from the image
152-
# when rendering the template
153-
bundles = [
154-
{"schema": bundle.get("schema"), "image": bundle.get("image")}
155-
for bundle in bundles
156-
]
157-
158-
return non_bundles + bundles
147+
for item in operator_items:
148+
if item.get("schema") == "olm.bundle":
149+
# Keep only image and schema fields - rest can be extracted from the image
150+
# when rendering the template
151+
entries.append({"schema": item["schema"], "image": item["image"]})
152+
else:
153+
# Keep only the non-bundle items - packages, channels, etc.
154+
entries.append(item)
155+
if not entries:
156+
return None
157+
return {"schema": "olm.template.basic", "entries": entries}
159158

160159

161160
def create_catalog_template_dir_if_not_exists(operator: Any) -> str:
@@ -182,7 +181,7 @@ def create_catalog_template_dir_if_not_exists(operator: Any) -> str:
182181

183182
def generate_and_save_base_templates(
184183
version: str, operator_name: str, cache_dir: str, template_dir: str
185-
) -> List[Any]:
184+
) -> Optional[Dict[str, Any]]:
186185
"""
187186
Generate and save basic templates for a given operator and version
188187
@@ -193,7 +192,8 @@ def generate_and_save_base_templates(
193192
template_dir (str): A directory where the templates will be stored
194193
195194
Returns:
196-
List[Any]: List of basic template items for a given operator
195+
Optional[Dict[str, Any]]: A basic template for the operator.
196+
In case an operator is not present in a catalog, None is returned
197197
"""
198198
with open(os.path.join(cache_dir, f"{version}.yaml"), "r", encoding="utf8") as f:
199199
catalog = yaml.safe_load_all(f)
@@ -206,7 +206,7 @@ def generate_and_save_base_templates(
206206

207207
template_path = os.path.join(template_dir, f"v{version}.yaml")
208208
with open(template_path, "w", encoding="utf8") as f:
209-
yaml.safe_dump_all(basic_template, f, explicit_start=True, indent=2)
209+
yaml.safe_dump(basic_template, f, explicit_start=True, indent=2)
210210

211211
LOGGER.info("Template for %s saved to %s", version, template_path)
212212
return basic_template
@@ -282,8 +282,9 @@ def onboard_operator_to_fbc(
282282
"""
283283
organization = repository.config.get("organization")
284284
supported_catalogs = get_supported_catalogs(organization, stage)
285-
supported_versions = [catalog.get("ocp_version") for catalog in supported_catalogs]
286-
285+
supported_versions = sorted(
286+
[catalog.get("ocp_version") for catalog in supported_catalogs]
287+
)
287288
LOGGER.info(
288289
"Generating FBC templates for following versions: %s", supported_versions
289290
)

operator-pipeline-images/tests/entrypoints/test_fbc_onboarding.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,16 @@ def test_get_base_template_from_catalog() -> None:
100100

101101
result = fbc_onboarding.get_base_template_from_catalog("pkg1", catalog)
102102

103-
assert result == [
104-
{"package": "pkg1", "schema": "olm.channel"},
105-
{"schema": "olm.bundle", "image": "img1"},
106-
]
103+
assert result == {
104+
"schema": "olm.template.basic",
105+
"entries": [
106+
{"schema": "olm.bundle", "image": "img1"},
107+
{"package": "pkg1", "schema": "olm.channel"},
108+
],
109+
}
110+
111+
result = fbc_onboarding.get_base_template_from_catalog("pkg3", catalog)
112+
assert result is None
107113

108114

109115
@patch("builtins.input")
@@ -128,13 +134,13 @@ def test_create_catalog_template_dir_if_not_exists(
128134

129135

130136
@patch("operatorcert.entrypoints.fbc_onboarding.get_base_template_from_catalog")
131-
@patch("operatorcert.entrypoints.fbc_onboarding.yaml.safe_dump_all")
137+
@patch("operatorcert.entrypoints.fbc_onboarding.yaml.safe_dump")
132138
@patch("operatorcert.entrypoints.fbc_onboarding.yaml.safe_load_all")
133139
def test_generate_and_save_base_templates(
134140
mock_yaml_load: MagicMock, mock_yaml_dump: MagicMock, mock_template: MagicMock
135141
) -> None:
136142
mock_yaml_load.return_value = []
137-
mock_template.return_value = [{}]
143+
mock_template.return_value = {"schema": "olm.template.basic"}
138144

139145
with mock.patch("builtins.open", mock.mock_open()) as mock_open:
140146
resp = fbc_onboarding.generate_and_save_base_templates(
@@ -146,7 +152,7 @@ def test_generate_and_save_base_templates(
146152

147153

148154
@patch("operatorcert.entrypoints.fbc_onboarding.get_base_template_from_catalog")
149-
@patch("operatorcert.entrypoints.fbc_onboarding.yaml.safe_dump_all")
155+
@patch("operatorcert.entrypoints.fbc_onboarding.yaml.safe_dump")
150156
@patch("operatorcert.entrypoints.fbc_onboarding.yaml.safe_load_all")
151157
def test_generate_and_save_base_templates_no_content(
152158
mock_yaml_load: MagicMock, mock_yaml_dump: MagicMock, mock_template: MagicMock

0 commit comments

Comments
 (0)