Skip to content

Commit

Permalink
Inject annotations for deployments in addonTemplates (#747)
Browse files Browse the repository at this point in the history
Signed-off-by: zhujian <[email protected]>
  • Loading branch information
zhujian7 authored Jun 12, 2024
1 parent 1c9fc28 commit ae12685
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
32 changes: 31 additions & 1 deletion hack/bundle-automation/generate-charts.py
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,36 @@ def updateDeployments(chartName, helmChart, exclusions, inclusions):
if 'pullSecretOverride' in inclusions:
addPullSecretOverride(deployment)

# injectAnnotationsForAddonTemplate injects following annotations for deployments in the AddonTemplate:
# - target.workload.openshift.io/management: '{"effect": "PreferredDuringScheduling"}'
def injectAnnotationsForAddonTemplate(helmChart):
logging.info("Injecting Annotations for deployments in the AddonTemplate ...")

addonTemplates = findTemplatesOfType(helmChart, 'AddOnTemplate')
for addonTemplate in addonTemplates:
injected = False
with open(addonTemplate, 'r') as f:
templateContent = yaml.safe_load(f)
agentSpec = templateContent['spec']['agentSpec']
if 'workload' not in agentSpec:
return
workload = agentSpec['workload']
if 'manifests' not in workload:
return
manifests = workload['manifests']
for manifest in manifests:
if manifest['kind'] == 'Deployment':
metadata = manifest['spec']['template']['metadata']
if 'annotations' not in metadata:
metadata['annotations'] = {}
if 'target.workload.openshift.io/management' not in metadata['annotations']:
metadata['annotations']['target.workload.openshift.io/management'] = '{"effect": "PreferredDuringScheduling"}'
injected = True
if injected:
with open(addonTemplate, 'w') as f:
yaml.dump(templateContent, f, width=float("inf"))
logging.info("Annotations injected successfully. \n")


# fixImageReferencesForAddonTemplate identify the image references for every deployment in addontemplates, if any exist
# in the image field, insert helm flow control code to reference it, and add image-key to the values.yaml file.
Expand Down Expand Up @@ -503,7 +533,6 @@ def fixImageReferencesForAddonTemplate(helmChart, imageKeyMapping):
logging.critical("No image key mapping provided for imageKey: %s" % image_key)
exit(1)
imageKeys.append(image_key)
temp = container['image']
container['image'] = "{{ .Values.global.imageOverrides." + image_key + " }}"
# container['imagePullPolicy'] = "{{ .Values.global.pullPolicy }}"
with open(addonTemplate, 'w') as f:
Expand Down Expand Up @@ -548,6 +577,7 @@ def injectRequirements(helmChart, chartName, imageKeyMapping, skipRBACOverrides,
fixImageReferences(helmChart, imageKeyMapping)
fixEnvVarImageReferences(helmChart, imageKeyMapping)
fixImageReferencesForAddonTemplate(helmChart, imageKeyMapping)
injectAnnotationsForAddonTemplate(helmChart)
if not skipRBACOverrides:
updateRBAC(helmChart, chartName)
updateDeployments(chartName, helmChart, exclusions, inclusions)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ spec:
addon-agent: managed-serviceaccount
template:
metadata:
annotations:
target.workload.openshift.io/management: '{"effect": "PreferredDuringScheduling"}'
labels:
addon-agent: managed-serviceaccount
spec:
Expand Down

0 comments on commit ae12685

Please sign in to comment.