Skip to content

Commit 29056c2

Browse files
committed
fix(router): add auth header for workload manager
Signed-off-by: Zhou Zihang <z@mcac.cc>
1 parent eb947f5 commit 29056c2

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

pkg/router/session_manager.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ import (
3434
"golang.org/x/net/http2"
3535
)
3636

37+
const serviceAccountTokenPath = "/var/run/secrets/kubernetes.io/serviceaccount/token"
38+
3739
// SessionManager defines the session management behavior on top of Store and the workload manager.
3840
type SessionManager interface {
3941
// GetSandboxBySession returns the sandbox associated with the given sessionID.
@@ -139,6 +141,9 @@ func (m *manager) createSandbox(ctx context.Context, namespace string, name stri
139141
return nil, fmt.Errorf("failed to create HTTP request: %w", err)
140142
}
141143
req.Header.Set("Content-Type", "application/json")
144+
if token := loadWorkloadManagerAuthToken(); token != "" {
145+
req.Header.Set("Authorization", "Bearer "+token)
146+
}
142147

143148
// Send the request
144149
resp, err := m.httpClient.Do(req)
@@ -186,3 +191,18 @@ func (m *manager) createSandbox(ctx context.Context, namespace string, name stri
186191

187192
return sandbox, nil
188193
}
194+
195+
func loadWorkloadManagerAuthToken() string {
196+
if token := strings.TrimSpace(os.Getenv("API_TOKEN")); token != "" {
197+
return token
198+
}
199+
200+
b, err := os.ReadFile(serviceAccountTokenPath)
201+
if err != nil {
202+
if os.IsNotExist(err) {
203+
return ""
204+
}
205+
return ""
206+
}
207+
return strings.TrimSpace(string(b))
208+
}

0 commit comments

Comments
 (0)