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
50b2b305
Commit
50b2b305
authored
Feb 26, 2018
by
Dan Winship
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Factor out duplicated NetworkPolicy validation code
parent
456ebf5d
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
53 additions
and
64 deletions
+53
-64
validation.go
pkg/apis/networking/validation/validation.go
+53
-64
No files found.
pkg/apis/networking/validation/validation.go
View file @
50b2b305
...
...
@@ -33,6 +33,55 @@ func ValidateNetworkPolicyName(name string, prefix bool) []string {
return
apivalidation
.
NameIsDNSSubdomain
(
name
,
prefix
)
}
// ValidateNetworkPolicyPort validates a NetworkPolicyPort
func
ValidateNetworkPolicyPort
(
port
*
networking
.
NetworkPolicyPort
,
portPath
*
field
.
Path
)
field
.
ErrorList
{
allErrs
:=
field
.
ErrorList
{}
if
port
.
Protocol
!=
nil
&&
*
port
.
Protocol
!=
api
.
ProtocolTCP
&&
*
port
.
Protocol
!=
api
.
ProtocolUDP
{
allErrs
=
append
(
allErrs
,
field
.
NotSupported
(
portPath
.
Child
(
"protocol"
),
*
port
.
Protocol
,
[]
string
{
string
(
api
.
ProtocolTCP
),
string
(
api
.
ProtocolUDP
)}))
}
if
port
.
Port
!=
nil
{
if
port
.
Port
.
Type
==
intstr
.
Int
{
for
_
,
msg
:=
range
validation
.
IsValidPortNum
(
int
(
port
.
Port
.
IntVal
))
{
allErrs
=
append
(
allErrs
,
field
.
Invalid
(
portPath
.
Child
(
"port"
),
port
.
Port
.
IntVal
,
msg
))
}
}
else
{
for
_
,
msg
:=
range
validation
.
IsValidPortName
(
port
.
Port
.
StrVal
)
{
allErrs
=
append
(
allErrs
,
field
.
Invalid
(
portPath
.
Child
(
"port"
),
port
.
Port
.
StrVal
,
msg
))
}
}
}
return
allErrs
}
// ValidateNetworkPolicyPeer validates a NetworkPolicyPeer
func
ValidateNetworkPolicyPeer
(
peer
*
networking
.
NetworkPolicyPeer
,
peerPath
*
field
.
Path
)
field
.
ErrorList
{
allErrs
:=
field
.
ErrorList
{}
numPeers
:=
0
if
peer
.
PodSelector
!=
nil
{
numPeers
++
allErrs
=
append
(
allErrs
,
unversionedvalidation
.
ValidateLabelSelector
(
peer
.
PodSelector
,
peerPath
.
Child
(
"podSelector"
))
...
)
}
if
peer
.
NamespaceSelector
!=
nil
{
numPeers
++
allErrs
=
append
(
allErrs
,
unversionedvalidation
.
ValidateLabelSelector
(
peer
.
NamespaceSelector
,
peerPath
.
Child
(
"namespaceSelector"
))
...
)
}
if
peer
.
IPBlock
!=
nil
{
numPeers
++
allErrs
=
append
(
allErrs
,
ValidateIPBlock
(
peer
.
IPBlock
,
peerPath
.
Child
(
"ipBlock"
))
...
)
}
if
numPeers
==
0
{
allErrs
=
append
(
allErrs
,
field
.
Required
(
peerPath
,
"must specify a peer"
))
}
else
if
numPeers
>
1
{
allErrs
=
append
(
allErrs
,
field
.
Forbidden
(
peerPath
,
"may not specify more than 1 peer"
))
}
return
allErrs
}
// ValidateNetworkPolicySpec tests if required fields in the networkpolicy spec are set.
func
ValidateNetworkPolicySpec
(
spec
*
networking
.
NetworkPolicySpec
,
fldPath
*
field
.
Path
)
field
.
ErrorList
{
allErrs
:=
field
.
ErrorList
{}
...
...
@@ -43,41 +92,11 @@ func ValidateNetworkPolicySpec(spec *networking.NetworkPolicySpec, fldPath *fiel
ingressPath
:=
fldPath
.
Child
(
"ingress"
)
.
Index
(
i
)
for
i
,
port
:=
range
ingress
.
Ports
{
portPath
:=
ingressPath
.
Child
(
"ports"
)
.
Index
(
i
)
if
port
.
Protocol
!=
nil
&&
*
port
.
Protocol
!=
api
.
ProtocolTCP
&&
*
port
.
Protocol
!=
api
.
ProtocolUDP
{
allErrs
=
append
(
allErrs
,
field
.
NotSupported
(
portPath
.
Child
(
"protocol"
),
*
port
.
Protocol
,
[]
string
{
string
(
api
.
ProtocolTCP
),
string
(
api
.
ProtocolUDP
)}))
}
if
port
.
Port
!=
nil
{
if
port
.
Port
.
Type
==
intstr
.
Int
{
for
_
,
msg
:=
range
validation
.
IsValidPortNum
(
int
(
port
.
Port
.
IntVal
))
{
allErrs
=
append
(
allErrs
,
field
.
Invalid
(
portPath
.
Child
(
"port"
),
port
.
Port
.
IntVal
,
msg
))
}
}
else
{
for
_
,
msg
:=
range
validation
.
IsValidPortName
(
port
.
Port
.
StrVal
)
{
allErrs
=
append
(
allErrs
,
field
.
Invalid
(
portPath
.
Child
(
"port"
),
port
.
Port
.
StrVal
,
msg
))
}
}
}
allErrs
=
append
(
allErrs
,
ValidateNetworkPolicyPort
(
&
port
,
portPath
)
...
)
}
for
i
,
from
:=
range
ingress
.
From
{
fromPath
:=
ingressPath
.
Child
(
"from"
)
.
Index
(
i
)
numFroms
:=
0
if
from
.
PodSelector
!=
nil
{
numFroms
++
allErrs
=
append
(
allErrs
,
unversionedvalidation
.
ValidateLabelSelector
(
from
.
PodSelector
,
fromPath
.
Child
(
"podSelector"
))
...
)
}
if
from
.
NamespaceSelector
!=
nil
{
numFroms
++
allErrs
=
append
(
allErrs
,
unversionedvalidation
.
ValidateLabelSelector
(
from
.
NamespaceSelector
,
fromPath
.
Child
(
"namespaceSelector"
))
...
)
}
if
from
.
IPBlock
!=
nil
{
numFroms
++
allErrs
=
append
(
allErrs
,
ValidateIPBlock
(
from
.
IPBlock
,
fromPath
.
Child
(
"ipBlock"
))
...
)
}
if
numFroms
==
0
{
allErrs
=
append
(
allErrs
,
field
.
Required
(
fromPath
,
"must specify a from type"
))
}
else
if
numFroms
>
1
{
allErrs
=
append
(
allErrs
,
field
.
Forbidden
(
fromPath
,
"may not specify more than 1 from type"
))
}
allErrs
=
append
(
allErrs
,
ValidateNetworkPolicyPeer
(
&
from
,
fromPath
)
...
)
}
}
// Validate egress rules
...
...
@@ -85,41 +104,11 @@ func ValidateNetworkPolicySpec(spec *networking.NetworkPolicySpec, fldPath *fiel
egressPath
:=
fldPath
.
Child
(
"egress"
)
.
Index
(
i
)
for
i
,
port
:=
range
egress
.
Ports
{
portPath
:=
egressPath
.
Child
(
"ports"
)
.
Index
(
i
)
if
port
.
Protocol
!=
nil
&&
*
port
.
Protocol
!=
api
.
ProtocolTCP
&&
*
port
.
Protocol
!=
api
.
ProtocolUDP
{
allErrs
=
append
(
allErrs
,
field
.
NotSupported
(
portPath
.
Child
(
"protocol"
),
*
port
.
Protocol
,
[]
string
{
string
(
api
.
ProtocolTCP
),
string
(
api
.
ProtocolUDP
)}))
}
if
port
.
Port
!=
nil
{
if
port
.
Port
.
Type
==
intstr
.
Int
{
for
_
,
msg
:=
range
validation
.
IsValidPortNum
(
int
(
port
.
Port
.
IntVal
))
{
allErrs
=
append
(
allErrs
,
field
.
Invalid
(
portPath
.
Child
(
"port"
),
port
.
Port
.
IntVal
,
msg
))
}
}
else
{
for
_
,
msg
:=
range
validation
.
IsValidPortName
(
port
.
Port
.
StrVal
)
{
allErrs
=
append
(
allErrs
,
field
.
Invalid
(
portPath
.
Child
(
"port"
),
port
.
Port
.
StrVal
,
msg
))
}
}
}
allErrs
=
append
(
allErrs
,
ValidateNetworkPolicyPort
(
&
port
,
portPath
)
...
)
}
for
i
,
to
:=
range
egress
.
To
{
toPath
:=
egressPath
.
Child
(
"to"
)
.
Index
(
i
)
numTo
:=
0
if
to
.
PodSelector
!=
nil
{
numTo
++
allErrs
=
append
(
allErrs
,
unversionedvalidation
.
ValidateLabelSelector
(
to
.
PodSelector
,
toPath
.
Child
(
"podSelector"
))
...
)
}
if
to
.
NamespaceSelector
!=
nil
{
numTo
++
allErrs
=
append
(
allErrs
,
unversionedvalidation
.
ValidateLabelSelector
(
to
.
NamespaceSelector
,
toPath
.
Child
(
"namespaceSelector"
))
...
)
}
if
to
.
IPBlock
!=
nil
{
numTo
++
allErrs
=
append
(
allErrs
,
ValidateIPBlock
(
to
.
IPBlock
,
toPath
.
Child
(
"ipBlock"
))
...
)
}
if
numTo
==
0
{
allErrs
=
append
(
allErrs
,
field
.
Required
(
toPath
,
"must specify a to type"
))
}
else
if
numTo
>
1
{
allErrs
=
append
(
allErrs
,
field
.
Forbidden
(
toPath
,
"may not specify more than 1 to type"
))
}
allErrs
=
append
(
allErrs
,
ValidateNetworkPolicyPeer
(
&
to
,
toPath
)
...
)
}
}
// Validate PolicyTypes
...
...
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