Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Correctly validate crd.spec.versions
Browse files Browse the repository at this point in the history
crd.spec.version got deprecated for crd.spec.versions
but in this case we are going to access a list of
dictionaries where the relevant value
is in the name field.

Fix all the validations on spec.versions and add
additional validations.

Fix also its test.

Signed-off-by: Simone Tiraboschi [email protected]

Fixes: operator-framework#188
tiraboschi committed Jul 15, 2020
1 parent 347ca21 commit 3243d39
Showing 3 changed files with 59 additions and 9 deletions.
56 changes: 50 additions & 6 deletions operatorcourier/validate.py
Original file line number Diff line number Diff line change
@@ -156,11 +156,52 @@ def _crd_validation(self, bundle):
if "group" not in crd['spec']:
self._log_error("crd spec.group not defined.")
valid = False
if "versions" not in crd['spec']:
if "version" not in crd['spec']:
self._log_error("crd spec.version or spec.versions not defined")
if (
"versions" not in crd['spec'] and
"version" not in crd['spec']
):
self._log_error(
"crd spec.version or spec.versions not defined."
)
valid = False
if "versions" in crd['spec']:
if not len(crd['spec']['versions']) > 0:
self._log_error("crd spec.versions is empty.")
valid = False

else:
for ver in crd['spec']['versions']:
if (
"name" not in ver or
"served" not in ver or
"storage" not in ver
):
self._log_error(
"crd spec.versions contains an invalid "
"CustomResourceDefinitionVersion."
)
valid = False
if "version" in crd['spec']:
if (
"name" in crd['spec']['version'][0] and
crd['spec']['version'][0]['name'] !=
crd['spec']['version']
):
self._log_error(
"crd spec.version and spec.versions are "
"defined but spec.versions[0].name "
"doesn't match spec.version."
)
valid = False
storage_version_list = [
v for v in crd['spec']['versions']
if "storage" in v and v['storage'] is True
]
if len(storage_version_list) != 1:
self._log_error(
"crd spec.version should contain exactly "
"one version flagged as storage version."
)
valid = False
return valid

def _csv_validation(self, bundle):
@@ -273,8 +314,11 @@ def _csv_spec_validation(self, spec, bundleData):
if 'version' in csvOwnedCrd:
if 'spec' in crd:
if 'versions' in crd['spec']:
if csvOwnedCrd['version'] not in crd['spec']['versions']:
self._log_error('CSV.spec.crd.owned.version is'
if csvOwnedCrd['version'] not in [
v['name'] for v in crd['spec']['versions']
if 'name' in v
]:
self._log_error('CSV.spec.crd.owned.version is '
'not in CRD.spec.versions list')
valid = False
if 'version' in crd['spec']:
Original file line number Diff line number Diff line change
@@ -263,7 +263,9 @@ data:
- packages
type: object
versions:
- v1beta1
- name: v1beta1
served: true
storage: true
- apiVersion: apiextensions.k8s.io/v1beta1
kind: CustomResourceDefinition
metadata:
Original file line number Diff line number Diff line change
@@ -263,8 +263,12 @@ data:
- packages
type: object
versions:
- v1alpha1
- v1beta1
- name: v1beta1
served: true
storage: true
- name: v1alpha1
served: true
storage: false
- apiVersion: apiextensions.k8s.io/v1beta1
kind: CustomResourceDefinition
metadata:

0 comments on commit 3243d39

Please sign in to comment.