MCPcopy
hub / github.com/kubernetes/kubectl / generatePodCopyWithDebugContainer

Method generatePodCopyWithDebugContainer

pkg/cmd/debug/debug.go:776–863  ·  view source on GitHub ↗

generatePodCopyWithDebugContainer takes a Pod and returns a copy and the debug container name of that copy

(pod *corev1.Pod)

Source from the content-addressed store, hash-verified

774
775// generatePodCopyWithDebugContainer takes a Pod and returns a copy and the debug container name of that copy
776func (o *DebugOptions) generatePodCopyWithDebugContainer(pod *corev1.Pod) (*corev1.Pod, string, error) {
777 copied := &corev1.Pod{
778 ObjectMeta: metav1.ObjectMeta{
779 Name: o.CopyTo,
780 Namespace: pod.Namespace,
781 Annotations: pod.Annotations,
782 Labels: pod.Labels,
783 },
784 Spec: *pod.Spec.DeepCopy(),
785 }
786 // set EphemeralContainers to nil so that the copy of pod can be created
787 copied.Spec.EphemeralContainers = nil
788 // change ShareProcessNamespace configuration only when commanded explicitly
789 if o.shareProcessedChanged {
790 copied.Spec.ShareProcessNamespace = ptr.To(o.ShareProcesses)
791 }
792 if !o.SameNode {
793 copied.Spec.NodeName = ""
794 }
795
796 // Apply image mutations
797 for i, c := range copied.Spec.Containers {
798 override := o.SetImages["*"]
799 if img, ok := o.SetImages[c.Name]; ok {
800 override = img
801 }
802 if len(override) > 0 {
803 copied.Spec.Containers[i].Image = override
804 }
805 }
806
807 name, containerByName := o.Container, containerNameToRef(copied)
808
809 c, ok := containerByName[name]
810 if !ok {
811 // Adding a new debug container
812 if len(o.Image) == 0 {
813 if len(o.SetImages) > 0 {
814 // This was a --set-image only invocation
815 return copied, "", nil
816 }
817 return nil, "", fmt.Errorf("you must specify image when creating new container")
818 }
819
820 if len(name) == 0 {
821 name = o.computeDebugContainerName(copied)
822 }
823 copied.Spec.Containers = append(copied.Spec.Containers, corev1.Container{
824 Name: name,
825 TerminationMessagePolicy: corev1.TerminationMessageReadFile,
826 })
827 c = &copied.Spec.Containers[len(copied.Spec.Containers)-1]
828 }
829
830 if len(o.Args) > 0 {
831 if o.ArgsOnly {
832 c.Args = o.Args
833 } else {

Calls 6

applyCustomProfileMethod · 0.95
containerNameToRefFunction · 0.85
ToMethod · 0.80
ApplyMethod · 0.65
DeepCopyMethod · 0.45