mirror of
https://github.com/k3s-io/kubernetes.git
synced 2026-01-06 07:57:35 +00:00
enforce the interface relationship between ServicePort and BaseServiceInfo
Currently the BaseServiceInfo struct implements the ServicePort interface, but only uses that interface sometimes. All the elements of BaseServiceInfo are exported and sometimes the interface is used to access them and othertimes not I extended the ServicePort interface so that all relevent values can be accessed through it and unexported all the elements of BaseServiceInfo
This commit is contained in:
@@ -147,30 +147,18 @@ func TestGetChainLinesMultipleTables(t *testing.T) {
|
||||
checkAllLines(t, utiliptables.TableNAT, []byte(iptablesSave), expected)
|
||||
}
|
||||
|
||||
func newFakeServiceInfo(service proxy.ServicePortName, ip net.IP, port int, protocol v1.Protocol, onlyNodeLocalEndpoints bool) *serviceInfo {
|
||||
return &serviceInfo{
|
||||
BaseServiceInfo: &proxy.BaseServiceInfo{
|
||||
SessionAffinityType: v1.ServiceAffinityNone, // default
|
||||
StickyMaxAgeSeconds: int(v1.DefaultClientIPServiceAffinitySeconds), // default
|
||||
ClusterIP: ip,
|
||||
Port: port,
|
||||
Protocol: protocol,
|
||||
OnlyNodeLocalEndpoints: onlyNodeLocalEndpoints,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func TestDeleteEndpointConnections(t *testing.T) {
|
||||
const (
|
||||
UDP = v1.ProtocolUDP
|
||||
TCP = v1.ProtocolTCP
|
||||
SCTP = v1.ProtocolSCTP
|
||||
)
|
||||
|
||||
testCases := []struct {
|
||||
description string
|
||||
svcName string
|
||||
svcIP string
|
||||
svcPort int
|
||||
svcPort int32
|
||||
protocol v1.Protocol
|
||||
endpoint string // IP:port endpoint
|
||||
epSvcPair proxy.ServiceEndpoint // Will be generated by test
|
||||
@@ -237,21 +225,6 @@ func TestDeleteEndpointConnections(t *testing.T) {
|
||||
},
|
||||
}
|
||||
|
||||
// Create a service map that has service info entries for all test cases
|
||||
// and generate an endpoint service pair for each test case
|
||||
serviceMap := make(map[proxy.ServicePortName]proxy.ServicePort)
|
||||
for i, tc := range testCases {
|
||||
svc := proxy.ServicePortName{
|
||||
NamespacedName: types.NamespacedName{Namespace: "ns1", Name: tc.svcName},
|
||||
Port: "p80",
|
||||
}
|
||||
serviceMap[svc] = newFakeServiceInfo(svc, net.ParseIP(tc.svcIP), 80, tc.protocol, false)
|
||||
testCases[i].epSvcPair = proxy.ServiceEndpoint{
|
||||
Endpoint: tc.endpoint,
|
||||
ServicePortName: svc,
|
||||
}
|
||||
}
|
||||
|
||||
// Create a fake executor for the conntrack utility. This should only be
|
||||
// invoked for UDP connections, since no conntrack cleanup is needed for TCP
|
||||
fcmd := fakeexec.FakeCmd{}
|
||||
@@ -276,16 +249,43 @@ func TestDeleteEndpointConnections(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// Create a proxier using the fake conntrack executor and service map
|
||||
fakeProxier := Proxier{exec: &fexec, serviceMap: serviceMap}
|
||||
ipt := iptablestest.NewFake()
|
||||
fp := NewFakeProxier(ipt)
|
||||
fp.exec = &fexec
|
||||
|
||||
for _, tc := range testCases {
|
||||
makeServiceMap(fp,
|
||||
makeTestService("ns1", tc.svcName, func(svc *v1.Service) {
|
||||
svc.Spec.ClusterIP = tc.svcIP
|
||||
svc.Spec.Ports = []v1.ServicePort{{
|
||||
Name: "p80",
|
||||
Port: tc.svcPort,
|
||||
Protocol: tc.protocol,
|
||||
}}
|
||||
svc.Spec.ExternalTrafficPolicy = v1.ServiceExternalTrafficPolicyTypeLocal
|
||||
}),
|
||||
)
|
||||
|
||||
proxy.UpdateServiceMap(fp.serviceMap, fp.serviceChanges)
|
||||
}
|
||||
|
||||
// Run the test cases
|
||||
for _, tc := range testCases {
|
||||
priorExecs := fexec.CommandCalls
|
||||
priorGlogErrs := klog.Stats.Error.Lines()
|
||||
|
||||
input := []proxy.ServiceEndpoint{tc.epSvcPair}
|
||||
fakeProxier.deleteEndpointConnections(input)
|
||||
svc := proxy.ServicePortName{
|
||||
NamespacedName: types.NamespacedName{Namespace: "ns1", Name: tc.svcName},
|
||||
Port: "p80",
|
||||
}
|
||||
input := []proxy.ServiceEndpoint{
|
||||
{
|
||||
Endpoint: tc.endpoint,
|
||||
ServicePortName: svc,
|
||||
},
|
||||
}
|
||||
|
||||
fp.deleteEndpointConnections(input)
|
||||
|
||||
// For UDP connections, check the executed conntrack command
|
||||
var expExecs int
|
||||
|
||||
Reference in New Issue
Block a user