Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Added ingress annotator #4

Merged
merged 3 commits into from
Oct 28, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
50 changes: 50 additions & 0 deletions ingress-annotator/ingress-annotator.star
Original file line number Diff line number Diff line change
@@ -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']
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need d? Why not directly edit yamlData?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

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")})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we format this line to be more readable by say aligning the values?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done


return {'pathMappings': pathMappings, 'artifacts': artifacts}
11 changes: 11 additions & 0 deletions ingress-annotator/ingress-annotator.yaml
Original file line number Diff line number Diff line change
@@ -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"