Skip to content

Commit

Permalink
Fix Python linting (kubeflow/kubeflow#7060)
Browse files Browse the repository at this point in the history
* gh-action: Workflow for testing python lint

Signed-off-by: Kimonas Sotirchos <kimwnasptd@arrikto.com>

* lint: Run autopep8 to lint files

Signed-off-by: Kimonas Sotirchos <kimwnasptd@arrikto.com>

* linting: Manual changes

Signed-off-by: Kimonas Sotirchos <kimwnasptd@arrikto.com>

---------

Signed-off-by: Kimonas Sotirchos <kimwnasptd@arrikto.com>
  • Loading branch information
kimwnasptd authored and Adembc committed Jun 23, 2024
1 parent 1c5f8e2 commit a7afebc
Show file tree
Hide file tree
Showing 16 changed files with 156 additions and 129 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def create_app(name, static_folder, config):
app.config.from_object(config)

if (config.ENV == BackendMode.DEVELOPMENT.value
or config.ENV == BackendMode.DEVELOPMENT_FULL.value):
or config.ENV == BackendMode.DEVELOPMENT_FULL.value): # noqa: W503
log.warn("RUNNING IN DEVELOPMENT MODE")

# Register all the blueprints
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from .. import authz
from . import custom_api, storage_api, v1_core
from . import v1_core


def list_nodes():
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from .. import authz
from . import custom_api, v1_core, utils, events
from . import custom_api, events, utils


def get_notebook(notebook, namespace):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@
from . import v1_core


def list_pods(namespace, auth=True, label_selector = None):
def list_pods(namespace, auth=True, label_selector=None):
if auth:
authz.ensure_authorized("list", "", "v1", "pods", namespace)

return v1_core.list_namespaced_pod(namespace = namespace, label_selector = label_selector)
return v1_core.list_namespaced_pod(
namespace=namespace,
label_selector=label_selector)


def get_pod_logs(namespace, pod, container, auth=True):
if auth:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,22 @@ def list_pvcs(namespace):
)
return v1_core.list_namespaced_persistent_volume_claim(namespace)


def get_pvc(pvc, namespace):
authz.ensure_authorized(
"get", "", "v1", "persistentvolumeclaims", namespace
)
return v1_core.read_namespaced_persistent_volume_claim(pvc, namespace)


def list_pvc_events(namespace, pvc_name):

field_selector = utils.events_field_selector("PersistentVolumeClaim", pvc_name)
field_selector = utils.events_field_selector(
"PersistentVolumeClaim", pvc_name)

return events.list_events(namespace, field_selector)


def patch_pvc(name, namespace, pvc, auth=True):
if auth:
authz.ensure_authorized("patch", "", "v1", "persistentvolumeclaims",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class BackendMode(enum.Enum):

def dev_mode_enabled():
env = current_app.config.get("ENV")
return (env == BackendMode.DEVELOPMENT_FULL.value or
return (env == BackendMode.DEVELOPMENT_FULL.value or # noqa: W504
env == BackendMode.DEVELOPMENT.value)


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@
"Strict".
References:
- OWASP CSRF Mitigation: https://cheatsheetseries.owasp.org/cheatsheets/Cross-Site_Request_Forgery_Prevention_Cheat_Sheet.html
- OWASP CSRF Mitigation:
https://cheatsheetseries.owasp.org/cheatsheets/Cross-Site_Request_Forgery_Prevention_Cheat_Sheet.html # noqa: E501
"""

import logging
Expand Down
6 changes: 3 additions & 3 deletions notebooks/crud-web-apps/jupyter/backend/apps/common/form.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import json

from werkzeug.exceptions import BadRequest

from kubeflow.kubeflow.crud_backend import logging
from werkzeug.exceptions import BadRequest

from . import utils

Expand Down Expand Up @@ -82,7 +81,8 @@ def set_notebook_image(notebook, body, defaults):
image_body_field = "customImage"

image = get_form_value(body, defaults, image_body_field, "image")
notebook["spec"]["template"]["spec"]["containers"][0]["image"] = image.strip()
container = notebook["spec"]["template"]["spec"]["containers"][0]
container["image"] = image.strip()


def set_notebook_image_pull_policy(notebook, body, defaults):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
"""GET request handlers."""
from flask import request

from kubeflow.kubeflow.crud_backend import api, logging

from werkzeug.exceptions import NotFound

from .. import utils
Expand Down Expand Up @@ -32,7 +30,8 @@ def get_pvcs(namespace):
def get_poddefaults(namespace):
pod_defaults = api.list_poddefaults(namespace)

# Return a list of pod defaults adding custom fields (label, desc) for forms
# Return a list of pod defaults adding custom fields (label, desc) for
# forms
contents = []
for pd in pod_defaults["items"]:
label = list(pd["spec"]["selector"]["matchLabels"].keys())[0]
Expand Down Expand Up @@ -81,7 +80,7 @@ def get_notebook_pod(notebook_name, namespace):
raise NotFound("No pod detected.")


@bp.route("/api/namespaces/<namespace>/notebooks/<notebook_name>/pod/<pod_name>/logs")
@bp.route("/api/namespaces/<namespace>/notebooks/<notebook_name>/pod/<pod_name>/logs") # noqa: E501
def get_pod_logs(namespace, notebook_name, pod_name):
container = notebook_name
logs = api.get_pod_logs(namespace, pod_name, container)
Expand Down
52 changes: 32 additions & 20 deletions notebooks/crud-web-apps/jupyter/backend/apps/common/status.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@

def process_status(notebook):
"""
Return status and reason. Status may be [ready|waiting|warning|terminating|stopped]
Return status and reason. Status may be:
[ready|waiting|warning|terminating|stopped]
"""
# In case the Notebook has no status
status_phase, status_message = get_empty_status(notebook)
Expand All @@ -30,27 +31,29 @@ def process_status(notebook):
if status_phase is not None:
return status.create_status(status_phase, status_message)

# Extract information about the status from the containerState of the Notebook's status
# Extract information about the status from the containerState of the
# Notebook's status
status_phase, status_message = get_status_from_container_state(notebook)
if status_phase is not None:
return status.create_status(status_phase, status_message)

# Extract information about the status from the conditions of the Notebook's status
# Extract information about the status from the conditions of the
# Notebook's status
status_phase, status_message = get_status_from_conditions(notebook)
if status_phase is not None:
return status.create_status(status_phase, status_message)

# Try to extract information about why the notebook is not starting
# from the notebook's events (see find_error_event)
notebook_events = get_notebook_events(notebook)

# In case there no Events available, show a generic message
status_phase, status_message = status.STATUS_PHASE.WARNING, "Couldn't find any information for the status of this notebook."

status_event, reason_event = get_status_from_events(notebook_events)
if status_event is not None:
status_phase, status_message = status_event, reason_event

# In case there no Events available, show a generic message
status_phase = status.STATUS_PHASE.WARNING
status_message = "Couldn't find any information for the status of this notebook." # noqa: E501

return status.create_status(status_phase, status_message)


Expand All @@ -65,10 +68,12 @@ def get_empty_status(notebook):
current_time = dt.datetime.utcnow().replace(microsecond=0)
delta = (current_time - nb_creation_time)

# If the Notebook has no status, the status will be waiting (instead of warning) and we will
# show a generic message for the first 10 seconds
# If the Notebook has no status, the status will be waiting
# (instead of warning) and we will show a generic message for the first 10
# seconds
if not container_state and not conditions and delta.total_seconds() <= 10:
status_phase, status_message = status.STATUS_PHASE.WAITING, "Waiting for StatefulSet to create the underlying Pod."
status_phase = status.STATUS_PHASE.WAITING
status_message = "Waiting for StatefulSet to create the underlying Pod." # noqa: E501
return status_phase, status_message

return None, None
Expand All @@ -82,11 +87,13 @@ def get_stopped_status(notebook):
if STOP_ANNOTATION in annotations:
# If the Notebook is stopped, the status will be stopped
if ready_replicas == 0:
status_phase, status_message = status.STATUS_PHASE.STOPPED, "No Pods are currently running for this Notebook Server."
status_phase = status.STATUS_PHASE.STOPPED
status_message = "No Pods are currently running for this Notebook Server." # noqa: E501
return status_phase, status_message
# If the Notebook is being stopped, the status will be waiting
else:
status_phase, status_message = status.STATUS_PHASE.WAITING, "Notebook Server is stopping."
status_phase = status.STATUS_PHASE.WAITING
status_message = "Notebook Server is stopping."
return status_phase, status_message

return None, None
Expand All @@ -97,7 +104,8 @@ def get_deleted_status(notebook):

# If the Notebook is being deleted, the status will be terminating
if "deletionTimestamp" in metadata:
status_phase, status_message = status.STATUS_PHASE.TERMINATING, "Deleting this Notebook Server."
status_phase = status.STATUS_PHASE.TERMINATING
status_message = "Deleting this Notebook Server."
return status_phase, status_message

return None, None
Expand All @@ -120,13 +128,17 @@ def get_status_from_container_state(notebook):
if "waiting" in container_state:
# If the Notebook is initializing, the status will be waiting
if container_state["waiting"]["reason"] == 'PodInitializing':
status_phase, status_message = status.STATUS_PHASE.WAITING, container_state[
"waiting"]["reason"]
status_phase = status.STATUS_PHASE.WAITING
status_message = container_state["waiting"]["reason"]
return status_phase, status_message
# In any other case, the status will be warning with a "reason: message" showing on hover
# In any other case, the status will be warning with a "reason:
# message" showing on hover
else:
status_phase, status_message = status.STATUS_PHASE.WARNING, container_state[
"waiting"]["reason"] + ': ' + container_state["waiting"]["message"]
status_phase = status.STATUS_PHASE.WARNING

reason = container_state["waiting"]["reason"]
message = container_state["waiting"]["message"]
status_message = '%s: %s' % (reason, message)
return status_phase, status_message

return None, None
Expand All @@ -138,8 +150,8 @@ def get_status_from_conditions(notebook):
for condition in conditions:
# The status will be warning with a "reason: message" showing on hover
if "reason" in condition:
status_phase, status_message = status.STATUS_PHASE.WARNING, condition[
"reason"] + ': ' + condition["message"]
status_phase = status.STATUS_PHASE.WARNING
status_message = condition["reason"] + ': ' + condition["message"]
return status_phase, status_message

return None, None
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

bp = Blueprint("base_routes", __name__)

from . import get, post, delete # noqa E402, F401
from . import get, post, delete # noqa E402, F401
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ def get_pvcs(namespace):
def get_poddefaults(namespace):
pod_defaults = api.list_poddefaults(namespace)

# Return a list of pod defaults adding custom fields (label, desc) for forms
# Return a list of pod defaults adding custom fields (label, desc) for
# forms
contents = []
for pd in pod_defaults["items"]:
label = list(pd["spec"]["selector"]["matchLabels"].keys())[0]
Expand Down
10 changes: 6 additions & 4 deletions notebooks/crud-web-apps/tensorboards/backend/app/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from kubeflow.kubeflow.crud_backend import status
from werkzeug.exceptions import BadRequest


def parse_tensorboard(tensorboard):
"""
Process the Tensorboard object and format it as the UI expects it.
Expand Down Expand Up @@ -29,7 +30,7 @@ def get_tensorboard_dict(namespace, body):
"""
metadata = {
"name": body["name"],
"namespace": namespace,
"namespace": namespace,
}
labels = get_tensorboard_configurations(body=body)
if labels:
Expand All @@ -44,14 +45,15 @@ def get_tensorboard_dict(namespace, body):

return tensorboard


def get_tensorboard_configurations(body):
labels = body.get("configurations", None)
cr_labels = {}

if not isinstance(labels, list):
raise BadRequest("Labels for PodDefaults are not list: %s" % labels)

for label in labels:
cr_labels[label] = "true"
return cr_labels

return cr_labels
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from kubeflow.kubeflow.crud_backend import api, helpers
from kubeflow.kubeflow.crud_backend import api

from . import status

Expand Down
Loading

0 comments on commit a7afebc

Please sign in to comment.