Skip to content

Commit 5134304

Browse files
committed
refactor: Clean up session handling and remove deprecated workspace access endpoints
- Removed deprecated workspace access endpoints from session routes, streamlining API. - Enhanced session metadata extraction for improved error handling in GetSession. - Updated comments and TODOs in reconciler and session handler files to reflect ongoing migration to controller-runtime patterns.
1 parent c4ad7b7 commit 5134304

File tree

4 files changed

+26
-60
lines changed

4 files changed

+26
-60
lines changed

components/backend/handlers/sessions.go

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -748,10 +748,18 @@ func GetSession(c *gin.Context) {
748748
return
749749
}
750750

751+
// Safely extract metadata using type-safe pattern
752+
metadata, ok := item.Object["metadata"].(map[string]interface{})
753+
if !ok {
754+
log.Printf("GetSession: invalid metadata for session %s", sessionName)
755+
c.JSON(http.StatusInternalServerError, gin.H{"error": "Invalid session metadata"})
756+
return
757+
}
758+
751759
session := types.AgenticSession{
752760
APIVersion: item.GetAPIVersion(),
753761
Kind: item.GetKind(),
754-
Metadata: item.Object["metadata"].(map[string]interface{}),
762+
Metadata: metadata,
755763
}
756764

757765
if spec, ok := item.Object["spec"].(map[string]interface{}); ok {
@@ -2102,27 +2110,6 @@ func StopSession(c *gin.Context) {
21022110
c.JSON(http.StatusAccepted, session)
21032111
}
21042112

2105-
// EnableWorkspaceAccess is deprecated - temporary content pods have been removed
2106-
// POST /api/projects/:projectName/agentic-sessions/:sessionName/workspace/enable
2107-
func EnableWorkspaceAccess(c *gin.Context) {
2108-
c.JSON(http.StatusGone, gin.H{
2109-
"error": "Temporary workspace access has been removed",
2110-
"message": "Session artifacts are now stored in S3. Access artifacts directly from your S3 bucket.",
2111-
"hint": "Configure S3 storage in project settings to persist session state and artifacts.",
2112-
"s3Path": fmt.Sprintf("s3://{bucket}/{namespace}/%s/", c.Param("sessionName")),
2113-
})
2114-
}
2115-
2116-
// TouchWorkspaceAccess updates the last-accessed timestamp to keep temp pod alive
2117-
// POST /api/projects/:projectName/agentic-sessions/:sessionName/workspace/touch
2118-
func TouchWorkspaceAccess(c *gin.Context) {
2119-
// Deprecated: Temp-content pods no longer exist
2120-
c.JSON(http.StatusGone, gin.H{
2121-
"error": "Temporary workspace access has been removed",
2122-
"message": "Session artifacts are stored in S3 and do not require touch/keepalive.",
2123-
})
2124-
}
2125-
21262113
// GetSessionK8sResources returns job, pod, and PVC information for a session
21272114
// GET /api/projects/:projectName/agentic-sessions/:sessionName/k8s-resources
21282115
func GetSessionK8sResources(c *gin.Context) {

components/backend/routes.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,6 @@ func registerRoutes(r *gin.Engine) {
5656
projectGroup.POST("/agentic-sessions/:sessionName/clone", handlers.CloneSession)
5757
projectGroup.POST("/agentic-sessions/:sessionName/start", handlers.StartSession)
5858
projectGroup.POST("/agentic-sessions/:sessionName/stop", handlers.StopSession)
59-
projectGroup.POST("/agentic-sessions/:sessionName/workspace/enable", handlers.EnableWorkspaceAccess)
60-
projectGroup.POST("/agentic-sessions/:sessionName/workspace/touch", handlers.TouchWorkspaceAccess)
6159
projectGroup.GET("/agentic-sessions/:sessionName/workspace", handlers.ListSessionWorkspace)
6260
projectGroup.GET("/agentic-sessions/:sessionName/workspace/*path", handlers.GetSessionWorkspaceFile)
6361
projectGroup.PUT("/agentic-sessions/:sessionName/workspace/*path", handlers.PutSessionWorkspaceFile)

components/operator/internal/handlers/reconciler.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,20 @@ import (
1010
"time"
1111

1212
corev1 "k8s.io/api/core/v1"
13-
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1413
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
1514

1615
"ambient-code-operator/internal/config"
17-
"ambient-code-operator/internal/types"
1816
)
1917

2018
// ReconcilePendingSession handles the Pending phase - creates pod and services.
2119
// This is the main entry point called from the controller for pending sessions.
20+
//
21+
// TODO(controller-runtime-migration): This is a transitional wrapper around the legacy
22+
// handleAgenticSessionEvent() function (2,300+ lines). Future work should:
23+
// 1. Extract phase-specific logic into separate functions (ReconcilePending, ReconcileRunning, etc.)
24+
// 2. Use controller-runtime patterns (Patch, StatusWriter, etc.) instead of direct API calls
25+
// 3. Remove handleAgenticSessionEvent() entirely
26+
// This approach allows adopting controller-runtime framework without rewriting all logic at once.
2227
func ReconcilePendingSession(ctx context.Context, session *unstructured.Unstructured, appConfig *config.Config) error {
2328
// Delegate to existing handleAgenticSessionEvent logic
2429
// This is a wrapper that allows the existing code to be called from the controller

components/operator/internal/handlers/sessions.go

Lines changed: 10 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,25 @@ import (
2525
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2626
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
2727
intstr "k8s.io/apimachinery/pkg/util/intstr"
28-
"k8s.io/apimachinery/pkg/watch"
2928
"k8s.io/client-go/util/retry"
3029
)
3130

3231
// Track which pods are currently being monitored to prevent duplicate goroutines
33-
// NOTE: This is used by the legacy handleAgenticSessionEvent function which is
34-
// kept for reference but no longer actively called by the operator.
35-
// The controller-runtime based reconciler in internal/controller/ handles all
36-
// AgenticSession reconciliation now.
3732
var (
3833
monitoredPods = make(map[string]bool)
3934
monitoredPodsMu sync.Mutex
4035
)
4136

37+
// handleAgenticSessionEvent is the legacy reconciliation function containing all session
38+
// lifecycle logic (~2,300 lines). It's called by ReconcilePendingSession() wrapper.
39+
//
40+
// TODO(controller-runtime-migration): This function should be refactored into smaller,
41+
// phase-specific reconcilers that use controller-runtime patterns. Current architecture:
42+
// - ✅ Controller-runtime framework adopted (work queue, leader election, metrics)
43+
// - ⚠️ Business logic still uses legacy patterns (direct API calls, manual status updates)
44+
// - 🔜 Future: Break into ReconcilePending, ReconcileRunning, ReconcileStopped functions
45+
//
46+
// This transitional approach allows framework adoption without rewriting 2,300 lines at once.
4247
func handleAgenticSessionEvent(obj *unstructured.Unstructured) error {
4348
name := obj.GetName()
4449
sessionNamespace := obj.GetNamespace()
@@ -681,35 +686,6 @@ func handleAgenticSessionEvent(obj *unstructured.Unstructured) error {
681686
})
682687
}
683688

684-
// Extract repos configuration (simplified format: url and branch)
685-
type RepoConfig struct {
686-
URL string
687-
Branch string
688-
}
689-
690-
var repos []RepoConfig
691-
692-
// Read repos[] array format
693-
if reposArr, found, _ := unstructured.NestedSlice(spec, "repos"); found && len(reposArr) > 0 {
694-
repos = make([]RepoConfig, 0, len(reposArr))
695-
for _, repoItem := range reposArr {
696-
if repoMap, ok := repoItem.(map[string]interface{}); ok {
697-
repo := RepoConfig{}
698-
if url, ok := repoMap["url"].(string); ok {
699-
repo.URL = url
700-
}
701-
if branch, ok := repoMap["branch"].(string); ok {
702-
repo.Branch = branch
703-
} else {
704-
repo.Branch = "main"
705-
}
706-
if repo.URL != "" {
707-
repos = append(repos, repo)
708-
}
709-
}
710-
}
711-
}
712-
713689
// Read autoPushOnComplete flag
714690
autoPushOnComplete, _, _ := unstructured.NestedBool(spec, "autoPushOnComplete")
715691

0 commit comments

Comments
 (0)