StartLogs print the logs and then attaches to the container
( ctx devspacecontext.Context, devContainer *latest.DevContainer, selector targetselector.TargetSelector, )
| 31 | |
| 32 | // StartLogs print the logs and then attaches to the container |
| 33 | func StartLogs( |
| 34 | ctx devspacecontext.Context, |
| 35 | devContainer *latest.DevContainer, |
| 36 | selector targetselector.TargetSelector, |
| 37 | ) (err error) { |
| 38 | // Restart on error |
| 39 | defer func() { |
| 40 | if err != nil { |
| 41 | if ctx.IsDone() { |
| 42 | return |
| 43 | } |
| 44 | |
| 45 | ctx.Log().Infof("Restarting because: %s", err) |
| 46 | select { |
| 47 | case <-ctx.Context().Done(): |
| 48 | return |
| 49 | case <-time.After(time.Second * 3): |
| 50 | } |
| 51 | err = StartLogs(ctx, devContainer, selector) |
| 52 | return |
| 53 | } |
| 54 | |
| 55 | ctx.Log().Debugf("Stopped logs") |
| 56 | }() |
| 57 | |
| 58 | containerObj, err := selector.WithContainer(devContainer.Container).SelectSingleContainer(ctx.Context(), ctx.KubeClient(), ctx.Log()) |
| 59 | if err != nil { |
| 60 | return err |
| 61 | } |
| 62 | |
| 63 | lines := int64(500) |
| 64 | if devContainer.Logs.LastLines > 0 { |
| 65 | lines = devContainer.Logs.LastLines |
| 66 | } |
| 67 | |
| 68 | ctx.Log().Infof("Streaming logs of pod:container %s:%s", ansi.Color(containerObj.Pod.Name, "white+b"), ansi.Color(containerObj.Container.Name, "white+b")) |
| 69 | reader, err := ctx.KubeClient().Logs(ctx.Context(), containerObj.Pod.Namespace, containerObj.Pod.Name, containerObj.Container.Name, false, &lines, true) |
| 70 | if err != nil { |
| 71 | return err |
| 72 | } |
| 73 | |
| 74 | errChan := make(chan error) |
| 75 | go func() { |
| 76 | s := scanner.NewScanner(reader) |
| 77 | for s.Scan() { |
| 78 | if devContainer.Container != "" { |
| 79 | ctx.Log().Info(devContainer.Container + ": " + s.Text()) |
| 80 | } else { |
| 81 | ctx.Log().Info(s.Text()) |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | errChan <- s.Err() |
| 86 | }() |
| 87 | |
| 88 | select { |
| 89 | case <-ctx.Context().Done(): |
| 90 | _ = reader.Close() |
no test coverage detected