Browse Source

#79 automatic column names for "customcols"

tags/19.9.3
Henning Jacobs 4 days ago
parent
commit
e07835fd93
3 changed files with 36 additions and 0 deletions
  1. 12
    0
      kube_web/web.py
  2. 16
    0
      tests/e2e/test_list.py
  3. 8
    0
      tests/unit/test_web.py

+ 12
- 0
kube_web/web.py View File

@@ -11,6 +11,7 @@ import time
import os
import pykube
import logging
import re
import requests.exceptions
import pykube.exceptions
from yarl import URL
@@ -98,6 +99,8 @@ SEARCH_MATCH_CONTEXT_LENGTH = 20

SECRET_CONTENT_HIDDEN = "**SECRET-CONTENT-HIDDEN-BY-KUBE-WEB-VIEW**"

NON_WORD_CHARS = re.compile("[^0-9a-zA-Z]+")


TABLE_CELL_FORMATTING = {
"events": {
@@ -376,6 +379,12 @@ async def get_cluster_resource_types(request, session):
}


def generate_name_from_spec(spec: str) -> str:
words = NON_WORD_CHARS.split(spec)
name = " ".join([word.capitalize() for word in words if word])
return name


async def join_custom_columns(
request,
session,
@@ -396,6 +405,9 @@ async def join_custom_columns(
custom_columns = {}
for part in filter(None, custom_columns_param.split(";")):
name, _, spec = part.partition("=")
if not spec:
spec = name
name = generate_name_from_spec(spec)
custom_column_names.append(name)
custom_columns[name] = jmespath.compile(spec)


+ 16
- 0
tests/e2e/test_list.py View File

@@ -123,6 +123,22 @@ def test_list_pods_with_custom_columns(session):
assert cells[-3].text.startswith("['hjacobs/")


def test_list_pods_with_custom_column_auto_name(session):
response = session.get(
"/clusters/local/namespaces/default/pods?customcols=spec.containers[*].image"
)
response.raise_for_status()
check_links(response, session)
ths = response.html.find("main table thead th")
# note: pods have an extra "Links" column (--object-links)
assert ths[-3].text == "Spec Containers Image"

rows = response.html.find("main table tbody tr")
for row in rows:
cells = row.find("td")
assert cells[-3].text.startswith("['hjacobs/")


def test_list_pods_with_multiple_custom_columns(session):
# note that the semicolon must be urlencoded as %3B!
response = session.get(

+ 8
- 0
tests/unit/test_web.py View File

@@ -0,0 +1,8 @@
from kube_web.web import generate_name_from_spec


def test_generate_name_from_spec():
assert generate_name_from_spec("a.b") == "A B"
assert generate_name_from_spec(" a.b ") == "A B"
assert generate_name_from_spec(" a[1].containers[*].Image ") == "A 1 Containers Image"
assert generate_name_from_spec("metadata.annotations.\"foo\"") == "Metadata Annotations Foo"

Loading…
Cancel
Save