Skip to content

Commit 42786e0

Browse files
authored
Merge pull request #341 from nmars/add-subscription-check
Parse OpenShift subscription annotation from CSV
2 parents 6b584f1 + cc92d65 commit 42786e0

File tree

4 files changed

+74
-3
lines changed

4 files changed

+74
-3
lines changed

Dockerfiles/ci/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ WORKDIR /project
33
ARG OPERATOR_SDK_VERSION=v1.16.0
44
RUN export ARCH=$(case $(arch) in x86_64) echo -n amd64 ;; aarch64) echo -n arm64 ;; *) echo -n $(arch) ;; esac);\
55
export OS=$(uname | awk '{print tolower($0)}');\
6-
export OPERATOR_SDK_DL_URL=https://github.com/operator-framework/operator-sdk/releases/download/$OPERATOR_SDK_VERSION/;\
6+
export OPERATOR_SDK_DL_URL=https://github.com/operator-framework/operator-sdk/releases/download/$OPERATOR_SDK_VERSION;\
77
curl -L -o /usr/local/bin/operator-sdk ${OPERATOR_SDK_DL_URL}/operator-sdk_${OS}_${ARCH} && \
88
chmod a+x /usr/local/bin/operator-sdk && \
99
curl -fL -o /usr/local/bin/yq https://github.com/mikefarah/yq/releases/download/2.2.1/yq_linux_amd64 && \

Dockerfiles/ci/run_tests.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,60 @@ def test_validate_invalid_ocp_version_failure(self):
165165
self.assertIn("Invalid semver in ocpfournine", playbook_command.stdout.decode("utf-8"))
166166
self.assertIn("Error collecting OCP version range from Pyxis", playbook_command.stdout.decode("utf-8"))
167167

168+
def test_extract_operator_bundle_no_subscription(self):
169+
operator_work_dir = "{}/test_extract_operator_bundle_no_subscription".format(self.test_dir)
170+
work_dir = operator_work_dir
171+
operator_dir = "{}/test-operator".format(operator_work_dir)
172+
operator_bundle_dir = "{}/operator-bundle".format(operator_work_dir)
173+
bundle_image = "quay.io/cvpops/test-operator:test-430-channel-positive-v1"
174+
exec_cmd = "ansible-playbook -vvv -i localhost, --connection local \
175+
operator-test-playbooks/extract-operator-bundle.yml \
176+
-e 'operator_dir={operator_dir}' \
177+
-e 'bundle_image={bundle_image}' \
178+
-e 'operator_work_dir={operator_work_dir}' \
179+
-e 'operator_bundle_dir={operator_bundle_dir}' \
180+
-e 'work_dir={work_dir}'".format(operator_dir=operator_dir,
181+
operator_work_dir=operator_work_dir,
182+
operator_bundle_dir=operator_bundle_dir,
183+
bundle_image=bundle_image,
184+
work_dir=work_dir)
185+
playbook_command = subprocess.run(exec_cmd, shell=True)
186+
187+
print(playbook_command.returncode)
188+
self.assertTrue(playbook_command.returncode == 0)
189+
self.assertTrue(path.exists("{}/parsed_operator_data.yml".format(work_dir)))
190+
with open("{}/parsed_operator_data.yml".format(work_dir), "r") as fd:
191+
parsed_output = fd.read()
192+
print(parsed_output)
193+
self.assertIn('operator_valid_subscription: []', parsed_output)
194+
195+
def test_extract_operator_bundle_with_subscription(self):
196+
operator_work_dir = "{}/test_extract_operator_bundle_with_subscription".format(self.test_dir)
197+
work_dir = operator_work_dir
198+
operator_dir = "{}/test-operator".format(operator_work_dir)
199+
operator_bundle_dir = "{}/operator-bundle".format(operator_work_dir)
200+
bundle_image = "quay.io/cvpops/test-operator:with-subscription"
201+
exec_cmd = "ansible-playbook -vvv -i localhost, --connection local \
202+
operator-test-playbooks/extract-operator-bundle.yml \
203+
-e 'operator_dir={operator_dir}' \
204+
-e 'bundle_image={bundle_image}' \
205+
-e 'operator_work_dir={operator_work_dir}' \
206+
-e 'operator_bundle_dir={operator_bundle_dir}' \
207+
-e 'work_dir={work_dir}'".format(operator_dir=operator_dir,
208+
operator_work_dir=operator_work_dir,
209+
operator_bundle_dir=operator_bundle_dir,
210+
bundle_image=bundle_image,
211+
work_dir=work_dir)
212+
playbook_command = subprocess.run(exec_cmd, shell=True)
213+
214+
print(playbook_command.returncode)
215+
self.assertTrue(playbook_command.returncode == 0)
216+
self.assertTrue(path.exists("{}/parsed_operator_data.yml".format(work_dir)))
217+
with open("{}/parsed_operator_data.yml".format(work_dir), "r") as fd:
218+
parsed_output = fd.read()
219+
print(parsed_output)
220+
self.assertIn('operator_valid_subscription:\n - "Test Subscription One"\n - "Test Subscription Two"', parsed_output)
221+
168222

169223
if __name__ == '__main__':
170224
unittest.main()

roles/parse_operator_bundle/tasks/main.yml

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,12 +104,13 @@
104104
set_fact:
105105
csv_vars: "{{ csv_data.stdout }}"
106106

107-
- name: "Determine and set fact for operator specific information - name, pod name, container name and capabilities"
107+
- name: "Determine and set fact for operator specific information - name, pod name, container name, capabilities, and subscription"
108108
set_fact:
109109
current_csv: "{{ (csv_vars | from_yaml).metadata.name }}"
110110
operator_pod_name: "{{ (csv_vars | from_yaml).spec.install.spec.deployments[0].name }}"
111111
operator_container_name: "{{ (csv_vars | from_yaml).spec.install.spec.deployments[0].spec.template.spec.containers[0].name }}"
112112
operator_capabilities: "{{ (csv_vars | from_yaml).metadata.annotations.capabilities }}"
113+
operator_valid_subscription: "{{ ((csv_vars | from_yaml).metadata.annotations['operators.openshift.io/valid-subscription'] | default('[]')) | from_json }}"
113114

114115
- name: "Determine operator_allnamespaces_support"
115116
set_fact:
@@ -148,6 +149,14 @@
148149
dest: "{{ work_dir }}/parsed_operator_data.yml"
149150
mode: 0644
150151

152+
- name: "Cat parsed_operator_data.yml"
153+
shell: "cat {{ work_dir }}/parsed_operator_data.yml"
154+
register: parsed_data_file
155+
156+
- name: "Print contents of parsed_operator_data.yml"
157+
debug:
158+
msg: "{{ parsed_data_file.stdout_lines }}"
159+
151160
- name: "Sanity check the operator bundle's information"
152161
include_tasks: bundle_sanity_checks.yml
153162
when: bundle_sanity_checks|bool

roles/parse_operator_bundle/templates/parsed_operator_data.yml.j2

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,14 @@ operator_allnamespaces_support: {{ operator_allnamespaces_support }}
1313
operator_ownnamespace_support: {{ operator_ownnamespace_support }}
1414
operator_singlenamespace_support: {{ operator_singlenamespace_support }}
1515
operator_multinamespace_support: {{ operator_multinamespace_support }}
16+
{% if operator_valid_subscription|length < 1 %}
17+
operator_valid_subscription: []
18+
{% else %}
19+
operator_valid_subscription:
20+
{% for valid_subscription in operator_valid_subscription %}
21+
- "{{ valid_subscription }}"
22+
{% endfor %}
23+
{% endif %}
1624
{% if crd_paths|length < 1 %}
1725
crd_paths: []
1826
{% else %}
@@ -21,4 +29,4 @@ crd_paths:
2129
- {{ crd_path }}
2230
{% endfor %}
2331
{% endif %}
24-
ocp_versions: "{{ ocp_versions }}"
32+
ocp_versions: "{{ ocp_versions }}"

0 commit comments

Comments
 (0)