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
d0441a9f
Commit
d0441a9f
authored
Jun 19, 2015
by
derekwaynecarr
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Prevent deletion of default namespace
parent
f6fb72ec
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
31 additions
and
5 deletions
+31
-5
admission.go
plugin/pkg/admission/namespace/lifecycle/admission.go
+18
-5
admission_test.go
plugin/pkg/admission/namespace/lifecycle/admission_test.go
+13
-0
No files found.
plugin/pkg/admission/namespace/lifecycle/admission.go
View file @
d0441a9f
...
@@ -22,6 +22,7 @@ import (
...
@@ -22,6 +22,7 @@ import (
"github.com/GoogleCloudPlatform/kubernetes/pkg/admission"
"github.com/GoogleCloudPlatform/kubernetes/pkg/admission"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/errors"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/latest"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/latest"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/meta"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/meta"
"github.com/GoogleCloudPlatform/kubernetes/pkg/client"
"github.com/GoogleCloudPlatform/kubernetes/pkg/client"
...
@@ -29,6 +30,7 @@ import (
...
@@ -29,6 +30,7 @@ import (
"github.com/GoogleCloudPlatform/kubernetes/pkg/fields"
"github.com/GoogleCloudPlatform/kubernetes/pkg/fields"
"github.com/GoogleCloudPlatform/kubernetes/pkg/labels"
"github.com/GoogleCloudPlatform/kubernetes/pkg/labels"
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
"github.com/GoogleCloudPlatform/kubernetes/pkg/watch"
"github.com/GoogleCloudPlatform/kubernetes/pkg/watch"
)
)
...
@@ -42,11 +44,21 @@ func init() {
...
@@ -42,11 +44,21 @@ func init() {
// It enforces life-cycle constraints around a Namespace depending on its Phase
// It enforces life-cycle constraints around a Namespace depending on its Phase
type
lifecycle
struct
{
type
lifecycle
struct
{
*
admission
.
Handler
*
admission
.
Handler
client
client
.
Interface
client
client
.
Interface
store
cache
.
Store
store
cache
.
Store
immortalNamespaces
util
.
StringSet
}
}
func
(
l
*
lifecycle
)
Admit
(
a
admission
.
Attributes
)
(
err
error
)
{
func
(
l
*
lifecycle
)
Admit
(
a
admission
.
Attributes
)
(
err
error
)
{
// prevent deletion of immortal namespaces
if
a
.
GetOperation
()
==
admission
.
Delete
{
if
a
.
GetKind
()
==
"Namespace"
&&
l
.
immortalNamespaces
.
Has
(
a
.
GetName
())
{
return
errors
.
NewForbidden
(
a
.
GetKind
(),
a
.
GetName
(),
fmt
.
Errorf
(
"namespace can never be deleted"
))
}
return
nil
}
defaultVersion
,
kind
,
err
:=
latest
.
RESTMapper
.
VersionAndKindForResource
(
a
.
GetResource
())
defaultVersion
,
kind
,
err
:=
latest
.
RESTMapper
.
VersionAndKindForResource
(
a
.
GetResource
())
if
err
!=
nil
{
if
err
!=
nil
{
return
admission
.
NewForbidden
(
a
,
err
)
return
admission
.
NewForbidden
(
a
,
err
)
...
@@ -96,8 +108,9 @@ func NewLifecycle(c client.Interface) admission.Interface {
...
@@ -96,8 +108,9 @@ func NewLifecycle(c client.Interface) admission.Interface {
)
)
reflector
.
Run
()
reflector
.
Run
()
return
&
lifecycle
{
return
&
lifecycle
{
Handler
:
admission
.
NewHandler
(
admission
.
Create
),
Handler
:
admission
.
NewHandler
(
admission
.
Create
,
admission
.
Delete
),
client
:
c
,
client
:
c
,
store
:
store
,
store
:
store
,
immortalNamespaces
:
util
.
NewStringSet
(
api
.
NamespaceDefault
),
}
}
}
}
plugin/pkg/admission/namespace/lifecycle/admission_test.go
View file @
d0441a9f
...
@@ -75,4 +75,17 @@ func TestAdmission(t *testing.T) {
...
@@ -75,4 +75,17 @@ func TestAdmission(t *testing.T) {
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 of namespace default can never proceed
err
=
handler
.
Admit
(
admission
.
NewAttributesRecord
(
nil
,
"Namespace"
,
""
,
api
.
NamespaceDefault
,
"namespaces"
,
""
,
admission
.
Delete
,
nil
))
if
err
==
nil
{
t
.
Errorf
(
"Expected an error that this namespace can never be deleted"
)
}
// verify delete of namespace other than default can proceed
err
=
handler
.
Admit
(
admission
.
NewAttributesRecord
(
nil
,
"Namespace"
,
""
,
"other"
,
"namespaces"
,
""
,
admission
.
Delete
,
nil
))
if
err
!=
nil
{
t
.
Errorf
(
"Did not expect an 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