This repository has been archived by the owner on Oct 9, 2023. It is now read-only.
/
template.go
142 lines (125 loc) · 4.02 KB
/
template.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
package tasklog
import (
"fmt"
"regexp"
"strings"
"github.com/lyft/flyteidl/gen/pb-go/flyteidl/core"
)
// A simple log plugin that supports templates in urls to build the final log link. Supported templates are:
// {{ .podName }}: Gets the pod name as it shows in k8s dashboard,
// {{ .namespace }}: K8s namespace where the pod runs,
// {{ .containerName }}: The container name that generated the log,
// {{ .containerId }}: The container id docker/crio generated at run time,
// {{ .logName }}: A deployment specific name where to expect the logs to be.
// {{ .hostname }}: The hostname where the pod is running and where logs reside.
type TemplateLogPlugin struct {
templateUris []string
messageFormat core.TaskLog_MessageFormat
}
type regexValPair struct {
regex *regexp.Regexp
val string
}
type templateRegexes struct {
PodName *regexp.Regexp
Namespace *regexp.Regexp
ContainerName *regexp.Regexp
ContainerID *regexp.Regexp
LogName *regexp.Regexp
Hostname *regexp.Regexp
}
func mustInitTemplateRegexes() templateRegexes {
return templateRegexes{
PodName: mustCreateRegex("podName"),
Namespace: mustCreateRegex("namespace"),
ContainerName: mustCreateRegex("containerName"),
ContainerID: mustCreateRegex("containerID"),
LogName: mustCreateRegex("logName"),
Hostname: mustCreateRegex("hostname"),
}
}
var regexes = mustInitTemplateRegexes()
func mustCreateRegex(varName string) *regexp.Regexp {
return regexp.MustCompile(fmt.Sprintf(`(?i){{\s*[\.$]%s\s*}}`, varName))
}
func replaceAll(template string, values []regexValPair) string {
for _, v := range values {
template = v.regex.ReplaceAllString(template, v.val)
}
return template
}
func (s TemplateLogPlugin) GetTaskLog(podName, namespace, containerName, containerID, logName string) (core.TaskLog, error) {
o, err := s.GetTaskLogs(Input{
LogName: logName,
Namespace: namespace,
PodName: podName,
ContainerName: containerName,
ContainerID: containerID,
})
if err != nil || len(o.TaskLogs) == 0 {
return core.TaskLog{}, err
}
return *o.TaskLogs[0], nil
}
func (s TemplateLogPlugin) GetTaskLogs(input Input) (Output, error) {
// Container IDs are prefixed with docker://, cri-o://, etc. which is stripped by fluentd before pushing to a log
// stream. Therefore, we must also strip the prefix.
containerID := input.ContainerID
stripDelimiter := "://"
if split := strings.Split(input.ContainerID, stripDelimiter); len(split) > 1 {
containerID = split[1]
}
taskLogs := make([]*core.TaskLog, 0, len(s.templateUris))
for _, templateURI := range s.templateUris {
taskLogs = append(taskLogs, &core.TaskLog{
Uri: replaceAll(
templateURI,
[]regexValPair{
{
regex: regexes.PodName,
val: input.PodName,
},
{
regex: regexes.Namespace,
val: input.Namespace,
},
{
regex: regexes.ContainerName,
val: input.ContainerName,
},
{
regex: regexes.ContainerID,
val: containerID,
},
{
regex: regexes.LogName,
val: input.LogName,
},
{
regex: regexes.Hostname,
val: input.HostName,
},
},
),
Name: input.LogName,
MessageFormat: s.messageFormat,
})
}
return Output{
TaskLogs: taskLogs,
}, nil
}
// NewTemplateLogPlugin creates a template-based log plugin with the provided template Uri and message format. Supported
// templates are:
// {{ .podName }}: Gets the pod name as it shows in k8s dashboard,
// {{ .namespace }}: K8s namespace where the pod runs,
// {{ .containerName }}: The container name that generated the log,
// {{ .containerId }}: The container id docker/crio generated at run time,
// {{ .logName }}: A deployment specific name where to expect the logs to be.
// {{ .hostname }}: The hostname where the pod is running and where logs reside.
func NewTemplateLogPlugin(templateUris []string, messageFormat core.TaskLog_MessageFormat) TemplateLogPlugin {
return TemplateLogPlugin{
templateUris: templateUris,
messageFormat: messageFormat,
}
}