Add support for tagging Apps/Bookmarks

This commit is contained in:
2021-12-24 12:51:18 +00:00
parent 96224f8a70
commit 04617350d8

View File

@@ -12,7 +12,16 @@ ANNOTATION_BASE = 'ohayodash.github.io'
base = Blueprint('base', __name__, template_folder='templates') base = Blueprint('base', __name__, template_folder='templates')
def get_k8s_applications() -> list: def check_tags(tag, object):
# Skip if we're limited to a tag, and the CM has tags but not that one.
tags = object.metadata.annotations.get('{0}/tags'.format(ANNOTATION_BASE), None)
if tag and tags:
if tag not in {x for x in tags.split(',') if x != ''}:
return False
return True
def get_k8s_applications(tag: str = None) -> list:
"""Get all ingresses from the cluster and produce a application list.""" """Get all ingresses from the cluster and produce a application list."""
if 'KUBERNETES_SERVICE_HOST' in os.environ: if 'KUBERNETES_SERVICE_HOST' in os.environ:
kubernetes.config.load_incluster_config() kubernetes.config.load_incluster_config()
@@ -30,6 +39,10 @@ def get_k8s_applications() -> list:
if ingress.metadata.annotations[enable_annotation] == 'false': if ingress.metadata.annotations[enable_annotation] == 'false':
continue continue
# Skip if we're limited to a tag, and the Ingress has tags but not that one.
if not check_tags(tag, ingress):
continue
# Set to some basic values from the ingress # Set to some basic values from the ingress
application_values = { application_values = {
'name': ingress.metadata.name, 'name': ingress.metadata.name,
@@ -48,7 +61,7 @@ def get_k8s_applications() -> list:
return sorted(applications, key=lambda item: item['name']) return sorted(applications, key=lambda item: item['name'])
def get_bookmarks() -> list: def get_bookmarks(tag: str = None) -> list:
"""Get all 'bookmark' ConfigMaps from the cluster and produce a bookmark list.""" """Get all 'bookmark' ConfigMaps from the cluster and produce a bookmark list."""
if 'KUBERNETES_SERVICE_HOST' in os.environ: if 'KUBERNETES_SERVICE_HOST' in os.environ:
kubernetes.config.load_incluster_config() kubernetes.config.load_incluster_config()
@@ -59,9 +72,16 @@ def get_bookmarks() -> list:
bookmarks = {} bookmarks = {}
for cm in ret.items: for cm in ret.items:
# Skip if the CM has no annotations
if cm.metadata.annotations is None:
continue
# Skip if # Skip if its not tagged as bookmark CM
if not cm.metadata.annotations or '{0}/bookmarks'.format(ANNOTATION_BASE) not in cm.metadata.annotations: if '{0}/bookmarks'.format(ANNOTATION_BASE) not in cm.metadata.annotations:
continue
# Skip if we're limited to a tag, and the CM has tags but not that one.
if not check_tags(tag, cm):
continue continue
bookmark_data = yaml.safe_load(cm.data['bookmarks']) bookmark_data = yaml.safe_load(cm.data['bookmarks'])
@@ -94,8 +114,9 @@ def get_greeting() -> tuple:
return 'こんにちは', "Thats 'Good day' in Japanese" return 'こんにちは', "Thats 'Good day' in Japanese"
# TODO: Replace with JS
@base.app_template_filter() @base.app_template_filter()
def format_datetime(value, format='medium'): def format_datetime(value):
return value.strftime(os.environ.get('DATE_FORMAT', '%Y-%m-%d %H:%M')) # noqa: WPS323 return value.strftime(os.environ.get('DATE_FORMAT', '%Y-%m-%d %H:%M')) # noqa: WPS323
@@ -109,6 +130,16 @@ def index():
) )
@base.route('/<tag>')
def tag(tag):
return render_template('index.j2',
greeting=get_greeting(),
now=datetime.datetime.utcnow(),
applications=get_k8s_applications(tag),
bookmarks=get_bookmarks(tag),
)
@base.route('/api/applications') @base.route('/api/applications')
def applications(): def applications():
return jsonify(get_k8s_applications()) return jsonify(get_k8s_applications())