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
58e7cc41
Commit
58e7cc41
authored
Sep 15, 2015
by
Brendan Burns
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add a not found error to admission control
parent
4c01b2a5
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
40 additions
and
19 deletions
+40
-19
errors.go
pkg/admission/errors.go
+27
-10
admission.go
plugin/pkg/admission/namespace/exists/admission.go
+8
-5
admission.go
plugin/pkg/admission/namespace/lifecycle/admission.go
+5
-4
No files found.
pkg/admission/errors.go
View file @
58e7cc41
...
@@ -19,22 +19,17 @@ package admission
...
@@ -19,22 +19,17 @@ package admission
import
(
import
(
"k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/api"
apierrors
"k8s.io/kubernetes/pkg/api/errors"
apierrors
"k8s.io/kubernetes/pkg/api/errors"
errs
"k8s.io/kubernetes/pkg/util/errors"
)
)
// NewForbidden is a utility function to return a well-formatted admission control error response
func
extractKindName
(
a
Attributes
)
(
name
,
kind
string
,
err
error
)
{
func
NewForbidden
(
a
Attributes
,
internalError
error
)
error
{
name
=
"Unknown"
// do not double wrap an error of same type
kind
=
a
.
GetKind
()
if
apierrors
.
IsForbidden
(
internalError
)
{
return
internalError
}
name
:=
"Unknown"
kind
:=
a
.
GetKind
()
obj
:=
a
.
GetObject
()
obj
:=
a
.
GetObject
()
if
obj
!=
nil
{
if
obj
!=
nil
{
objectMeta
,
err
:=
api
.
ObjectMetaFor
(
obj
)
objectMeta
,
err
:=
api
.
ObjectMetaFor
(
obj
)
if
err
!=
nil
{
if
err
!=
nil
{
return
apierrors
.
NewForbidden
(
kind
,
name
,
internalError
)
return
""
,
""
,
err
}
}
// this is necessary because name object name generation has not occurred yet
// this is necessary because name object name generation has not occurred yet
...
@@ -44,5 +39,27 @@ func NewForbidden(a Attributes, internalError error) error {
...
@@ -44,5 +39,27 @@ func NewForbidden(a Attributes, internalError error) error {
name
=
objectMeta
.
GenerateName
name
=
objectMeta
.
GenerateName
}
}
}
}
return
name
,
kind
,
nil
}
// NewForbidden is a utility function to return a well-formatted admission control error response
func
NewForbidden
(
a
Attributes
,
internalError
error
)
error
{
// do not double wrap an error of same type
if
apierrors
.
IsForbidden
(
internalError
)
{
return
internalError
}
name
,
kind
,
err
:=
extractKindName
(
a
)
if
err
!=
nil
{
return
apierrors
.
NewInternalError
(
errs
.
NewAggregate
([]
error
{
internalError
,
err
}))
}
return
apierrors
.
NewForbidden
(
kind
,
name
,
internalError
)
return
apierrors
.
NewForbidden
(
kind
,
name
,
internalError
)
}
}
// NewNotFound is a utility function to return a well-formatted admission control error response
func
NewNotFound
(
a
Attributes
)
error
{
name
,
kind
,
err
:=
extractKindName
(
a
)
if
err
!=
nil
{
return
apierrors
.
NewInternalError
(
err
)
}
return
apierrors
.
NewNotFound
(
kind
,
name
)
}
plugin/pkg/admission/namespace/exists/admission.go
View file @
58e7cc41
...
@@ -17,12 +17,12 @@ limitations under the License.
...
@@ -17,12 +17,12 @@ limitations under the License.
package
exists
package
exists
import
(
import
(
"fmt"
"io"
"io"
"time"
"time"
"k8s.io/kubernetes/pkg/admission"
"k8s.io/kubernetes/pkg/admission"
"k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/api/errors"
"k8s.io/kubernetes/pkg/api/meta"
"k8s.io/kubernetes/pkg/api/meta"
"k8s.io/kubernetes/pkg/client/cache"
"k8s.io/kubernetes/pkg/client/cache"
client
"k8s.io/kubernetes/pkg/client/unversioned"
client
"k8s.io/kubernetes/pkg/client/unversioned"
...
@@ -50,11 +50,11 @@ type exists struct {
...
@@ -50,11 +50,11 @@ type exists struct {
func
(
e
*
exists
)
Admit
(
a
admission
.
Attributes
)
(
err
error
)
{
func
(
e
*
exists
)
Admit
(
a
admission
.
Attributes
)
(
err
error
)
{
defaultVersion
,
kind
,
err
:=
api
.
RESTMapper
.
VersionAndKindForResource
(
a
.
GetResource
())
defaultVersion
,
kind
,
err
:=
api
.
RESTMapper
.
VersionAndKindForResource
(
a
.
GetResource
())
if
err
!=
nil
{
if
err
!=
nil
{
return
admission
.
NewForbidden
(
a
,
err
)
return
errors
.
NewInternalError
(
err
)
}
}
mapping
,
err
:=
api
.
RESTMapper
.
RESTMapping
(
kind
,
defaultVersion
)
mapping
,
err
:=
api
.
RESTMapper
.
RESTMapping
(
kind
,
defaultVersion
)
if
err
!=
nil
{
if
err
!=
nil
{
return
admission
.
NewForbidden
(
a
,
err
)
return
errors
.
NewInternalError
(
err
)
}
}
if
mapping
.
Scope
.
Name
()
!=
meta
.
RESTScopeNameNamespace
{
if
mapping
.
Scope
.
Name
()
!=
meta
.
RESTScopeNameNamespace
{
return
nil
return
nil
...
@@ -68,7 +68,7 @@ func (e *exists) Admit(a admission.Attributes) (err error) {
...
@@ -68,7 +68,7 @@ func (e *exists) Admit(a admission.Attributes) (err error) {
}
}
_
,
exists
,
err
:=
e
.
store
.
Get
(
namespace
)
_
,
exists
,
err
:=
e
.
store
.
Get
(
namespace
)
if
err
!=
nil
{
if
err
!=
nil
{
return
admission
.
NewForbidden
(
a
,
err
)
return
errors
.
NewInternalError
(
err
)
}
}
if
exists
{
if
exists
{
return
nil
return
nil
...
@@ -77,7 +77,10 @@ func (e *exists) Admit(a admission.Attributes) (err error) {
...
@@ -77,7 +77,10 @@ func (e *exists) Admit(a admission.Attributes) (err error) {
// in case of latency in our caches, make a call direct to storage to verify that it truly exists or not
// in case of latency in our caches, make a call direct to storage to verify that it truly exists or not
_
,
err
=
e
.
client
.
Namespaces
()
.
Get
(
a
.
GetNamespace
())
_
,
err
=
e
.
client
.
Namespaces
()
.
Get
(
a
.
GetNamespace
())
if
err
!=
nil
{
if
err
!=
nil
{
return
admission
.
NewForbidden
(
a
,
fmt
.
Errorf
(
"Namespace %s does not exist"
,
a
.
GetNamespace
()))
if
errors
.
IsNotFound
(
err
)
{
return
err
}
return
errors
.
NewInternalError
(
err
)
}
}
return
nil
return
nil
...
...
plugin/pkg/admission/namespace/lifecycle/admission.go
View file @
58e7cc41
...
@@ -58,11 +58,11 @@ func (l *lifecycle) Admit(a admission.Attributes) (err error) {
...
@@ -58,11 +58,11 @@ func (l *lifecycle) Admit(a admission.Attributes) (err error) {
defaultVersion
,
kind
,
err
:=
api
.
RESTMapper
.
VersionAndKindForResource
(
a
.
GetResource
())
defaultVersion
,
kind
,
err
:=
api
.
RESTMapper
.
VersionAndKindForResource
(
a
.
GetResource
())
if
err
!=
nil
{
if
err
!=
nil
{
return
admission
.
NewForbidden
(
a
,
err
)
return
errors
.
NewInternalError
(
err
)
}
}
mapping
,
err
:=
api
.
RESTMapper
.
RESTMapping
(
kind
,
defaultVersion
)
mapping
,
err
:=
api
.
RESTMapper
.
RESTMapping
(
kind
,
defaultVersion
)
if
err
!=
nil
{
if
err
!=
nil
{
return
admission
.
NewForbidden
(
a
,
err
)
return
errors
.
NewInternalError
(
err
)
}
}
if
mapping
.
Scope
.
Name
()
!=
meta
.
RESTScopeNameNamespace
{
if
mapping
.
Scope
.
Name
()
!=
meta
.
RESTScopeNameNamespace
{
return
nil
return
nil
...
@@ -74,7 +74,7 @@ func (l *lifecycle) Admit(a admission.Attributes) (err error) {
...
@@ -74,7 +74,7 @@ func (l *lifecycle) Admit(a admission.Attributes) (err error) {
},
},
})
})
if
err
!=
nil
{
if
err
!=
nil
{
return
admission
.
NewForbidden
(
a
,
err
)
return
errors
.
NewInternalError
(
err
)
}
}
// refuse to operate on non-existent namespaces
// refuse to operate on non-existent namespaces
...
@@ -82,7 +82,7 @@ func (l *lifecycle) Admit(a admission.Attributes) (err error) {
...
@@ -82,7 +82,7 @@ func (l *lifecycle) Admit(a admission.Attributes) (err error) {
// in case of latency in our caches, make a call direct to storage to verify that it truly exists or not
// in case of latency in our caches, make a call direct to storage to verify that it truly exists or not
namespaceObj
,
err
=
l
.
client
.
Namespaces
()
.
Get
(
a
.
GetNamespace
())
namespaceObj
,
err
=
l
.
client
.
Namespaces
()
.
Get
(
a
.
GetNamespace
())
if
err
!=
nil
{
if
err
!=
nil
{
return
admission
.
New
Forbidden
(
a
,
fmt
.
Errorf
(
"Namespace %s does not exist"
,
a
.
GetNamespace
())
)
return
admission
.
New
NotFound
(
a
)
}
}
}
}
...
@@ -93,6 +93,7 @@ func (l *lifecycle) Admit(a admission.Attributes) (err error) {
...
@@ -93,6 +93,7 @@ func (l *lifecycle) Admit(a admission.Attributes) (err error) {
return
nil
return
nil
}
}
// TODO: This should probably not be a 403
return
admission
.
NewForbidden
(
a
,
fmt
.
Errorf
(
"Unable to create new content in namespace %s because it is being terminated."
,
a
.
GetNamespace
()))
return
admission
.
NewForbidden
(
a
,
fmt
.
Errorf
(
"Unable to create new content in namespace %s because it is being terminated."
,
a
.
GetNamespace
()))
}
}
...
...
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