Skip to content

Commit 1672739

Browse files
committed
fix: address remaining review feedback from gemini bot
Signed-off-by: Abhinav Singh <abhinavsingh717073@gmail.com>
1 parent eef63fc commit 1672739

3 files changed

Lines changed: 16 additions & 5 deletions

File tree

pkg/router/jwt.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"encoding/pem"
2525
"fmt"
2626
"os"
27+
"sync"
2728
"time"
2829

2930
"github.com/golang-jwt/jwt/v5"
@@ -62,6 +63,7 @@ type JWTManager struct {
6263
privateKey *rsa.PrivateKey
6364
publicKey *rsa.PublicKey
6465
clientset kubernetes.Interface
66+
keyCache sync.Map
6567
}
6668

6769
// NewJWTManager creates a new JWT manager with a fresh RSA key pair
@@ -105,9 +107,17 @@ func (jm *JWTManager) GenerateToken(claims map[string]interface{}) (string, erro
105107

106108
// GenerateTokenWithKey generates a JWT token signed with a specific PEM-encoded private key
107109
func (jm *JWTManager) GenerateTokenWithKey(claims map[string]interface{}, privateKeyPEM string) (string, error) {
108-
privKey, err := jwt.ParseRSAPrivateKeyFromPEM([]byte(privateKeyPEM))
109-
if err != nil {
110-
return "", fmt.Errorf("failed to parse private key: %w", err)
110+
var privKey *rsa.PrivateKey
111+
112+
if cachedKey, ok := jm.keyCache.Load(privateKeyPEM); ok {
113+
privKey = cachedKey.(*rsa.PrivateKey)
114+
} else {
115+
parsedKey, err := jwt.ParseRSAPrivateKeyFromPEM([]byte(privateKeyPEM))
116+
if err != nil {
117+
return "", fmt.Errorf("failed to parse private key: %w", err)
118+
}
119+
privKey = parsedKey
120+
jm.keyCache.Store(privateKeyPEM, privKey)
111121
}
112122

113123
// Create JWT claims

pkg/workloadmanager/sandbox_helper.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,8 +203,7 @@ func (s *Server) initializePicoD(ctx context.Context, podIP string, entry *sandb
203203
}
204204
req.Header.Set("Content-Type", "application/json")
205205

206-
client := &http.Client{Timeout: s.config.PicoInitTimeout}
207-
resp, err := client.Do(req)
206+
resp, err := s.httpClient.Do(req)
208207
if err != nil {
209208
return fmt.Errorf("POST /init failed: %w", err)
210209
}

pkg/workloadmanager/server.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ type Server struct {
4141
tokenCache *TokenCache
4242
informers *Informers
4343
storeClient store.Store
44+
httpClient *http.Client
4445
wg sync.WaitGroup
4546
}
4647

@@ -107,6 +108,7 @@ func NewServer(config *Config, sandboxController *SandboxReconciler) (*Server, e
107108
tokenCache: tokenCache,
108109
informers: NewInformers(k8sClient),
109110
storeClient: store.Storage(),
111+
httpClient: &http.Client{Timeout: config.PicoInitTimeout},
110112
}
111113

112114
// Setup routes

0 commit comments

Comments
 (0)