This repository has been archived by the owner on Dec 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
310 lines (276 loc) · 10.4 KB
/
main.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
package main
import (
"context"
"encoding/json"
"errors"
"fmt"
"os"
"strings"
"time"
"github.com/alecthomas/kong"
"github.com/docker/distribution/reference"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
corev1 "k8s.io/api/core/v1"
kubeerrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
utilrand "k8s.io/apimachinery/pkg/util/rand"
"k8s.io/apimachinery/pkg/watch"
"k8s.io/cli-runtime/pkg/genericclioptions"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/cache"
watchtools "k8s.io/client-go/tools/watch"
"k8s.io/kubectl/pkg/cmd/attach"
"k8s.io/kubectl/pkg/cmd/exec"
cmdutil "k8s.io/kubectl/pkg/cmd/util"
"k8s.io/kubectl/pkg/util/interrupt"
)
// EphemeralContainerPatch Used to generate Strategic Patch JSON.
type EphemeralContainerPatch struct {
EphemeralContainer []corev1.EphemeralContainer `json:"ephemeralContainers"`
}
// Patch Used to generate Strategic Patch JSON.
type Patch struct {
Spec EphemeralContainerPatch `json:"spec"`
}
// CLI Flags.
var CLI struct {
// kubectl debug -it ephemeral-demo --image=busybox:1.28 --target=ephemeral-demo
Image string `required:"" help:"Container image to use for debug container."`
PodName string `arg:"" name:"pod"`
Target string `required:"" help:"When using an ephemeral container, target processes in this container name."`
Attach bool `name:"If true, wait for the container to start running, and then attach as if 'kubectl attach ...' were called. Default false, unless '-i/--stdin' is set, in which case the default is true."`
Container string `short:"c" help:"Container name to use for debug container."`
Env map[string]string `mapsep:"," help:"Environment variables to set in the container."`
Interactive bool `short:"i" help:"Keep stdin open on the container(s) in the pod, even if nothing is attached."`
TTY bool `short:"t" help:"Allocate a TTY for the debugging container."`
Quiet bool `short:"q" help:"If true, suppress informational messages."`
Args []string `arg:"" required:"" help:"Command and args"`
Privileged bool `help:"Give extended privileges to this container"`
CapAdd []string `help:"Add Linux capabilities"`
CapDrop []string `help:"Drop Linux capabilities"`
Verbose uint `short:"v" help:"number for the log level verbosity" default:"0"`
Namespace string `short:"n" help:"If present, the namespace scope for this CLI request"`
}
// getContainerStatusByName Extracts the status of a container from a Pod struct given the container name.
func getContainerStatusByName(pod *corev1.Pod, containerName string) *corev1.ContainerStatus {
allContainerStatus := [][]corev1.ContainerStatus{pod.Status.InitContainerStatuses, pod.Status.ContainerStatuses, pod.Status.EphemeralContainerStatuses}
for _, statusSlice := range allContainerStatus {
for i := range statusSlice {
if statusSlice[i].Name == containerName {
return &statusSlice[i]
}
}
}
return nil
}
// waitForContainer Wait for a container to start.
func waitForContainer(ns, podName, containerName string, clientset *kubernetes.Clientset) (*corev1.Pod, error) {
ctx := context.Background()
ctx, cancel := watchtools.ContextWithOptionalTimeout(ctx, 0*time.Second)
defer cancel()
fieldSelector := fields.OneTermEqualSelector("metadata.name", podName).String()
lw := &cache.ListWatch{
ListFunc: func(options metav1.ListOptions) (runtime.Object, error) {
options.FieldSelector = fieldSelector
return clientset.CoreV1().Pods(ns).List(ctx, options)
},
WatchFunc: func(options metav1.ListOptions) (watch.Interface, error) {
options.FieldSelector = fieldSelector
return clientset.CoreV1().Pods(ns).Watch(ctx, options)
},
}
intr := interrupt.New(nil, cancel)
var result *corev1.Pod
err := intr.Run(func() error {
ev, err := watchtools.UntilWithSync(ctx, lw, &corev1.Pod{}, nil, func(ev watch.Event) (bool, error) {
log.Debug().Msgf("watch received event %q with object %T", ev.Type, ev.Object)
if ev.Type == watch.Deleted {
log.Fatal().Msg("container not found")
}
p, ok := ev.Object.(*corev1.Pod)
if !ok {
log.Fatal().Msgf("watch did not return pod: %v", ev.Object)
}
s := getContainerStatusByName(p, containerName)
if s == nil {
return false, nil
}
log.Debug().Msgf("debug container status is %v", s)
if s.State.Running != nil || s.State.Terminated != nil {
return true, nil
}
if !CLI.Quiet && s.State.Waiting != nil && s.State.Waiting.Message != "" {
fmt.Printf("container %s: %s\n", containerName, s.State.Waiting.Message)
}
return false, nil
})
if ev != nil {
result = ev.Object.(*corev1.Pod)
}
return err
})
return result, err
}
func main() {
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr})
zerolog.SetGlobalLevel(zerolog.ErrorLevel)
kong.Parse(&CLI,
kong.Name("kubectl pdebug"),
kong.Description("Similar to kubectl debug but supporting privileged containers"),
kong.UsageOnError())
if CLI.Verbose > 0 {
zerolog.SetGlobalLevel(zerolog.DebugLevel)
}
if len(CLI.Image) > 0 && !reference.ReferenceRegexp.MatchString(CLI.Image) {
fmt.Printf("invalid image name %q: %v\n", CLI.Image, reference.ErrReferenceInvalidFormat)
os.Exit(1)
}
if !CLI.Quiet {
fmt.Printf("Targeting container %q. If you don't see processes from this container it may be because the container runtime doesn't support this feature.\n", CLI.Target)
}
if CLI.TTY && !CLI.Interactive {
fmt.Printf("-i/--stdin is required for containers with -t/--tty=true")
os.Exit(1)
}
defaultConfigFlags := genericclioptions.NewConfigFlags(true).WithDeprecatedPasswordFlag().WithDiscoveryBurst(300).WithDiscoveryQPS(50.0)
f := cmdutil.NewFactory(cmdutil.NewMatchVersionFlags(defaultConfigFlags))
namespace := CLI.Namespace
if len(namespace) == 0 {
var err error
namespace, _, err = f.ToRawKubeConfigLoader().Namespace()
if err != nil {
log.Fatal().Err(err).Msg("Failed to get namespace")
}
}
clientset, err := f.KubernetesClientSet()
if err != nil {
log.Fatal().Err(err).Msg("Failed to generate clientset")
}
// Look for existing pod
pod, err := clientset.CoreV1().Pods(namespace).Get(context.Background(), CLI.PodName, metav1.GetOptions{})
if err != nil {
var serr *kubeerrors.StatusError
if errors.As(err, &serr) && serr.Status().Reason == metav1.StatusReasonNotFound {
fmt.Println(serr.Status().Message)
os.Exit(1)
}
log.Fatal().Err(err).Msg("Failed to generate clientset")
}
foundTarget := false
foundExistingDebugContainer := false
for _, container := range pod.Spec.Containers {
if container.Name == CLI.Target {
foundTarget = true
}
if container.Name == CLI.Container {
foundExistingDebugContainer = true
}
}
if !foundTarget {
fmt.Printf("Pod \"%s\" does not have a container called \"%s\"", CLI.PodName, CLI.Target)
os.Exit(1)
}
if len(CLI.Container) == 0 {
CLI.Container = fmt.Sprintf("debugger-%s", utilrand.String(5))
if !CLI.Quiet {
fmt.Printf("Defaulting debug container name to %s.\n", CLI.Container)
}
}
if !foundExistingDebugContainer {
patchPod(namespace, clientset)
}
if CLI.Interactive || CLI.Attach {
attachContainer(namespace, clientset, f)
}
}
// patchPod JSON Patch ephemeral container into pod.
func patchPod(namespace string, clientset *kubernetes.Clientset) {
capAdd := make([]corev1.Capability, len(CLI.CapAdd))
capDrop := make([]corev1.Capability, len(CLI.CapDrop))
for i, addCap := range CLI.CapAdd {
capAdd[i] = corev1.Capability(strings.TrimPrefix(addCap, "CAP_"))
}
for i, dropCap := range CLI.CapDrop {
capDrop[i] = corev1.Capability(strings.TrimPrefix(dropCap, "CAP_"))
}
debugSpec := corev1.EphemeralContainer{
EphemeralContainerCommon: corev1.EphemeralContainerCommon{
Name: CLI.Container,
Image: CLI.Image,
Command: CLI.Args,
SecurityContext: &corev1.SecurityContext{
Capabilities: &corev1.Capabilities{
Add: capAdd,
Drop: capDrop,
},
Privileged: &CLI.Privileged,
},
Stdin: CLI.Interactive,
TTY: CLI.TTY,
TerminationMessagePolicy: corev1.TerminationMessageReadFile,
},
TargetContainerName: CLI.Target,
}
patch := Patch{Spec: EphemeralContainerPatch{EphemeralContainer: []corev1.EphemeralContainer{debugSpec}}}
patchBytes, err := json.Marshal(patch)
if err != nil {
log.Fatal().Err(err).Msg("Failed to generate patch set")
}
_, err = clientset.CoreV1().Pods(namespace).Patch(context.Background(), CLI.PodName, types.StrategicMergePatchType, patchBytes, metav1.PatchOptions{}, "ephemeralcontainers")
if err != nil {
var serr *kubeerrors.StatusError
if errors.As(err, &serr) && serr.Status().Reason == metav1.StatusReasonNotFound && serr.ErrStatus.Details.Name == "" {
fmt.Printf("ephemeral containers are disabled for this cluster (error from server: %q).\n", err)
os.Exit(1)
}
log.Fatal().Err(err).Msg("Failed to patch")
}
}
// attachContainer Attach stdin/out/err to container like kubectl exec -it.
func attachContainer(namespace string, clientset *kubernetes.Clientset, f cmdutil.Factory) {
streams := genericclioptions.IOStreams{
In: os.Stdin,
Out: os.Stdout,
ErrOut: os.Stderr,
}
opts := &attach.AttachOptions{
StreamOptions: exec.StreamOptions{
IOStreams: streams,
Stdin: CLI.Interactive,
TTY: CLI.TTY,
Quiet: CLI.Quiet,
},
CommandName: "kubectl attach",
Attach: &attach.DefaultRemoteAttach{},
}
config, err := f.ToRESTConfig()
if err != nil {
log.Fatal().Err(err).Msg("Failed to generate attach rest config")
}
opts.Config = config
opts.AttachFunc = attach.DefaultAttachFunc
// Wait for container
podSpec, err := waitForContainer(namespace, CLI.PodName, CLI.Container, clientset)
if err != nil {
log.Fatal().Err(err).Msg("Failed to wait for container")
}
opts.Namespace = namespace
opts.Pod = podSpec
opts.PodName = CLI.PodName
opts.ContainerName = CLI.Container
status := getContainerStatusByName(podSpec, CLI.Container)
if status == nil {
log.Fatal().Msg("Failed to get container status")
return // staticcheck does not recognise log.Fatal() as exiting
}
if status.State.Terminated != nil {
log.Fatal().Msg("Ephemeral container terminated")
}
if err = opts.Run(); err != nil {
log.Fatal().Err(err).Msg("Could not attach to container")
}
}