Delete port forwards when pods get deleted or killed (#2247)

mine
Alexandru Placinta 2023-11-11 01:51:26 +01:00 committed by GitHub
parent a3155d015d
commit 19952cd282
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 9 additions and 1 deletions

View File

@ -1,6 +1,7 @@
package watch package watch
import ( import (
"fmt"
"strings" "strings"
"github.com/derailed/k9s/internal/port" "github.com/derailed/k9s/internal/port"
@ -85,8 +86,15 @@ func (ff Forwarders) DeleteAll() {
// Kill stops and delete a port-forwards associated with pod. // Kill stops and delete a port-forwards associated with pod.
func (ff Forwarders) Kill(path string) int { func (ff Forwarders) Kill(path string) int {
var stats int var stats int
// The way port forwards are stored is `pod_fqn|container|local_port:container_port`
// The '|' is added to make sure we do not delete port forwards from other pods that have the same prefix
// Without the `|` port forwards for pods, default/web-0 and default/web-0-bla would be both deleted
// even if we want only port forwards for default/web-0 to be deleted
prefix := fmt.Sprintf("%s|", path)
for k, f := range ff { for k, f := range ff {
if strings.HasPrefix(k, path) { if k == path || strings.HasPrefix(k, prefix) {
stats++ stats++
log.Debug().Msgf("Stop + Delete port-forward %s", k) log.Debug().Msgf("Stop + Delete port-forward %s", k)
f.Stop() f.Stop()