Skip to content

Commit 2af9a07

Browse files
committed
feat: refactor error handling and improve logging in Run function
This pull request changes: - Eliminates unnecessary error messages when specific operations cannot be performed (e.g., when verbs do not include "watch"). - Updates error logging to provide more context by using the `%w` verb for wrapping errors, allowing better error tracking. - Enhances concurrency handling in the port-forwarding logic by explicitly passing `containerPort` and `hostPort` to the goroutine, reducing reliance on closures that capture variables. ```mermaid sequenceDiagram participant User participant Server User->>Server: Request operation alt Success Server-->>User: Successfully executed operation else Error Occurred Server-->>User: Error message with context end ```
1 parent f21a7c7 commit 2af9a07

File tree

1 file changed

+8
-9
lines changed

1 file changed

+8
-9
lines changed

internal/run.go

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"net/http"
2323
"os"
2424
"slices"
25+
"strings"
2526
"sync"
2627
"time"
2728
)
@@ -83,7 +84,6 @@ func Run(ctx context.Context, group, namespace, labels, container string, allCon
8384

8485
// if the verbs do not include "watch" we cannot watch this resource
8586
if !slices.Contains(resource.Verbs, "watch") {
86-
fmt.Printf("cannot watch %s (%s)\n", resource.Name, resource.Kind)
8787
continue
8888
}
8989

@@ -200,8 +200,6 @@ func Run(ctx context.Context, group, namespace, labels, container string, allCon
200200
defer func() {
201201
if r := recover(); r != nil {
202202
fmt.Printf(color("pods", "[pods/%s/%s] error while tailing logs: %v\n"), pod.Name, ctr.Name, r)
203-
} else {
204-
fmt.Printf(color("pods", "[pods/%s/%s] tailing logs stopped\n"), pod.Name, ctr.Name)
205203
}
206204
}()
207205

@@ -212,12 +210,12 @@ func Run(ctx context.Context, group, namespace, labels, container string, allCon
212210
})
213211
podLogs, err := req.Stream(ctx)
214212
if err != nil {
215-
panic(fmt.Errorf("Error opening stream: %s\n", err))
213+
panic(fmt.Errorf("Error opening stream: %w\n", err))
216214
}
217215
defer podLogs.Close()
218216
_, err = io.Copy(out, podLogs)
219217
if err != nil && !errors.Is(err, context.Canceled) {
220-
panic(fmt.Errorf("Error copying stream: %s\n", err))
218+
panic(fmt.Errorf("Error copying stream: %w\n", err))
221219
}
222220
}()
223221
for _, port := range ctr.Ports {
@@ -226,7 +224,7 @@ func Run(ctx context.Context, group, namespace, labels, container string, allCon
226224
hostPort := hostPortOffset + int(containerPort)
227225

228226
// start port-forwarding
229-
go func() {
227+
go func(containerPort int32, hostPort int) {
230228
// check if the pod is already being port-forwarded
231229
obj, _ := portForwarding.LoadOrStore(hostPort, &sync.Mutex{})
232230
mu := obj.(*sync.Mutex)
@@ -237,8 +235,6 @@ func Run(ctx context.Context, group, namespace, labels, container string, allCon
237235
defer func() {
238236
if r := recover(); r != nil {
239237
fmt.Printf(color("pods", "[pods/%s/%s] error while port-forwarding: %d -> %d: %v\n"), pod.Name, ctr.Name, hostPort, containerPort, r)
240-
} else {
241-
fmt.Printf(color("pods", "[pods/%s/%s] port-forwarding %d -> %d stopped\n"), pod.Name, ctr.Name, hostPort, containerPort)
242238
}
243239
}()
244240

@@ -250,6 +246,9 @@ func Run(ctx context.Context, group, namespace, labels, container string, allCon
250246

251247
transport, upgrader, err := spdy.RoundTripperFor(config)
252248
if err != nil {
249+
if strings.Contains(err.Error(), "not found") {
250+
return
251+
}
253252
panic(err)
254253
}
255254

@@ -291,7 +290,7 @@ func Run(ctx context.Context, group, namespace, labels, container string, allCon
291290
if err := fw.ForwardPorts(); err != nil {
292291
panic(err)
293292
}
294-
}()
293+
}(containerPort, hostPort)
295294
}
296295
}
297296
}

0 commit comments

Comments
 (0)