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
cbfa0cc8
Commit
cbfa0cc8
authored
Jan 24, 2018
by
NickrenREN
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
reuse PVC protection admission plugin for PV protection
parent
2a2f88b9
Hide whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
129 additions
and
35 deletions
+129
-35
config-default.sh
cluster/centos/config-default.sh
+1
-1
config-default.sh
cluster/gce/config-default.sh
+1
-1
BUILD
pkg/kubeapiserver/options/BUILD
+1
-1
plugins.go
pkg/kubeapiserver/options/plugins.go
+3
-3
finalizer.go
pkg/volume/util/finalizer.go
+3
-0
BUILD
plugin/BUILD
+1
-1
BUILD
plugin/pkg/admission/storage/storageprotection/BUILD
+3
-2
admission.go
plugin/pkg/admission/storage/storageprotection/admission.go
+66
-21
admission_test.go
...pkg/admission/storage/storageprotection/admission_test.go
+50
-5
No files found.
cluster/centos/config-default.sh
View file @
cbfa0cc8
...
...
@@ -120,7 +120,7 @@ export FLANNEL_NET=${FLANNEL_NET:-"172.16.0.0/16"}
# Admission Controllers to invoke prior to persisting objects in cluster
# If we included ResourceQuota, we should keep it at the end of the list to prevent incrementing quota usage prematurely.
export
ADMISSION_CONTROL
=
${
ADMISSION_CONTROL
:-
"Initializers,NamespaceLifecycle,LimitRanger,ServiceAccount,PersistentVolumeClaimResize,DefaultTolerationSeconds,Priority,
PVC
Protection,ResourceQuota"
}
export
ADMISSION_CONTROL
=
${
ADMISSION_CONTROL
:-
"Initializers,NamespaceLifecycle,LimitRanger,ServiceAccount,PersistentVolumeClaimResize,DefaultTolerationSeconds,Priority,
Storage
Protection,ResourceQuota"
}
# Extra options to set on the Docker command line.
# This is useful for setting --insecure-registry for local registries.
...
...
cluster/gce/config-default.sh
View file @
cbfa0cc8
...
...
@@ -298,7 +298,7 @@ if [[ -n "${GCE_GLBC_IMAGE:-}" ]]; then
fi
# Admission Controllers to invoke prior to persisting objects in cluster
ADMISSION_CONTROL
=
Initializers,NamespaceLifecycle,LimitRanger,ServiceAccount,PersistentVolumeLabel,DefaultStorageClass,PersistentVolumeClaimResize,DefaultTolerationSeconds,NodeRestriction,Priority,
PVC
Protection
ADMISSION_CONTROL
=
Initializers,NamespaceLifecycle,LimitRanger,ServiceAccount,PersistentVolumeLabel,DefaultStorageClass,PersistentVolumeClaimResize,DefaultTolerationSeconds,NodeRestriction,Priority,
Storage
Protection
if
[[
"
${
ENABLE_POD_SECURITY_POLICY
:-}
"
==
"true"
]]
;
then
ADMISSION_CONTROL
=
"
${
ADMISSION_CONTROL
}
,PodSecurityPolicy"
...
...
pkg/kubeapiserver/options/BUILD
View file @
cbfa0cc8
...
...
@@ -44,7 +44,6 @@ go_library(
"//plugin/pkg/admission/noderestriction:go_default_library",
"//plugin/pkg/admission/persistentvolume/label:go_default_library",
"//plugin/pkg/admission/persistentvolume/resize:go_default_library",
"//plugin/pkg/admission/persistentvolumeclaim/pvcprotection:go_default_library",
"//plugin/pkg/admission/podnodeselector:go_default_library",
"//plugin/pkg/admission/podpreset:go_default_library",
"//plugin/pkg/admission/podtolerationrestriction:go_default_library",
...
...
@@ -53,6 +52,7 @@ go_library(
"//plugin/pkg/admission/security/podsecuritypolicy:go_default_library",
"//plugin/pkg/admission/securitycontext/scdeny:go_default_library",
"//plugin/pkg/admission/serviceaccount:go_default_library",
"//plugin/pkg/admission/storage/storageprotection:go_default_library",
"//plugin/pkg/admission/storageclass/setdefault:go_default_library",
"//vendor/github.com/golang/glog:go_default_library",
"//vendor/github.com/pborman/uuid:go_default_library",
...
...
pkg/kubeapiserver/options/plugins.go
View file @
cbfa0cc8
...
...
@@ -41,7 +41,6 @@ import (
"k8s.io/kubernetes/plugin/pkg/admission/noderestriction"
"k8s.io/kubernetes/plugin/pkg/admission/persistentvolume/label"
"k8s.io/kubernetes/plugin/pkg/admission/persistentvolume/resize"
"k8s.io/kubernetes/plugin/pkg/admission/persistentvolumeclaim/pvcprotection"
"k8s.io/kubernetes/plugin/pkg/admission/podnodeselector"
"k8s.io/kubernetes/plugin/pkg/admission/podpreset"
"k8s.io/kubernetes/plugin/pkg/admission/podtolerationrestriction"
...
...
@@ -50,6 +49,7 @@ import (
"k8s.io/kubernetes/plugin/pkg/admission/security/podsecuritypolicy"
"k8s.io/kubernetes/plugin/pkg/admission/securitycontext/scdeny"
"k8s.io/kubernetes/plugin/pkg/admission/serviceaccount"
"k8s.io/kubernetes/plugin/pkg/admission/storage/storageprotection"
"k8s.io/kubernetes/plugin/pkg/admission/storageclass/setdefault"
"k8s.io/apimachinery/pkg/util/sets"
...
...
@@ -86,7 +86,7 @@ var AllOrderedPlugins = []string{
extendedresourcetoleration
.
PluginName
,
// ExtendedResourceToleration
label
.
PluginName
,
// PersistentVolumeLabel
setdefault
.
PluginName
,
// DefaultStorageClass
pvcprotection
.
PluginName
,
// PVC
Protection
storageprotection
.
PluginName
,
// Storage
Protection
gc
.
PluginName
,
// OwnerReferencesPermissionEnforcement
resize
.
PluginName
,
// PersistentVolumeClaimResize
mutatingwebhook
.
PluginName
,
// MutatingAdmissionWebhook
...
...
@@ -125,7 +125,7 @@ func RegisterAllAdmissionPlugins(plugins *admission.Plugins) {
serviceaccount
.
Register
(
plugins
)
setdefault
.
Register
(
plugins
)
resize
.
Register
(
plugins
)
pvc
protection
.
Register
(
plugins
)
storage
protection
.
Register
(
plugins
)
}
// DefaultOffAdmissionPlugins get admission plugins off by default for kube-apiserver.
...
...
pkg/volume/util/finalizer.go
View file @
cbfa0cc8
...
...
@@ -19,4 +19,7 @@ package util
const
(
// Name of finalizer on PVCs that have a running pod.
PVCProtectionFinalizer
=
"kubernetes.io/pvc-protection"
// Name of finalizer on PVs that are bound by PVCs
PVProtectionFinalizer
=
"kubernetes.io/pv-protection"
)
plugin/BUILD
View file @
cbfa0cc8
...
...
@@ -28,7 +28,6 @@ filegroup(
"//plugin/pkg/admission/noderestriction:all-srcs",
"//plugin/pkg/admission/persistentvolume/label:all-srcs",
"//plugin/pkg/admission/persistentvolume/resize:all-srcs",
"//plugin/pkg/admission/persistentvolumeclaim/pvcprotection:all-srcs",
"//plugin/pkg/admission/podnodeselector:all-srcs",
"//plugin/pkg/admission/podpreset:all-srcs",
"//plugin/pkg/admission/podtolerationrestriction:all-srcs",
...
...
@@ -37,6 +36,7 @@ filegroup(
"//plugin/pkg/admission/security:all-srcs",
"//plugin/pkg/admission/securitycontext/scdeny:all-srcs",
"//plugin/pkg/admission/serviceaccount:all-srcs",
"//plugin/pkg/admission/storage/storageprotection:all-srcs",
"//plugin/pkg/admission/storageclass/setdefault:all-srcs",
"//plugin/pkg/auth:all-srcs",
],
...
...
plugin/pkg/admission/
persistentvolumeclaim/pvc
protection/BUILD
→
plugin/pkg/admission/
storage/storage
protection/BUILD
View file @
cbfa0cc8
...
...
@@ -3,7 +3,7 @@ load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")
go_library(
name = "go_default_library",
srcs = ["admission.go"],
importpath = "k8s.io/kubernetes/plugin/pkg/admission/
persistentvolumeclaim/pvc
protection",
importpath = "k8s.io/kubernetes/plugin/pkg/admission/
storage/storage
protection",
visibility = ["//visibility:public"],
deps = [
"//pkg/apis/core:go_default_library",
...
...
@@ -22,7 +22,7 @@ go_test(
name = "go_default_test",
srcs = ["admission_test.go"],
embed = [":go_default_library"],
importpath = "k8s.io/kubernetes/plugin/pkg/admission/
persistentvolumeclaim/pvc
protection",
importpath = "k8s.io/kubernetes/plugin/pkg/admission/
storage/storage
protection",
deps = [
"//pkg/apis/core:go_default_library",
"//pkg/client/informers/informers_generated/internalversion:go_default_library",
...
...
@@ -31,6 +31,7 @@ go_test(
"//vendor/github.com/davecgh/go-spew/spew:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/runtime:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/runtime/schema:go_default_library",
"//vendor/k8s.io/apiserver/pkg/admission:go_default_library",
"//vendor/k8s.io/apiserver/pkg/util/feature:go_default_library",
],
...
...
plugin/pkg/admission/
persistentvolumeclaim/pvc
protection/admission.go
→
plugin/pkg/admission/
storage/storage
protection/admission.go
View file @
cbfa0cc8
...
...
@@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/
package
pvc
protection
package
storage
protection
import
(
"fmt"
...
...
@@ -34,7 +34,7 @@ import (
const
(
// PluginName is the name of this admission controller plugin
PluginName
=
"
PVC
Protection"
PluginName
=
"
Storage
Protection"
)
// Register registers a plugin
...
...
@@ -45,55 +45,100 @@ func Register(plugins *admission.Plugins) {
})
}
//
pvc
ProtectionPlugin holds state for and implements the admission plugin.
type
pvc
ProtectionPlugin
struct
{
//
storage
ProtectionPlugin holds state for and implements the admission plugin.
type
storage
ProtectionPlugin
struct
{
*
admission
.
Handler
lister
corelisters
.
PersistentVolumeClaimLister
pvcLister
corelisters
.
PersistentVolumeClaimLister
pvLister
corelisters
.
PersistentVolumeLister
}
var
_
admission
.
Interface
=
&
pvc
ProtectionPlugin
{}
var
_
=
kubeapiserveradmission
.
WantsInternalKubeInformerFactory
(
&
pvc
ProtectionPlugin
{})
var
_
admission
.
Interface
=
&
storage
ProtectionPlugin
{}
var
_
=
kubeapiserveradmission
.
WantsInternalKubeInformerFactory
(
&
storage
ProtectionPlugin
{})
// newPlugin creates a new admission plugin.
func
newPlugin
()
*
pvc
ProtectionPlugin
{
return
&
pvc
ProtectionPlugin
{
func
newPlugin
()
*
storage
ProtectionPlugin
{
return
&
storage
ProtectionPlugin
{
Handler
:
admission
.
NewHandler
(
admission
.
Create
),
}
}
func
(
c
*
pvcProtectionPlugin
)
SetInternalKubeInformerFactory
(
f
informers
.
SharedInformerFactory
)
{
informer
:=
f
.
Core
()
.
InternalVersion
()
.
PersistentVolumeClaims
()
c
.
lister
=
informer
.
Lister
()
c
.
SetReadyFunc
(
informer
.
Informer
()
.
HasSynced
)
func
(
c
*
storageProtectionPlugin
)
SetInternalKubeInformerFactory
(
f
informers
.
SharedInformerFactory
)
{
pvcInformer
:=
f
.
Core
()
.
InternalVersion
()
.
PersistentVolumeClaims
()
c
.
pvcLister
=
pvcInformer
.
Lister
()
pvInformer
:=
f
.
Core
()
.
InternalVersion
()
.
PersistentVolumes
()
c
.
pvLister
=
pvInformer
.
Lister
()
c
.
SetReadyFunc
(
func
()
bool
{
return
pvcInformer
.
Informer
()
.
HasSynced
()
&&
pvInformer
.
Informer
()
.
HasSynced
()
})
}
// ValidateInitialization ensures lister is set.
func
(
c
*
pvcProtectionPlugin
)
ValidateInitialization
()
error
{
if
c
.
lister
==
nil
{
return
fmt
.
Errorf
(
"missing lister"
)
func
(
c
*
storageProtectionPlugin
)
ValidateInitialization
()
error
{
if
c
.
pvcLister
==
nil
{
return
fmt
.
Errorf
(
"missing PVC lister"
)
}
if
c
.
pvLister
==
nil
{
return
fmt
.
Errorf
(
"missing PV lister"
)
}
return
nil
}
// Admit sets finalizer on all PVCs. The finalizer is removed by
// PVCProtectionController when it's not referenced by any pod.
var
(
pvResource
=
api
.
Resource
(
"persistentvolumes"
)
pvcResource
=
api
.
Resource
(
"persistentvolumeclaims"
)
)
// Admit sets finalizer on all PVCs(PVs). The finalizer is removed by
// PVCProtectionController(PVProtectionController) when it's not referenced.
//
// This prevents users from deleting a PVC that's used by a running pod.
func
(
c
*
pvcProtectionPlugin
)
Admit
(
a
admission
.
Attributes
)
error
{
// This also prevents admin from deleting a PV that's bound by a PVC
func
(
c
*
storageProtectionPlugin
)
Admit
(
a
admission
.
Attributes
)
error
{
if
!
feature
.
DefaultFeatureGate
.
Enabled
(
features
.
StorageProtection
)
{
return
nil
}
if
a
.
GetResource
()
.
GroupResource
()
!=
api
.
Resource
(
"persistentvolumeclaims"
)
{
switch
a
.
GetResource
()
.
GroupResource
()
{
case
pvResource
:
return
c
.
admitPV
(
a
)
case
pvcResource
:
return
c
.
admitPVC
(
a
)
default
:
return
nil
}
}
func
(
c
*
storageProtectionPlugin
)
admitPV
(
a
admission
.
Attributes
)
error
{
if
len
(
a
.
GetSubresource
())
!=
0
{
return
nil
}
pv
,
ok
:=
a
.
GetObject
()
.
(
*
api
.
PersistentVolume
)
// if we can't convert the obj to PV, just return
if
!
ok
{
return
nil
}
for
_
,
f
:=
range
pv
.
Finalizers
{
if
f
==
volumeutil
.
PVProtectionFinalizer
{
// Finalizer is already present, nothing to do
return
nil
}
}
glog
.
V
(
4
)
.
Infof
(
"adding PV protection finalizer to %s"
,
pv
.
Name
)
pv
.
Finalizers
=
append
(
pv
.
Finalizers
,
volumeutil
.
PVProtectionFinalizer
)
return
nil
}
func
(
c
*
storageProtectionPlugin
)
admitPVC
(
a
admission
.
Attributes
)
error
{
if
len
(
a
.
GetSubresource
())
!=
0
{
return
nil
}
pvc
,
ok
:=
a
.
GetObject
()
.
(
*
api
.
PersistentVolumeClaim
)
// if we can't convert the
n we don't handle this object so
just return
// if we can't convert the
obj to PVC,
just return
if
!
ok
{
return
nil
}
...
...
plugin/pkg/admission/
persistentvolumeclaim/pvc
protection/admission_test.go
→
plugin/pkg/admission/
storage/storage
protection/admission_test.go
View file @
cbfa0cc8
...
...
@@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/
package
pvc
protection
package
storage
protection
import
(
"fmt"
...
...
@@ -25,6 +25,7 @@ import (
metav1
"k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apiserver/pkg/admission"
"k8s.io/apiserver/pkg/util/feature"
api
"k8s.io/kubernetes/pkg/apis/core"
...
...
@@ -43,32 +44,76 @@ func TestAdmit(t *testing.T) {
Namespace
:
"ns"
,
},
}
pv
:=
&
api
.
PersistentVolume
{
TypeMeta
:
metav1
.
TypeMeta
{
Kind
:
"PersistentVolume"
,
},
ObjectMeta
:
metav1
.
ObjectMeta
{
Name
:
"pv"
,
},
}
claimWithFinalizer
:=
claim
.
DeepCopy
()
claimWithFinalizer
.
Finalizers
=
[]
string
{
volumeutil
.
PVCProtectionFinalizer
}
pvWithFinalizer
:=
pv
.
DeepCopy
()
pvWithFinalizer
.
Finalizers
=
[]
string
{
volumeutil
.
PVProtectionFinalizer
}
tests
:=
[]
struct
{
name
string
resource
schema
.
GroupVersionResource
object
runtime
.
Object
expectedObject
runtime
.
Object
featureEnabled
bool
namespace
string
}{
{
"create -> add finalizer"
,
api
.
SchemeGroupVersion
.
WithResource
(
"persistentvolumeclaims"
),
claim
,
claimWithFinalizer
,
true
,
claim
.
Namespace
,
},
{
"finalizer already exists -> no new finalizer"
,
api
.
SchemeGroupVersion
.
WithResource
(
"persistentvolumeclaims"
),
claimWithFinalizer
,
claimWithFinalizer
,
true
,
claimWithFinalizer
.
Namespace
,
},
{
"disabled feature -> no finalizer"
,
api
.
SchemeGroupVersion
.
WithResource
(
"persistentvolumeclaims"
),
claim
,
claim
,
false
,
claim
.
Namespace
,
},
{
"create -> add finalizer"
,
api
.
SchemeGroupVersion
.
WithResource
(
"persistentvolumes"
),
pv
,
pvWithFinalizer
,
true
,
pv
.
Namespace
,
},
{
"finalizer already exists -> no new finalizer"
,
api
.
SchemeGroupVersion
.
WithResource
(
"persistentvolumes"
),
pvWithFinalizer
,
pvWithFinalizer
,
true
,
pvWithFinalizer
.
Namespace
,
},
{
"disabled feature -> no finalizer"
,
api
.
SchemeGroupVersion
.
WithResource
(
"persistentvolumes"
),
pv
,
pv
,
false
,
pv
.
Namespace
,
},
}
...
...
@@ -82,10 +127,10 @@ func TestAdmit(t *testing.T) {
attrs
:=
admission
.
NewAttributesRecord
(
obj
,
// new object
obj
.
DeepCopyObject
(),
// old object, copy to be sure it's not modified
api
.
Kind
(
"PersistentVolumeClaim"
)
.
WithVersion
(
"version"
)
,
claim
.
N
amespace
,
claim
.
Name
,
api
.
Resource
(
"persistentvolumeclaims"
)
.
WithVersion
(
"version"
)
,
schema
.
GroupVersionKind
{}
,
test
.
n
amespace
,
"foo"
,
test
.
resource
,
""
,
// subresource
admission
.
Create
,
nil
,
// userInfo
...
...
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