Browse Source

allow setting default label columns

tags/0.16.2
Henning Jacobs 4 months ago
parent
commit
b6e4293b4a
7 changed files with 58 additions and 7 deletions
  1. +1
    -1
      Makefile
  2. +19
    -0
      docs/customization.rst
  3. +14
    -0
      kube_web/main.py
  4. +9
    -2
      kube_web/table.py
  5. +3
    -1
      kube_web/templates/resource-list.html
  6. +4
    -1
      kube_web/web.py
  7. +8
    -2
      tests/unit/test_table.py

+ 1
- 1
Makefile View File

@@ -71,7 +71,7 @@ run:

.PHONY: run.kind
run.kind:
poetry run python3 -m kube_web --kubeconfig-path=$$(./kind get kubeconfig-path --name=kube-web-view-e2e) --debug --show-container-logs --search-default-resource-types=deployments,pods,configmaps
poetry run python3 -m kube_web --kubeconfig-path=$$(./kind get kubeconfig-path --name=kube-web-view-e2e) --debug --show-container-logs --search-default-resource-types=deployments,pods,configmaps --default-label-columns=pods=app

.PHONY: mirror
mirror:

+ 19
- 0
docs/customization.rst View File

@@ -5,11 +5,30 @@ Customization
Kubernetes Web View's behavior and appearance can be customized
for the needs of your organization:

* default :ref:`label-columns` can be defined to show values of standardized object labels (e.g. "app", "version", etc)
* :ref:`external-links` can be added to link objects to monitoring tools, internal application registries, custom UIs, etc
* the :ref:`customize-search` can be customized to search in CRDs, or to cover frequent search cases
* :ref:`html-templates` can be customized to match your branding, to add static links, and to inject custom JS/CSS
* :ref:`static-assets` can be included to add images, JS, or CSS files

.. _label-columns:

Label Columns
=============

Most organizations have a standard set of labels for Kubernetes resources, e.g. all pods might have "app" and "version" labels.
You can instruct Kubernetes Web View to show these labels as columns for the respective resource types via the ``--default-label-columns`` command line option.

Example command line argument to show the "application" and "version" labels for pods and the "team" label for deployments:

.. code-block:: bash

--default-label-columns=pods=application,version;deployments=team

Note that the label names are separated by comma (",") whereas multiple different entries for different resource types are separated by semicolon (";").

Users of the web UI can remove the pre-configured label columns by passing a single comma as the ``labelcols`` query parameter: ``/clusters/../namespaces/_all/pods?labelcols=,``.


.. _external-links:


+ 14
- 0
kube_web/main.py View File

@@ -22,6 +22,14 @@ def comma_separated_values(value):
return list(filter(None, value.split(",")))


def key_value_pairs(value):
data = {}
for kv_pair in value.split(";"):
key, sep, value = kv_pair.partition("=")
data[key] = value
return data


def main(argv=None):

parser = argparse.ArgumentParser(description=f"Kubernetes Web View v{__version__}")
@@ -90,6 +98,12 @@ def main(argv=None):
type=comma_separated_values,
help="Comma-separated list of resource types to offer on search page, e.g. 'deployments,pods,nodes'",
)
parser.add_argument(
"--default-label-columns",
type=key_value_pairs,
help="Comma-separated list of label columns per resource type; multiple entries separated by semicolon, e.g. 'pods=app,version;deployments=team'",
default={},
)

args = parser.parse_args(argv)


+ 9
- 2
kube_web/table.py View File

@@ -34,14 +34,21 @@ def sort_table(table, sort_param):
def add_label_columns(table, label_columns_param):
if not label_columns_param:
return
label_columns = list(l.strip() for l in label_columns_param.split(","))
label_columns = list(
filter(None, [l.strip() for l in label_columns_param.split(",")])
)
for i, label_column in enumerate(label_columns):
if label_column == "*":
name = "Labels"
else:
name = label_column.capitalize()
table.columns.insert(
i + 1, {"name": name, "description": f"{label_column} label"}
i + 1,
{
"name": name,
"description": f"{label_column} label",
"label": label_column,
},
)
for row in table.rows:
for i, label in enumerate(label_columns):

+ 3
- 1
kube_web/templates/resource-list.html View File

@@ -160,7 +160,9 @@
style="color:{{ row.object.metadata.creationTimestamp|age_color(days=1) }}"
{% endif %}
>
{% if table.columns[loop.index0].name == 'Node': %}
{% if table.columns[loop.index0].label and table.columns[loop.index0].label != '*': %}
<a href="{{ rel_url.update_query(selector=table.columns[loop.index0].label+'='+cell) }}">{{ cell if cell is not none}}</a>
{% elif table.columns[loop.index0].name == 'Node': %}
<a href="/clusters/{{ row.cluster.name }}/nodes/{{ cell }}">{{ cell if cell is not none}}</a>
{% elif table.columns[loop.index0].name == 'CPU Usage': %}
{{ cell|cpu if cell is not none }}

+ 4
- 1
kube_web/web.py View File

@@ -359,7 +359,10 @@ async def do_get_resource_list(
# table.rows might be None, e.g. for "csinodes"
if table.rows is None:
table.obj["rows"] = []
add_label_columns(table, params.get("labelcols"))
label_columns = params.get("labelcols") or request.app[
CONFIG
].default_label_columns.get(_type)
add_label_columns(table, label_columns)
filter_table(table, params.get("filter"))

# note: we join before sorting, so sorting works on the joined columns, too

+ 8
- 2
tests/unit/test_table.py View File

@@ -46,11 +46,17 @@ def test_add_label_columns():
table = Table(Pod, {"kind": "Table", "columnDefinitions": [], "rows": []})
add_label_columns(table, "foo, bar")
assert table.columns == [
{"name": "Foo", "description": "foo label"},
{"name": "Bar", "description": "bar label"},
{"name": "Foo", "description": "foo label", "label": "foo"},
{"name": "Bar", "description": "bar label", "label": "bar"},
]


def test_add_label_columns_empty():
table = Table(Pod, {"kind": "Table", "columnDefinitions": [], "rows": []})
add_label_columns(table, ", ")
assert table.columns == []


def test_filter_table_wrong_column_name(single_pod_table):
table = single_pod_table
filter_table(table, "a=b")

Loading…
Cancel
Save