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
f6fb72ec
Commit
f6fb72ec
authored
Jun 17, 2015
by
derekwaynecarr
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Admission control attributes has access to resource name
parent
5e258279
Hide whitespace changes
Inline
Side-by-side
Showing
12 changed files
with
71 additions
and
56 deletions
+71
-56
attributes.go
pkg/admission/attributes.go
+7
-1
chain_test.go
pkg/admission/chain_test.go
+1
-1
interfaces.go
pkg/admission/interfaces.go
+4
-0
resthandler.go
pkg/apiserver/resthandler.go
+10
-5
admission_test.go
plugin/pkg/admission/deny/admission_test.go
+1
-1
admission_test.go
plugin/pkg/admission/exec/denyprivileged/admission_test.go
+1
-1
admission_test.go
plugin/pkg/admission/limitranger/admission_test.go
+2
-2
admission_test.go
...n/pkg/admission/namespace/autoprovision/admission_test.go
+4
-4
admission_test.go
plugin/pkg/admission/namespace/lifecycle/admission_test.go
+4
-4
admission_test.go
plugin/pkg/admission/resourcequota/admission_test.go
+17
-17
admission_test.go
...in/pkg/admission/securitycontext/scdeny/admission_test.go
+2
-2
admission_test.go
plugin/pkg/admission/serviceaccount/admission_test.go
+18
-18
No files found.
pkg/admission/attributes.go
View file @
f6fb72ec
...
...
@@ -24,6 +24,7 @@ import (
type
attributesRecord
struct
{
kind
string
namespace
string
name
string
resource
string
subresource
string
operation
Operation
...
...
@@ -31,10 +32,11 @@ type attributesRecord struct {
userInfo
user
.
Info
}
func
NewAttributesRecord
(
object
runtime
.
Object
,
kind
,
namespace
,
resource
,
subresource
string
,
operation
Operation
,
userInfo
user
.
Info
)
Attributes
{
func
NewAttributesRecord
(
object
runtime
.
Object
,
kind
,
namespace
,
name
,
resource
,
subresource
string
,
operation
Operation
,
userInfo
user
.
Info
)
Attributes
{
return
&
attributesRecord
{
kind
:
kind
,
namespace
:
namespace
,
name
:
name
,
resource
:
resource
,
subresource
:
subresource
,
operation
:
operation
,
...
...
@@ -51,6 +53,10 @@ func (record *attributesRecord) GetNamespace() string {
return
record
.
namespace
}
func
(
record
*
attributesRecord
)
GetName
()
string
{
return
record
.
name
}
func
(
record
*
attributesRecord
)
GetResource
()
string
{
return
record
.
resource
}
...
...
pkg/admission/chain_test.go
View file @
f6fb72ec
...
...
@@ -98,7 +98,7 @@ func TestAdmit(t *testing.T) {
},
}
for
_
,
test
:=
range
tests
{
err
:=
test
.
chain
.
Admit
(
NewAttributesRecord
(
nil
,
""
,
""
,
""
,
""
,
test
.
operation
,
nil
))
err
:=
test
.
chain
.
Admit
(
NewAttributesRecord
(
nil
,
""
,
""
,
""
,
""
,
""
,
test
.
operation
,
nil
))
accepted
:=
(
err
==
nil
)
if
accepted
!=
test
.
accept
{
t
.
Errorf
(
"%s: unexpected result of admit call: %v
\n
"
,
test
.
name
,
accepted
)
...
...
pkg/admission/interfaces.go
View file @
f6fb72ec
...
...
@@ -24,6 +24,10 @@ import (
// Attributes is an interface used by AdmissionController to get information about a request
// that is used to make an admission decision.
type
Attributes
interface
{
// GetName returns the name of the object as presented in the request. On a CREATE operation, the client
// may omit name and rely on the server to generate the name. If that is the case, this method will return
// the empty string
GetName
()
string
// GetNamespace is the namespace associated with the request (if any)
GetNamespace
()
string
// GetResource is the name of the resource being requested. This is not the kind. For example: pods
...
...
pkg/apiserver/resthandler.go
View file @
f6fb72ec
...
...
@@ -165,7 +165,8 @@ func ConnectResource(connecter rest.Connecter, scope RequestScope, admit admissi
ResourcePath
:
restPath
,
}
userInfo
,
_
:=
api
.
UserFrom
(
ctx
)
err
=
admit
.
Admit
(
admission
.
NewAttributesRecord
(
connectRequest
,
scope
.
Kind
,
namespace
,
scope
.
Resource
,
scope
.
Subresource
,
admission
.
Connect
,
userInfo
))
err
=
admit
.
Admit
(
admission
.
NewAttributesRecord
(
connectRequest
,
scope
.
Kind
,
namespace
,
name
,
scope
.
Resource
,
scope
.
Subresource
,
admission
.
Connect
,
userInfo
))
if
err
!=
nil
{
errorJSON
(
err
,
scope
.
Codec
,
w
)
return
...
...
@@ -309,7 +310,8 @@ func createHandler(r rest.NamedCreater, scope RequestScope, typer runtime.Object
if
admit
.
Handles
(
admission
.
Create
)
{
userInfo
,
_
:=
api
.
UserFrom
(
ctx
)
err
=
admit
.
Admit
(
admission
.
NewAttributesRecord
(
obj
,
scope
.
Kind
,
namespace
,
scope
.
Resource
,
scope
.
Subresource
,
admission
.
Create
,
userInfo
))
err
=
admit
.
Admit
(
admission
.
NewAttributesRecord
(
obj
,
scope
.
Kind
,
namespace
,
name
,
scope
.
Resource
,
scope
.
Subresource
,
admission
.
Create
,
userInfo
))
if
err
!=
nil
{
errorJSON
(
err
,
scope
.
Codec
,
w
)
return
...
...
@@ -379,7 +381,8 @@ func PatchResource(r rest.Patcher, scope RequestScope, typer runtime.ObjectTyper
// PATCH requires same permission as UPDATE
if
admit
.
Handles
(
admission
.
Update
)
{
userInfo
,
_
:=
api
.
UserFrom
(
ctx
)
err
=
admit
.
Admit
(
admission
.
NewAttributesRecord
(
obj
,
scope
.
Kind
,
namespace
,
scope
.
Resource
,
scope
.
Subresource
,
admission
.
Update
,
userInfo
))
err
=
admit
.
Admit
(
admission
.
NewAttributesRecord
(
obj
,
scope
.
Kind
,
namespace
,
name
,
scope
.
Resource
,
scope
.
Subresource
,
admission
.
Update
,
userInfo
))
if
err
!=
nil
{
errorJSON
(
err
,
scope
.
Codec
,
w
)
return
...
...
@@ -479,7 +482,8 @@ func UpdateResource(r rest.Updater, scope RequestScope, typer runtime.ObjectType
if
admit
.
Handles
(
admission
.
Update
)
{
userInfo
,
_
:=
api
.
UserFrom
(
ctx
)
err
=
admit
.
Admit
(
admission
.
NewAttributesRecord
(
obj
,
scope
.
Kind
,
namespace
,
scope
.
Resource
,
scope
.
Subresource
,
admission
.
Update
,
userInfo
))
err
=
admit
.
Admit
(
admission
.
NewAttributesRecord
(
obj
,
scope
.
Kind
,
namespace
,
name
,
scope
.
Resource
,
scope
.
Subresource
,
admission
.
Update
,
userInfo
))
if
err
!=
nil
{
errorJSON
(
err
,
scope
.
Codec
,
w
)
return
...
...
@@ -543,7 +547,8 @@ func DeleteResource(r rest.GracefulDeleter, checkBody bool, scope RequestScope,
if
admit
.
Handles
(
admission
.
Delete
)
{
userInfo
,
_
:=
api
.
UserFrom
(
ctx
)
err
=
admit
.
Admit
(
admission
.
NewAttributesRecord
(
nil
,
scope
.
Kind
,
namespace
,
scope
.
Resource
,
scope
.
Subresource
,
admission
.
Delete
,
userInfo
))
err
=
admit
.
Admit
(
admission
.
NewAttributesRecord
(
nil
,
scope
.
Kind
,
namespace
,
name
,
scope
.
Resource
,
scope
.
Subresource
,
admission
.
Delete
,
userInfo
))
if
err
!=
nil
{
errorJSON
(
err
,
scope
.
Codec
,
w
)
return
...
...
plugin/pkg/admission/deny/admission_test.go
View file @
f6fb72ec
...
...
@@ -24,7 +24,7 @@ import (
func
TestAdmission
(
t
*
testing
.
T
)
{
handler
:=
NewAlwaysDeny
()
err
:=
handler
.
Admit
(
admission
.
NewAttributesRecord
(
nil
,
"kind"
,
"namespace"
,
"resource"
,
"subresource"
,
admission
.
Create
,
nil
))
err
:=
handler
.
Admit
(
admission
.
NewAttributesRecord
(
nil
,
"kind"
,
"namespace"
,
"
name"
,
"
resource"
,
"subresource"
,
admission
.
Create
,
nil
))
if
err
==
nil
{
t
.
Errorf
(
"Expected error returned from admission handler"
)
}
...
...
plugin/pkg/admission/exec/denyprivileged/admission_test.go
View file @
f6fb72ec
...
...
@@ -49,7 +49,7 @@ func testAdmission(t *testing.T, pod *api.Pod, shouldAccept bool) {
client
:
mockClient
,
}
req
:=
&
rest
.
ConnectRequest
{
Name
:
pod
.
Name
,
ResourcePath
:
"pods/exec"
}
err
:=
handler
.
Admit
(
admission
.
NewAttributesRecord
(
req
,
"Pod"
,
"test"
,
"pods"
,
"exec"
,
admission
.
Connect
,
nil
))
err
:=
handler
.
Admit
(
admission
.
NewAttributesRecord
(
req
,
"Pod"
,
"test"
,
"
name"
,
"
pods"
,
"exec"
,
admission
.
Connect
,
nil
))
if
shouldAccept
&&
err
!=
nil
{
t
.
Errorf
(
"Unexpected error returned from admission handler: %v"
,
err
)
}
...
...
plugin/pkg/admission/limitranger/admission_test.go
View file @
f6fb72ec
...
...
@@ -234,12 +234,12 @@ func TestLimitRangerIgnoresSubresource(t *testing.T) {
testPod
:=
validPod
(
"testPod"
,
1
,
api
.
ResourceRequirements
{})
indexer
.
Add
(
&
limitRange
)
err
:=
handler
.
Admit
(
admission
.
NewAttributesRecord
(
&
testPod
,
"Pod"
,
limitRange
.
Namespace
,
"pods"
,
""
,
admission
.
Update
,
nil
))
err
:=
handler
.
Admit
(
admission
.
NewAttributesRecord
(
&
testPod
,
"Pod"
,
limitRange
.
Namespace
,
"
testPod"
,
"
pods"
,
""
,
admission
.
Update
,
nil
))
if
err
==
nil
{
t
.
Errorf
(
"Expected an error since the pod did not specify resource limits in its update call"
)
}
err
=
handler
.
Admit
(
admission
.
NewAttributesRecord
(
&
testPod
,
"Pod"
,
limitRange
.
Namespace
,
"pods"
,
"status"
,
admission
.
Update
,
nil
))
err
=
handler
.
Admit
(
admission
.
NewAttributesRecord
(
&
testPod
,
"Pod"
,
limitRange
.
Namespace
,
"
testPod"
,
"
pods"
,
"status"
,
admission
.
Update
,
nil
))
if
err
!=
nil
{
t
.
Errorf
(
"Should have ignored calls to any subresource of pod %v"
,
err
)
}
...
...
plugin/pkg/admission/namespace/autoprovision/admission_test.go
View file @
f6fb72ec
...
...
@@ -41,7 +41,7 @@ func TestAdmission(t *testing.T) {
Containers
:
[]
api
.
Container
{{
Name
:
"ctr"
,
Image
:
"image"
}},
},
}
err
:=
handler
.
Admit
(
admission
.
NewAttributesRecord
(
&
pod
,
"Pod"
,
namespac
e
,
"pods"
,
""
,
admission
.
Create
,
nil
))
err
:=
handler
.
Admit
(
admission
.
NewAttributesRecord
(
&
pod
,
"Pod"
,
pod
.
Namespace
,
pod
.
Nam
e
,
"pods"
,
""
,
admission
.
Create
,
nil
))
if
err
!=
nil
{
t
.
Errorf
(
"Unexpected error returned from admission handler"
)
}
...
...
@@ -72,7 +72,7 @@ func TestAdmissionNamespaceExists(t *testing.T) {
Containers
:
[]
api
.
Container
{{
Name
:
"ctr"
,
Image
:
"image"
}},
},
}
err
:=
handler
.
Admit
(
admission
.
NewAttributesRecord
(
&
pod
,
"Pod"
,
namespac
e
,
"pods"
,
""
,
admission
.
Create
,
nil
))
err
:=
handler
.
Admit
(
admission
.
NewAttributesRecord
(
&
pod
,
"Pod"
,
pod
.
Namespace
,
pod
.
Nam
e
,
"pods"
,
""
,
admission
.
Create
,
nil
))
if
err
!=
nil
{
t
.
Errorf
(
"Unexpected error returned from admission handler"
)
}
...
...
@@ -93,7 +93,7 @@ func TestIgnoreAdmission(t *testing.T) {
Containers
:
[]
api
.
Container
{{
Name
:
"ctr"
,
Image
:
"image"
}},
},
}
err
:=
handler
.
Admit
(
admission
.
NewAttributesRecord
(
&
pod
,
"Pod"
,
namespac
e
,
"pods"
,
""
,
admission
.
Update
,
nil
))
err
:=
handler
.
Admit
(
admission
.
NewAttributesRecord
(
&
pod
,
"Pod"
,
pod
.
Namespace
,
pod
.
Nam
e
,
"pods"
,
""
,
admission
.
Update
,
nil
))
if
err
!=
nil
{
t
.
Errorf
(
"Unexpected error returned from admission handler"
)
}
...
...
@@ -120,7 +120,7 @@ func TestAdmissionNamespaceExistsUnknownToHandler(t *testing.T) {
Containers
:
[]
api
.
Container
{{
Name
:
"ctr"
,
Image
:
"image"
}},
},
}
err
:=
handler
.
Admit
(
admission
.
NewAttributesRecord
(
&
pod
,
"Pod"
,
namespac
e
,
"pods"
,
""
,
admission
.
Create
,
nil
))
err
:=
handler
.
Admit
(
admission
.
NewAttributesRecord
(
&
pod
,
"Pod"
,
pod
.
Namespace
,
pod
.
Nam
e
,
"pods"
,
""
,
admission
.
Create
,
nil
))
if
err
!=
nil
{
t
.
Errorf
(
"Unexpected error returned from admission handler"
)
}
...
...
plugin/pkg/admission/namespace/lifecycle/admission_test.go
View file @
f6fb72ec
...
...
@@ -49,7 +49,7 @@ func TestAdmission(t *testing.T) {
Containers
:
[]
api
.
Container
{{
Name
:
"ctr"
,
Image
:
"image"
}},
},
}
err
:=
handler
.
Admit
(
admission
.
NewAttributesRecord
(
&
pod
,
"Pod"
,
namespaceObj
.
Namespac
e
,
"pods"
,
""
,
admission
.
Create
,
nil
))
err
:=
handler
.
Admit
(
admission
.
NewAttributesRecord
(
&
pod
,
"Pod"
,
pod
.
Namespace
,
pod
.
Nam
e
,
"pods"
,
""
,
admission
.
Create
,
nil
))
if
err
!=
nil
{
t
.
Errorf
(
"Unexpected error returned from admission handler: %v"
,
err
)
}
...
...
@@ -59,19 +59,19 @@ func TestAdmission(t *testing.T) {
store
.
Add
(
namespaceObj
)
// verify create operations in the namespace cause an error
err
=
handler
.
Admit
(
admission
.
NewAttributesRecord
(
&
pod
,
"Pod"
,
namespaceObj
.
Namespac
e
,
"pods"
,
""
,
admission
.
Create
,
nil
))
err
=
handler
.
Admit
(
admission
.
NewAttributesRecord
(
&
pod
,
"Pod"
,
pod
.
Namespace
,
pod
.
Nam
e
,
"pods"
,
""
,
admission
.
Create
,
nil
))
if
err
==
nil
{
t
.
Errorf
(
"Expected error rejecting creates in a namespace when it is terminating"
)
}
// verify update operations in the namespace can proceed
err
=
handler
.
Admit
(
admission
.
NewAttributesRecord
(
&
pod
,
"Pod"
,
namespaceObj
.
Namespac
e
,
"pods"
,
""
,
admission
.
Update
,
nil
))
err
=
handler
.
Admit
(
admission
.
NewAttributesRecord
(
&
pod
,
"Pod"
,
pod
.
Namespace
,
pod
.
Nam
e
,
"pods"
,
""
,
admission
.
Update
,
nil
))
if
err
!=
nil
{
t
.
Errorf
(
"Unexpected error returned from admission handler: %v"
,
err
)
}
// verify delete operations in the namespace can proceed
err
=
handler
.
Admit
(
admission
.
NewAttributesRecord
(
nil
,
"Pod"
,
namespaceObj
.
Namespac
e
,
"pods"
,
""
,
admission
.
Delete
,
nil
))
err
=
handler
.
Admit
(
admission
.
NewAttributesRecord
(
nil
,
"Pod"
,
pod
.
Namespace
,
pod
.
Nam
e
,
"pods"
,
""
,
admission
.
Delete
,
nil
))
if
err
!=
nil
{
t
.
Errorf
(
"Unexpected error returned from admission handler: %v"
,
err
)
}
...
...
plugin/pkg/admission/resourcequota/admission_test.go
View file @
f6fb72ec
...
...
@@ -42,7 +42,7 @@ func getResourceRequirements(cpu, memory string) api.ResourceRequirements {
func
TestAdmissionIgnoresDelete
(
t
*
testing
.
T
)
{
namespace
:=
"default"
handler
:=
createResourceQuota
(
&
testclient
.
Fake
{},
nil
)
err
:=
handler
.
Admit
(
admission
.
NewAttributesRecord
(
nil
,
"Pod"
,
namespace
,
"pods"
,
""
,
admission
.
Delete
,
nil
))
err
:=
handler
.
Admit
(
admission
.
NewAttributesRecord
(
nil
,
"Pod"
,
namespace
,
"
name"
,
"
pods"
,
""
,
admission
.
Delete
,
nil
))
if
err
!=
nil
{
t
.
Errorf
(
"ResourceQuota should admit all deletes: %v"
,
err
)
}
...
...
@@ -71,12 +71,12 @@ func TestAdmissionIgnoresSubresources(t *testing.T) {
Containers
:
[]
api
.
Container
{{
Name
:
"ctr"
,
Image
:
"image"
,
Resources
:
getResourceRequirements
(
"100m"
,
"2Gi"
)}},
}}
err
:=
handler
.
Admit
(
admission
.
NewAttributesRecord
(
newPod
,
"Pod"
,
newPod
.
Namespace
,
"pods"
,
""
,
admission
.
Create
,
nil
))
err
:=
handler
.
Admit
(
admission
.
NewAttributesRecord
(
newPod
,
"Pod"
,
newPod
.
Namespace
,
"
123"
,
"
pods"
,
""
,
admission
.
Create
,
nil
))
if
err
==
nil
{
t
.
Errorf
(
"Expected an error because the pod exceeded allowed quota"
)
}
err
=
handler
.
Admit
(
admission
.
NewAttributesRecord
(
newPod
,
"Pod"
,
newPod
.
Namespace
,
"pods"
,
"subresource"
,
admission
.
Create
,
nil
))
err
=
handler
.
Admit
(
admission
.
NewAttributesRecord
(
newPod
,
"Pod"
,
newPod
.
Namespace
,
"
123"
,
"
pods"
,
"subresource"
,
admission
.
Create
,
nil
))
if
err
!=
nil
{
t
.
Errorf
(
"Did not expect an error because the action went to a subresource: %v"
,
err
)
}
...
...
@@ -103,7 +103,7 @@ func TestIncrementUsagePods(t *testing.T) {
r
:=
api
.
ResourcePods
status
.
Hard
[
r
]
=
resource
.
MustParse
(
"2"
)
status
.
Used
[
r
]
=
resource
.
MustParse
(
"1"
)
dirty
,
err
:=
IncrementUsage
(
admission
.
NewAttributesRecord
(
&
api
.
Pod
{},
"Pod"
,
namespace
,
"pods"
,
""
,
admission
.
Create
,
nil
),
status
,
client
)
dirty
,
err
:=
IncrementUsage
(
admission
.
NewAttributesRecord
(
&
api
.
Pod
{},
"Pod"
,
namespace
,
"
name"
,
"
pods"
,
""
,
admission
.
Create
,
nil
),
status
,
client
)
if
err
!=
nil
{
t
.
Errorf
(
"Unexpected error: %v"
,
err
)
}
...
...
@@ -143,7 +143,7 @@ func TestIncrementUsageMemory(t *testing.T) {
Volumes
:
[]
api
.
Volume
{{
Name
:
"vol"
}},
Containers
:
[]
api
.
Container
{{
Name
:
"ctr"
,
Image
:
"image"
,
Resources
:
getResourceRequirements
(
"100m"
,
"1Gi"
)}},
}}
dirty
,
err
:=
IncrementUsage
(
admission
.
NewAttributesRecord
(
newPod
,
"Pod"
,
namespace
,
"pods"
,
""
,
admission
.
Create
,
nil
),
status
,
client
)
dirty
,
err
:=
IncrementUsage
(
admission
.
NewAttributesRecord
(
newPod
,
"Pod"
,
namespace
,
"
name"
,
"
pods"
,
""
,
admission
.
Create
,
nil
),
status
,
client
)
if
err
!=
nil
{
t
.
Errorf
(
"Unexpected error: %v"
,
err
)
}
...
...
@@ -184,7 +184,7 @@ func TestExceedUsageMemory(t *testing.T) {
Volumes
:
[]
api
.
Volume
{{
Name
:
"vol"
}},
Containers
:
[]
api
.
Container
{{
Name
:
"ctr"
,
Image
:
"image"
,
Resources
:
getResourceRequirements
(
"100m"
,
"3Gi"
)}},
}}
_
,
err
:=
IncrementUsage
(
admission
.
NewAttributesRecord
(
newPod
,
"Pod"
,
namespace
,
"pods"
,
""
,
admission
.
Create
,
nil
),
status
,
client
)
_
,
err
:=
IncrementUsage
(
admission
.
NewAttributesRecord
(
newPod
,
"Pod"
,
namespace
,
"
name"
,
"
pods"
,
""
,
admission
.
Create
,
nil
),
status
,
client
)
if
err
==
nil
{
t
.
Errorf
(
"Expected memory usage exceeded error"
)
}
...
...
@@ -217,7 +217,7 @@ func TestIncrementUsageCPU(t *testing.T) {
Volumes
:
[]
api
.
Volume
{{
Name
:
"vol"
}},
Containers
:
[]
api
.
Container
{{
Name
:
"ctr"
,
Image
:
"image"
,
Resources
:
getResourceRequirements
(
"100m"
,
"1Gi"
)}},
}}
dirty
,
err
:=
IncrementUsage
(
admission
.
NewAttributesRecord
(
newPod
,
"Pod"
,
namespace
,
"pods"
,
""
,
admission
.
Create
,
nil
),
status
,
client
)
dirty
,
err
:=
IncrementUsage
(
admission
.
NewAttributesRecord
(
newPod
,
"Pod"
,
namespace
,
"
name"
,
"
pods"
,
""
,
admission
.
Create
,
nil
),
status
,
client
)
if
err
!=
nil
{
t
.
Errorf
(
"Unexpected error: %v"
,
err
)
}
...
...
@@ -258,7 +258,7 @@ func TestUnboundedCPU(t *testing.T) {
Volumes
:
[]
api
.
Volume
{{
Name
:
"vol"
}},
Containers
:
[]
api
.
Container
{{
Name
:
"ctr"
,
Image
:
"image"
,
Resources
:
getResourceRequirements
(
"0m"
,
"1Gi"
)}},
}}
_
,
err
:=
IncrementUsage
(
admission
.
NewAttributesRecord
(
newPod
,
"Pod"
,
namespace
,
"pods"
,
""
,
admission
.
Create
,
nil
),
status
,
client
)
_
,
err
:=
IncrementUsage
(
admission
.
NewAttributesRecord
(
newPod
,
"Pod"
,
namespace
,
"
name"
,
"
pods"
,
""
,
admission
.
Create
,
nil
),
status
,
client
)
if
err
==
nil
{
t
.
Errorf
(
"Expected CPU unbounded usage error"
)
}
...
...
@@ -291,7 +291,7 @@ func TestUnboundedMemory(t *testing.T) {
Volumes
:
[]
api
.
Volume
{{
Name
:
"vol"
}},
Containers
:
[]
api
.
Container
{{
Name
:
"ctr"
,
Image
:
"image"
,
Resources
:
getResourceRequirements
(
"250m"
,
"0"
)}},
}}
_
,
err
:=
IncrementUsage
(
admission
.
NewAttributesRecord
(
newPod
,
"Pod"
,
namespace
,
"pods"
,
""
,
admission
.
Create
,
nil
),
status
,
client
)
_
,
err
:=
IncrementUsage
(
admission
.
NewAttributesRecord
(
newPod
,
"Pod"
,
namespace
,
"
name"
,
"
pods"
,
""
,
admission
.
Create
,
nil
),
status
,
client
)
if
err
==
nil
{
t
.
Errorf
(
"Expected memory unbounded usage error"
)
}
...
...
@@ -324,7 +324,7 @@ func TestExceedUsageCPU(t *testing.T) {
Volumes
:
[]
api
.
Volume
{{
Name
:
"vol"
}},
Containers
:
[]
api
.
Container
{{
Name
:
"ctr"
,
Image
:
"image"
,
Resources
:
getResourceRequirements
(
"500m"
,
"1Gi"
)}},
}}
_
,
err
:=
IncrementUsage
(
admission
.
NewAttributesRecord
(
newPod
,
"Pod"
,
namespace
,
"pods"
,
""
,
admission
.
Create
,
nil
),
status
,
client
)
_
,
err
:=
IncrementUsage
(
admission
.
NewAttributesRecord
(
newPod
,
"Pod"
,
namespace
,
newPod
.
Name
,
"pods"
,
""
,
admission
.
Create
,
nil
),
status
,
client
)
if
err
==
nil
{
t
.
Errorf
(
"Expected CPU usage exceeded error"
)
}
...
...
@@ -350,7 +350,7 @@ func TestExceedUsagePods(t *testing.T) {
r
:=
api
.
ResourcePods
status
.
Hard
[
r
]
=
resource
.
MustParse
(
"1"
)
status
.
Used
[
r
]
=
resource
.
MustParse
(
"1"
)
_
,
err
:=
IncrementUsage
(
admission
.
NewAttributesRecord
(
&
api
.
Pod
{},
"Pod"
,
namespace
,
"pods"
,
""
,
admission
.
Create
,
nil
),
status
,
client
)
_
,
err
:=
IncrementUsage
(
admission
.
NewAttributesRecord
(
&
api
.
Pod
{},
"Pod"
,
namespace
,
"
name"
,
"
pods"
,
""
,
admission
.
Create
,
nil
),
status
,
client
)
if
err
==
nil
{
t
.
Errorf
(
"Expected error because this would exceed your quota"
)
}
...
...
@@ -372,7 +372,7 @@ func TestIncrementUsageServices(t *testing.T) {
r
:=
api
.
ResourceServices
status
.
Hard
[
r
]
=
resource
.
MustParse
(
"2"
)
status
.
Used
[
r
]
=
resource
.
MustParse
(
"1"
)
dirty
,
err
:=
IncrementUsage
(
admission
.
NewAttributesRecord
(
&
api
.
Service
{},
"Service"
,
namespace
,
"services"
,
""
,
admission
.
Create
,
nil
),
status
,
client
)
dirty
,
err
:=
IncrementUsage
(
admission
.
NewAttributesRecord
(
&
api
.
Service
{},
"Service"
,
namespace
,
"
name"
,
"
services"
,
""
,
admission
.
Create
,
nil
),
status
,
client
)
if
err
!=
nil
{
t
.
Errorf
(
"Unexpected error: %v"
,
err
)
}
...
...
@@ -401,7 +401,7 @@ func TestExceedUsageServices(t *testing.T) {
r
:=
api
.
ResourceServices
status
.
Hard
[
r
]
=
resource
.
MustParse
(
"1"
)
status
.
Used
[
r
]
=
resource
.
MustParse
(
"1"
)
_
,
err
:=
IncrementUsage
(
admission
.
NewAttributesRecord
(
&
api
.
Service
{},
"Service"
,
namespace
,
"services"
,
""
,
admission
.
Create
,
nil
),
status
,
client
)
_
,
err
:=
IncrementUsage
(
admission
.
NewAttributesRecord
(
&
api
.
Service
{},
"Service"
,
namespace
,
"
name"
,
"
services"
,
""
,
admission
.
Create
,
nil
),
status
,
client
)
if
err
==
nil
{
t
.
Errorf
(
"Expected error because this would exceed usage"
)
}
...
...
@@ -423,7 +423,7 @@ func TestIncrementUsageReplicationControllers(t *testing.T) {
r
:=
api
.
ResourceReplicationControllers
status
.
Hard
[
r
]
=
resource
.
MustParse
(
"2"
)
status
.
Used
[
r
]
=
resource
.
MustParse
(
"1"
)
dirty
,
err
:=
IncrementUsage
(
admission
.
NewAttributesRecord
(
&
api
.
ReplicationController
{},
"ReplicationController"
,
namespace
,
"replicationControllers"
,
""
,
admission
.
Create
,
nil
),
status
,
client
)
dirty
,
err
:=
IncrementUsage
(
admission
.
NewAttributesRecord
(
&
api
.
ReplicationController
{},
"ReplicationController"
,
namespace
,
"
name"
,
"
replicationControllers"
,
""
,
admission
.
Create
,
nil
),
status
,
client
)
if
err
!=
nil
{
t
.
Errorf
(
"Unexpected error: %v"
,
err
)
}
...
...
@@ -452,7 +452,7 @@ func TestExceedUsageReplicationControllers(t *testing.T) {
r
:=
api
.
ResourceReplicationControllers
status
.
Hard
[
r
]
=
resource
.
MustParse
(
"1"
)
status
.
Used
[
r
]
=
resource
.
MustParse
(
"1"
)
_
,
err
:=
IncrementUsage
(
admission
.
NewAttributesRecord
(
&
api
.
ReplicationController
{},
"ReplicationController"
,
namespace
,
"replicationControllers"
,
""
,
admission
.
Create
,
nil
),
status
,
client
)
_
,
err
:=
IncrementUsage
(
admission
.
NewAttributesRecord
(
&
api
.
ReplicationController
{},
"ReplicationController"
,
namespace
,
"
name"
,
"
replicationControllers"
,
""
,
admission
.
Create
,
nil
),
status
,
client
)
if
err
==
nil
{
t
.
Errorf
(
"Expected error for exceeding hard limits"
)
}
...
...
@@ -474,7 +474,7 @@ func TestExceedUsageSecrets(t *testing.T) {
r
:=
api
.
ResourceSecrets
status
.
Hard
[
r
]
=
resource
.
MustParse
(
"1"
)
status
.
Used
[
r
]
=
resource
.
MustParse
(
"1"
)
_
,
err
:=
IncrementUsage
(
admission
.
NewAttributesRecord
(
&
api
.
Secret
{},
"Secret"
,
namespace
,
"secrets"
,
""
,
admission
.
Create
,
nil
),
status
,
client
)
_
,
err
:=
IncrementUsage
(
admission
.
NewAttributesRecord
(
&
api
.
Secret
{},
"Secret"
,
namespace
,
"
name"
,
"
secrets"
,
""
,
admission
.
Create
,
nil
),
status
,
client
)
if
err
==
nil
{
t
.
Errorf
(
"Expected error for exceeding hard limits"
)
}
...
...
@@ -496,7 +496,7 @@ func TestExceedUsagePersistentVolumeClaims(t *testing.T) {
r
:=
api
.
ResourcePersistentVolumeClaims
status
.
Hard
[
r
]
=
resource
.
MustParse
(
"1"
)
status
.
Used
[
r
]
=
resource
.
MustParse
(
"1"
)
_
,
err
:=
IncrementUsage
(
admission
.
NewAttributesRecord
(
&
api
.
PersistentVolumeClaim
{},
"PersistentVolumeClaim"
,
namespace
,
"persistentVolumeClaims"
,
""
,
admission
.
Create
,
nil
),
status
,
client
)
_
,
err
:=
IncrementUsage
(
admission
.
NewAttributesRecord
(
&
api
.
PersistentVolumeClaim
{},
"PersistentVolumeClaim"
,
namespace
,
"
name"
,
"
persistentVolumeClaims"
,
""
,
admission
.
Create
,
nil
),
status
,
client
)
if
err
==
nil
{
t
.
Errorf
(
"Expected error for exceeding hard limits"
)
}
...
...
plugin/pkg/admission/securitycontext/scdeny/admission_test.go
View file @
f6fb72ec
...
...
@@ -44,7 +44,7 @@ func TestAdmission(t *testing.T) {
}
for
k
,
v
:=
range
successCases
{
pod
.
Spec
.
Containers
[
0
]
.
SecurityContext
=
v
err
:=
handler
.
Admit
(
admission
.
NewAttributesRecord
(
&
pod
,
"Pod"
,
"foo"
,
string
(
api
.
ResourcePods
),
""
,
"ignored"
,
nil
))
err
:=
handler
.
Admit
(
admission
.
NewAttributesRecord
(
&
pod
,
"Pod"
,
"foo"
,
"name"
,
string
(
api
.
ResourcePods
),
""
,
"ignored"
,
nil
))
if
err
!=
nil
{
t
.
Errorf
(
"Unexpected error returned from admission handler for case %s"
,
k
)
}
...
...
@@ -57,7 +57,7 @@ func TestAdmission(t *testing.T) {
}
for
k
,
v
:=
range
errorCases
{
pod
.
Spec
.
Containers
[
0
]
.
SecurityContext
=
v
err
:=
handler
.
Admit
(
admission
.
NewAttributesRecord
(
&
pod
,
"Pod"
,
"foo"
,
string
(
api
.
ResourcePods
),
""
,
"ignored"
,
nil
))
err
:=
handler
.
Admit
(
admission
.
NewAttributesRecord
(
&
pod
,
"Pod"
,
"foo"
,
"name"
,
string
(
api
.
ResourcePods
),
""
,
"ignored"
,
nil
))
if
err
==
nil
{
t
.
Errorf
(
"Expected error returned from admission handler for case %s"
,
k
)
}
...
...
plugin/pkg/admission/serviceaccount/admission_test.go
View file @
f6fb72ec
...
...
@@ -30,7 +30,7 @@ import (
func
TestIgnoresNonCreate
(
t
*
testing
.
T
)
{
pod
:=
&
api
.
Pod
{}
for
_
,
op
:=
range
[]
admission
.
Operation
{
admission
.
Update
,
admission
.
Delete
,
admission
.
Connect
}
{
attrs
:=
admission
.
NewAttributesRecord
(
pod
,
"Pod"
,
"myns"
,
string
(
api
.
ResourcePods
),
""
,
op
,
nil
)
attrs
:=
admission
.
NewAttributesRecord
(
pod
,
"Pod"
,
"myns"
,
"myname"
,
string
(
api
.
ResourcePods
),
""
,
op
,
nil
)
handler
:=
admission
.
NewChainHandler
(
NewServiceAccount
(
nil
))
err
:=
handler
.
Admit
(
attrs
)
if
err
!=
nil
{
...
...
@@ -41,7 +41,7 @@ func TestIgnoresNonCreate(t *testing.T) {
func
TestIgnoresNonPodResource
(
t
*
testing
.
T
)
{
pod
:=
&
api
.
Pod
{}
attrs
:=
admission
.
NewAttributesRecord
(
pod
,
"Pod"
,
"myns"
,
"CustomResource"
,
""
,
admission
.
Create
,
nil
)
attrs
:=
admission
.
NewAttributesRecord
(
pod
,
"Pod"
,
"myns"
,
"
myname"
,
"
CustomResource"
,
""
,
admission
.
Create
,
nil
)
err
:=
NewServiceAccount
(
nil
)
.
Admit
(
attrs
)
if
err
!=
nil
{
t
.
Errorf
(
"Expected non-pod resource allowed, got err: %v"
,
err
)
...
...
@@ -49,7 +49,7 @@ func TestIgnoresNonPodResource(t *testing.T) {
}
func
TestIgnoresNilObject
(
t
*
testing
.
T
)
{
attrs
:=
admission
.
NewAttributesRecord
(
nil
,
"Pod"
,
"myns"
,
string
(
api
.
ResourcePods
),
""
,
admission
.
Create
,
nil
)
attrs
:=
admission
.
NewAttributesRecord
(
nil
,
"Pod"
,
"myns"
,
"myname"
,
string
(
api
.
ResourcePods
),
""
,
admission
.
Create
,
nil
)
err
:=
NewServiceAccount
(
nil
)
.
Admit
(
attrs
)
if
err
!=
nil
{
t
.
Errorf
(
"Expected nil object allowed allowed, got err: %v"
,
err
)
...
...
@@ -58,7 +58,7 @@ func TestIgnoresNilObject(t *testing.T) {
func
TestIgnoresNonPodObject
(
t
*
testing
.
T
)
{
obj
:=
&
api
.
Namespace
{}
attrs
:=
admission
.
NewAttributesRecord
(
obj
,
"Pod"
,
"myns"
,
string
(
api
.
ResourcePods
),
""
,
admission
.
Create
,
nil
)
attrs
:=
admission
.
NewAttributesRecord
(
obj
,
"Pod"
,
"myns"
,
"myname"
,
string
(
api
.
ResourcePods
),
""
,
admission
.
Create
,
nil
)
err
:=
NewServiceAccount
(
nil
)
.
Admit
(
attrs
)
if
err
!=
nil
{
t
.
Errorf
(
"Expected non pod object allowed, got err: %v"
,
err
)
...
...
@@ -78,7 +78,7 @@ func TestIgnoresMirrorPod(t *testing.T) {
},
},
}
attrs
:=
admission
.
NewAttributesRecord
(
pod
,
"Pod"
,
"myns"
,
string
(
api
.
ResourcePods
),
""
,
admission
.
Create
,
nil
)
attrs
:=
admission
.
NewAttributesRecord
(
pod
,
"Pod"
,
"myns"
,
"myname"
,
string
(
api
.
ResourcePods
),
""
,
admission
.
Create
,
nil
)
err
:=
NewServiceAccount
(
nil
)
.
Admit
(
attrs
)
if
err
!=
nil
{
t
.
Errorf
(
"Expected mirror pod without service account or secrets allowed, got err: %v"
,
err
)
...
...
@@ -96,7 +96,7 @@ func TestRejectsMirrorPodWithServiceAccount(t *testing.T) {
ServiceAccountName
:
"default"
,
},
}
attrs
:=
admission
.
NewAttributesRecord
(
pod
,
"Pod"
,
"myns"
,
string
(
api
.
ResourcePods
),
""
,
admission
.
Create
,
nil
)
attrs
:=
admission
.
NewAttributesRecord
(
pod
,
"Pod"
,
"myns"
,
"myname"
,
string
(
api
.
ResourcePods
),
""
,
admission
.
Create
,
nil
)
err
:=
NewServiceAccount
(
nil
)
.
Admit
(
attrs
)
if
err
==
nil
{
t
.
Errorf
(
"Expected a mirror pod to be prevented from referencing a service account"
)
...
...
@@ -116,7 +116,7 @@ func TestRejectsMirrorPodWithSecretVolumes(t *testing.T) {
},
},
}
attrs
:=
admission
.
NewAttributesRecord
(
pod
,
"Pod"
,
"myns"
,
string
(
api
.
ResourcePods
),
""
,
admission
.
Create
,
nil
)
attrs
:=
admission
.
NewAttributesRecord
(
pod
,
"Pod"
,
"myns"
,
"myname"
,
string
(
api
.
ResourcePods
),
""
,
admission
.
Create
,
nil
)
err
:=
NewServiceAccount
(
nil
)
.
Admit
(
attrs
)
if
err
==
nil
{
t
.
Errorf
(
"Expected a mirror pod to be prevented from referencing a secret volume"
)
...
...
@@ -138,7 +138,7 @@ func TestAssignsDefaultServiceAccountAndToleratesMissingAPIToken(t *testing.T) {
})
pod
:=
&
api
.
Pod
{}
attrs
:=
admission
.
NewAttributesRecord
(
pod
,
"Pod"
,
ns
,
string
(
api
.
ResourcePods
),
""
,
admission
.
Create
,
nil
)
attrs
:=
admission
.
NewAttributesRecord
(
pod
,
"Pod"
,
ns
,
"myname"
,
string
(
api
.
ResourcePods
),
""
,
admission
.
Create
,
nil
)
err
:=
admit
.
Admit
(
attrs
)
if
err
!=
nil
{
t
.
Errorf
(
"Unexpected error: %v"
,
err
)
...
...
@@ -162,7 +162,7 @@ func TestFetchesUncachedServiceAccount(t *testing.T) {
admit
:=
NewServiceAccount
(
client
)
pod
:=
&
api
.
Pod
{}
attrs
:=
admission
.
NewAttributesRecord
(
pod
,
"Pod"
,
ns
,
string
(
api
.
ResourcePods
),
""
,
admission
.
Create
,
nil
)
attrs
:=
admission
.
NewAttributesRecord
(
pod
,
"Pod"
,
ns
,
"myname"
,
string
(
api
.
ResourcePods
),
""
,
admission
.
Create
,
nil
)
err
:=
admit
.
Admit
(
attrs
)
if
err
!=
nil
{
t
.
Errorf
(
"Unexpected error: %v"
,
err
)
...
...
@@ -181,7 +181,7 @@ func TestDeniesInvalidServiceAccount(t *testing.T) {
admit
:=
NewServiceAccount
(
client
)
pod
:=
&
api
.
Pod
{}
attrs
:=
admission
.
NewAttributesRecord
(
pod
,
"Pod"
,
ns
,
string
(
api
.
ResourcePods
),
""
,
admission
.
Create
,
nil
)
attrs
:=
admission
.
NewAttributesRecord
(
pod
,
"Pod"
,
ns
,
"myname"
,
string
(
api
.
ResourcePods
),
""
,
admission
.
Create
,
nil
)
err
:=
admit
.
Admit
(
attrs
)
if
err
==
nil
{
t
.
Errorf
(
"Expected error for missing service account, got none"
)
...
...
@@ -243,7 +243,7 @@ func TestAutomountsAPIToken(t *testing.T) {
},
},
}
attrs
:=
admission
.
NewAttributesRecord
(
pod
,
"Pod"
,
ns
,
string
(
api
.
ResourcePods
),
""
,
admission
.
Create
,
nil
)
attrs
:=
admission
.
NewAttributesRecord
(
pod
,
"Pod"
,
ns
,
"myname"
,
string
(
api
.
ResourcePods
),
""
,
admission
.
Create
,
nil
)
err
:=
admit
.
Admit
(
attrs
)
if
err
!=
nil
{
t
.
Errorf
(
"Unexpected error: %v"
,
err
)
...
...
@@ -321,7 +321,7 @@ func TestRespectsExistingMount(t *testing.T) {
},
},
}
attrs
:=
admission
.
NewAttributesRecord
(
pod
,
"Pod"
,
ns
,
string
(
api
.
ResourcePods
),
""
,
admission
.
Create
,
nil
)
attrs
:=
admission
.
NewAttributesRecord
(
pod
,
"Pod"
,
ns
,
"myname"
,
string
(
api
.
ResourcePods
),
""
,
admission
.
Create
,
nil
)
err
:=
admit
.
Admit
(
attrs
)
if
err
!=
nil
{
t
.
Errorf
(
"Unexpected error: %v"
,
err
)
...
...
@@ -364,7 +364,7 @@ func TestAllowsReferencedSecretVolumes(t *testing.T) {
},
},
}
attrs
:=
admission
.
NewAttributesRecord
(
pod
,
"Pod"
,
ns
,
string
(
api
.
ResourcePods
),
""
,
admission
.
Create
,
nil
)
attrs
:=
admission
.
NewAttributesRecord
(
pod
,
"Pod"
,
ns
,
"myname"
,
string
(
api
.
ResourcePods
),
""
,
admission
.
Create
,
nil
)
err
:=
admit
.
Admit
(
attrs
)
if
err
!=
nil
{
t
.
Errorf
(
"Unexpected error: %v"
,
err
)
...
...
@@ -392,7 +392,7 @@ func TestRejectsUnreferencedSecretVolumes(t *testing.T) {
},
},
}
attrs
:=
admission
.
NewAttributesRecord
(
pod
,
"Pod"
,
ns
,
string
(
api
.
ResourcePods
),
""
,
admission
.
Create
,
nil
)
attrs
:=
admission
.
NewAttributesRecord
(
pod
,
"Pod"
,
ns
,
"myname"
,
string
(
api
.
ResourcePods
),
""
,
admission
.
Create
,
nil
)
err
:=
admit
.
Admit
(
attrs
)
if
err
==
nil
{
t
.
Errorf
(
"Expected rejection for using a secret the service account does not reference"
)
...
...
@@ -421,7 +421,7 @@ func TestAllowsReferencedImagePullSecrets(t *testing.T) {
ImagePullSecrets
:
[]
api
.
LocalObjectReference
{{
Name
:
"foo"
}},
},
}
attrs
:=
admission
.
NewAttributesRecord
(
pod
,
"Pod"
,
ns
,
string
(
api
.
ResourcePods
),
""
,
admission
.
Create
,
nil
)
attrs
:=
admission
.
NewAttributesRecord
(
pod
,
"Pod"
,
ns
,
"myname"
,
string
(
api
.
ResourcePods
),
""
,
admission
.
Create
,
nil
)
err
:=
admit
.
Admit
(
attrs
)
if
err
!=
nil
{
t
.
Errorf
(
"Unexpected error: %v"
,
err
)
...
...
@@ -447,7 +447,7 @@ func TestRejectsUnreferencedImagePullSecrets(t *testing.T) {
ImagePullSecrets
:
[]
api
.
LocalObjectReference
{{
Name
:
"foo"
}},
},
}
attrs
:=
admission
.
NewAttributesRecord
(
pod
,
"Pod"
,
ns
,
string
(
api
.
ResourcePods
),
""
,
admission
.
Create
,
nil
)
attrs
:=
admission
.
NewAttributesRecord
(
pod
,
"Pod"
,
ns
,
"myname"
,
string
(
api
.
ResourcePods
),
""
,
admission
.
Create
,
nil
)
err
:=
admit
.
Admit
(
attrs
)
if
err
==
nil
{
t
.
Errorf
(
"Expected rejection for using a secret the service account does not reference"
)
...
...
@@ -477,7 +477,7 @@ func TestDoNotAddImagePullSecrets(t *testing.T) {
ImagePullSecrets
:
[]
api
.
LocalObjectReference
{{
Name
:
"foo"
}},
},
}
attrs
:=
admission
.
NewAttributesRecord
(
pod
,
"Pod"
,
ns
,
string
(
api
.
ResourcePods
),
""
,
admission
.
Create
,
nil
)
attrs
:=
admission
.
NewAttributesRecord
(
pod
,
"Pod"
,
ns
,
"myname"
,
string
(
api
.
ResourcePods
),
""
,
admission
.
Create
,
nil
)
err
:=
admit
.
Admit
(
attrs
)
if
err
!=
nil
{
t
.
Errorf
(
"Unexpected error: %v"
,
err
)
...
...
@@ -508,7 +508,7 @@ func TestAddImagePullSecrets(t *testing.T) {
admit
.
serviceAccounts
.
Add
(
sa
)
pod
:=
&
api
.
Pod
{}
attrs
:=
admission
.
NewAttributesRecord
(
pod
,
"Pod"
,
ns
,
string
(
api
.
ResourcePods
),
""
,
admission
.
Create
,
nil
)
attrs
:=
admission
.
NewAttributesRecord
(
pod
,
"Pod"
,
ns
,
"myname"
,
string
(
api
.
ResourcePods
),
""
,
admission
.
Create
,
nil
)
err
:=
admit
.
Admit
(
attrs
)
if
err
!=
nil
{
t
.
Errorf
(
"Unexpected error: %v"
,
err
)
...
...
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