Repository: redhat-openshift-ecosystem/operator-pipelines

Issue:
add-bundle-to-fbc does not add channel
When trying to add a new channel to a basic template using `add-bundle-to-fbc`, it does not pick up the new channel:

```yaml
# ci.yaml
---
updateGraph: replaces-mode

fbc:
  enabled: true
  catalog_mapping:
  - template_name: basic.yaml
    catalog_names: ["v4.17"]
    type: olm.template.basic
```

```yaml
# release-config.yaml
---
catalog_templates:
  - template_name: basic.yaml
    channels: [stable]
```

```yaml
# basic.yaml
---
schema: olm.template.basic
entries:
- schema: olm.package
  name: memcached-operator
  defaultChannel: alpha
- schema: olm.channel
  name: alpha
  package: memcached-operator
  entries:
  - name: memcached-operator.v0.0.1
- schema: olm.bundle
  image: memcached-operator-bundle:v0.0.1
```

Then after running `add-bundle-to-fbc --operator-name memcached-operator --operator-version 0.0.2 --bundle-pullspec ... ` where 0.0.2 has a new stable channel, this is the result:

```yaml
# basic.yaml
---
schema: olm.template.basic
entries:
- schema: olm.package
  name: memcached-operator
  defaultChannel: alpha
- schema: olm.channel
  name: alpha
  package: memcached-operator
  entries:
  - name: memcached-operator.v0.0.1
- schema: olm.bundle
  image: memcached-operator-bundle:v0.0.1
- schema: olm.bundle
  image: memcached-operator-bundle:v0.0.2
```

No errors are present in the logs.

I'm running version `v1.0.165`

Hints from issue comments:
@Allda: @plagerweij Thanks for reporting the issue. It looks like this case was indeed missed in the script. We will fix it and release it soon and let you know when it is ready.@plagerweij: @Allda thanks!

Solution patch:
diff --git a/operator-pipeline-images/operatorcert/entrypoints/add_bundle_to_fbc.py b/operator-pipeline-images/operatorcert/entrypoints/add_bundle_to_fbc.py
index c6abbb7..b6982ab 100644
--- a/operator-pipeline-images/operatorcert/entrypoints/add_bundle_to_fbc.py
+++ b/operator-pipeline-images/operatorcert/entrypoints/add_bundle_to_fbc.py
@@ -256,6 +256,31 @@ class BasicTemplate(CatalogTemplate):
             }
         )
 
+    def add_channels_if_not_present(self, release_config: dict[str, Any]) -> None:
+        """
+        Add channels to the template if they are not present.
+        A channel is added as a placeholder for a new bundle.
+
+        Args:
+            release_config (dict[str, Any]): A release configuration for the bundle.
+        """
+        current_channels = [
+            item["name"]
+            for item in self.template["entries"]
+            if item["schema"] == "olm.channel"
+        ]
+        for channel in release_config["channels"]:
+            if channel in current_channels:
+                continue
+            self.template["entries"].append(
+                {
+                    "schema": "olm.channel",
+                    "name": channel,
+                    "package": self.operator.operator_name,
+                    "entries": [],
+                }
+            )
+
     def amend(
         self, release_config: dict[str, Any], bundle_pullspec: str, bundle: Bundle
     ) -> None:
@@ -268,6 +293,7 @@ class BasicTemplate(CatalogTemplate):
             bundle (Bundle): A bundle object to be added to the template.
         """
         LOGGER.info("Amending basic template %s", self.template_name)
+        self.add_channels_if_not_present(release_config)
         self.add_bundle_if_not_present(self.template, bundle_pullspec)
         channels_names = release_config["channels"]
         for item in self.template["entries"]:


Test patch:
diff --git a/operator-pipeline-images/tests/entrypoints/test_add_bundle_to_fbc.py b/operator-pipeline-images/tests/entrypoints/test_add_bundle_to_fbc.py
index f12e1de..e98c715 100644
--- a/operator-pipeline-images/tests/entrypoints/test_add_bundle_to_fbc.py
+++ b/operator-pipeline-images/tests/entrypoints/test_add_bundle_to_fbc.py
@@ -113,7 +113,7 @@ def test_BasicCatalogTemplate_amend(
     basic_catalog_template: add_bundle_to_fbc.BasicTemplate,
 ) -> None:
     release_config = {
-        "channels": ["alpha"],
+        "channels": ["alpha", "beta"],
         "replaces": "fake-replaces",
         "skipRange": "fake-range",
         "skips": "fake-skips",
@@ -174,6 +174,22 @@ def test_BasicCatalogTemplate_amend(
                 "image": "quay.io/fake-bundle:1",
                 "schema": "olm.bundle",
             },
+            {
+                "entries": [
+                    {
+                        "name": "fake-bundle-2",
+                        # Values below don't make sense
+                        # in the context of the catalog graph but it covers the
+                        # usecase of adding a bundle to new channel
+                        "replaces": "fake-replaces",
+                        "skipRange": "fake-range",
+                        "skips": "fake-skips",
+                    },
+                ],
+                "name": "beta",
+                "package": "fake-operator",
+                "schema": "olm.channel",
+            },
             {
                 "image": "quay.io/fake-bundle:2",
                 "schema": "olm.bundle",