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
fcf8853d
Commit
fcf8853d
authored
Sep 08, 2016
by
Chao Xu
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
make sure finalizer prevents deletion on storage that supports graceful deletion
parent
f61a677a
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
74 additions
and
4 deletions
+74
-4
store.go
pkg/registry/generic/registry/store.go
+1
-1
store_test.go
pkg/registry/generic/registry/store_test.go
+73
-3
No files found.
pkg/registry/generic/registry/store.go
View file @
fcf8853d
...
@@ -599,9 +599,9 @@ func (e *Store) updateForGracefulDeletionAndFinalizers(ctx api.Context, name, ke
...
@@ -599,9 +599,9 @@ func (e *Store) updateForGracefulDeletionAndFinalizers(ctx api.Context, name, ke
existingAccessor
.
SetFinalizers
(
newFinalizers
)
existingAccessor
.
SetFinalizers
(
newFinalizers
)
}
}
pendingFinalizers
=
len
(
existingAccessor
.
GetFinalizers
())
!=
0
if
!
graceful
{
if
!
graceful
{
// set the DeleteGracePeriods to 0 if the object has pendingFinalizers but not supporting graceful deletion
// set the DeleteGracePeriods to 0 if the object has pendingFinalizers but not supporting graceful deletion
pendingFinalizers
=
len
(
existingAccessor
.
GetFinalizers
())
!=
0
if
pendingFinalizers
{
if
pendingFinalizers
{
glog
.
V
(
6
)
.
Infof
(
"update the DeletionTimestamp to
\"
now
\"
and GracePeriodSeconds to 0 for object %s, because it has pending finalizers"
,
name
)
glog
.
V
(
6
)
.
Infof
(
"update the DeletionTimestamp to
\"
now
\"
and GracePeriodSeconds to 0 for object %s, because it has pending finalizers"
,
name
)
err
=
markAsDeleting
(
existing
)
err
=
markAsDeleting
(
existing
)
...
...
pkg/registry/generic/registry/store_test.go
View file @
fcf8853d
...
@@ -21,6 +21,7 @@ import (
...
@@ -21,6 +21,7 @@ import (
"path"
"path"
"reflect"
"reflect"
"strconv"
"strconv"
"strings"
"sync"
"sync"
"testing"
"testing"
"time"
"time"
...
@@ -46,9 +47,18 @@ import (
...
@@ -46,9 +47,18 @@ import (
"k8s.io/kubernetes/pkg/util/sets"
"k8s.io/kubernetes/pkg/util/sets"
"k8s.io/kubernetes/pkg/util/validation/field"
"k8s.io/kubernetes/pkg/util/validation/field"
"k8s.io/kubernetes/pkg/util/wait"
"k8s.io/kubernetes/pkg/util/wait"
"strings"
)
)
type
testGracefulStrategy
struct
{
testRESTStrategy
}
func
(
t
testGracefulStrategy
)
CheckGracefulDelete
(
ctx
api
.
Context
,
obj
runtime
.
Object
,
options
*
api
.
DeleteOptions
)
bool
{
return
true
}
var
_
rest
.
RESTGracefulDeleteStrategy
=
testGracefulStrategy
{}
type
testOrphanDeleteStrategy
struct
{
type
testOrphanDeleteStrategy
struct
{
*
testRESTStrategy
*
testRESTStrategy
}
}
...
@@ -608,7 +618,67 @@ func TestStoreDelete(t *testing.T) {
...
@@ -608,7 +618,67 @@ func TestStoreDelete(t *testing.T) {
}
}
}
}
func
TestStoreHandleFinalizers
(
t
*
testing
.
T
)
{
func
TestGracefulStoreHandleFinalizers
(
t
*
testing
.
T
)
{
EnableGarbageCollector
=
true
initialGeneration
:=
int64
(
1
)
defer
func
()
{
EnableGarbageCollector
=
false
}()
podWithFinalizer
:=
&
api
.
Pod
{
ObjectMeta
:
api
.
ObjectMeta
{
Name
:
"foo"
,
Finalizers
:
[]
string
{
"foo.com/x"
},
Generation
:
initialGeneration
},
Spec
:
api
.
PodSpec
{
NodeName
:
"machine"
},
}
testContext
:=
api
.
WithNamespace
(
api
.
NewContext
(),
"test"
)
destroyFunc
,
registry
:=
NewTestGenericStoreRegistry
(
t
)
defaultDeleteStrategy
:=
testRESTStrategy
{
api
.
Scheme
,
api
.
SimpleNameGenerator
,
true
,
false
,
true
}
registry
.
DeleteStrategy
=
testGracefulStrategy
{
defaultDeleteStrategy
}
defer
destroyFunc
()
// create pod
_
,
err
:=
registry
.
Create
(
testContext
,
podWithFinalizer
)
if
err
!=
nil
{
t
.
Errorf
(
"Unexpected error: %v"
,
err
)
}
// delete the pod with grace period=0, the pod should still exist because it has a finalizer
_
,
err
=
registry
.
Delete
(
testContext
,
podWithFinalizer
.
Name
,
api
.
NewDeleteOptions
(
0
))
if
err
!=
nil
{
t
.
Fatalf
(
"Unexpected error: %v"
,
err
)
}
_
,
err
=
registry
.
Get
(
testContext
,
podWithFinalizer
.
Name
)
if
err
!=
nil
{
t
.
Fatalf
(
"Unexpected error: %v"
,
err
)
}
updatedPodWithFinalizer
:=
&
api
.
Pod
{
ObjectMeta
:
api
.
ObjectMeta
{
Name
:
"foo"
,
Finalizers
:
[]
string
{
"foo.com/x"
},
ResourceVersion
:
podWithFinalizer
.
ObjectMeta
.
ResourceVersion
},
Spec
:
api
.
PodSpec
{
NodeName
:
"machine"
},
}
_
,
_
,
err
=
registry
.
Update
(
testContext
,
updatedPodWithFinalizer
.
ObjectMeta
.
Name
,
rest
.
DefaultUpdatedObjectInfo
(
updatedPodWithFinalizer
,
api
.
Scheme
))
if
err
!=
nil
{
t
.
Fatalf
(
"Unexpected error: %v"
,
err
)
}
// the object should still exist, because it still has a finalizer
_
,
err
=
registry
.
Get
(
testContext
,
podWithFinalizer
.
Name
)
if
err
!=
nil
{
t
.
Fatalf
(
"Unexpected error: %v"
,
err
)
}
podWithNoFinalizer
:=
&
api
.
Pod
{
ObjectMeta
:
api
.
ObjectMeta
{
Name
:
"foo"
,
ResourceVersion
:
podWithFinalizer
.
ObjectMeta
.
ResourceVersion
},
Spec
:
api
.
PodSpec
{
NodeName
:
"anothermachine"
},
}
_
,
_
,
err
=
registry
.
Update
(
testContext
,
podWithFinalizer
.
ObjectMeta
.
Name
,
rest
.
DefaultUpdatedObjectInfo
(
podWithNoFinalizer
,
api
.
Scheme
))
if
err
!=
nil
{
t
.
Fatalf
(
"Unexpected error: %v"
,
err
)
}
// the pod should be removed, because its finalizer is removed
_
,
err
=
registry
.
Get
(
testContext
,
podWithFinalizer
.
Name
)
if
!
errors
.
IsNotFound
(
err
)
{
t
.
Fatalf
(
"Unexpected error: %v"
,
err
)
}
}
func
TestNonGracefulStoreHandleFinalizers
(
t
*
testing
.
T
)
{
EnableGarbageCollector
=
true
EnableGarbageCollector
=
true
initialGeneration
:=
int64
(
1
)
initialGeneration
:=
int64
(
1
)
defer
func
()
{
EnableGarbageCollector
=
false
}()
defer
func
()
{
EnableGarbageCollector
=
false
}()
...
@@ -678,7 +748,7 @@ func TestStoreHandleFinalizers(t *testing.T) {
...
@@ -678,7 +748,7 @@ func TestStoreHandleFinalizers(t *testing.T) {
if
err
!=
nil
{
if
err
!=
nil
{
t
.
Errorf
(
"Unexpected error: %v"
,
err
)
t
.
Errorf
(
"Unexpected error: %v"
,
err
)
}
}
// the pod should be removed, because it
'
s finalizer is removed
// the pod should be removed, because its finalizer is removed
_
,
err
=
registry
.
Get
(
testContext
,
podWithFinalizer
.
Name
)
_
,
err
=
registry
.
Get
(
testContext
,
podWithFinalizer
.
Name
)
if
!
errors
.
IsNotFound
(
err
)
{
if
!
errors
.
IsNotFound
(
err
)
{
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