@@ -5,11 +5,9 @@ import (
5
5
"fmt"
6
6
"os"
7
7
"os/exec"
8
- "strings"
9
8
10
9
"github.com/manifoldco/promptui"
11
10
12
- v1 "k8s.io/api/apps/v1"
13
11
core_v1 "k8s.io/api/core/v1"
14
12
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
15
13
"k8s.io/client-go/kubernetes"
@@ -36,23 +34,15 @@ func Setup(client kubernetes.Interface, cfg Config) *Debug {
36
34
}
37
35
}
38
36
39
- func (d * Debug ) getApp () (* v1.Deployment , error ) {
40
- app , err := d .client .AppsV1 ().Deployments (d .cfg .Namespace ).Get (d .ctx , d .cfg .AppName , metav1.GetOptions {})
41
- if err != nil {
42
- return nil , fmt .Errorf ("failed to get application in namespace \" %s\" : %w" , d .cfg .Namespace , err )
43
- }
44
- return app , nil
45
- }
46
-
47
- func (d * Debug ) getPods (app * v1.Deployment ) (* core_v1.PodList , error ) {
37
+ func (d * Debug ) getPods () (* core_v1.PodList , error ) {
48
38
var podList * core_v1.PodList
49
39
var err error
50
40
podList , err = d .client .CoreV1 ().Pods (d .cfg .Namespace ).List (d .ctx , metav1.ListOptions {
51
- LabelSelector : fmt .Sprintf ("app.kubernetes.io/name=%s" , app . Name ),
41
+ LabelSelector : fmt .Sprintf ("app.kubernetes.io/name=%s" , d . cfg . AppName ),
52
42
})
53
43
if len (podList .Items ) == 0 {
54
44
podList , err = d .client .CoreV1 ().Pods (d .cfg .Namespace ).List (d .ctx , metav1.ListOptions {
55
- LabelSelector : fmt .Sprintf ("app=%s" , app . Name ),
45
+ LabelSelector : fmt .Sprintf ("app=%s" , d . cfg . AppName ),
56
46
})
57
47
}
58
48
if err != nil {
@@ -61,45 +51,6 @@ func (d *Debug) getPods(app *v1.Deployment) (*core_v1.PodList, error) {
61
51
return podList , nil
62
52
}
63
53
64
- func (d * Debug ) Debug () error {
65
- app , err := d .getApp ()
66
- if err != nil {
67
- return err
68
- }
69
-
70
- pods , err := d .getPods (app )
71
- if err != nil {
72
- return err
73
- }
74
-
75
- var podNames []string
76
- for _ , pod := range pods .Items {
77
- podNames = append (podNames , pod .Name )
78
- }
79
-
80
- if len (podNames ) == 0 {
81
- fmt .Println ("No pods found." )
82
- return nil
83
- }
84
-
85
- prompt := promptui.Select {
86
- Label : "Select pod to Debug" ,
87
- Items : podNames ,
88
- }
89
-
90
- _ , podName , err := prompt .Run ()
91
- if err != nil {
92
- fmt .Printf ("prompt failed %v\n " , err )
93
- return err
94
- }
95
-
96
- if err := d .debugPod (podName ); err != nil {
97
- fmt .Printf ("failed to debug pod %s: %v\n " , podName , err )
98
- }
99
-
100
- return nil
101
- }
102
-
103
54
func (d * Debug ) debugPod (podName string ) error {
104
55
cmd := exec .Command (
105
56
"kubectl" ,
@@ -128,13 +79,8 @@ func (d *Debug) debugPod(podName string) error {
128
79
return nil
129
80
}
130
81
131
- func (d * Debug ) Tidy () error {
132
- app , err := d .getApp ()
133
- if err != nil {
134
- return err
135
- }
136
-
137
- pods , err := d .getPods (app )
82
+ func (d * Debug ) Debug () error {
83
+ pods , err := d .getPods ()
138
84
if err != nil {
139
85
return err
140
86
}
@@ -149,42 +95,20 @@ func (d *Debug) Tidy() error {
149
95
return nil
150
96
}
151
97
152
- deleted := 0
153
- for _ , pod := range pods .Items {
154
- if len (pod .Spec .EphemeralContainers ) == 0 {
155
- continue
156
- }
157
-
158
- prompt := promptui.Prompt {
159
- Label : fmt .Sprintf ("Do you want to delete pod %s" , pod .Name ),
160
- IsConfirm : true ,
161
- }
162
-
163
- answer , err := prompt .Run ()
164
- if err != nil {
165
- if err == promptui .ErrAbort {
166
- fmt .Printf ("Skipping deletion for pod: %s\n " , pod .Name )
167
- continue
168
- }
169
- fmt .Printf ("Error reading input for pod %s: %v\n " , pod .Name , err )
170
- return err
171
- }
172
-
173
- // Delete pod if user confirms with "y" or "yes"
174
- if strings .ToLower (answer ) == "y" || strings .ToLower (answer ) == "yes" {
175
- if err := d .client .CoreV1 ().Pods (d .cfg .Namespace ).Delete (d .ctx , pod .Name , metav1.DeleteOptions {}); err != nil {
176
- fmt .Printf ("Failed to delete pod %s: %v\n " , pod .Name , err )
177
- } else {
178
- deleted ++
179
- fmt .Println ("Deleted pod:" , pod .Name )
180
- }
181
- } else {
182
- fmt .Println ("Skipped pod:" , pod .Name )
183
- }
98
+ prompt := promptui.Select {
99
+ Label : "Select pod to Debug" ,
100
+ Items : podNames ,
101
+ }
102
+
103
+ _ , podName , err := prompt .Run ()
104
+ if err != nil {
105
+ fmt .Printf ("prompt failed %v\n " , err )
106
+ return err
184
107
}
185
108
186
- if deleted == 0 {
187
- fmt .Println ( "No pods with ephemeral containers found." )
109
+ if err := d . debugPod ( podName ); err != nil {
110
+ fmt .Printf ( "failed to debug pod %s: %v \n " , podName , err )
188
111
}
112
+
189
113
return nil
190
114
}
0 commit comments