Skip to content

Commit d76b08d

Browse files
deprecate created-by annotation for pod drain
1 parent 7560142 commit d76b08d

File tree

3 files changed

+81
-28
lines changed

3 files changed

+81
-28
lines changed

pkg/kubectl/cmd/BUILD

+1
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ go_library(
8484
"//pkg/client/clientset_generated/internalclientset/typed/core/internalversion:go_default_library",
8585
"//pkg/client/clientset_generated/internalclientset/typed/rbac/internalversion:go_default_library",
8686
"//pkg/client/unversioned:go_default_library",
87+
"//pkg/controller:go_default_library",
8788
"//pkg/kubectl:go_default_library",
8889
"//pkg/kubectl/cmd/auth:go_default_library",
8990
"//pkg/kubectl/cmd/config:go_default_library",

pkg/kubectl/cmd/drain.go

+24-25
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import (
3838
"k8s.io/kubernetes/pkg/api"
3939
"k8s.io/kubernetes/pkg/apis/policy"
4040
"k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset"
41+
"k8s.io/kubernetes/pkg/controller"
4142
"k8s.io/kubernetes/pkg/kubectl"
4243
"k8s.io/kubernetes/pkg/kubectl/cmd/templates"
4344
cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
@@ -266,40 +267,38 @@ func (o *DrainOptions) deleteOrEvictPodsSimple() error {
266267
return err
267268
}
268269

269-
func (o *DrainOptions) getController(sr *api.SerializedReference) (interface{}, error) {
270-
switch sr.Reference.Kind {
270+
func (o *DrainOptions) getController(namespace string, controllerRef *metav1.OwnerReference) (interface{}, error) {
271+
switch controllerRef.Kind {
271272
case "ReplicationController":
272-
return o.client.Core().ReplicationControllers(sr.Reference.Namespace).Get(sr.Reference.Name, metav1.GetOptions{})
273+
return o.client.Core().ReplicationControllers(namespace).Get(controllerRef.Name, metav1.GetOptions{})
273274
case "DaemonSet":
274-
return o.client.Extensions().DaemonSets(sr.Reference.Namespace).Get(sr.Reference.Name, metav1.GetOptions{})
275+
return o.client.Extensions().DaemonSets(namespace).Get(controllerRef.Name, metav1.GetOptions{})
275276
case "Job":
276-
return o.client.Batch().Jobs(sr.Reference.Namespace).Get(sr.Reference.Name, metav1.GetOptions{})
277+
return o.client.Batch().Jobs(namespace).Get(controllerRef.Name, metav1.GetOptions{})
277278
case "ReplicaSet":
278-
return o.client.Extensions().ReplicaSets(sr.Reference.Namespace).Get(sr.Reference.Name, metav1.GetOptions{})
279+
return o.client.Extensions().ReplicaSets(namespace).Get(controllerRef.Name, metav1.GetOptions{})
279280
case "StatefulSet":
280-
return o.client.Apps().StatefulSets(sr.Reference.Namespace).Get(sr.Reference.Name, metav1.GetOptions{})
281+
return o.client.Apps().StatefulSets(namespace).Get(controllerRef.Name, metav1.GetOptions{})
281282
}
282-
return nil, fmt.Errorf("Unknown controller kind %q", sr.Reference.Kind)
283+
return nil, fmt.Errorf("Unknown controller kind %q", controllerRef.Kind)
283284
}
284285

285-
func (o *DrainOptions) getPodCreator(pod api.Pod) (*api.SerializedReference, error) {
286-
creatorRef, found := pod.ObjectMeta.Annotations[api.CreatedByAnnotation]
287-
if !found {
286+
func (o *DrainOptions) getPodController(pod api.Pod) (*metav1.OwnerReference, error) {
287+
controllerRef := controller.GetControllerOf(&pod)
288+
if controllerRef == nil {
288289
return nil, nil
289290
}
290-
// Now verify that the specified creator actually exists.
291-
sr := &api.SerializedReference{}
292-
if err := runtime.DecodeInto(o.Factory.Decoder(true), []byte(creatorRef), sr); err != nil {
293-
return nil, err
294-
}
291+
295292
// We assume the only reason for an error is because the controller is
296-
// gone/missing, not for any other cause. TODO(mml): something more
297-
// sophisticated than this
298-
_, err := o.getController(sr)
293+
// gone/missing, not for any other cause.
294+
// TODO(mml): something more sophisticated than this
295+
// TODO(juntee): determine if it's safe to remove getController(),
296+
// so that drain can work for controller types that we don't know about
297+
_, err := o.getController(pod.Namespace, controllerRef)
299298
if err != nil {
300299
return nil, err
301300
}
302-
return sr, nil
301+
return controllerRef, nil
303302
}
304303

305304
func (o *DrainOptions) unreplicatedFilter(pod api.Pod) (bool, *warning, *fatal) {
@@ -308,15 +307,15 @@ func (o *DrainOptions) unreplicatedFilter(pod api.Pod) (bool, *warning, *fatal)
308307
return true, nil, nil
309308
}
310309

311-
sr, err := o.getPodCreator(pod)
310+
controllerRef, err := o.getPodController(pod)
312311
if err != nil {
313312
// if we're forcing, remove orphaned pods with a warning
314313
if apierrors.IsNotFound(err) && o.Force {
315314
return true, &warning{err.Error()}, nil
316315
}
317316
return false, nil, &fatal{err.Error()}
318317
}
319-
if sr != nil {
318+
if controllerRef != nil {
320319
return true, nil, nil
321320
}
322321
if !o.Force {
@@ -333,18 +332,18 @@ func (o *DrainOptions) daemonsetFilter(pod api.Pod) (bool, *warning, *fatal) {
333332
// The exception is for pods that are orphaned (the referencing
334333
// management resource - including DaemonSet - is not found).
335334
// Such pods will be deleted if --force is used.
336-
sr, err := o.getPodCreator(pod)
335+
controllerRef, err := o.getPodController(pod)
337336
if err != nil {
338337
// if we're forcing, remove orphaned pods with a warning
339338
if apierrors.IsNotFound(err) && o.Force {
340339
return true, &warning{err.Error()}, nil
341340
}
342341
return false, nil, &fatal{err.Error()}
343342
}
344-
if sr == nil || sr.Reference.Kind != "DaemonSet" {
343+
if controllerRef == nil || controllerRef.Kind != "DaemonSet" {
345344
return true, nil, nil
346345
}
347-
if _, err := o.client.Extensions().DaemonSets(sr.Reference.Namespace).Get(sr.Reference.Name, metav1.GetOptions{}); err != nil {
346+
if _, err := o.client.Extensions().DaemonSets(pod.Namespace).Get(controllerRef.Name, metav1.GetOptions{}); err != nil {
348347
return false, nil, &fatal{err.Error()}
349348
}
350349
if !o.IgnoreDaemonsets {

pkg/kubectl/cmd/drain_test.go

+56-3
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ const (
5858
var node *api.Node
5959
var cordoned_node *api.Node
6060

61+
func boolptr(b bool) *bool { return &b }
62+
6163
func TestMain(m *testing.M) {
6264
// Create a node.
6365
node = &api.Node{
@@ -244,7 +246,18 @@ func TestDrain(t *testing.T) {
244246
Namespace: "default",
245247
CreationTimestamp: metav1.Time{Time: time.Now()},
246248
Labels: labels,
249+
SelfLink: testapi.Default.SelfLink("pods", "bar"),
247250
Annotations: rc_anno,
251+
OwnerReferences: []metav1.OwnerReference{
252+
{
253+
APIVersion: "v1",
254+
Kind: "ReplicationController",
255+
Name: "rc",
256+
UID: "123",
257+
BlockOwnerDeletion: boolptr(true),
258+
Controller: boolptr(true),
259+
},
260+
},
248261
},
249262
Spec: api.PodSpec{
250263
NodeName: "node",
@@ -256,7 +269,7 @@ func TestDrain(t *testing.T) {
256269
Name: "ds",
257270
Namespace: "default",
258271
CreationTimestamp: metav1.Time{Time: time.Now()},
259-
SelfLink: "/apis/extensions/v1beta1/namespaces/default/daemonsets/ds",
272+
SelfLink: testapi.Default.SelfLink("daemonsets", "ds"),
260273
},
261274
Spec: extensions.DaemonSetSpec{
262275
Selector: &metav1.LabelSelector{MatchLabels: labels},
@@ -272,7 +285,17 @@ func TestDrain(t *testing.T) {
272285
Namespace: "default",
273286
CreationTimestamp: metav1.Time{Time: time.Now()},
274287
Labels: labels,
288+
SelfLink: testapi.Default.SelfLink("pods", "bar"),
275289
Annotations: ds_anno,
290+
OwnerReferences: []metav1.OwnerReference{
291+
{
292+
APIVersion: "extensions/v1beta1",
293+
Kind: "DaemonSet",
294+
Name: "ds",
295+
BlockOwnerDeletion: boolptr(true),
296+
Controller: boolptr(true),
297+
},
298+
},
276299
},
277300
Spec: api.PodSpec{
278301
NodeName: "node",
@@ -284,7 +307,7 @@ func TestDrain(t *testing.T) {
284307
Name: "missing-ds",
285308
Namespace: "default",
286309
CreationTimestamp: metav1.Time{Time: time.Now()},
287-
SelfLink: "/apis/extensions/v1beta1/namespaces/default/daemonsets/missing-ds",
310+
SelfLink: testapi.Default.SelfLink("daemonsets", "missing-ds"),
288311
},
289312
Spec: extensions.DaemonSetSpec{
290313
Selector: &metav1.LabelSelector{MatchLabels: labels},
@@ -300,7 +323,17 @@ func TestDrain(t *testing.T) {
300323
Namespace: "default",
301324
CreationTimestamp: metav1.Time{Time: time.Now()},
302325
Labels: labels,
326+
SelfLink: testapi.Default.SelfLink("pods", "bar"),
303327
Annotations: missing_ds_anno,
328+
OwnerReferences: []metav1.OwnerReference{
329+
{
330+
APIVersion: "extensions/v1beta1",
331+
Kind: "DaemonSet",
332+
Name: "missing-ds",
333+
BlockOwnerDeletion: boolptr(true),
334+
Controller: boolptr(true),
335+
},
336+
},
304337
},
305338
Spec: api.PodSpec{
306339
NodeName: "node",
@@ -312,7 +345,7 @@ func TestDrain(t *testing.T) {
312345
Name: "job",
313346
Namespace: "default",
314347
CreationTimestamp: metav1.Time{Time: time.Now()},
315-
SelfLink: "/apis/batch/v1/namespaces/default/jobs/job",
348+
SelfLink: testapi.Default.SelfLink("jobs", "job"),
316349
},
317350
Spec: batch.JobSpec{
318351
Selector: &metav1.LabelSelector{MatchLabels: labels},
@@ -325,7 +358,17 @@ func TestDrain(t *testing.T) {
325358
Namespace: "default",
326359
CreationTimestamp: metav1.Time{Time: time.Now()},
327360
Labels: labels,
361+
SelfLink: testapi.Default.SelfLink("pods", "bar"),
328362
Annotations: map[string]string{api.CreatedByAnnotation: refJson(t, &job)},
363+
OwnerReferences: []metav1.OwnerReference{
364+
{
365+
APIVersion: "v1",
366+
Kind: "Job",
367+
Name: "job",
368+
BlockOwnerDeletion: boolptr(true),
369+
Controller: boolptr(true),
370+
},
371+
},
329372
},
330373
}
331374

@@ -351,7 +394,17 @@ func TestDrain(t *testing.T) {
351394
Namespace: "default",
352395
CreationTimestamp: metav1.Time{Time: time.Now()},
353396
Labels: labels,
397+
SelfLink: testapi.Default.SelfLink("pods", "bar"),
354398
Annotations: rs_anno,
399+
OwnerReferences: []metav1.OwnerReference{
400+
{
401+
APIVersion: "v1",
402+
Kind: "ReplicaSet",
403+
Name: "rs",
404+
BlockOwnerDeletion: boolptr(true),
405+
Controller: boolptr(true),
406+
},
407+
},
355408
},
356409
Spec: api.PodSpec{
357410
NodeName: "node",

0 commit comments

Comments
 (0)