Skip to content
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
28 changes: 28 additions & 0 deletions scripts/bundle-generation/bundles-to-charts.py
Original file line number Diff line number Diff line change
Expand Up @@ -764,6 +764,32 @@ def update_helm_resources(chartName, helmChart, skip_rbac_overrides, exclusions,

logging.info("Resource updating process completed.")


def wrapNetworkPoliciesWithCondition(helmChart, branch):
"""Wraps NetworkPolicy templates with a Helm conditional so they are only
deployed when global.networkPolicies.enabled is true.

Args:
helmChart: Path to the Helm chart directory
branch: Branch name for version compatibility checks
"""
logging.info("Wrapping NetworkPolicy templates with networkPolicies.enabled condition ...")
networkPolicyTemplates = find_templates_of_type(helmChart, "NetworkPolicy", branch)
for template_path in networkPolicyTemplates:
f = open(template_path, "r")
content = f.read()
f.close()
if '{{- if .Values.global.networkPolicies.enabled }}' not in content:
if not content.endswith('\n'):
content += '\n'
wrapped = '{{- if .Values.global.networkPolicies.enabled }}\n' + content + '{{- end }}\n'
a_file = open(template_path, "w")
a_file.write(wrapped)
a_file.close()
logging.info("Wrapped NetworkPolicy template: %s", template_path)
logging.info("NetworkPolicy wrapping complete.\n")


# Given a resource Kind, return all filepaths of that resource type in a chart directory
def find_templates_of_type(helmChart, kind, branch):
"""_summary_
Expand Down Expand Up @@ -1436,6 +1462,8 @@ def injectRequirements(helm_chart_path, operator, sizes, branch):
updateRBAC(helm_chart_path, branch)
updateDeployments(helm_chart_path, operator, exclusions, sizes, branch)

wrapNetworkPoliciesWithCondition(helm_chart_path, branch)

logging.info("Updated Chart '%s' successfully\n", helm_chart_path)
return []

Expand Down
21 changes: 21 additions & 0 deletions scripts/bundle-generation/generate-charts.py
Original file line number Diff line number Diff line change
Expand Up @@ -1246,6 +1246,25 @@ def update_helm_resources(chartName, helmChart, skip_rbac_overrides, exclusions,

logging.info("Resource updating process completed.")


def wrapNetworkPoliciesWithCondition(helmChart):
logging.info("Wrapping NetworkPolicy templates with networkPolicies.enabled condition ...")
networkPolicyTemplates = find_templates_of_type(helmChart, "NetworkPolicy")
for template_path in networkPolicyTemplates:
f = open(template_path, "r")
content = f.read()
f.close()
if '{{- if .Values.global.networkPolicies.enabled }}' not in content:
if not content.endswith('\n'):
content += '\n'
wrapped = '{{- if .Values.global.networkPolicies.enabled }}\n' + content + '{{- end }}\n'
a_file = open(template_path, "w")
a_file.write(wrapped)
a_file.close()
logging.info("Wrapped NetworkPolicy template: %s", template_path)
logging.info("NetworkPolicy wrapping complete.\n")


# injectAnnotationsForAddonTemplate injects following annotations for deployments in the AddonTemplate:
# - target.workload.openshift.io/management: '{"effect": "PreferredDuringScheduling"}'
def injectAnnotationsForAddonTemplate(helmChart):
Expand Down Expand Up @@ -1466,6 +1485,8 @@ def injectRequirements(helm_chart_path, chart, branch):

updateDeployments(chart_name, helm_chart_path, exclusions, inclusions, branch)

wrapNetworkPoliciesWithCondition(helm_chart_path)

logging.info("Updated Chart '%s' successfully", helm_chart_path)

def split_at(the_str, the_delim, favor_right=True):
Expand Down
Loading