From bb0b3aca1a1f57c93637a5597703b4d8449fc614 Mon Sep 17 00:00:00 2001 From: seshapad Date: Thu, 28 Oct 2021 14:12:08 +0530 Subject: [PATCH 1/3] Added ingress annotator Signed-off-by: seshapad --- ingress-annotator/ingress-annotator.star | 50 ++++++++++++++++++++++++ ingress-annotator/ingress-annotator.yaml | 11 ++++++ 2 files changed, 61 insertions(+) create mode 100644 ingress-annotator/ingress-annotator.star create mode 100644 ingress-annotator/ingress-annotator.yaml diff --git a/ingress-annotator/ingress-annotator.star b/ingress-annotator/ingress-annotator.star new file mode 100644 index 0000000..82a502e --- /dev/null +++ b/ingress-annotator/ingress-annotator.star @@ -0,0 +1,50 @@ +# 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. +# load("encoding/yaml", "yaml") + +# transform creates a new artifact of type "KubernetesYamlsWithAnnotatedIngress" 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 v in new_artifacts: + if v["artifact"] != "KubernetesYamls": + continue + if v['name'] != "Kubernetes": + continue + v["artifact"] = "KubernetesYamlsWithAnnotatedIngress" + artifacts.append(v) + yamlsPath = v["paths"]["KubernetesYamls"][0] + fileList = fs.readdir(yamlsPath) + for f in fileList: + filePath = fs.pathjoin(yamlsPath, f) + s = fs.read(filePath) + yamlData = yaml.loads(s) + if yamlData['kind'] != 'Ingress': + continue + md = yamlData['metadata'] + d = {} + if 'annotations' in md: + d = md['annotations'] + d["kubernetes.io/ingress.class"] = "haproxy" + md["annotations"] = d + yamlData['metadata'] = md + s = yaml.dumps(yamlData) + fs.write(filePath, s) + pathMappings.append({'type': 'Default', \ + 'sourcePath': yamlsPath, \ + 'destinationPath': fs.pathjoin("deploy", "yamls")}) + + return {'pathMappings': pathMappings, 'artifacts': artifacts} \ No newline at end of file diff --git a/ingress-annotator/ingress-annotator.yaml b/ingress-annotator/ingress-annotator.yaml new file mode 100644 index 0000000..73e0675 --- /dev/null +++ b/ingress-annotator/ingress-annotator.yaml @@ -0,0 +1,11 @@ +apiVersion: move2kube.konveyor.io/v1alpha1 +kind: Transformer +metadata: + name: IngressAnnotator +spec: + mode: "Container" + class: "Starlark" + consumes: + - "KubernetesYamls" + config: + starFile: "ingress-annotator.star" From 048414df8beef293eefa9279c539dc6fbfb26a07 Mon Sep 17 00:00:00 2001 From: seshapad Date: Thu, 28 Oct 2021 14:57:36 +0530 Subject: [PATCH 2/3] Removed unwanted variables and code. Aligned code to avoid confusion. Signed-off-by: seshapad --- ingress-annotator/ingress-annotator.star | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/ingress-annotator/ingress-annotator.star b/ingress-annotator/ingress-annotator.star index 82a502e..50ff647 100644 --- a/ingress-annotator/ingress-annotator.star +++ b/ingress-annotator/ingress-annotator.star @@ -11,7 +11,6 @@ # 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. -# load("encoding/yaml", "yaml") # transform creates a new artifact of type "KubernetesYamlsWithAnnotatedIngress" which # has an annotation for ingress-class added to every ingress resource yaml created during @@ -22,8 +21,6 @@ def transform(new_artifacts, old_artifacts): for v in new_artifacts: if v["artifact"] != "KubernetesYamls": continue - if v['name'] != "Kubernetes": - continue v["artifact"] = "KubernetesYamlsWithAnnotatedIngress" artifacts.append(v) yamlsPath = v["paths"]["KubernetesYamls"][0] @@ -34,17 +31,14 @@ def transform(new_artifacts, old_artifacts): yamlData = yaml.loads(s) if yamlData['kind'] != 'Ingress': continue - md = yamlData['metadata'] - d = {} - if 'annotations' in md: - d = md['annotations'] - d["kubernetes.io/ingress.class"] = "haproxy" - md["annotations"] = d - yamlData['metadata'] = md + 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': fs.pathjoin("deploy", "yamls")}) + 'sourcePath': yamlsPath, \ + 'destinationPath': fs.pathjoin("deploy", "yamls")}) return {'pathMappings': pathMappings, 'artifacts': artifacts} \ No newline at end of file From 70a1f77897bac2c6c4c5f14c5590e50e95facb36 Mon Sep 17 00:00:00 2001 From: seshapad Date: Thu, 28 Oct 2021 14:58:20 +0530 Subject: [PATCH 3/3] Added newline between header and code Signed-off-by: seshapad --- ingress-annotator/ingress-annotator.star | 1 + 1 file changed, 1 insertion(+) diff --git a/ingress-annotator/ingress-annotator.star b/ingress-annotator/ingress-annotator.star index 50ff647..c75c059 100644 --- a/ingress-annotator/ingress-annotator.star +++ b/ingress-annotator/ingress-annotator.star @@ -15,6 +15,7 @@ # transform creates a new artifact of type "KubernetesYamlsWithAnnotatedIngress" which # has an annotation for ingress-class added to every ingress resource yaml created during # transformation + def transform(new_artifacts, old_artifacts): pathMappings = [] artifacts = []