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
3f28074c
Commit
3f28074c
authored
Jun 12, 2017
by
xiangpengzhao
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Validate if service has duplicate port
parent
1bc56825
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
31 additions
and
0 deletions
+31
-0
validation.go
pkg/api/validation/validation.go
+13
-0
validation_test.go
pkg/api/validation/validation_test.go
+18
-0
No files found.
pkg/api/validation/validation.go
View file @
3f28074c
...
@@ -2913,6 +2913,19 @@ func ValidateService(service *api.Service) field.ErrorList {
...
@@ -2913,6 +2913,19 @@ func ValidateService(service *api.Service) field.ErrorList {
nodePorts
[
key
]
=
true
nodePorts
[
key
]
=
true
}
}
// Check for duplicate Ports, considering (protocol,port) pairs
portsPath
=
specPath
.
Child
(
"ports"
)
ports
:=
make
(
map
[
api
.
ServicePort
]
bool
)
for
i
,
port
:=
range
service
.
Spec
.
Ports
{
portPath
:=
portsPath
.
Index
(
i
)
key
:=
api
.
ServicePort
{
Protocol
:
port
.
Protocol
,
Port
:
port
.
Port
}
_
,
found
:=
ports
[
key
]
if
found
{
allErrs
=
append
(
allErrs
,
field
.
Duplicate
(
portPath
,
key
))
}
ports
[
key
]
=
true
}
// Check for duplicate TargetPort
// Check for duplicate TargetPort
portsPath
=
specPath
.
Child
(
"ports"
)
portsPath
=
specPath
.
Child
(
"ports"
)
targetPorts
:=
make
(
map
[
api
.
ServicePort
]
bool
)
targetPorts
:=
make
(
map
[
api
.
ServicePort
]
bool
)
...
...
pkg/api/validation/validation_test.go
View file @
3f28074c
...
@@ -6303,6 +6303,24 @@ func TestValidateService(t *testing.T) {
...
@@ -6303,6 +6303,24 @@ func TestValidateService(t *testing.T) {
numErrs
:
0
,
numErrs
:
0
,
},
},
{
{
name
:
"invalid duplicate ports (with same protocol)"
,
tweakSvc
:
func
(
s
*
api
.
Service
)
{
s
.
Spec
.
Type
=
api
.
ServiceTypeClusterIP
s
.
Spec
.
Ports
=
append
(
s
.
Spec
.
Ports
,
api
.
ServicePort
{
Name
:
"q"
,
Port
:
12345
,
Protocol
:
"TCP"
,
TargetPort
:
intstr
.
FromInt
(
8080
)})
s
.
Spec
.
Ports
=
append
(
s
.
Spec
.
Ports
,
api
.
ServicePort
{
Name
:
"r"
,
Port
:
12345
,
Protocol
:
"TCP"
,
TargetPort
:
intstr
.
FromInt
(
80
)})
},
numErrs
:
1
,
},
{
name
:
"valid duplicate ports (with different protocols)"
,
tweakSvc
:
func
(
s
*
api
.
Service
)
{
s
.
Spec
.
Type
=
api
.
ServiceTypeClusterIP
s
.
Spec
.
Ports
=
append
(
s
.
Spec
.
Ports
,
api
.
ServicePort
{
Name
:
"q"
,
Port
:
12345
,
Protocol
:
"TCP"
,
TargetPort
:
intstr
.
FromInt
(
8080
)})
s
.
Spec
.
Ports
=
append
(
s
.
Spec
.
Ports
,
api
.
ServicePort
{
Name
:
"r"
,
Port
:
12345
,
Protocol
:
"UDP"
,
TargetPort
:
intstr
.
FromInt
(
80
)})
},
numErrs
:
0
,
},
{
name
:
"invalid duplicate targetports (number with same protocol)"
,
name
:
"invalid duplicate targetports (number with same protocol)"
,
tweakSvc
:
func
(
s
*
api
.
Service
)
{
tweakSvc
:
func
(
s
*
api
.
Service
)
{
s
.
Spec
.
Type
=
api
.
ServiceTypeClusterIP
s
.
Spec
.
Type
=
api
.
ServiceTypeClusterIP
...
...
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