/
pod.go
170 lines (142 loc) · 5.21 KB
/
pod.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
// Copyright © 2021 Alibaba Group Holding Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package debug
import (
"context"
"fmt"
"github.com/pkg/errors"
"github.com/spf13/cobra"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
utilrand "k8s.io/apimachinery/pkg/util/rand"
"github.com/alibaba/sealer/common"
)
func NewDebugPodCommand(options *DebuggerOptions) *cobra.Command {
cmd := &cobra.Command{
Use: "pod",
Short: "Debug pod or container",
Args: cobra.MinimumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
debugger := NewDebugger(options)
debugger.AdminKubeConfigPath = common.KubeAdminConf
debugger.Type = TypeDebugPod
debugger.Motd = SealerDebugMotd
imager := NewDebugImagesManager()
if err := debugger.CompleteAndVerifyOptions(cmd, args, imager); err != nil {
return err
}
str, err := debugger.Run()
if err != nil {
return err
}
if len(str) != 0 {
fmt.Println("The debug ID:", str)
}
return nil
},
}
cmd.Flags().StringVarP(&options.TargetContainer, "container", "c", "", "The container to be debugged.")
return cmd
}
func (debugger *Debugger) DebugPod(ctx context.Context) (*corev1.Pod, error) {
// get the target pod object
targetPod, err := debugger.kubeClientCorev1.Pods(debugger.Namespace).Get(ctx, debugger.TargetName, metav1.GetOptions{})
if err != nil {
return nil, errors.Wrapf(err, "failed to get the target pod %s", debugger.TargetName)
}
if err := debugger.addPodInfoIntoEnv(targetPod); err != nil {
return nil, err
}
if err := debugger.addClusterInfoIntoEnv(ctx); err != nil {
return nil, err
}
// add an ephemeral container into target pod and used as a debug container
debugPod, err := debugger.debugPodByEphemeralContainer(ctx, targetPod)
if err != nil {
return nil, errors.Wrapf(err, "failed to add an ephemeral container into pod: %s", targetPod.Name)
}
return debugPod, nil
}
// debugPodByEphemeralContainer runs an ephemeral container in target pod and use as a debug container.
func (debugger *Debugger) debugPodByEphemeralContainer(ctx context.Context, pod *corev1.Pod) (*corev1.Pod, error) {
// get ephemeral containers
pods := debugger.kubeClientCorev1.Pods(pod.Namespace)
ec, err := pods.GetEphemeralContainers(ctx, pod.Name, metav1.GetOptions{})
if err != nil {
if serr, ok := err.(*apierrors.StatusError); ok && serr.Status().Reason == metav1.StatusReasonNotFound && serr.ErrStatus.Details.Name == "" {
return nil, errors.Wrapf(err, "ephemeral container are disabled for this cluster")
}
return nil, err
}
// generate an ephemeral container
debugContainer := debugger.generateDebugContainer(pod)
// add the ephemeral container and update the pod
ec.EphemeralContainers = append(ec.EphemeralContainers, *debugContainer)
_, err = pods.UpdateEphemeralContainers(ctx, pod.Name, ec, metav1.UpdateOptions{})
if err != nil {
return nil, errors.Wrapf(err, "error updating ephermeral containers")
}
return pod, nil
}
// generateDebugContainer returns an ephemeral container suitable for use as a debug container in the given pod.
func (debugger *Debugger) generateDebugContainer(pod *corev1.Pod) *corev1.EphemeralContainer {
debugContainerName := debugger.getDebugContainerName(pod)
debugger.DebugContainerName = debugContainerName
if len(debugger.TargetContainer) == 0 {
debugger.TargetContainer = pod.Spec.Containers[0].Name
}
ec := &corev1.EphemeralContainer{
EphemeralContainerCommon: corev1.EphemeralContainerCommon{
Name: debugContainerName,
Env: debugger.Env,
Image: debugger.Image,
ImagePullPolicy: corev1.PullPolicy(debugger.PullPolicy),
Stdin: true,
TerminationMessagePolicy: corev1.TerminationMessageReadFile,
TTY: true,
},
TargetContainerName: debugger.TargetContainer,
}
return ec
}
// getDebugContainerName generates and returns the debug container name.
func (debugger *Debugger) getDebugContainerName(pod *corev1.Pod) string {
if len(debugger.DebugContainerName) > 0 {
return debugger.DebugContainerName
}
name := debugger.DebugContainerName
containerByName := ContainerNameToRef(pod)
for len(name) == 0 || containerByName[name] != nil {
name = fmt.Sprintf("%s-%s", PodDebugPrefix, utilrand.String(5))
}
return name
}
// addPodInfoIntoEnv adds pod info into env
func (debugger *Debugger) addPodInfoIntoEnv(pod *corev1.Pod) error {
if pod == nil {
return fmt.Errorf("pod must not nil")
}
debugger.Env = append(debugger.Env,
corev1.EnvVar{
Name: "POD_NAME",
Value: pod.Name,
},
corev1.EnvVar{
Name: "POD_IP",
Value: pod.Status.PodIP,
},
)
return nil
}