Skip to content

Commit 6d4250e

Browse files
authored
Merge pull request kubernetes#123149 from mochizuki875/remove_probe_from_copy_pod
kubectl debug: Add --keep-* flag to control the removal of probes, labels, annotations, and initContainers from copy pod
2 parents c276258 + b63fa13 commit 6d4250e

File tree

6 files changed

+902
-230
lines changed

6 files changed

+902
-230
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
apiVersion: v1
2+
kind: Pod
3+
metadata:
4+
annotations:
5+
test: test
6+
labels:
7+
run: target
8+
name: target
9+
spec:
10+
containers:
11+
- image: registry.k8s.io/nginx:1.7.9
12+
name: target
13+
readinessProbe:
14+
exec:
15+
command: ["/bin/sh", "-c", "cat probe"]
16+
livenessProbe:
17+
exec:
18+
command: ["/bin/sh", "-c", "cat probe"]
19+
startupProbe:
20+
exec:
21+
command: ["/bin/sh", "-c", "cat probe"]
22+
initContainers:
23+
- image: busybox
24+
name: init

staging/src/k8s.io/kubectl/pkg/cmd/debug/debug.go

Lines changed: 50 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -111,29 +111,35 @@ type DebugAttachFunc func(ctx context.Context, restClientGetter genericclioption
111111

112112
// DebugOptions holds the options for an invocation of kubectl debug.
113113
type DebugOptions struct {
114-
Args []string
115-
ArgsOnly bool
116-
Attach bool
117-
AttachFunc DebugAttachFunc
118-
Container string
119-
CopyTo string
120-
Replace bool
121-
Env []corev1.EnvVar
122-
Image string
123-
Interactive bool
124-
Namespace string
125-
TargetNames []string
126-
PullPolicy corev1.PullPolicy
127-
Quiet bool
128-
SameNode bool
129-
SetImages map[string]string
130-
ShareProcesses bool
131-
TargetContainer string
132-
TTY bool
133-
Profile string
134-
CustomProfileFile string
135-
CustomProfile *corev1.Container
136-
Applier ProfileApplier
114+
Args []string
115+
ArgsOnly bool
116+
Attach bool
117+
AttachFunc DebugAttachFunc
118+
Container string
119+
CopyTo string
120+
Replace bool
121+
Env []corev1.EnvVar
122+
Image string
123+
Interactive bool
124+
KeepLabels bool
125+
KeepAnnotations bool
126+
KeepLiveness bool
127+
KeepReadiness bool
128+
KeepStartup bool
129+
KeepInitContainers bool
130+
Namespace string
131+
TargetNames []string
132+
PullPolicy corev1.PullPolicy
133+
Quiet bool
134+
SameNode bool
135+
SetImages map[string]string
136+
ShareProcesses bool
137+
TargetContainer string
138+
TTY bool
139+
Profile string
140+
CustomProfileFile string
141+
CustomProfile *corev1.Container
142+
Applier ProfileApplier
137143

138144
explicitNamespace bool
139145
attachChanged bool
@@ -151,10 +157,11 @@ type DebugOptions struct {
151157
// NewDebugOptions returns a DebugOptions initialized with default values.
152158
func NewDebugOptions(streams genericiooptions.IOStreams) *DebugOptions {
153159
return &DebugOptions{
154-
Args: []string{},
155-
IOStreams: streams,
156-
TargetNames: []string{},
157-
ShareProcesses: true,
160+
Args: []string{},
161+
IOStreams: streams,
162+
KeepInitContainers: true,
163+
TargetNames: []string{},
164+
ShareProcesses: true,
158165
}
159166
}
160167

@@ -189,6 +196,12 @@ func (o *DebugOptions) AddFlags(cmd *cobra.Command) {
189196
cmd.Flags().BoolVar(&o.Replace, "replace", o.Replace, i18n.T("When used with '--copy-to', delete the original Pod."))
190197
cmd.Flags().StringToString("env", nil, i18n.T("Environment variables to set in the container."))
191198
cmd.Flags().StringVar(&o.Image, "image", o.Image, i18n.T("Container image to use for debug container."))
199+
cmd.Flags().BoolVar(&o.KeepLabels, "keep-labels", o.KeepLabels, i18n.T("If true, keep the original pod labels.(This flag only works when used with '--copy-to')"))
200+
cmd.Flags().BoolVar(&o.KeepAnnotations, "keep-annotations", o.KeepAnnotations, i18n.T("If true, keep the original pod annotations.(This flag only works when used with '--copy-to')"))
201+
cmd.Flags().BoolVar(&o.KeepLiveness, "keep-liveness", o.KeepLiveness, i18n.T("If true, keep the original pod liveness probes.(This flag only works when used with '--copy-to')"))
202+
cmd.Flags().BoolVar(&o.KeepReadiness, "keep-readiness", o.KeepReadiness, i18n.T("If true, keep the original pod readiness probes.(This flag only works when used with '--copy-to')"))
203+
cmd.Flags().BoolVar(&o.KeepStartup, "keep-startup", o.KeepStartup, i18n.T("If true, keep the original startup probes.(This flag only works when used with '--copy-to')"))
204+
cmd.Flags().BoolVar(&o.KeepInitContainers, "keep-init-containers", o.KeepInitContainers, i18n.T("Run the init containers for the pod. Defaults to true.(This flag only works when used with '--copy-to')"))
192205
cmd.Flags().StringToStringVar(&o.SetImages, "set-image", o.SetImages, i18n.T("When used with '--copy-to', a list of name=image pairs for changing container images, similar to how 'kubectl set image' works."))
193206
cmd.Flags().String("image-pull-policy", "", i18n.T("The image pull policy for the container. If left empty, this value will not be specified by the client and defaulted by the server."))
194207
cmd.Flags().BoolVarP(&o.Interactive, "stdin", "i", o.Interactive, i18n.T("Keep stdin open on the container(s) in the pod, even if nothing is attached."))
@@ -257,7 +270,15 @@ func (o *DebugOptions) Complete(restClientGetter genericclioptions.RESTClientGet
257270
}
258271

259272
if o.Applier == nil {
260-
applier, err := NewProfileApplier(o.Profile)
273+
kflags := KeepFlags{
274+
Labels: o.KeepLabels,
275+
Annotations: o.KeepAnnotations,
276+
Liveness: o.KeepLiveness,
277+
Readiness: o.KeepReadiness,
278+
Startup: o.KeepStartup,
279+
InitContainers: o.KeepInitContainers,
280+
}
281+
applier, err := NewProfileApplier(o.Profile, kflags)
261282
if err != nil {
262283
return err
263284
}
@@ -708,6 +729,7 @@ func (o *DebugOptions) generatePodCopyWithDebugContainer(pod *corev1.Pod) (*core
708729
Name: o.CopyTo,
709730
Namespace: pod.Namespace,
710731
Annotations: pod.Annotations,
732+
Labels: pod.Labels,
711733
},
712734
Spec: *pod.Spec.DeepCopy(),
713735
}

0 commit comments

Comments
 (0)