@@ -17,6 +17,7 @@ import (
1717 "github.com/revolyssup/k8sdebug/pkg/forwarder"
1818 "github.com/revolyssup/k8sdebug/pkg/portforward/mock"
1919 "github.com/revolyssup/k8sdebug/pkg/portforward/roundrobin"
20+ "github.com/revolyssup/k8sdebug/pkg/portforward/sticky"
2021 "github.com/spf13/cobra"
2122 v1 "k8s.io/api/core/v1"
2223 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -48,8 +49,8 @@ func forwardToPod(hostConn net.Conn, podCon net.Conn) {
4849 io .Copy (podCon , hostConn )
4950}
5051
51- func getPodConnection (fw forwarder.Forwarder ) (net.Conn , error ) {
52- port := fw .NextPort ()
52+ func getPodConnection (fw forwarder.Forwarder , hostConn net. Conn ) (net.Conn , error ) {
53+ port := fw .NextPort (hostConn )
5354 if port == "" {
5455 return nil , fmt .Errorf ("no available port" )
5556 }
@@ -66,6 +67,8 @@ func getForwarder(policy string) forwarder.Forwarder {
6667 return roundrobin .New (& connPool )
6768 case "mock" :
6869 return mock .New ()
70+ case "sticky" :
71+ return sticky .New (& connPool )
6972 }
7073 return nil
7174}
@@ -107,7 +110,7 @@ func listenAndAccept(ctx context.Context, listener net.Listener, fw forwarder.Fo
107110 fmt .Printf ("Accept error: %v" , err )
108111 continue
109112 }
110- podConn , err := getPodConnection (fw )
113+ podConn , err := getPodConnection (fw , hostConn )
111114 if err != nil {
112115 fmt .Printf ("Pod connection error: %v" , err )
113116 continue
@@ -247,7 +250,11 @@ func NewCommand() *cobra.Command {
247250
248251 cmd .PersistentFlags ().StringVarP (& namespace , "namespace" , "n" , "default" , "Name of the pod" )
249252 cmd .PersistentFlags ().StringVarP (& typ , "type" , "t" , "pod" , "Name of the pod" )
250- cmd .PersistentFlags ().StringVar (& policy , "policy" , "round-robin" , "policy to use while sending requests" )
253+ cmd .PersistentFlags ().StringVar (& policy , "policy" , "round-robin" , `policy to use while sending requests.
254+ Default policy is round-robin.
255+ round-robin: In this mode, requests are balanced across all the pods much like Kubernetes service.
256+ sticky: In this mode, requests from a particular source IP will always be directed to a single pod.
257+ ` )
251258 cmd .Flags ().StringVarP (& labels , "labels" , "l" , "" , "list of key value pairs to use as labels while filtering pods." )
252259 cmd .Flags ().StringVar (& hostport , "hostport" , "3000" , "host port on which requests will be sent" )
253260 cmd .Flags ().StringVar (& containerPort , "containerport" , "80" , "container port on which requests will be sent" )
0 commit comments