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