Skip to content

Commit 7b28a11

Browse files
authored
Merge pull request kubernetes#127810 from aojea/proxy_conntrack_flags
unit test kube-proxy conntrack flags
2 parents 60efdae + df7215a commit 7b28a11

File tree

3 files changed

+186
-5
lines changed

3 files changed

+186
-5
lines changed

cmd/kube-proxy/app/options_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -525,6 +525,22 @@ kind: KubeProxyConfiguration
525525
"empty": {
526526
expected: expected,
527527
},
528+
"conntrack": {
529+
flags: []string{
530+
"--conntrack-max-per-core=0",
531+
"--conntrack-min=0",
532+
"--conntrack-tcp-timeout-established=0",
533+
"--conntrack-tcp-timeout-close-wait=0",
534+
},
535+
expected: func() *kubeproxyconfig.KubeProxyConfiguration {
536+
c := expected.DeepCopy()
537+
c.Linux.Conntrack.MaxPerCore = ptr.To(int32(0))
538+
c.Linux.Conntrack.Min = ptr.To(int32(0))
539+
c.Linux.Conntrack.TCPEstablishedTimeout = ptr.To(metav1.Duration{})
540+
c.Linux.Conntrack.TCPCloseWaitTimeout = ptr.To(metav1.Duration{})
541+
return c
542+
}(),
543+
},
528544
"empty-config": {
529545
config: header,
530546
expected: expected,

cmd/kube-proxy/app/server_linux.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,8 @@ func (s *ProxyServer) platformSetup(ctx context.Context) error {
9393
logger.Info("NodeInfo", "podCIDRs", node.Spec.PodCIDRs)
9494
}
9595

96-
err := s.setupConntrack(ctx)
96+
ct := &realConntracker{}
97+
err := s.setupConntrack(ctx, ct)
9798
if err != nil {
9899
return err
99100
}
@@ -334,9 +335,7 @@ func (s *ProxyServer) createProxier(ctx context.Context, config *proxyconfigapi.
334335
return proxier, nil
335336
}
336337

337-
func (s *ProxyServer) setupConntrack(ctx context.Context) error {
338-
ct := &realConntracker{}
339-
338+
func (s *ProxyServer) setupConntrack(ctx context.Context, ct Conntracker) error {
340339
max, err := getConntrackMax(ctx, s.Config.Linux.Conntrack)
341340
if err != nil {
342341
return err

cmd/kube-proxy/app/server_linux_test.go

Lines changed: 167 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ limitations under the License.
2020
package app
2121

2222
import (
23+
"context"
24+
"errors"
2325
"fmt"
2426
"net"
2527
"os"
@@ -30,8 +32,8 @@ import (
3032
"testing"
3133
"time"
3234

35+
"github.com/google/go-cmp/cmp"
3336
"github.com/spf13/pflag"
34-
3537
v1 "k8s.io/api/core/v1"
3638
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3739
"k8s.io/apimachinery/pkg/runtime"
@@ -719,3 +721,167 @@ func TestProxyServer_platformSetup(t *testing.T) {
719721
})
720722
}
721723
}
724+
725+
type fakeConntracker struct {
726+
called []string
727+
err error
728+
}
729+
730+
// SetMax value is calculated based on the number of CPUs by getConntrackMax()
731+
func (fc *fakeConntracker) SetMax(ctx context.Context, max int) error {
732+
fc.called = append(fc.called, "SetMax")
733+
return fc.err
734+
}
735+
func (fc *fakeConntracker) SetTCPEstablishedTimeout(ctx context.Context, seconds int) error {
736+
fc.called = append(fc.called, fmt.Sprintf("SetTCPEstablishedTimeout(%d)", seconds))
737+
return fc.err
738+
}
739+
func (fc *fakeConntracker) SetTCPCloseWaitTimeout(ctx context.Context, seconds int) error {
740+
fc.called = append(fc.called, fmt.Sprintf("SetTCPCloseWaitTimeout(%d)", seconds))
741+
return fc.err
742+
}
743+
func (fc *fakeConntracker) SetTCPBeLiberal(ctx context.Context, value int) error {
744+
fc.called = append(fc.called, fmt.Sprintf("SetTCPBeLiberal(%d)", value))
745+
return fc.err
746+
}
747+
func (fc *fakeConntracker) SetUDPTimeout(ctx context.Context, seconds int) error {
748+
fc.called = append(fc.called, fmt.Sprintf("SetUDPTimeout(%d)", seconds))
749+
return fc.err
750+
}
751+
func (fc *fakeConntracker) SetUDPStreamTimeout(ctx context.Context, seconds int) error {
752+
fc.called = append(fc.called, fmt.Sprintf("SetUDPStreamTimeout(%d)", seconds))
753+
return fc.err
754+
}
755+
756+
func TestSetupConntrack(t *testing.T) {
757+
_, ctx := ktesting.NewTestContext(t)
758+
tests := []struct {
759+
name string
760+
config proxyconfigapi.KubeProxyConntrackConfiguration
761+
expect []string
762+
conntrackErr error
763+
wantErr bool
764+
}{
765+
{
766+
name: "do nothing if conntrack config is empty",
767+
config: proxyconfigapi.KubeProxyConntrackConfiguration{},
768+
expect: nil,
769+
},
770+
{
771+
name: "SetMax is called if conntrack.maxPerCore is specified",
772+
config: proxyconfigapi.KubeProxyConntrackConfiguration{
773+
MaxPerCore: ptr.To(int32(12)),
774+
},
775+
expect: []string{"SetMax"},
776+
},
777+
{
778+
name: "SetMax is not called if conntrack.maxPerCore is 0",
779+
config: proxyconfigapi.KubeProxyConntrackConfiguration{
780+
MaxPerCore: ptr.To(int32(0)),
781+
},
782+
expect: nil,
783+
},
784+
{
785+
name: "SetTCPEstablishedTimeout is called if conntrack.tcpEstablishedTimeout is specified",
786+
config: proxyconfigapi.KubeProxyConntrackConfiguration{
787+
TCPEstablishedTimeout: &metav1.Duration{Duration: 5 * time.Second},
788+
},
789+
expect: []string{"SetTCPEstablishedTimeout(5)"},
790+
},
791+
{
792+
name: "SetTCPEstablishedTimeout is not called if conntrack.tcpEstablishedTimeout is 0",
793+
config: proxyconfigapi.KubeProxyConntrackConfiguration{
794+
TCPEstablishedTimeout: &metav1.Duration{Duration: 0 * time.Second},
795+
},
796+
expect: nil,
797+
},
798+
{
799+
name: "SetTCPCloseWaitTimeout is called if conntrack.tcpCloseWaitTimeout is specified",
800+
config: proxyconfigapi.KubeProxyConntrackConfiguration{
801+
TCPCloseWaitTimeout: &metav1.Duration{Duration: 5 * time.Second},
802+
},
803+
expect: []string{"SetTCPCloseWaitTimeout(5)"},
804+
},
805+
{
806+
name: "SetTCPCloseWaitTimeout is not called if conntrack.tcpCloseWaitTimeout is 0",
807+
config: proxyconfigapi.KubeProxyConntrackConfiguration{
808+
TCPCloseWaitTimeout: &metav1.Duration{Duration: 0 * time.Second},
809+
},
810+
expect: nil,
811+
},
812+
{
813+
name: "SetTCPBeLiberal is called if conntrack.tcpBeLiberal is true",
814+
config: proxyconfigapi.KubeProxyConntrackConfiguration{
815+
TCPBeLiberal: true,
816+
},
817+
expect: []string{"SetTCPBeLiberal(1)"},
818+
},
819+
{
820+
name: "SetTCPBeLiberal is not called if conntrack.tcpBeLiberal is false",
821+
config: proxyconfigapi.KubeProxyConntrackConfiguration{
822+
TCPBeLiberal: false,
823+
},
824+
expect: nil,
825+
},
826+
{
827+
name: "SetUDPTimeout is called if conntrack.udpTimeout is specified",
828+
config: proxyconfigapi.KubeProxyConntrackConfiguration{
829+
UDPTimeout: metav1.Duration{Duration: 5 * time.Second},
830+
},
831+
expect: []string{"SetUDPTimeout(5)"},
832+
},
833+
{
834+
name: "SetUDPTimeout is called if conntrack.udpTimeout is zero",
835+
config: proxyconfigapi.KubeProxyConntrackConfiguration{
836+
UDPTimeout: metav1.Duration{Duration: 0 * time.Second},
837+
},
838+
expect: nil,
839+
},
840+
{
841+
name: "SetUDPStreamTimeout is called if conntrack.udpStreamTimeout is specified",
842+
config: proxyconfigapi.KubeProxyConntrackConfiguration{
843+
UDPStreamTimeout: metav1.Duration{Duration: 5 * time.Second},
844+
},
845+
expect: []string{"SetUDPStreamTimeout(5)"},
846+
},
847+
{
848+
name: "SetUDPStreamTimeout is called if conntrack.udpStreamTimeout is zero",
849+
config: proxyconfigapi.KubeProxyConntrackConfiguration{
850+
UDPStreamTimeout: metav1.Duration{Duration: 0 * time.Second},
851+
},
852+
expect: nil,
853+
},
854+
{
855+
name: "an error is returned if conntrack.SetTCPEstablishedTimeout fails",
856+
config: proxyconfigapi.KubeProxyConntrackConfiguration{
857+
TCPEstablishedTimeout: &metav1.Duration{Duration: 5 * time.Second},
858+
},
859+
expect: []string{"SetTCPEstablishedTimeout(5)"},
860+
conntrackErr: errors.New("random error"),
861+
wantErr: true,
862+
},
863+
}
864+
865+
for _, test := range tests {
866+
t.Run(test.name, func(t *testing.T) {
867+
fc := &fakeConntracker{err: test.conntrackErr}
868+
s := &ProxyServer{
869+
Config: &proxyconfigapi.KubeProxyConfiguration{
870+
Linux: proxyconfigapi.KubeProxyLinuxConfiguration{
871+
Conntrack: test.config,
872+
},
873+
},
874+
}
875+
err := s.setupConntrack(ctx, fc)
876+
if test.wantErr && err == nil {
877+
t.Errorf("Test %q: Expected error, got nil", test.name)
878+
}
879+
if !test.wantErr && err != nil {
880+
t.Errorf("Test %q: Expected no error, got %v", test.name, err)
881+
}
882+
if !cmp.Equal(fc.called, test.expect) {
883+
t.Errorf("Test %q: Expected conntrack calls: %v, got: %v", test.name, test.expect, fc.called)
884+
}
885+
})
886+
}
887+
}

0 commit comments

Comments
 (0)