MCPcopy
hub / github.com/microsoft/retina / RunInPod

Function RunInPod

shell/shell.go:31–67  ·  view source on GitHub ↗

RunInPod starts an interactive shell in a pod by creating and attaching to an ephemeral container.

(config Config, podNamespace, podName string)

Source from the content-addressed store, hash-verified

29
30// RunInPod starts an interactive shell in a pod by creating and attaching to an ephemeral container.
31func RunInPod(config Config, podNamespace, podName string) error {
32 ctx := context.Background()
33
34 clientset, err := kubernetes.NewForConfig(config.RestConfig)
35 if err != nil {
36 return fmt.Errorf("error constructing kube clientset: %w", err)
37 }
38
39 pod, err := clientset.CoreV1().
40 Pods(podNamespace).
41 Get(ctx, podName, metav1.GetOptions{})
42 if err != nil {
43 return fmt.Errorf("error retrieving pod %s from namespace %s: %w", podName, podNamespace, err)
44 }
45
46 err = validateOperatingSystemSupportedForNode(ctx, clientset, pod.Spec.NodeName)
47 if err != nil {
48 return fmt.Errorf("error validating operating system for node %s: %w", pod.Spec.NodeName, err)
49 }
50
51 fmt.Printf("Starting ephemeral container in pod %s/%s\n", podNamespace, podName)
52 ephemeralContainer := ephemeralContainerForPodDebug(config)
53 pod.Spec.EphemeralContainers = append(pod.Spec.EphemeralContainers, ephemeralContainer)
54
55 _, err = clientset.CoreV1().
56 Pods(podNamespace).
57 UpdateEphemeralContainers(ctx, podName, pod, metav1.UpdateOptions{})
58 if err != nil {
59 return fmt.Errorf("error updating ephemeral containers: %w", err)
60 }
61
62 if err := waitForContainerRunning(ctx, config.Timeout, clientset, podNamespace, podName, ephemeralContainer.Name); err != nil {
63 return fmt.Errorf("error waiting for containers running: %w", err)
64 }
65
66 return attachToShell(config.RestConfig, podNamespace, podName, ephemeralContainer.Name, pod)
67}
68
69// RunInNode starts an interactive shell on a node by creating a HostNetwork pod and attaching to it.
70func RunInNode(config Config, nodeName, debugPodNamespace string) error {

Callers 1

shell.goFile · 0.92

Calls 5

waitForContainerRunningFunction · 0.85
attachToShellFunction · 0.85
GetMethod · 0.45

Tested by

no test coverage detected