Skip to content

Commit

Permalink
Feature/36 namespacealert (#40)
Browse files Browse the repository at this point in the history
* feat(update-setuptools): version 57.0.0

Signed-off-by: imjoseangel <[email protected]>

* feat(namespace): alert

Signed-off-by: imjoseangel <[email protected]>

* test(alert-unit): add unit tests

Signed-off-by: imjoseangel <[email protected]>

* docs(readme): add documentation

Signed-off-by: imjoseangel <[email protected]>
  • Loading branch information
imjoseangel authored Jun 12, 2021
1 parent d1a3327 commit 7218243
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 10 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2020 Jose Angel Munoz
Copyright (c) 2021 Jose Angel Munoz

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
20 changes: 16 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ You can also:

* Toggle on or off the powerline-k8sstatus segment using an environment variable which can easily be mapped to a function in your ~/.profile file.

* Define certain contexts to be colored differently for alerting purposes. For instance, you can have your production context showing up in bright red.
* Define certain contexts or namespaces to be colored differently for alerting purposes. For instance, you can have your production context or kube-system namespace showing up in bright red or solarized yellow respectively.

## Requirements

Expand Down Expand Up @@ -48,13 +48,20 @@ for example in `.config/powerline/colorschemes/default.json`:
"k8sstatus:alert": {
"fg": "white",
"bg": "solarized:red",
"attrs": []
"attrs": [
"bold"
]
},
"k8sstatus_namespace": {
"fg": "gray10",
"bg": "darkestblue",
"attrs": []
},
"k8sstatus_namespace:alert": {
"fg": "darkestred",
"bg": "solarized:yellow",
"attrs": []
},
"k8sstatus_user": {
"fg": "white",
"bg": "green",
Expand Down Expand Up @@ -82,12 +89,17 @@ for example in `.config/powerline/themes/shell/default.json`:
"context_alert": [
"minikube",
"production"
],
"namespace_alert": [
"kube-system",
"production"
]
}
},
}
```

Context names added to the `context_alert` arguments will be outlined in the segment by a different colour.
* Context names added to the `context_alert` arguments will be outlined in the segment by a different colour.
* Namespace names added to the `namespace_alert` arguments will be outlined in the segment by a different colour. Note that `default` namespace won't be shown and as result not colorized.

Reload powerline running `powerline-daemon --replace` to load the new settings.

Expand Down
16 changes: 12 additions & 4 deletions powerline_k8sstatus/segments.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ class K8SStatusSegment(Segment):
divider_highlight_group = None

@staticmethod
def build_segments(context, namespace, user, contextalert):
def build_segments(context, namespace, user, contextalert, namespacealert):
segments = [{'contents': (u'\U00002388 {}').format(
context), 'highlight_groups': [contextalert]}]

if namespace and namespace.lower() != 'default':
segments.append({
'contents': namespace,
'highlight_groups': ['k8sstatus_namespace', contextalert],
'highlight_groups': [namespacealert, contextalert],
'divider_highlight_group': 'k8sstatus:divider'
})

Expand All @@ -37,11 +37,14 @@ def build_segments(context, namespace, user, contextalert):
return segments

def __call__(self, pl, segment_info, create_watcher=None, context_alert: list = None,
show_namespace=False, show_user=False):
namespace_alert: list = None, show_namespace=False, show_user=False):

if context_alert is None:
context_alert = []

if namespace_alert is None:
namespace_alert = []

try:
if segment_info['environ'].get('POWERLINE_K8SSTATUS') == "0":
return
Expand All @@ -64,9 +67,14 @@ def __call__(self, pl, segment_info, create_watcher=None, context_alert: list =
if context in context_alert:
contextstatus = "k8sstatus:alert"

namespacestatus = "k8sstatus_namespace"

if show_namespace:
try:
namespace = active_context['context']['namespace']
if namespace in namespace_alert:
namespacestatus = "k8sstatus_namespace:alert"

except KeyError:
namespace = 'default'
else:
Expand All @@ -77,7 +85,7 @@ def __call__(self, pl, segment_info, create_watcher=None, context_alert: list =
else:
user = None

return self.build_segments(context, namespace, user, contextstatus)
return self.build_segments(context, namespace, user, contextstatus, namespacestatus)


k8sstatus = with_docstring(
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
[build-system]
requires = ["setuptools >= 40.6.0", "wheel"]
requires = ["setuptools >= 57.0.0", "wheel"]
17 changes: 17 additions & 0 deletions tests/test_powerlinek8sstatus.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,19 @@
CONTEXT = 'minikube'
NAMESPACE = 'tools'
USER = 'minikube'

EXPECTED_NAMESPACE = {
'contents': NAMESPACE,
'highlight_groups': ['k8sstatus_namespace', 'k8sstatus'],
'divider_highlight_group': 'k8sstatus:divider'
}

EXPECTED_NAMESPACE_ALERT = {
'contents': NAMESPACE,
'highlight_groups': ['k8sstatus_namespace:alert', 'k8sstatus'],
'divider_highlight_group': 'k8sstatus:divider'
}

EXPECTED_USER = {
'contents': USER,
'highlight_groups': ['k8sstatus_user'],
Expand Down Expand Up @@ -194,3 +202,12 @@ def test_context_defaultalert(pl, segment_info, expected_symbol):
pl=pl, segment_info=segment_info,
context_alert=['minikube'])
assert output == [expected_symbol]


@pytest.mark.parametrize('expected_symbol', ['k8sstatus'], indirect=True)
@pytest.mark.usefixtures('setup_namespacemocked_context', 'expected_symbol')
def test_namespace_defaultalert(pl, segment_info, expected_symbol):
output = powerlinek8s.k8sstatus(
pl=pl, segment_info=segment_info, show_namespace=True,
namespace_alert=['tools'])
assert output == [expected_symbol, EXPECTED_NAMESPACE_ALERT]

0 comments on commit 7218243

Please sign in to comment.