Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add conditional components #118

Merged
merged 1 commit into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Development.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ helm template helm \
oc create namespace poolboy-dev
------------------------------

. Change project to `poolboy-dev` namespace:
+
----------------------
oc project poolboy-dev
----------------------

. Grant privileges for cluster role `poolboy-dev` to default service account:
+
-------------------------------------------------------------
Expand Down
4 changes: 4 additions & 0 deletions helm/crds/resourceproviders.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,10 @@ spec:
Condition is given in Jinja2 syntax similar to ansible "when" clauses.
The linked provider's resource state may be referenced with "resource_state".
type: string
when:
description: >-
Condition which is used to determine if this provider should be used.
type: string
match:
description: >-
Partial resource definition used to check if a resource in a handle or claim
Expand Down
4 changes: 4 additions & 0 deletions helm/templates/crds/resourceproviders.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,10 @@ spec:
Condition is given in Jinja2 syntax similar to ansible "when" clauses.
The linked provider's resource state may be referenced with "resource_state".
type: string
when:
description: >-
Condition which is used to determine if this provider should be used.
type: string
match:
description: >-
Partial resource definition used to check if a resource in a handle or claim
Expand Down
6 changes: 3 additions & 3 deletions operator/resourcehandle.py
Original file line number Diff line number Diff line change
Expand Up @@ -974,12 +974,12 @@ async def manage(self, logger: kopf.ObjectLogger) -> None:
linked_resource_state = resource_states[pn]
break
else:
raise kopf.TemporaryError(
logger.debug(
f"{self} uses {resource_provider} which has "
f"linked ResourceProvider {resource_provider.name} but no resource in this "
f"ResourceHandle use this provider.",
delay=600
f"ResourceHandle uses this provider."
)
continue

if not linked_provider.check_wait_for(
linked_resource_provider = linked_resource_provider,
Expand Down
37 changes: 37 additions & 0 deletions operator/resourceprovider.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ def __init__(self, spec):
self.parameter_values = spec.get('parameterValues', {})
self.resource_name = spec.get('resourceName', self.name)
self.wait_for = spec.get('waitFor')
self.when = spec.get('when')
self.template_vars = [
_TemplateVar(item) for item in spec.get('templateVars', [])
]
Expand Down Expand Up @@ -72,6 +73,35 @@ def check_wait_for(self,
'{{(' + self.wait_for + ')|bool}}', resource_provider.template_style, vars_
)

def check_when(self,
parameter_values: Optional[Mapping] = None,
resource_claim: Optional[ResourceClaimT] = None,
resource_handle: Optional[ResourceHandleT] = None,
resource_provider: Optional[ResourceProviderT] = None,
) -> bool:
if not self.when:
return True

if parameter_values == None:
parameter_values = {**self.parameter_defaults}
if resource_claim:
parameter_values.update(resource_claim.parameter_values)
elif resource_handle:
parameter_values.update(resource_handle.parameter_values)

resource_handle_vars = resource_handle.vars if resource_handle else {}

return recursive_process_template_strings(
'{{(' + self.when + ')|bool}}', resource_provider.template_style, {
**resource_provider.vars,
**resource_handle_vars,
**parameter_values,
"resource_claim": resource_claim,
"resource_handle": resource_handle,
"resource_provider": self,
}
)


class _Parameter:
def __init__(self, definition):
Expand Down Expand Up @@ -512,6 +542,13 @@ async def get_resources(self,
resources = []
for linked_resource_provider in self.linked_resource_providers:
resource_provider = await self.get(linked_resource_provider.name)
if not linked_resource_provider.check_when(
parameter_values=parameter_values,
resource_claim=resource_claim,
resource_handle=resource_handle,
resource_provider=self,
):
continue
resources.extend(
await resource_provider.get_resources(
resource_claim = resource_claim,
Expand Down
Loading
Loading