From ae12685a14a21e4352b38ee6ca62eaf9be538bc6 Mon Sep 17 00:00:00 2001 From: Jian Zhu Date: Wed, 12 Jun 2024 22:51:25 +0800 Subject: [PATCH] Inject annotations for deployments in addonTemplates (#747) Signed-off-by: zhujian --- hack/bundle-automation/generate-charts.py | 32 ++++++++++++++++++- .../templates/addontemplate.yaml | 2 ++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/hack/bundle-automation/generate-charts.py b/hack/bundle-automation/generate-charts.py index 9f288e93..fbe53a52 100755 --- a/hack/bundle-automation/generate-charts.py +++ b/hack/bundle-automation/generate-charts.py @@ -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. @@ -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: @@ -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) diff --git a/pkg/templates/charts/toggle/managed-serviceaccount/templates/addontemplate.yaml b/pkg/templates/charts/toggle/managed-serviceaccount/templates/addontemplate.yaml index 84f373c9..08ea23b2 100644 --- a/pkg/templates/charts/toggle/managed-serviceaccount/templates/addontemplate.yaml +++ b/pkg/templates/charts/toggle/managed-serviceaccount/templates/addontemplate.yaml @@ -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: