Skip to content

Commit

Permalink
get ws port
Browse files Browse the repository at this point in the history
Signed-off-by: Adem Baccara <71262172+Adembc@users.noreply.github.com>
  • Loading branch information
Adembc committed Oct 10, 2024
1 parent 4934408 commit c69b062
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -99,38 +99,38 @@ spec:
## - if the Workspace had activity in the last 60 seconds this command
## should return status 0, otherwise it should return status 1
##
exec:
outputPath: "/tmp/activity_probe.json"
timeoutSeconds: 60
script: |-
#!/usr/bin/env bash
set -euo pipefail

# Define the output path
output_path="/tmp/activity_probe.json"
# Find the most recent modification time in the $HOME directory
last_activity_epoch=$(find "$HOME" -type f -printf '%T@\n' 2>/dev/null | awk 'max < $1 { max = $1 } END { print max }')
# Write the last activity time to the output path
if [ -n "$last_activity_epoch" ]; then
# Convert epoch time to ISO 8601 format
last_activity=$(date -d "@$last_activity_epoch" -Iseconds)
echo "{\"last_activity\": \"$last_activity\"}" > "$output_path"
else
# Handle the case where no files are found
echo "{\"last_activity\": null}" > "$output_path"
fi
# exec:
# outputPath: "/tmp/activity_probe.json"
# timeoutSeconds: 60
# script: |-
# #!/usr/bin/env bash
#
# set -euo pipefail
#
# # Define the output path
# output_path="/tmp/activity_probe.json"
#
# # Find the most recent modification time in the $HOME directory
# last_activity_epoch=$(find "$HOME" -type f -printf '%T@\n' 2>/dev/null | awk 'max < $1 { max = $1 } END { print max }')
#
# # Write the last activity time to the output path
# if [ -n "$last_activity_epoch" ]; then
# # Convert epoch time to ISO 8601 format
# last_activity=$(date -d "@$last_activity_epoch" -Iseconds)
# echo "{\"last_activity\": \"$last_activity\"}" > "$output_path"
# else
# # Handle the case where no files are found
# echo "{\"last_activity\": null}" > "$output_path"
# fi
## OPTION 2: a Jupyter-specific probe
## - will poll the `/api/status` endpoint of the Jupyter API, and use the `last_activity` field
## https://github.com/jupyter-server/jupyter_server/blob/v2.13.0/jupyter_server/services/api/handlers.py#L62-L67
## - note, users need to be careful that their other probes don't trigger a "last_activity" update
## e.g. they should only check the health of Jupyter using the `/api/status` endpoint
##
# jupyter:
# lastActivity: true
# portId: jupyterlab
#
jupyter:
lastActivity: true
portId: jupyterlab

## standard probes to determine Container health (MUTABLE)
## - spec for Probe:
Expand Down
27 changes: 25 additions & 2 deletions workspaces/controller/internal/controller/culling_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,17 @@ func (r *CullingReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
Message: "Failed to fetch service name for workspace",
}, nil, nil)
}
port := "8888"
jupyterAPIEndpoint := fmt.Sprintf("http://%s.%s.svc.%s:%s/workspace/%s/%s/jupyterlab/api/status", serviceName, workspace.Namespace, defaultClusterDomain, port, workspace.Namespace, workspace.Name)
port, err := r.getWorkspacePort(ctx, workspace, workspaceKind)
if err != nil {
log.Error(err, "Error fetching port for workspace")
return r.updateWorkspaceActivityStatus(ctx, log, workspace, &minRequeueAfter, &kubefloworgv1beta1.ProbeStatus{
StartTimeMs: probeStartTime.UnixMilli(),
EndTimeMs: time.Now().UnixMilli(),
Result: kubefloworgv1beta1.ProbeResultFailure,
Message: "Failed to fetch port for workspace",
}, nil, nil)
}
jupyterAPIEndpoint := fmt.Sprintf("http://%s.%s.svc.%s:%d/workspace/%s/%s/jupyterlab/api/status", serviceName, workspace.Namespace, defaultClusterDomain, port, workspace.Namespace, workspace.Name)

lastActivity, err, probeMessage, probeResult := fetchLastActivityFromJupyterAPI(jupyterAPIEndpoint)
if err != nil {
Expand Down Expand Up @@ -211,6 +220,7 @@ func (r *CullingReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
}, ptr.To(probeStartTime.Unix()), ptr.To(lastActivity.Unix()))
}

// Check if Bash probing is enabled
if workspaceKind.Spec.PodTemplate.Culling.ActivityProbe.Exec != nil {
probeStartTime := time.Now()
podName, err := r.getPodName(ctx, workspace)
Expand Down Expand Up @@ -369,6 +379,19 @@ func (r *CullingReconciler) getServiceName(ctx context.Context, workspace *kubef
return ownedServices.Items[0].Name, nil
}

func (r *CullingReconciler) getWorkspacePort(ctx context.Context, workspace *kubefloworgv1beta1.Workspace, workspaceKind *kubefloworgv1beta1.WorkspaceKind) (int32, error) {
for _, imageConfigValue := range workspaceKind.Spec.PodTemplate.Options.ImageConfig.Values {
if imageConfigValue.Id == workspace.Spec.PodTemplate.Options.ImageConfig {
for _, port := range imageConfigValue.Spec.Ports {
if port.Id == workspaceKind.Spec.PodTemplate.Culling.ActivityProbe.Jupyter.PortId {
return port.Port, nil
}
}
}
}
return 0, errors.New("port not found")
}

func (r *CullingReconciler) getPodName(ctx context.Context, workspace *kubefloworgv1beta1.Workspace) (string, error) {
var statefulSetName string
ownedStatefulSets := &appsv1.StatefulSetList{}
Expand Down

0 comments on commit c69b062

Please sign in to comment.