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
b3846ea7
Commit
b3846ea7
authored
Dec 02, 2015
by
k8s-merge-robot
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #17862 from feihujiang/ensureHeadlessServicePortEqualToTargetPort
Auto commit by PR queue bot
parents
b69018ea
81b095ee
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
37 additions
and
2 deletions
+37
-2
validation.go
pkg/api/validation/validation.go
+10
-2
validation_test.go
pkg/api/validation/validation_test.go
+27
-0
No files found.
pkg/api/validation/validation.go
View file @
b3846ea7
...
...
@@ -1253,9 +1253,11 @@ func ValidateService(service *api.Service) validation.ErrorList {
}
}
}
isHeadlessService
:=
service
.
Spec
.
ClusterIP
==
api
.
ClusterIPNone
allPortNames
:=
sets
.
String
{}
for
i
:=
range
service
.
Spec
.
Ports
{
allErrs
=
append
(
allErrs
,
validateServicePort
(
&
service
.
Spec
.
Ports
[
i
],
len
(
service
.
Spec
.
Ports
)
>
1
,
&
allPortNames
)
.
PrefixIndex
(
i
)
.
Prefix
(
"spec.ports"
)
...
)
allErrs
=
append
(
allErrs
,
validateServicePort
(
&
service
.
Spec
.
Ports
[
i
],
len
(
service
.
Spec
.
Ports
)
>
1
,
isHeadlessService
,
&
allPortNames
)
.
PrefixIndex
(
i
)
.
Prefix
(
"spec.ports"
)
...
)
}
if
service
.
Spec
.
Selector
!=
nil
{
...
...
@@ -1323,7 +1325,7 @@ func ValidateService(service *api.Service) validation.ErrorList {
return
allErrs
}
func
validateServicePort
(
sp
*
api
.
ServicePort
,
requireName
bool
,
allNames
*
sets
.
String
)
validation
.
ErrorList
{
func
validateServicePort
(
sp
*
api
.
ServicePort
,
requireName
,
isHeadlessService
bool
,
allNames
*
sets
.
String
)
validation
.
ErrorList
{
allErrs
:=
validation
.
ErrorList
{}
if
requireName
&&
sp
.
Name
==
""
{
...
...
@@ -1355,6 +1357,12 @@ func validateServicePort(sp *api.ServicePort, requireName bool, allNames *sets.S
allErrs
=
append
(
allErrs
,
validation
.
NewInvalidError
(
"targetPort"
,
sp
.
TargetPort
,
PortNameErrorMsg
))
}
if
isHeadlessService
{
if
sp
.
TargetPort
.
Type
==
intstr
.
String
||
(
sp
.
TargetPort
.
Type
==
intstr
.
Int
&&
sp
.
Port
!=
sp
.
TargetPort
.
IntValue
())
{
allErrs
=
append
(
allErrs
,
validation
.
NewInvalidError
(
"port"
,
sp
.
Port
,
"must be equal to targetPort when clusterIP = None"
))
}
}
return
allErrs
}
...
...
pkg/api/validation/validation_test.go
View file @
b3846ea7
...
...
@@ -1882,6 +1882,33 @@ func TestValidateService(t *testing.T) {
numErrs
:
1
,
},
{
name
:
"valid port headless"
,
tweakSvc
:
func
(
s
*
api
.
Service
)
{
s
.
Spec
.
Ports
[
0
]
.
Port
=
11722
s
.
Spec
.
Ports
[
0
]
.
TargetPort
=
intstr
.
FromInt
(
11722
)
s
.
Spec
.
ClusterIP
=
api
.
ClusterIPNone
},
numErrs
:
0
,
},
{
name
:
"invalid port headless"
,
tweakSvc
:
func
(
s
*
api
.
Service
)
{
s
.
Spec
.
Ports
[
0
]
.
Port
=
11722
s
.
Spec
.
Ports
[
0
]
.
TargetPort
=
intstr
.
FromInt
(
11721
)
s
.
Spec
.
ClusterIP
=
api
.
ClusterIPNone
},
numErrs
:
1
,
},
{
name
:
"invalid port headless"
,
tweakSvc
:
func
(
s
*
api
.
Service
)
{
s
.
Spec
.
Ports
[
0
]
.
Port
=
11722
s
.
Spec
.
Ports
[
0
]
.
TargetPort
=
intstr
.
FromString
(
"target"
)
s
.
Spec
.
ClusterIP
=
api
.
ClusterIPNone
},
numErrs
:
1
,
},
{
name
:
"invalid publicIPs localhost"
,
tweakSvc
:
func
(
s
*
api
.
Service
)
{
s
.
Spec
.
ExternalIPs
=
[]
string
{
"127.0.0.1"
}
...
...
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