StreamLogs opens a goroutine to execute stern on all the pods that match the labelSelector. Their logs will be written to disk in the outputBaseDir, split by namespace, pod and container, using a namespace/pod/container.log file for each container of matching pods. Close the ctx context to terminate
( ctx context.Context, client kubernetes.Interface, labelSelector labels.Selector, outputBaseDir string, )
| 44 | // Returns a channel that will be closed when all the logs have been written to disk |
| 45 | // and the ones we asked to remove have been deleted. |
| 46 | func StreamLogs( |
| 47 | ctx context.Context, |
| 48 | client kubernetes.Interface, |
| 49 | labelSelector labels.Selector, |
| 50 | outputBaseDir string, |
| 51 | ) chan struct{} { |
| 52 | outPipeReader, outPipeWriter := io.Pipe() |
| 53 | errOut := os.Stdout |
| 54 | |
| 55 | // JSON output |
| 56 | pod := regexp.MustCompile(".*") |
| 57 | container := regexp.MustCompile(".*") |
| 58 | t := "{ \"message\": {{json .Message}}, " + |
| 59 | "\"namespace\": \"{{.Namespace}}\", " + |
| 60 | "\"podName\": \"{{.PodName}}\", " + |
| 61 | "\"containerName\": \"{{.ContainerName}}\" }\n" |
| 62 | |
| 63 | funs := template.FuncMap{ |
| 64 | "json": func(v interface{}) (string, error) { |
| 65 | b, err := json.Marshal(v) |
| 66 | if err != nil { |
| 67 | return "", err |
| 68 | } |
| 69 | return string(b), nil |
| 70 | }, |
| 71 | } |
| 72 | |
| 73 | parsedTemplate, _ := template.New("log").Funcs(funs).Parse(t) |
| 74 | |
| 75 | config := &stern.Config{ |
| 76 | Namespaces: []string{}, |
| 77 | PodQuery: pod, |
| 78 | ContainerQuery: container, |
| 79 | ExcludePodQuery: []*regexp.Regexp{}, |
| 80 | Timestamps: false, |
| 81 | TimestampFormat: stern.TimestampFormatDefault, |
| 82 | Location: time.UTC, |
| 83 | ExcludeContainerQuery: []*regexp.Regexp{}, |
| 84 | ContainerStates: []stern.ContainerState{ |
| 85 | stern.RUNNING, |
| 86 | }, |
| 87 | Exclude: []*regexp.Regexp{}, |
| 88 | Include: []*regexp.Regexp{}, |
| 89 | Highlight: []*regexp.Regexp{}, |
| 90 | InitContainers: true, |
| 91 | EphemeralContainers: true, |
| 92 | Since: 48 * time.Hour, |
| 93 | AllNamespaces: true, |
| 94 | LabelSelector: labelSelector, |
| 95 | FieldSelector: fields.Everything(), |
| 96 | TailLines: nil, |
| 97 | Template: parsedTemplate, |
| 98 | Follow: true, |
| 99 | Resource: "", |
| 100 | OnlyLogLines: true, |
| 101 | MaxLogRequests: 50, |
| 102 | Stdin: false, |
| 103 | DiffContainer: false, |
no test coverage detected