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
6fd36792
Unverified
Commit
6fd36792
authored
May 16, 2017
by
Jordan Liggitt
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add NodeRestriction admission plugin
parent
0c516c3a
Expand all
Show whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
271 additions
and
0 deletions
+271
-0
BUILD
cmd/kube-apiserver/app/BUILD
+1
-0
plugins.go
cmd/kube-apiserver/app/plugins.go
+1
-0
local-up-cluster.sh
hack/local-up-cluster.sh
+3
-0
BUILD
plugin/BUILD
+1
-0
BUILD
plugin/pkg/admission/noderestriction/BUILD
+54
-0
OWNERS
plugin/pkg/admission/noderestriction/OWNERS
+8
-0
admission.go
plugin/pkg/admission/noderestriction/admission.go
+203
-0
admission_test.go
plugin/pkg/admission/noderestriction/admission_test.go
+0
-0
No files found.
cmd/kube-apiserver/app/BUILD
View file @
6fd36792
...
...
@@ -54,6 +54,7 @@ go_library(
"//plugin/pkg/admission/namespace/autoprovision:go_default_library",
"//plugin/pkg/admission/namespace/exists:go_default_library",
"//plugin/pkg/admission/namespace/lifecycle:go_default_library",
"//plugin/pkg/admission/noderestriction:go_default_library",
"//plugin/pkg/admission/persistentvolume/label:go_default_library",
"//plugin/pkg/admission/podnodeselector:go_default_library",
"//plugin/pkg/admission/podpreset:go_default_library",
...
...
cmd/kube-apiserver/app/plugins.go
View file @
6fd36792
...
...
@@ -37,6 +37,7 @@ import (
_
"k8s.io/kubernetes/plugin/pkg/admission/namespace/autoprovision"
_
"k8s.io/kubernetes/plugin/pkg/admission/namespace/exists"
_
"k8s.io/kubernetes/plugin/pkg/admission/namespace/lifecycle"
_
"k8s.io/kubernetes/plugin/pkg/admission/noderestriction"
_
"k8s.io/kubernetes/plugin/pkg/admission/persistentvolume/label"
_
"k8s.io/kubernetes/plugin/pkg/admission/podnodeselector"
_
"k8s.io/kubernetes/plugin/pkg/admission/podpreset"
...
...
hack/local-up-cluster.sh
View file @
6fd36792
...
...
@@ -388,6 +388,9 @@ function start_apiserver {
if
[[
-n
"
${
PSP_ADMISSION
}
"
]]
;
then
security_admission
=
",PodSecurityPolicy"
fi
if
[[
-n
"
${
NODE_ADMISSION
}
"
]]
;
then
security_admission
=
",NodeRestriction"
fi
# Admission Controllers to invoke prior to persisting objects in cluster
ADMISSION_CONTROL
=
NamespaceLifecycle,LimitRanger,ServiceAccount
${
security_admission
}
,ResourceQuota,DefaultStorageClass,DefaultTolerationSeconds
...
...
plugin/BUILD
View file @
6fd36792
...
...
@@ -27,6 +27,7 @@ filegroup(
"//plugin/pkg/admission/namespace/autoprovision:all-srcs",
"//plugin/pkg/admission/namespace/exists:all-srcs",
"//plugin/pkg/admission/namespace/lifecycle:all-srcs",
"//plugin/pkg/admission/noderestriction:all-srcs",
"//plugin/pkg/admission/persistentvolume/label:all-srcs",
"//plugin/pkg/admission/podnodeselector:all-srcs",
"//plugin/pkg/admission/podpreset:all-srcs",
...
...
plugin/pkg/admission/noderestriction/BUILD
0 → 100644
View file @
6fd36792
package(default_visibility = ["//visibility:public"])
licenses(["notice"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_library",
"go_test",
)
go_library(
name = "go_default_library",
srcs = ["admission.go"],
tags = ["automanaged"],
deps = [
"//pkg/api:go_default_library",
"//pkg/api/pod:go_default_library",
"//pkg/auth/nodeidentifier:go_default_library",
"//pkg/client/clientset_generated/internalclientset:go_default_library",
"//pkg/client/clientset_generated/internalclientset/typed/core/internalversion:go_default_library",
"//pkg/kubeapiserver/admission:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
"//vendor/k8s.io/apiserver/pkg/admission:go_default_library",
],
)
go_test(
name = "go_default_test",
srcs = ["admission_test.go"],
library = ":go_default_library",
tags = ["automanaged"],
deps = [
"//pkg/api:go_default_library",
"//pkg/auth/nodeidentifier:go_default_library",
"//pkg/client/clientset_generated/internalclientset/fake:go_default_library",
"//pkg/client/clientset_generated/internalclientset/typed/core/internalversion:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
"//vendor/k8s.io/apiserver/pkg/admission:go_default_library",
"//vendor/k8s.io/apiserver/pkg/authentication/user:go_default_library",
],
)
filegroup(
name = "package-srcs",
srcs = glob(["**"]),
tags = ["automanaged"],
visibility = ["//visibility:private"],
)
filegroup(
name = "all-srcs",
srcs = [":package-srcs"],
tags = ["automanaged"],
)
plugin/pkg/admission/noderestriction/OWNERS
0 → 100644
View file @
6fd36792
approvers:
- deads2k
- liggitt
- timstclair
reviewers:
- deads2k
- liggitt
- timstclair
plugin/pkg/admission/noderestriction/admission.go
0 → 100644
View file @
6fd36792
/*
Copyright 2017 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package
node
import
(
"fmt"
"io"
"k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apiserver/pkg/admission"
"k8s.io/kubernetes/pkg/api"
podutil
"k8s.io/kubernetes/pkg/api/pod"
"k8s.io/kubernetes/pkg/auth/nodeidentifier"
"k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset"
coreinternalversion
"k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset/typed/core/internalversion"
kubeapiserveradmission
"k8s.io/kubernetes/pkg/kubeapiserver/admission"
)
const
(
PluginName
=
"NodeRestriction"
)
func
init
()
{
kubeapiserveradmission
.
Plugins
.
Register
(
PluginName
,
func
(
config
io
.
Reader
)
(
admission
.
Interface
,
error
)
{
return
NewPlugin
(
nodeidentifier
.
NewDefaultNodeIdentifier
(),
false
),
nil
})
}
// NewPlugin creates a new NodeRestriction admission plugin.
// This plugin identifies requests from nodes
func
NewPlugin
(
nodeIdentifier
nodeidentifier
.
NodeIdentifier
,
strict
bool
)
*
nodePlugin
{
return
&
nodePlugin
{
Handler
:
admission
.
NewHandler
(
admission
.
Create
,
admission
.
Update
,
admission
.
Delete
),
nodeIdentifier
:
nodeIdentifier
,
strict
:
strict
,
}
}
// nodePlugin holds state for and implements the admission plugin.
type
nodePlugin
struct
{
*
admission
.
Handler
strict
bool
nodeIdentifier
nodeidentifier
.
NodeIdentifier
podsGetter
coreinternalversion
.
PodsGetter
}
var
(
_
=
admission
.
Interface
(
&
nodePlugin
{})
_
=
kubeapiserveradmission
.
WantsInternalKubeClientSet
(
&
nodePlugin
{})
)
func
(
p
*
nodePlugin
)
SetInternalKubeClientSet
(
f
internalclientset
.
Interface
)
{
p
.
podsGetter
=
f
.
Core
()
}
func
(
p
*
nodePlugin
)
Validate
()
error
{
if
p
.
nodeIdentifier
==
nil
{
return
fmt
.
Errorf
(
"%s requires a node identifier"
,
PluginName
)
}
if
p
.
podsGetter
==
nil
{
return
fmt
.
Errorf
(
"%s requires a pod getter"
,
PluginName
)
}
return
nil
}
var
(
podResource
=
api
.
Resource
(
"pods"
)
nodeResource
=
api
.
Resource
(
"nodes"
)
)
func
(
c
*
nodePlugin
)
Admit
(
a
admission
.
Attributes
)
error
{
nodeName
,
isNode
:=
c
.
nodeIdentifier
.
NodeIdentity
(
a
.
GetUserInfo
())
// Our job is just to restrict nodes
if
!
isNode
{
return
nil
}
if
len
(
nodeName
)
==
0
{
if
c
.
strict
{
// In strict mode, disallow requests from nodes we cannot match to a particular node
return
admission
.
NewForbidden
(
a
,
fmt
.
Errorf
(
"could not determine node identity from user"
))
}
// Our job is just to restrict identifiable nodes
return
nil
}
switch
a
.
GetResource
()
.
GroupResource
()
{
case
podResource
:
switch
a
.
GetSubresource
()
{
case
""
:
return
c
.
admitPod
(
nodeName
,
a
)
case
"status"
:
return
c
.
admitPodStatus
(
nodeName
,
a
)
default
:
return
admission
.
NewForbidden
(
a
,
fmt
.
Errorf
(
"unexpected pod subresource %s"
,
a
.
GetSubresource
()))
}
case
nodeResource
:
return
c
.
admitNode
(
nodeName
,
a
)
default
:
return
nil
}
}
func
(
c
*
nodePlugin
)
admitPod
(
nodeName
string
,
a
admission
.
Attributes
)
error
{
switch
a
.
GetOperation
()
{
case
admission
.
Create
:
// require a pod object
pod
,
ok
:=
a
.
GetObject
()
.
(
*
api
.
Pod
)
if
!
ok
{
return
admission
.
NewForbidden
(
a
,
fmt
.
Errorf
(
"unexpected type %T"
,
a
.
GetObject
()))
}
// only allow nodes to create mirror pods
if
_
,
isMirrorPod
:=
pod
.
Annotations
[
api
.
MirrorPodAnnotationKey
];
!
isMirrorPod
{
return
admission
.
NewForbidden
(
a
,
fmt
.
Errorf
(
"pod does not have %q annotation, node %s can only create mirror pods"
,
api
.
MirrorPodAnnotationKey
,
nodeName
))
}
// only allow nodes to create a pod bound to itself
if
pod
.
Spec
.
NodeName
!=
nodeName
{
return
admission
.
NewForbidden
(
a
,
fmt
.
Errorf
(
"node %s can only create pods with spec.nodeName set to itself"
,
nodeName
))
}
// don't allow a node to create a pod that references any other API objects
if
pod
.
Spec
.
ServiceAccountName
!=
""
{
return
admission
.
NewForbidden
(
a
,
fmt
.
Errorf
(
"node %s can not create pods that reference a service account"
,
nodeName
))
}
hasSecrets
:=
false
podutil
.
VisitPodSecretNames
(
pod
,
func
(
name
string
)
(
shouldContinue
bool
)
{
hasSecrets
=
true
;
return
false
})
if
hasSecrets
{
return
admission
.
NewForbidden
(
a
,
fmt
.
Errorf
(
"node %s can not create pods that reference secrets"
,
nodeName
))
}
hasConfigMaps
:=
false
podutil
.
VisitPodConfigmapNames
(
pod
,
func
(
name
string
)
(
shouldContinue
bool
)
{
hasConfigMaps
=
true
;
return
false
})
if
hasConfigMaps
{
return
admission
.
NewForbidden
(
a
,
fmt
.
Errorf
(
"node %s can not create pods that reference configmaps"
,
nodeName
))
}
for
_
,
v
:=
range
pod
.
Spec
.
Volumes
{
if
v
.
PersistentVolumeClaim
!=
nil
{
return
admission
.
NewForbidden
(
a
,
fmt
.
Errorf
(
"node %s can not create pods that reference persistentvolumeclaims"
,
nodeName
))
}
}
return
nil
case
admission
.
Delete
:
// get the existing pod
existingPod
,
err
:=
c
.
podsGetter
.
Pods
(
a
.
GetNamespace
())
.
Get
(
a
.
GetName
(),
v1
.
GetOptions
{
ResourceVersion
:
"0"
})
if
err
!=
nil
{
return
admission
.
NewForbidden
(
a
,
err
)
}
// only allow a node to delete a pod bound to itself
if
existingPod
.
Spec
.
NodeName
!=
nodeName
{
return
admission
.
NewForbidden
(
a
,
fmt
.
Errorf
(
"node %s can only delete pods with spec.nodeName set to itself"
,
nodeName
))
}
return
nil
default
:
return
admission
.
NewForbidden
(
a
,
fmt
.
Errorf
(
"unexpected operation %s"
,
a
.
GetOperation
()))
}
}
func
(
c
*
nodePlugin
)
admitPodStatus
(
nodeName
string
,
a
admission
.
Attributes
)
error
{
switch
a
.
GetOperation
()
{
case
admission
.
Update
:
// require an existing pod
pod
,
ok
:=
a
.
GetOldObject
()
.
(
*
api
.
Pod
)
if
!
ok
{
return
admission
.
NewForbidden
(
a
,
fmt
.
Errorf
(
"unexpected type %T"
,
a
.
GetObject
()))
}
// only allow a node to update status of a pod bound to itself
if
pod
.
Spec
.
NodeName
!=
nodeName
{
return
admission
.
NewForbidden
(
a
,
fmt
.
Errorf
(
"node %s can only update pod status for pods with spec.nodeName set to itself"
,
nodeName
))
}
return
nil
default
:
return
admission
.
NewForbidden
(
a
,
fmt
.
Errorf
(
"unexpected operation %s"
,
a
.
GetOperation
()))
}
}
func
(
c
*
nodePlugin
)
admitNode
(
nodeName
string
,
a
admission
.
Attributes
)
error
{
if
a
.
GetName
()
!=
nodeName
{
return
admission
.
NewForbidden
(
a
,
fmt
.
Errorf
(
"cannot modify other nodes"
))
}
return
nil
}
plugin/pkg/admission/noderestriction/admission_test.go
0 → 100644
View file @
6fd36792
This diff is collapsed.
Click to expand it.
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