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
895da2cd
Commit
895da2cd
authored
Jul 06, 2017
by
xiangpengzhao
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Remove shouldAssignNodePorts logic in initNodePort; add test cases.
parent
dc7fb0c9
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
190 additions
and
26 deletions
+190
-26
rest.go
pkg/registry/core/service/rest.go
+9
-24
rest_test.go
pkg/registry/core/service/rest_test.go
+181
-2
No files found.
pkg/registry/core/service/rest.go
View file @
895da2cd
...
@@ -106,7 +106,7 @@ func (rs *REST) Create(ctx genericapirequest.Context, obj runtime.Object, includ
...
@@ -106,7 +106,7 @@ func (rs *REST) Create(ctx genericapirequest.Context, obj runtime.Object, includ
defer
nodePortOp
.
Finish
()
defer
nodePortOp
.
Finish
()
if
service
.
Spec
.
Type
==
api
.
ServiceTypeNodePort
||
service
.
Spec
.
Type
==
api
.
ServiceTypeLoadBalancer
{
if
service
.
Spec
.
Type
==
api
.
ServiceTypeNodePort
||
service
.
Spec
.
Type
==
api
.
ServiceTypeLoadBalancer
{
if
err
:=
rs
.
initNodePort
(
service
,
nodePortOp
);
err
!=
nil
{
if
err
:=
rs
.
initNodePort
s
(
service
,
nodePortOp
);
err
!=
nil
{
return
nil
,
err
return
nil
,
err
}
}
}
}
...
@@ -333,11 +333,11 @@ func (rs *REST) Update(ctx genericapirequest.Context, name string, objInfo rest.
...
@@ -333,11 +333,11 @@ func (rs *REST) Update(ctx genericapirequest.Context, name string, objInfo rest.
// Update service from NodePort or LoadBalancer to ExternalName or ClusterIP, should release NodePort if exists.
// Update service from NodePort or LoadBalancer to ExternalName or ClusterIP, should release NodePort if exists.
if
(
oldService
.
Spec
.
Type
==
api
.
ServiceTypeNodePort
||
oldService
.
Spec
.
Type
==
api
.
ServiceTypeLoadBalancer
)
&&
if
(
oldService
.
Spec
.
Type
==
api
.
ServiceTypeNodePort
||
oldService
.
Spec
.
Type
==
api
.
ServiceTypeLoadBalancer
)
&&
(
service
.
Spec
.
Type
==
api
.
ServiceTypeExternalName
||
service
.
Spec
.
Type
==
api
.
ServiceTypeClusterIP
)
{
(
service
.
Spec
.
Type
==
api
.
ServiceTypeExternalName
||
service
.
Spec
.
Type
==
api
.
ServiceTypeClusterIP
)
{
rs
.
releaseNodePort
(
oldService
,
nodePortOp
)
rs
.
releaseNodePort
s
(
oldService
,
nodePortOp
)
}
}
// Update service from any type to NodePort or LoadBalancer, should update NodePort.
// Update service from any type to NodePort or LoadBalancer, should update NodePort.
if
service
.
Spec
.
Type
==
api
.
ServiceTypeNodePort
||
service
.
Spec
.
Type
==
api
.
ServiceTypeLoadBalancer
{
if
service
.
Spec
.
Type
==
api
.
ServiceTypeNodePort
||
service
.
Spec
.
Type
==
api
.
ServiceTypeLoadBalancer
{
if
err
:=
rs
.
updateNodePort
(
oldService
,
service
,
nodePortOp
);
err
!=
nil
{
if
err
:=
rs
.
updateNodePort
s
(
oldService
,
service
,
nodePortOp
);
err
!=
nil
{
return
nil
,
false
,
err
return
nil
,
false
,
err
}
}
}
}
...
@@ -456,22 +456,6 @@ func CollectServiceNodePorts(service *api.Service) []int {
...
@@ -456,22 +456,6 @@ func CollectServiceNodePorts(service *api.Service) []int {
return
servicePorts
return
servicePorts
}
}
func
shouldAssignNodePorts
(
service
*
api
.
Service
)
bool
{
switch
service
.
Spec
.
Type
{
case
api
.
ServiceTypeLoadBalancer
:
return
true
case
api
.
ServiceTypeNodePort
:
return
true
case
api
.
ServiceTypeClusterIP
:
return
false
case
api
.
ServiceTypeExternalName
:
return
false
default
:
glog
.
Errorf
(
"Unknown service type: %v"
,
service
.
Spec
.
Type
)
return
false
}
}
// Loop through the service ports list, find one with the same port number and
// Loop through the service ports list, find one with the same port number and
// NodePort specified, return this NodePort otherwise return 0.
// NodePort specified, return this NodePort otherwise return 0.
func
findRequestedNodePort
(
port
int
,
servicePorts
[]
api
.
ServicePort
)
int
{
func
findRequestedNodePort
(
port
int
,
servicePorts
[]
api
.
ServicePort
)
int
{
...
@@ -534,8 +518,7 @@ func (rs *REST) initClusterIP(service *api.Service) (bool, error) {
...
@@ -534,8 +518,7 @@ func (rs *REST) initClusterIP(service *api.Service) (bool, error) {
return
false
,
nil
return
false
,
nil
}
}
func
(
rs
*
REST
)
initNodePort
(
service
*
api
.
Service
,
nodePortOp
*
portallocator
.
PortAllocationOperation
)
error
{
func
(
rs
*
REST
)
initNodePorts
(
service
*
api
.
Service
,
nodePortOp
*
portallocator
.
PortAllocationOperation
)
error
{
assignNodePorts
:=
shouldAssignNodePorts
(
service
)
svcPortToNodePort
:=
map
[
int
]
int
{}
svcPortToNodePort
:=
map
[
int
]
int
{}
for
i
:=
range
service
.
Spec
.
Ports
{
for
i
:=
range
service
.
Spec
.
Ports
{
servicePort
:=
&
service
.
Spec
.
Ports
[
i
]
servicePort
:=
&
service
.
Spec
.
Ports
[
i
]
...
@@ -554,7 +537,7 @@ func (rs *REST) initNodePort(service *api.Service, nodePortOp *portallocator.Por
...
@@ -554,7 +537,7 @@ func (rs *REST) initNodePort(service *api.Service, nodePortOp *portallocator.Por
}
}
servicePort
.
NodePort
=
int32
(
np
)
servicePort
.
NodePort
=
int32
(
np
)
svcPortToNodePort
[
int
(
servicePort
.
Port
)]
=
np
svcPortToNodePort
[
int
(
servicePort
.
Port
)]
=
np
}
else
if
assignNodePorts
{
}
else
{
nodePort
,
err
:=
nodePortOp
.
AllocateNext
()
nodePort
,
err
:=
nodePortOp
.
AllocateNext
()
if
err
!=
nil
{
if
err
!=
nil
{
// TODO: what error should be returned here? It's not a
// TODO: what error should be returned here? It's not a
...
@@ -566,6 +549,8 @@ func (rs *REST) initNodePort(service *api.Service, nodePortOp *portallocator.Por
...
@@ -566,6 +549,8 @@ func (rs *REST) initNodePort(service *api.Service, nodePortOp *portallocator.Por
svcPortToNodePort
[
int
(
servicePort
.
Port
)]
=
nodePort
svcPortToNodePort
[
int
(
servicePort
.
Port
)]
=
nodePort
}
}
}
else
if
int
(
servicePort
.
NodePort
)
!=
allocatedNodePort
{
}
else
if
int
(
servicePort
.
NodePort
)
!=
allocatedNodePort
{
// TODO(xiangpengzhao): do we need to allocate a new NodePort in this case?
// Note: the current implementation is better, because it saves a NodePort.
if
servicePort
.
NodePort
==
0
{
if
servicePort
.
NodePort
==
0
{
servicePort
.
NodePort
=
int32
(
allocatedNodePort
)
servicePort
.
NodePort
=
int32
(
allocatedNodePort
)
}
else
{
}
else
{
...
@@ -582,7 +567,7 @@ func (rs *REST) initNodePort(service *api.Service, nodePortOp *portallocator.Por
...
@@ -582,7 +567,7 @@ func (rs *REST) initNodePort(service *api.Service, nodePortOp *portallocator.Por
return
nil
return
nil
}
}
func
(
rs
*
REST
)
updateNodePort
(
oldService
,
newService
*
api
.
Service
,
nodePortOp
*
portallocator
.
PortAllocationOperation
)
error
{
func
(
rs
*
REST
)
updateNodePort
s
(
oldService
,
newService
*
api
.
Service
,
nodePortOp
*
portallocator
.
PortAllocationOperation
)
error
{
oldNodePorts
:=
CollectServiceNodePorts
(
oldService
)
oldNodePorts
:=
CollectServiceNodePorts
(
oldService
)
newNodePorts
:=
[]
int
{}
newNodePorts
:=
[]
int
{}
...
@@ -626,7 +611,7 @@ func (rs *REST) updateNodePort(oldService, newService *api.Service, nodePortOp *
...
@@ -626,7 +611,7 @@ func (rs *REST) updateNodePort(oldService, newService *api.Service, nodePortOp *
return
nil
return
nil
}
}
func
(
rs
*
REST
)
releaseNodePort
(
service
*
api
.
Service
,
nodePortOp
*
portallocator
.
PortAllocationOperation
)
{
func
(
rs
*
REST
)
releaseNodePort
s
(
service
*
api
.
Service
,
nodePortOp
*
portallocator
.
PortAllocationOperation
)
{
nodePorts
:=
CollectServiceNodePorts
(
service
)
nodePorts
:=
CollectServiceNodePorts
(
service
)
for
_
,
nodePort
:=
range
nodePorts
{
for
_
,
nodePort
:=
range
nodePorts
{
...
...
pkg/registry/core/service/rest_test.go
View file @
895da2cd
...
@@ -1362,7 +1362,186 @@ func TestInitClusterIP(t *testing.T) {
...
@@ -1362,7 +1362,186 @@ func TestInitClusterIP(t *testing.T) {
}
}
}
}
func
TestUpdateNodePort
(
t
*
testing
.
T
)
{
func
TestInitNodePorts
(
t
*
testing
.
T
)
{
storage
,
_
:=
NewTestREST
(
t
,
nil
)
nodePortOp
:=
portallocator
.
StartOperation
(
storage
.
serviceNodePorts
)
defer
nodePortOp
.
Finish
()
testCases
:=
[]
struct
{
name
string
service
*
api
.
Service
expectSpecifiedNodePorts
[]
int
}{
{
name
:
"Service doesn't have specified NodePort"
,
service
:
&
api
.
Service
{
ObjectMeta
:
metav1
.
ObjectMeta
{
Name
:
"foo"
},
Spec
:
api
.
ServiceSpec
{
Selector
:
map
[
string
]
string
{
"bar"
:
"baz"
},
Type
:
api
.
ServiceTypeNodePort
,
Ports
:
[]
api
.
ServicePort
{
{
Name
:
"port-tcp"
,
Port
:
53
,
TargetPort
:
intstr
.
FromInt
(
6502
),
Protocol
:
api
.
ProtocolTCP
,
},
},
},
},
expectSpecifiedNodePorts
:
[]
int
{},
},
{
name
:
"Service has one specified NodePort"
,
service
:
&
api
.
Service
{
ObjectMeta
:
metav1
.
ObjectMeta
{
Name
:
"foo"
},
Spec
:
api
.
ServiceSpec
{
Selector
:
map
[
string
]
string
{
"bar"
:
"baz"
},
Type
:
api
.
ServiceTypeNodePort
,
Ports
:
[]
api
.
ServicePort
{{
Name
:
"port-tcp"
,
Port
:
53
,
TargetPort
:
intstr
.
FromInt
(
6502
),
Protocol
:
api
.
ProtocolTCP
,
NodePort
:
30053
,
}},
},
},
expectSpecifiedNodePorts
:
[]
int
{
30053
},
},
{
name
:
"Service has two same ports with different protocols and specifies same NodePorts"
,
service
:
&
api
.
Service
{
ObjectMeta
:
metav1
.
ObjectMeta
{
Name
:
"foo"
},
Spec
:
api
.
ServiceSpec
{
Selector
:
map
[
string
]
string
{
"bar"
:
"baz"
},
Type
:
api
.
ServiceTypeNodePort
,
Ports
:
[]
api
.
ServicePort
{
{
Name
:
"port-tcp"
,
Port
:
53
,
TargetPort
:
intstr
.
FromInt
(
6502
),
Protocol
:
api
.
ProtocolTCP
,
NodePort
:
30054
,
},
{
Name
:
"port-udp"
,
Port
:
53
,
TargetPort
:
intstr
.
FromInt
(
6502
),
Protocol
:
api
.
ProtocolUDP
,
NodePort
:
30054
,
},
},
},
},
expectSpecifiedNodePorts
:
[]
int
{
30054
,
30054
},
},
{
name
:
"Service has two same ports with different protocols and specifies different NodePorts"
,
service
:
&
api
.
Service
{
ObjectMeta
:
metav1
.
ObjectMeta
{
Name
:
"foo"
},
Spec
:
api
.
ServiceSpec
{
Selector
:
map
[
string
]
string
{
"bar"
:
"baz"
},
Type
:
api
.
ServiceTypeNodePort
,
Ports
:
[]
api
.
ServicePort
{
{
Name
:
"port-tcp"
,
Port
:
53
,
TargetPort
:
intstr
.
FromInt
(
6502
),
Protocol
:
api
.
ProtocolTCP
,
NodePort
:
30055
,
},
{
Name
:
"port-udp"
,
Port
:
53
,
TargetPort
:
intstr
.
FromInt
(
6502
),
Protocol
:
api
.
ProtocolUDP
,
NodePort
:
30056
,
},
},
},
},
expectSpecifiedNodePorts
:
[]
int
{
30055
,
30056
},
},
{
name
:
"Service has two different ports with different protocols and specifies different NodePorts"
,
service
:
&
api
.
Service
{
ObjectMeta
:
metav1
.
ObjectMeta
{
Name
:
"foo"
},
Spec
:
api
.
ServiceSpec
{
Selector
:
map
[
string
]
string
{
"bar"
:
"baz"
},
Type
:
api
.
ServiceTypeNodePort
,
Ports
:
[]
api
.
ServicePort
{
{
Name
:
"port-tcp"
,
Port
:
53
,
TargetPort
:
intstr
.
FromInt
(
6502
),
Protocol
:
api
.
ProtocolTCP
,
NodePort
:
30057
,
},
{
Name
:
"port-udp"
,
Port
:
54
,
TargetPort
:
intstr
.
FromInt
(
6502
),
Protocol
:
api
.
ProtocolUDP
,
NodePort
:
30058
,
},
},
},
},
expectSpecifiedNodePorts
:
[]
int
{
30057
,
30058
},
},
{
name
:
"Service has two same ports with different protocols but only specifies one NodePort"
,
service
:
&
api
.
Service
{
ObjectMeta
:
metav1
.
ObjectMeta
{
Name
:
"foo"
},
Spec
:
api
.
ServiceSpec
{
Selector
:
map
[
string
]
string
{
"bar"
:
"baz"
},
Type
:
api
.
ServiceTypeNodePort
,
Ports
:
[]
api
.
ServicePort
{
{
Name
:
"port-tcp"
,
Port
:
53
,
TargetPort
:
intstr
.
FromInt
(
6502
),
Protocol
:
api
.
ProtocolTCP
,
NodePort
:
30059
,
},
{
Name
:
"port-udp"
,
Port
:
53
,
TargetPort
:
intstr
.
FromInt
(
6502
),
Protocol
:
api
.
ProtocolUDP
,
},
},
},
},
expectSpecifiedNodePorts
:
[]
int
{
30059
,
30059
},
},
}
for
_
,
test
:=
range
testCases
{
err
:=
storage
.
initNodePorts
(
test
.
service
,
nodePortOp
)
if
err
!=
nil
{
t
.
Errorf
(
"%q: unexpected error: %v"
,
test
.
name
,
err
)
continue
}
_
=
nodePortOp
.
Commit
()
serviceNodePorts
:=
CollectServiceNodePorts
(
test
.
service
)
if
len
(
test
.
expectSpecifiedNodePorts
)
==
0
{
for
_
,
nodePort
:=
range
serviceNodePorts
{
if
!
storage
.
serviceNodePorts
.
Has
(
nodePort
)
{
t
.
Errorf
(
"%q: unexpected NodePort %d, out of range"
,
test
.
name
,
nodePort
)
}
}
}
else
if
!
reflect
.
DeepEqual
(
serviceNodePorts
,
test
.
expectSpecifiedNodePorts
)
{
t
.
Errorf
(
"%q: expected NodePorts %v, but got %v"
,
test
.
name
,
test
.
expectSpecifiedNodePorts
,
serviceNodePorts
)
}
}
}
func
TestUpdateNodePorts
(
t
*
testing
.
T
)
{
storage
,
_
:=
NewTestREST
(
t
,
nil
)
storage
,
_
:=
NewTestREST
(
t
,
nil
)
nodePortOp
:=
portallocator
.
StartOperation
(
storage
.
serviceNodePorts
)
nodePortOp
:=
portallocator
.
StartOperation
(
storage
.
serviceNodePorts
)
defer
nodePortOp
.
Finish
()
defer
nodePortOp
.
Finish
()
...
@@ -1521,7 +1700,7 @@ func TestUpdateNodePort(t *testing.T) {
...
@@ -1521,7 +1700,7 @@ func TestUpdateNodePort(t *testing.T) {
}
}
for
_
,
test
:=
range
testCases
{
for
_
,
test
:=
range
testCases
{
err
:=
storage
.
updateNodePort
(
test
.
oldService
,
test
.
newService
,
nodePortOp
)
err
:=
storage
.
updateNodePort
s
(
test
.
oldService
,
test
.
newService
,
nodePortOp
)
if
err
!=
nil
{
if
err
!=
nil
{
t
.
Errorf
(
"%q: unexpected error: %v"
,
test
.
name
,
err
)
t
.
Errorf
(
"%q: unexpected error: %v"
,
test
.
name
,
err
)
continue
continue
...
...
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