Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
K
k3s
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Jacklull
k3s
Commits
c5cbdbe3
Commit
c5cbdbe3
authored
Mar 20, 2017
by
Wojciech Tyczynski
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Edge-based userspace proxy
parent
f7c06ad2
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
99 additions
and
51 deletions
+99
-51
server.go
cmd/kube-proxy/app/server.go
+1
-1
proxier_test.go
pkg/proxy/userspace/proxier_test.go
+0
-0
roundrobin.go
pkg/proxy/userspace/roundrobin.go
+98
-50
roundrobin_test.go
pkg/proxy/userspace/roundrobin_test.go
+0
-0
No files found.
cmd/kube-proxy/app/server.go
View file @
c5cbdbe3
...
@@ -281,7 +281,7 @@ func NewProxyServerDefault(config *options.ProxyServerConfig) (*ProxyServer, err
...
@@ -281,7 +281,7 @@ func NewProxyServerDefault(config *options.ProxyServerConfig) (*ProxyServer, err
// our config.EndpointsConfigHandler.
// our config.EndpointsConfigHandler.
loadBalancer
:=
userspace
.
NewLoadBalancerRR
()
loadBalancer
:=
userspace
.
NewLoadBalancerRR
()
// set EndpointsConfigHandler to our loadBalancer
// set EndpointsConfigHandler to our loadBalancer
endpointsHandler
=
loadBalancer
endpoints
Event
Handler
=
loadBalancer
proxierUserspace
,
err
:=
userspace
.
NewProxier
(
proxierUserspace
,
err
:=
userspace
.
NewProxier
(
loadBalancer
,
loadBalancer
,
net
.
ParseIP
(
config
.
BindAddress
),
net
.
ParseIP
(
config
.
BindAddress
),
...
...
pkg/proxy/userspace/proxier_test.go
View file @
c5cbdbe3
This diff is collapsed.
Click to expand it.
pkg/proxy/userspace/roundrobin.go
View file @
c5cbdbe3
...
@@ -243,65 +243,92 @@ func (lb *LoadBalancerRR) updateAffinityMap(svcPort proxy.ServicePortName, newEn
...
@@ -243,65 +243,92 @@ func (lb *LoadBalancerRR) updateAffinityMap(svcPort proxy.ServicePortName, newEn
}
}
}
}
// OnEndpointsUpdate manages the registered service endpoints.
// buildPortsToEndpointsMap builds a map of portname -> all ip:ports for that
// Registered endpoints are updated if found in the update set or
// portname. Expode Endpoints.Subsets[*] into this structure.
// unregistered if missing from the update set.
func
buildPortsToEndpointsMap
(
endpoints
*
api
.
Endpoints
)
map
[
string
][]
hostPortPair
{
func
(
lb
*
LoadBalancerRR
)
OnEndpointsUpdate
(
allEndpoints
[]
*
api
.
Endpoints
)
{
portsToEndpoints
:=
map
[
string
][]
hostPortPair
{}
for
i
:=
range
endpoints
.
Subsets
{
ss
:=
&
endpoints
.
Subsets
[
i
]
for
i
:=
range
ss
.
Ports
{
port
:=
&
ss
.
Ports
[
i
]
for
i
:=
range
ss
.
Addresses
{
addr
:=
&
ss
.
Addresses
[
i
]
portsToEndpoints
[
port
.
Name
]
=
append
(
portsToEndpoints
[
port
.
Name
],
hostPortPair
{
addr
.
IP
,
int
(
port
.
Port
)})
// Ignore the protocol field - we'll get that from the Service objects.
}
}
}
return
portsToEndpoints
}
func
(
lb
*
LoadBalancerRR
)
OnEndpointsAdd
(
endpoints
*
api
.
Endpoints
)
{
portsToEndpoints
:=
buildPortsToEndpointsMap
(
endpoints
)
lb
.
lock
.
Lock
()
defer
lb
.
lock
.
Unlock
()
for
portname
:=
range
portsToEndpoints
{
svcPort
:=
proxy
.
ServicePortName
{
NamespacedName
:
types
.
NamespacedName
{
Namespace
:
endpoints
.
Namespace
,
Name
:
endpoints
.
Name
},
Port
:
portname
}
newEndpoints
:=
flattenValidEndpoints
(
portsToEndpoints
[
portname
])
state
,
exists
:=
lb
.
services
[
svcPort
]
if
!
exists
||
state
==
nil
||
len
(
newEndpoints
)
>
0
{
glog
.
V
(
1
)
.
Infof
(
"LoadBalancerRR: Setting endpoints for %s to %+v"
,
svcPort
,
newEndpoints
)
lb
.
updateAffinityMap
(
svcPort
,
newEndpoints
)
// OnEndpointsAdd can be called without NewService being called externally.
// To be safe we will call it here. A new service will only be created
// if one does not already exist. The affinity will be updated
// later, once NewService is called.
state
=
lb
.
newServiceInternal
(
svcPort
,
api
.
ServiceAffinity
(
""
),
0
)
state
.
endpoints
=
slice
.
ShuffleStrings
(
newEndpoints
)
// Reset the round-robin index.
state
.
index
=
0
}
}
}
func
(
lb
*
LoadBalancerRR
)
OnEndpointsUpdate
(
oldEndpoints
,
endpoints
*
api
.
Endpoints
)
{
portsToEndpoints
:=
buildPortsToEndpointsMap
(
endpoints
)
oldPortsToEndpoints
:=
buildPortsToEndpointsMap
(
oldEndpoints
)
registeredEndpoints
:=
make
(
map
[
proxy
.
ServicePortName
]
bool
)
registeredEndpoints
:=
make
(
map
[
proxy
.
ServicePortName
]
bool
)
lb
.
lock
.
Lock
()
lb
.
lock
.
Lock
()
defer
lb
.
lock
.
Unlock
()
defer
lb
.
lock
.
Unlock
()
// Update endpoints for services.
for
portname
:=
range
portsToEndpoints
{
for
i
:=
range
allEndpoints
{
svcPort
:=
proxy
.
ServicePortName
{
NamespacedName
:
types
.
NamespacedName
{
Namespace
:
endpoints
.
Namespace
,
Name
:
endpoints
.
Name
},
Port
:
portname
}
// svcEndpoints object should NOT be modified.
newEndpoints
:=
flattenValidEndpoints
(
portsToEndpoints
[
portname
])
svcEndpoints
:=
allEndpoints
[
i
]
state
,
exists
:=
lb
.
services
[
svcPort
]
// We need to build a map of portname -> all ip:ports for that
curEndpoints
:=
[]
string
{}
// portname. Explode Endpoints.Subsets[*] into this structure.
if
state
!=
nil
{
portsToEndpoints
:=
map
[
string
][]
hostPortPair
{}
curEndpoints
=
state
.
endpoints
for
i
:=
range
svcEndpoints
.
Subsets
{
ss
:=
&
svcEndpoints
.
Subsets
[
i
]
for
i
:=
range
ss
.
Ports
{
port
:=
&
ss
.
Ports
[
i
]
for
i
:=
range
ss
.
Addresses
{
addr
:=
&
ss
.
Addresses
[
i
]
portsToEndpoints
[
port
.
Name
]
=
append
(
portsToEndpoints
[
port
.
Name
],
hostPortPair
{
addr
.
IP
,
int
(
port
.
Port
)})
// Ignore the protocol field - we'll get that from the Service objects.
}
}
}
}
for
portname
:=
range
portsToEndpoints
{
if
!
exists
||
state
==
nil
||
len
(
curEndpoints
)
!=
len
(
newEndpoints
)
||
!
slicesEquiv
(
slice
.
CopyStrings
(
curEndpoints
),
newEndpoints
)
{
svcPort
:=
proxy
.
ServicePortName
{
NamespacedName
:
types
.
NamespacedName
{
Namespace
:
svcEndpoints
.
Namespace
,
Name
:
svcEndpoints
.
Name
},
Port
:
portname
}
glog
.
V
(
1
)
.
Infof
(
"LoadBalancerRR: Setting endpoints for %s to %+v"
,
svcPort
,
newEndpoints
)
state
,
exists
:=
lb
.
services
[
svcPort
]
lb
.
updateAffinityMap
(
svcPort
,
newEndpoints
)
curEndpoints
:=
[]
string
{}
// OnEndpointsUpdate can be called without NewService being called externally.
if
state
!=
nil
{
// To be safe we will call it here. A new service will only be created
curEndpoints
=
state
.
endpoints
// if one does not already exist. The affinity will be updated
}
// later, once NewService is called.
newEndpoints
:=
flattenValidEndpoints
(
portsToEndpoints
[
portname
])
state
=
lb
.
newServiceInternal
(
svcPort
,
api
.
ServiceAffinity
(
""
),
0
)
state
.
endpoints
=
slice
.
ShuffleStrings
(
newEndpoints
)
if
!
exists
||
state
==
nil
||
len
(
curEndpoints
)
!=
len
(
newEndpoints
)
||
!
slicesEquiv
(
slice
.
CopyStrings
(
curEndpoints
),
newEndpoints
)
{
glog
.
V
(
1
)
.
Infof
(
"LoadBalancerRR: Setting endpoints for %s to %+v"
,
svcPort
,
newEndpoints
)
// Reset the round-robin index.
lb
.
updateAffinityMap
(
svcPort
,
newEndpoints
)
state
.
index
=
0
// OnEndpointsUpdate can be called without NewService being called externally.
// To be safe we will call it here. A new service will only be created
// if one does not already exist. The affinity will be updated
// later, once NewService is called.
state
=
lb
.
newServiceInternal
(
svcPort
,
api
.
ServiceAffinity
(
""
),
0
)
state
.
endpoints
=
slice
.
ShuffleStrings
(
newEndpoints
)
// Reset the round-robin index.
state
.
index
=
0
}
registeredEndpoints
[
svcPort
]
=
true
}
}
registeredEndpoints
[
svcPort
]
=
true
}
}
// Remove endpoints missing from the update.
for
k
:=
range
lb
.
services
{
// Now remove all endpoints missing from the update.
if
_
,
exists
:=
registeredEndpoints
[
k
];
!
exists
{
for
portname
:=
range
oldPortsToEndpoints
{
glog
.
V
(
2
)
.
Infof
(
"LoadBalancerRR: Removing endpoints for %s"
,
k
)
svcPort
:=
proxy
.
ServicePortName
{
NamespacedName
:
types
.
NamespacedName
{
Namespace
:
oldEndpoints
.
Namespace
,
Name
:
oldEndpoints
.
Name
},
Port
:
portname
}
if
_
,
exists
:=
registeredEndpoints
[
svcPort
];
!
exists
{
glog
.
V
(
2
)
.
Infof
(
"LoadBalancerRR: Removing endpoints for %s"
,
svcPort
)
// Reset but don't delete.
// Reset but don't delete.
state
:=
lb
.
services
[
k
]
state
:=
lb
.
services
[
svcPort
]
state
.
endpoints
=
[]
string
{}
state
.
endpoints
=
[]
string
{}
state
.
index
=
0
state
.
index
=
0
state
.
affinity
.
affinityMap
=
map
[
string
]
*
affinityState
{}
state
.
affinity
.
affinityMap
=
map
[
string
]
*
affinityState
{}
...
@@ -309,6 +336,27 @@ func (lb *LoadBalancerRR) OnEndpointsUpdate(allEndpoints []*api.Endpoints) {
...
@@ -309,6 +336,27 @@ func (lb *LoadBalancerRR) OnEndpointsUpdate(allEndpoints []*api.Endpoints) {
}
}
}
}
func
(
lb
*
LoadBalancerRR
)
OnEndpointsDelete
(
endpoints
*
api
.
Endpoints
)
{
portsToEndpoints
:=
buildPortsToEndpointsMap
(
endpoints
)
lb
.
lock
.
Lock
()
defer
lb
.
lock
.
Unlock
()
for
portname
:=
range
portsToEndpoints
{
svcPort
:=
proxy
.
ServicePortName
{
NamespacedName
:
types
.
NamespacedName
{
Namespace
:
endpoints
.
Namespace
,
Name
:
endpoints
.
Name
},
Port
:
portname
}
glog
.
V
(
2
)
.
Infof
(
"LoadBalancerRR: Removing endpoints for %s"
,
svcPort
)
// If the service is still around, reset but don't delete.
if
state
,
ok
:=
lb
.
services
[
svcPort
];
ok
{
state
.
endpoints
=
[]
string
{}
state
.
index
=
0
state
.
affinity
.
affinityMap
=
map
[
string
]
*
affinityState
{}
}
}
}
func
(
lb
*
LoadBalancerRR
)
OnEndpointsSynced
()
{
}
// Tests whether two slices are equivalent. This sorts both slices in-place.
// Tests whether two slices are equivalent. This sorts both slices in-place.
func
slicesEquiv
(
lhs
,
rhs
[]
string
)
bool
{
func
slicesEquiv
(
lhs
,
rhs
[]
string
)
bool
{
if
len
(
lhs
)
!=
len
(
rhs
)
{
if
len
(
lhs
)
!=
len
(
rhs
)
{
...
...
pkg/proxy/userspace/roundrobin_test.go
View file @
c5cbdbe3
This diff is collapsed.
Click to expand it.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment