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
de3f0978
Commit
de3f0978
authored
Jul 30, 2017
by
Mayank Kumar
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
simplify disruption controller
parent
58c85e27
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
22 additions
and
34 deletions
+22
-34
disruption.go
pkg/controller/disruption/disruption.go
+22
-34
No files found.
pkg/controller/disruption/disruption.go
View file @
de3f0978
...
@@ -107,7 +107,7 @@ type controllerAndScale struct {
...
@@ -107,7 +107,7 @@ type controllerAndScale struct {
// podControllerFinder is a function type that maps a pod to a list of
// podControllerFinder is a function type that maps a pod to a list of
// controllers and their scale.
// controllers and their scale.
type
podControllerFinder
func
(
*
v1
.
Pod
)
(
[]
controllerAndScale
,
error
)
type
podControllerFinder
func
(
*
v1
.
Pod
)
(
*
controllerAndScale
,
error
)
func
NewDisruptionController
(
func
NewDisruptionController
(
podInformer
coreinformers
.
PodInformer
,
podInformer
coreinformers
.
PodInformer
,
...
@@ -170,8 +170,8 @@ func NewDisruptionController(
...
@@ -170,8 +170,8 @@ func NewDisruptionController(
// and we may well need further tweaks just to be able to access scale
// and we may well need further tweaks just to be able to access scale
// subresources.
// subresources.
func
(
dc
*
DisruptionController
)
finders
()
[]
podControllerFinder
{
func
(
dc
*
DisruptionController
)
finders
()
[]
podControllerFinder
{
return
[]
podControllerFinder
{
dc
.
getPodReplicationController
s
,
dc
.
getPodDeployments
,
dc
.
getPodReplicaSets
,
return
[]
podControllerFinder
{
dc
.
getPodReplicationController
,
dc
.
getPodDeployment
,
dc
.
getPodReplicaSet
,
dc
.
getPodStatefulSet
s
}
dc
.
getPodStatefulSet
}
}
}
var
(
var
(
...
@@ -181,9 +181,8 @@ var (
...
@@ -181,9 +181,8 @@ var (
controllerKindDep
=
v1beta1
.
SchemeGroupVersion
.
WithKind
(
"Deployment"
)
controllerKindDep
=
v1beta1
.
SchemeGroupVersion
.
WithKind
(
"Deployment"
)
)
)
// getPodReplicaSets finds replicasets which have no matching deployments.
// getPodReplicaSet finds a replicaset which has no matching deployments.
func
(
dc
*
DisruptionController
)
getPodReplicaSets
(
pod
*
v1
.
Pod
)
([]
controllerAndScale
,
error
)
{
func
(
dc
*
DisruptionController
)
getPodReplicaSet
(
pod
*
v1
.
Pod
)
(
*
controllerAndScale
,
error
)
{
var
casSlice
[]
controllerAndScale
controllerRef
:=
metav1
.
GetControllerOf
(
pod
)
controllerRef
:=
metav1
.
GetControllerOf
(
pod
)
if
controllerRef
==
nil
{
if
controllerRef
==
nil
{
return
nil
,
nil
return
nil
,
nil
...
@@ -204,13 +203,11 @@ func (dc *DisruptionController) getPodReplicaSets(pod *v1.Pod) ([]controllerAndS
...
@@ -204,13 +203,11 @@ func (dc *DisruptionController) getPodReplicaSets(pod *v1.Pod) ([]controllerAndS
// Skip RS if it's controlled by a Deployment.
// Skip RS if it's controlled by a Deployment.
return
nil
,
nil
return
nil
,
nil
}
}
casSlice
=
append
(
casSlice
,
controllerAndScale
{
rs
.
UID
,
*
(
rs
.
Spec
.
Replicas
)})
return
&
controllerAndScale
{
rs
.
UID
,
*
(
rs
.
Spec
.
Replicas
)},
nil
return
casSlice
,
nil
}
}
// getPodStatefulSet returns the statefulset managing the given pod.
// getPodStatefulSet returns the statefulset managing the given pod.
func
(
dc
*
DisruptionController
)
getPodStatefulSets
(
pod
*
v1
.
Pod
)
([]
controllerAndScale
,
error
)
{
func
(
dc
*
DisruptionController
)
getPodStatefulSet
(
pod
*
v1
.
Pod
)
(
*
controllerAndScale
,
error
)
{
var
casSlice
[]
controllerAndScale
controllerRef
:=
metav1
.
GetControllerOf
(
pod
)
controllerRef
:=
metav1
.
GetControllerOf
(
pod
)
if
controllerRef
==
nil
{
if
controllerRef
==
nil
{
return
nil
,
nil
return
nil
,
nil
...
@@ -227,13 +224,11 @@ func (dc *DisruptionController) getPodStatefulSets(pod *v1.Pod) ([]controllerAnd
...
@@ -227,13 +224,11 @@ func (dc *DisruptionController) getPodStatefulSets(pod *v1.Pod) ([]controllerAnd
return
nil
,
nil
return
nil
,
nil
}
}
casSlice
=
append
(
casSlice
,
controllerAndScale
{
ss
.
UID
,
*
(
ss
.
Spec
.
Replicas
)})
return
&
controllerAndScale
{
ss
.
UID
,
*
(
ss
.
Spec
.
Replicas
)},
nil
return
casSlice
,
nil
}
}
// getPodDeployments finds deployments for any replicasets which are being managed by deployments.
// getPodDeployments finds deployments for any replicasets which are being managed by deployments.
func
(
dc
*
DisruptionController
)
getPodDeployments
(
pod
*
v1
.
Pod
)
([]
controllerAndScale
,
error
)
{
func
(
dc
*
DisruptionController
)
getPodDeployment
(
pod
*
v1
.
Pod
)
(
*
controllerAndScale
,
error
)
{
var
casSlice
[]
controllerAndScale
controllerRef
:=
metav1
.
GetControllerOf
(
pod
)
controllerRef
:=
metav1
.
GetControllerOf
(
pod
)
if
controllerRef
==
nil
{
if
controllerRef
==
nil
{
return
nil
,
nil
return
nil
,
nil
...
@@ -264,12 +259,10 @@ func (dc *DisruptionController) getPodDeployments(pod *v1.Pod) ([]controllerAndS
...
@@ -264,12 +259,10 @@ func (dc *DisruptionController) getPodDeployments(pod *v1.Pod) ([]controllerAndS
if
deployment
.
UID
!=
controllerRef
.
UID
{
if
deployment
.
UID
!=
controllerRef
.
UID
{
return
nil
,
nil
return
nil
,
nil
}
}
casSlice
=
append
(
casSlice
,
controllerAndScale
{
deployment
.
UID
,
*
(
deployment
.
Spec
.
Replicas
)})
return
&
controllerAndScale
{
deployment
.
UID
,
*
(
deployment
.
Spec
.
Replicas
)},
nil
return
casSlice
,
nil
}
}
func
(
dc
*
DisruptionController
)
getPodReplicationControllers
(
pod
*
v1
.
Pod
)
([]
controllerAndScale
,
error
)
{
func
(
dc
*
DisruptionController
)
getPodReplicationController
(
pod
*
v1
.
Pod
)
(
*
controllerAndScale
,
error
)
{
var
casSlice
[]
controllerAndScale
controllerRef
:=
metav1
.
GetControllerOf
(
pod
)
controllerRef
:=
metav1
.
GetControllerOf
(
pod
)
if
controllerRef
==
nil
{
if
controllerRef
==
nil
{
return
nil
,
nil
return
nil
,
nil
...
@@ -285,8 +278,7 @@ func (dc *DisruptionController) getPodReplicationControllers(pod *v1.Pod) ([]con
...
@@ -285,8 +278,7 @@ func (dc *DisruptionController) getPodReplicationControllers(pod *v1.Pod) ([]con
if
rc
.
UID
!=
controllerRef
.
UID
{
if
rc
.
UID
!=
controllerRef
.
UID
{
return
nil
,
nil
return
nil
,
nil
}
}
casSlice
=
append
(
casSlice
,
controllerAndScale
{
rc
.
UID
,
*
(
rc
.
Spec
.
Replicas
)})
return
&
controllerAndScale
{
rc
.
UID
,
*
(
rc
.
Spec
.
Replicas
)},
nil
return
casSlice
,
nil
}
}
func
(
dc
*
DisruptionController
)
Run
(
stopCh
<-
chan
struct
{})
{
func
(
dc
*
DisruptionController
)
Run
(
stopCh
<-
chan
struct
{})
{
...
@@ -590,30 +582,26 @@ func (dc *DisruptionController) getExpectedScale(pdb *policy.PodDisruptionBudget
...
@@ -590,30 +582,26 @@ func (dc *DisruptionController) getExpectedScale(pdb *policy.PodDisruptionBudget
// A mapping from controllers to their scale.
// A mapping from controllers to their scale.
controllerScale
:=
map
[
types
.
UID
]
int32
{}
controllerScale
:=
map
[
types
.
UID
]
int32
{}
// 1. Find the controller(s) for each pod. If any pod has 0 controllers,
// 1. Find the controller for each pod. If any pod has 0 controllers,
// that's an error. If any pod has more than 1 controller, that's also an
// that's an error. With ControllerRef, a pod can only have 1 controller.
// error.
for
_
,
pod
:=
range
pods
{
for
_
,
pod
:=
range
pods
{
controllerCount
:=
0
foundController
:=
false
for
_
,
finder
:=
range
dc
.
finders
()
{
for
_
,
finder
:=
range
dc
.
finders
()
{
var
controller
s
[]
controllerAndScale
var
controller
NScale
*
controllerAndScale
controller
s
,
err
=
finder
(
pod
)
controller
NScale
,
err
=
finder
(
pod
)
if
err
!=
nil
{
if
err
!=
nil
{
return
return
}
}
for
_
,
controller
:=
range
controllers
{
if
controllerNScale
!=
nil
{
controllerScale
[
controller
.
UID
]
=
controller
.
scale
controllerScale
[
controllerNScale
.
UID
]
=
controllerNScale
.
scale
controllerCount
++
foundController
=
true
break
}
}
}
}
if
controllerCount
==
0
{
if
!
foundController
{
err
=
fmt
.
Errorf
(
"found no controllers for pod %q"
,
pod
.
Name
)
err
=
fmt
.
Errorf
(
"found no controllers for pod %q"
,
pod
.
Name
)
dc
.
recorder
.
Event
(
pdb
,
v1
.
EventTypeWarning
,
"NoControllers"
,
err
.
Error
())
dc
.
recorder
.
Event
(
pdb
,
v1
.
EventTypeWarning
,
"NoControllers"
,
err
.
Error
())
return
return
}
else
if
controllerCount
>
1
{
err
=
fmt
.
Errorf
(
"pod %q has %v>1 controllers"
,
pod
.
Name
,
controllerCount
)
dc
.
recorder
.
Event
(
pdb
,
v1
.
EventTypeWarning
,
"TooManyControllers"
,
err
.
Error
())
return
}
}
}
}
...
...
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