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
60f13391
Commit
60f13391
authored
Apr 28, 2016
by
Marcin Wielgus
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Small refactoring around drain - move drain logic to separate function.
parent
a8d9a8da
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
54 additions
and
36 deletions
+54
-36
drain.go
pkg/kubectl/cmd/drain.go
+54
-36
No files found.
pkg/kubectl/cmd/drain.go
View file @
60f13391
...
...
@@ -183,7 +183,7 @@ func (o *DrainOptions) RunDrain() error {
return
err
}
pods
,
err
:=
o
.
G
etPodsForDeletion
()
pods
,
err
:=
o
.
g
etPodsForDeletion
()
if
err
!=
nil
{
return
err
}
...
...
@@ -195,17 +195,55 @@ func (o *DrainOptions) RunDrain() error {
return
nil
}
//
G
etPodsForDeletion returns all the pods we're going to delete. If there are
//
g
etPodsForDeletion returns all the pods we're going to delete. If there are
// any unmanaged pods and the user didn't pass --force, we return that list in
// an error.
func
(
o
*
DrainOptions
)
GetPodsForDeletion
()
([]
api
.
Pod
,
error
)
{
pods
:=
[]
api
.
Pod
{}
podList
,
err
:=
o
.
client
.
Pods
(
api
.
NamespaceAll
)
.
List
(
api
.
ListOptions
{
FieldSelector
:
fields
.
SelectorFromSet
(
fields
.
Set
{
"spec.nodeName"
:
o
.
nodeInfo
.
Name
})})
func
(
o
*
DrainOptions
)
getPodsForDeletion
()
([]
api
.
Pod
,
error
)
{
pods
,
unreplicatedPodNames
,
daemonSetPodNames
,
err
:=
GetPodsForDeletionOnNodeDrain
(
o
.
client
,
o
.
nodeInfo
.
Name
,
o
.
factory
.
Decoder
(
true
),
o
.
Force
,
o
.
IgnoreDaemonsets
,
)
if
err
!=
nil
{
return
pods
,
err
return
[]
api
.
Pod
{},
err
}
daemonSetErrors
:=
!
o
.
IgnoreDaemonsets
&&
len
(
daemonSetPodNames
)
>
0
unreplicatedErrors
:=
!
o
.
Force
&&
len
(
unreplicatedPodNames
)
>
0
switch
{
case
daemonSetErrors
&&
unreplicatedErrors
:
return
[]
api
.
Pod
{},
errors
.
New
(
unmanagedMsg
(
unreplicatedPodNames
,
daemonSetPodNames
,
true
))
case
daemonSetErrors
&&
!
unreplicatedErrors
:
return
[]
api
.
Pod
{},
errors
.
New
(
unmanagedMsg
([]
string
{},
daemonSetPodNames
,
true
))
case
unreplicatedErrors
&&
!
daemonSetErrors
:
return
[]
api
.
Pod
{},
errors
.
New
(
unmanagedMsg
(
unreplicatedPodNames
,
[]
string
{},
true
))
}
if
len
(
unreplicatedPodNames
)
>
0
{
fmt
.
Fprintf
(
o
.
out
,
"WARNING: About to delete these %s
\n
"
,
unmanagedMsg
(
unreplicatedPodNames
,
[]
string
{},
false
))
}
if
len
(
daemonSetPodNames
)
>
0
{
fmt
.
Fprintf
(
o
.
out
,
"WARNING: Skipping %s
\n
"
,
unmanagedMsg
([]
string
{},
daemonSetPodNames
,
false
))
}
return
pods
,
nil
}
// GetPodsForDeletionOnNodeDrain returns pods that should be deleted on node drain as well as some extra information
// about possibly problematic pods (unreplicated and deamon sets).
func
GetPodsForDeletionOnNodeDrain
(
client
*
client
.
Client
,
nodename
string
,
decoder
runtime
.
Decoder
,
force
bool
,
ignoreDeamonSet
bool
)
(
pods
[]
api
.
Pod
,
unreplicatedPodNames
[]
string
,
daemonSetPodNames
[]
string
,
finalError
error
)
{
pods
=
[]
api
.
Pod
{}
unreplicatedPodNames
=
[]
string
{}
daemonSetPodNames
=
[]
string
{}
podList
,
err
:=
client
.
Pods
(
api
.
NamespaceAll
)
.
List
(
api
.
ListOptions
{
FieldSelector
:
fields
.
SelectorFromSet
(
fields
.
Set
{
"spec.nodeName"
:
nodename
})})
if
err
!=
nil
{
return
[]
api
.
Pod
{},
[]
string
{},
[]
string
{},
err
}
unreplicatedPodNames
:=
[]
string
{}
daemonSetPodNames
:=
[]
string
{}
for
_
,
pod
:=
range
podList
.
Items
{
_
,
found
:=
pod
.
ObjectMeta
.
Annotations
[
types
.
ConfigMirrorAnnotationKey
]
...
...
@@ -220,11 +258,11 @@ func (o *DrainOptions) GetPodsForDeletion() ([]api.Pod, error) {
if
found
{
// Now verify that the specified creator actually exists.
var
sr
api
.
SerializedReference
if
err
:=
runtime
.
DecodeInto
(
o
.
factory
.
Decoder
(
true
)
,
[]
byte
(
creatorRef
),
&
sr
);
err
!=
nil
{
return
pods
,
err
if
err
:=
runtime
.
DecodeInto
(
decoder
,
[]
byte
(
creatorRef
),
&
sr
);
err
!=
nil
{
return
[]
api
.
Pod
{},
[]
string
{},
[]
string
{}
,
err
}
if
sr
.
Reference
.
Kind
==
"ReplicationController"
{
rc
,
err
:=
o
.
client
.
ReplicationControllers
(
sr
.
Reference
.
Namespace
)
.
Get
(
sr
.
Reference
.
Name
)
rc
,
err
:=
client
.
ReplicationControllers
(
sr
.
Reference
.
Namespace
)
.
Get
(
sr
.
Reference
.
Name
)
// Assume the only reason for an error is because the RC is
// gone/missing, not for any other cause. TODO(mml): something more
// sophisticated than this
...
...
@@ -232,7 +270,7 @@ func (o *DrainOptions) GetPodsForDeletion() ([]api.Pod, error) {
replicated
=
true
}
}
else
if
sr
.
Reference
.
Kind
==
"DaemonSet"
{
ds
,
err
:=
o
.
client
.
DaemonSets
(
sr
.
Reference
.
Namespace
)
.
Get
(
sr
.
Reference
.
Name
)
ds
,
err
:=
client
.
DaemonSets
(
sr
.
Reference
.
Namespace
)
.
Get
(
sr
.
Reference
.
Name
)
// Assume the only reason for an error is because the DaemonSet is
// gone/missing, not for any other cause. TODO(mml): something more
...
...
@@ -245,7 +283,7 @@ func (o *DrainOptions) GetPodsForDeletion() ([]api.Pod, error) {
daemonset_pod
=
true
}
}
else
if
sr
.
Reference
.
Kind
==
"Job"
{
job
,
err
:=
o
.
client
.
ExtensionsClient
.
Jobs
(
sr
.
Reference
.
Namespace
)
.
Get
(
sr
.
Reference
.
Name
)
job
,
err
:=
client
.
ExtensionsClient
.
Jobs
(
sr
.
Reference
.
Namespace
)
.
Get
(
sr
.
Reference
.
Name
)
// Assume the only reason for an error is because the Job is
// gone/missing, not for any other cause. TODO(mml): something more
...
...
@@ -254,7 +292,7 @@ func (o *DrainOptions) GetPodsForDeletion() ([]api.Pod, error) {
replicated
=
true
}
}
else
if
sr
.
Reference
.
Kind
==
"ReplicaSet"
{
rs
,
err
:=
o
.
client
.
ExtensionsClient
.
ReplicaSets
(
sr
.
Reference
.
Namespace
)
.
Get
(
sr
.
Reference
.
Name
)
rs
,
err
:=
client
.
ExtensionsClient
.
ReplicaSets
(
sr
.
Reference
.
Namespace
)
.
Get
(
sr
.
Reference
.
Name
)
// Assume the only reason for an error is because the RS is
// gone/missing, not for any other cause. TODO(mml): something more
...
...
@@ -270,34 +308,14 @@ func (o *DrainOptions) GetPodsForDeletion() ([]api.Pod, error) {
daemonSetPodNames
=
append
(
daemonSetPodNames
,
pod
.
Name
)
case
!
replicated
:
unreplicatedPodNames
=
append
(
unreplicatedPodNames
,
pod
.
Name
)
if
o
.
F
orce
{
if
f
orce
{
pods
=
append
(
pods
,
pod
)
}
default
:
pods
=
append
(
pods
,
pod
)
}
}
daemonSetErrors
:=
!
o
.
IgnoreDaemonsets
&&
len
(
daemonSetPodNames
)
>
0
unreplicatedErrors
:=
!
o
.
Force
&&
len
(
unreplicatedPodNames
)
>
0
switch
{
case
daemonSetErrors
&&
unreplicatedErrors
:
return
[]
api
.
Pod
{},
errors
.
New
(
unmanagedMsg
(
unreplicatedPodNames
,
daemonSetPodNames
,
true
))
case
daemonSetErrors
&&
!
unreplicatedErrors
:
return
[]
api
.
Pod
{},
errors
.
New
(
unmanagedMsg
([]
string
{},
daemonSetPodNames
,
true
))
case
unreplicatedErrors
&&
!
daemonSetErrors
:
return
[]
api
.
Pod
{},
errors
.
New
(
unmanagedMsg
(
unreplicatedPodNames
,
[]
string
{},
true
))
}
if
len
(
unreplicatedPodNames
)
>
0
{
fmt
.
Fprintf
(
o
.
out
,
"WARNING: About to delete these %s
\n
"
,
unmanagedMsg
(
unreplicatedPodNames
,
[]
string
{},
false
))
}
if
len
(
daemonSetPodNames
)
>
0
{
fmt
.
Fprintf
(
o
.
out
,
"WARNING: Skipping %s
\n
"
,
unmanagedMsg
([]
string
{},
daemonSetPodNames
,
false
))
}
return
pods
,
nil
return
pods
,
unreplicatedPodNames
,
daemonSetPodNames
,
nil
}
// Helper for generating errors or warnings about unmanaged pods.
...
...
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