-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathingress-annotator.star
52 lines (48 loc) · 2.35 KB
/
ingress-annotator.star
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# Copyright IBM Corporation 2021
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# transform creates a new artifact of type "KubernetesYamls" which
# has an annotation for ingress-class added to every ingress resource yaml created during
# transformation
def transform(new_artifacts, old_artifacts):
pathMappings = []
artifacts = []
for a in new_artifacts:
yamlsPath = a["paths"]["KubernetesYamls"][0]
serviceName = a["name"]
artifacts.append(a)
fileList = fs.read_dir(yamlsPath)
yamlsBasePath = yamlsPath.split("/")[-1]
# Create a custom path template for the service, whose values gets filled and can be used in other pathmappings
pathTemplateName = serviceName + yamlsBasePath
pathTemplateName = pathTemplateName.replace("-", "")
tplPathData = {'PathTemplateName': pathTemplateName}
pathMappings.append({'type': 'PathTemplate', \
'sourcePath': "{{ OutputRel \"" + yamlsPath + "\" }}", \
'templateConfig': tplPathData})
for f in fileList:
filePath = fs.path_join(yamlsPath, f)
s = fs.read_as_string(filePath)
yamlData = yaml.loads(s)
if yamlData['kind'] != 'Ingress':
continue
if 'annotations' not in yamlData['metadata']:
yamlData['metadata']['annotations'] = {'kubernetes.io/ingress.class': 'haproxy'}
else:
yamlData['metadata']['annotations']['kubernetes.io/ingress.class'] = 'haproxy'
s = yaml.dumps(yamlData)
fs.write(filePath, s)
pathMappings.append({'type': 'Default', \
'sourcePath': yamlsPath, \
'destinationPath': "{{ ." + pathTemplateName + " }}"})
return {'pathMappings': pathMappings, 'artifacts': artifacts}