Skip to content

Commit b49abfc

Browse files
committed
update
1 parent 3773c35 commit b49abfc

File tree

2 files changed

+51
-11
lines changed

2 files changed

+51
-11
lines changed

cmd/root.go

+51-11
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ func watchPodStatus(client *kubernetes.Clientset, tcpdumpPod *apicore.Pod) wait.
204204
wpod, err := client.CoreV1().Pods(tcpdumpPod.ObjectMeta.Namespace).Get(context.TODO(), tcpdumpPod.ObjectMeta.Name, metav1.GetOptions{})
205205
if err != nil {
206206
log.Warn(fmt.Sprintf("Failed to get the pod '%s' in the namespace '%s'. Will exit.", tcpdumpPod.ObjectMeta.Name, tcpdumpPod.ObjectMeta.Namespace))
207-
log.Fatal(err)
207+
208208
//return false, err
209209
}
210210
if wpod.Status.Phase == "Running" {
@@ -288,24 +288,64 @@ func downloadFromPod(restConfig *rest.Config, client *kubernetes.Clientset, tcpd
288288

289289
func cleanUp(client *kubernetes.Clientset, tcpdumpPod *apicore.Pod) error {
290290
log.Info(fmt.Sprintf("Cleanup the Pod '%s' in the namespace '%s'", tcpdumpPod.ObjectMeta.Name, tcpdumpPod.ObjectMeta.Namespace))
291+
//var GracePeriodSeconds int64
292+
//GracePeriodSeconds = 0
291293
err := client.CoreV1().Pods(tcpdumpPod.ObjectMeta.Namespace).Delete(context.TODO(), tcpdumpPod.ObjectMeta.Name, metav1.DeleteOptions{})
292294
return err
293295
}
294296

297+
//func watchPodStatus(client *kubernetes.Clientset, tcpdumpPod *apicore.Pod) bool {
298+
// listOption := "metadata.name=" + tcpdumpPod.ObjectMeta.Name
299+
// podWatcher, err := client.CoreV1().Pods(tcpdumpPod.ObjectMeta.Namespace).Watch(context.TODO(), metav1.ListOptions{
300+
// FieldSelector: listOption})
301+
// if err != nil {
302+
// log.Warn(fmt.Sprintf("Failed to get the status of the pod '%s' in the namespace '%s'", tcpdumpPod.ObjectMeta.Name, tcpdumpPod.ObjectMeta.Namespace))
303+
// }
304+
//var podWatcherRes struct
305+
// podWatcherEvent := <-podWatcher.ResultChan()
306+
// log.Info(podWatcherEvent.Type)
307+
// if podWatcherEvent.Type == "MODIFIED" {
308+
// return true
309+
// }
310+
// return false
311+
//}
312+
295313
func podOperation(workerGroup *sync.WaitGroup, restConfig *rest.Config, client *kubernetes.Clientset, targetPod *target, duration string, sleepTime time.Duration) error {
296314
defer workerGroup.Done()
297-
tcpdumpPod, res := createPod(client, targetPod, duration)
298-
if res == nil {
299-
res = wait.PollImmediate(1, sleepTime, watchPodStatus(client, tcpdumpPod))
300-
if res != nil {
315+
tcpdumpPod, err := createPod(client, targetPod, duration)
316+
if err == nil {
317+
318+
err = wait.PollImmediate(time.Second*1, sleepTime, watchPodStatus(client, tcpdumpPod))
319+
//for {
320+
//if modified := watchPodStatus(client, tcpdumpPod); modified == true {
321+
// tcpdumpPod, err := client.CoreV1().Pods(tcpdumpPod.ObjectMeta.Namespace).Get(context.TODO(), tcpdumpPod.ObjectMeta.Name, metav1.GetOptions{})
322+
// if err != nil {
323+
// log.Warn(fmt.Sprintf("Failed to get the pod '%s' in the namespace '%s'. Will EXIT.", tcpdumpPod.ObjectMeta.Name, tcpdumpPod.ObjectMeta.Namespace))
324+
// log.Fatal(err)
325+
//return false, err
326+
// }
327+
// if tcpdumpPod.Status.Phase == "Running" {
328+
// if tcpdumpPod.Status.Conditions[1].Type == apicore.PodReady {
329+
// if tcpdumpPod.Status.Conditions[1].Status == apicore.ConditionTrue {
330+
// log.Info(fmt.Sprintf("Tcpdump process in the Pod '%s' in the namesapce '%s' has completed now", tcpdumpPod.ObjectMeta.Name, tcpdumpPod.ObjectMeta.Namespace))
331+
// break
332+
// }
333+
// }
334+
// }
335+
//}
336+
//}
337+
338+
if err != nil {
301339
log.Warn(fmt.Sprintf("Timeout while waiting tcpdump for pod '%s' in the namespace '%s' to complete", tcpdumpPod.ObjectMeta.Name, tcpdumpPod.ObjectMeta.Namespace))
340+
//log.Fatal(err)
341+
} else {
342+
err = downloadFromPod(restConfig, client, tcpdumpPod)
343+
if err != nil {
344+
log.Warn(fmt.Sprintf("Failed to download dump file from pod '%s' in the namespace '%s'", tcpdumpPod.ObjectMeta.Name, tcpdumpPod.ObjectMeta.Namespace))
345+
}
302346
}
303-
res = downloadFromPod(restConfig, client, tcpdumpPod)
304-
if res != nil {
305-
log.Warn(fmt.Sprintf("Failed to download dump file from pod '%s' in the namespace '%s'", tcpdumpPod.ObjectMeta.Name, tcpdumpPod.ObjectMeta.Namespace))
306-
}
307-
res = cleanUp(client, tcpdumpPod)
308-
if res != nil {
347+
err = cleanUp(client, tcpdumpPod)
348+
if err != nil {
309349
log.Warn(fmt.Sprintf("Failed to clean up the pod '%s'", tcpdumpPod.ObjectMeta.Name))
310350
}
311351
}

k8sTcpdump

-40.9 MB
Binary file not shown.

0 commit comments

Comments
 (0)