@@ -77,6 +77,9 @@ You may set default configuration such as image and command in the config file,
77
77
78
78
defaultRegistrySecretName = "kubectl-debug-registry-secret"
79
79
defaultRegistrySecretNamespace = "default"
80
+
81
+ defaultPodLabelsConfigMapName = "kubectl-debug-pod-labels-configmap"
82
+ defaultPodLabelsConfigMapNamespace = "default"
80
83
)
81
84
82
85
// DebugOptions specify how to run debug container in a running pod
@@ -87,15 +90,17 @@ type DebugOptions struct {
87
90
PodName string
88
91
89
92
// Debug options
90
- Image string
91
- RegistrySecretName string
92
- RegistrySecretNamespace string
93
- ContainerName string
94
- Command []string
95
- AgentPort int
96
- AppName string
97
- ConfigLocation string
98
- Fork bool
93
+ Image string
94
+ RegistrySecretName string
95
+ RegistrySecretNamespace string
96
+ PodLabelsConfigMapName string
97
+ PodLabelsConfigMapNamespace string
98
+ ContainerName string
99
+ Command []string
100
+ AgentPort int
101
+ AppName string
102
+ ConfigLocation string
103
+ Fork bool
99
104
100
105
//used for agentless mode
101
106
AgentLess bool
@@ -164,6 +169,10 @@ func NewDebugCmd(streams genericclioptions.IOStreams) *cobra.Command {
164
169
"private registry secret name, default is kubectl-debug-registry-secret" )
165
170
cmd .Flags ().StringVar (& opts .RegistrySecretNamespace , "registry-secret-namespace" , "" ,
166
171
"private registry secret namespace, default is default" )
172
+ cmd .Flags ().StringVar (& opts .PodLabelsConfigMapName , "pod-labels-configmap-name" , "" ,
173
+ "in fork mode the pod labels configmap name, default is kubectl-debug-pod-labels-configmap" )
174
+ cmd .Flags ().StringVar (& opts .PodLabelsConfigMapNamespace , "pod-labels-configmap-namespace" , "" ,
175
+ "in fork mode the pod labels configmap namespaces, default is default" )
167
176
cmd .Flags ().StringVarP (& opts .ContainerName , "container" , "c" , "" ,
168
177
"Target container to debug, default to the first container in pod" )
169
178
cmd .Flags ().IntVarP (& opts .AgentPort , "port" , "p" , 0 ,
@@ -261,6 +270,20 @@ func (o *DebugOptions) Complete(cmd *cobra.Command, args []string, argsLenAtDash
261
270
o .RegistrySecretNamespace = defaultRegistrySecretNamespace
262
271
}
263
272
}
273
+ if len (o .PodLabelsConfigMapName ) < 1 {
274
+ if len (config .PodLabelsConfigMapName ) > 0 {
275
+ o .PodLabelsConfigMapName = config .PodLabelsConfigMapName
276
+ } else {
277
+ o .PodLabelsConfigMapName = defaultPodLabelsConfigMapName
278
+ }
279
+ }
280
+ if len (o .PodLabelsConfigMapNamespace ) < 1 {
281
+ if len (config .PodLabelsConfigMapNamespace ) > 0 {
282
+ o .PodLabelsConfigMapNamespace = config .PodLabelsConfigMapNamespace
283
+ } else {
284
+ o .PodLabelsConfigMapNamespace = defaultPodLabelsConfigMapNamespace
285
+ }
286
+ }
264
287
if o .AgentPort < 1 {
265
288
if config .AgentPort > 0 {
266
289
o .AgentPort = config .AgentPort
@@ -378,7 +401,14 @@ func (o *DebugOptions) Run() error {
378
401
// and hack the entry point of the target container with sleep command
379
402
// which keeps the container running.
380
403
if o .Fork {
381
- pod = copyAndStripPod (pod , containerName )
404
+ // build the fork pod labels
405
+ podLabels , err := o .buildForkPodLabels ()
406
+ if err != nil {
407
+ o .deleteAgent (agentPod )
408
+ return err
409
+ }
410
+ // copy pod and run
411
+ pod = copyAndStripPod (pod , containerName , podLabels )
382
412
pod , err = o .launchPod (pod )
383
413
if err != nil {
384
414
fmt .Fprintf (o .Out , "the ForkedPod is not running, you should check the reason and delete the failed ForkedPod and retry\n " )
@@ -591,15 +621,28 @@ func (o *DebugOptions) setupTTY() term.TTY {
591
621
return t
592
622
}
593
623
624
+ func (o * DebugOptions ) buildForkPodLabels () (map [string ]string , error ) {
625
+ podLabelConfigMap , err := o .CoreClient .ConfigMaps (o .PodLabelsConfigMapNamespace ).Get (o .PodLabelsConfigMapName , v1.GetOptions {})
626
+ if err != nil {
627
+ if errors .IsNotFound (err ) {
628
+ return nil , nil
629
+ } else {
630
+ return nil , err
631
+ }
632
+ } else {
633
+ return podLabelConfigMap .Data , nil
634
+ }
635
+ }
636
+
594
637
// copyAndStripPod copy the given pod template, strip the probes and labels,
595
638
// and replace the entry point
596
- func copyAndStripPod (pod * corev1.Pod , targetContainer string ) * corev1.Pod {
639
+ func copyAndStripPod (pod * corev1.Pod , targetContainer string , podLabels map [ string ] string ) * corev1.Pod {
597
640
copied := & corev1.Pod {
598
641
ObjectMeta : * pod .ObjectMeta .DeepCopy (),
599
642
Spec : * pod .Spec .DeepCopy (),
600
643
}
601
644
copied .Name = fmt .Sprintf ("%s-%s-debug" , pod .Name , uuid .NewUUID ())
602
- copied .Labels = nil
645
+ copied .Labels = podLabels
603
646
copied .Spec .RestartPolicy = corev1 .RestartPolicyNever
604
647
for i , c := range copied .Spec .Containers {
605
648
copied .Spec .Containers [i ].LivenessProbe = nil
0 commit comments