Skip to content

Commit

Permalink
Use capture groups to create dynamic log regexes.
Browse files Browse the repository at this point in the history
Signed-off-by: Eduardo Apolinario <[email protected]>
  • Loading branch information
eapolinario committed Jan 29, 2024
1 parent 6ca8fec commit 634c17c
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 86 deletions.
18 changes: 11 additions & 7 deletions flyteplugins/go/tasks/pluginmachinery/tasklog/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,13 +183,6 @@ func (input Input) templateVars() []TemplateVar {
},
)

// Add values from task template config as dynamic regexes (i.e. templates prefixed by .taskConfig)
for key, value := range input.TaskTemplate.GetConfig() {
if value != "" {
vars = append(vars, TemplateVar{MustCreateDynamicLogRegex(key), value})
}
}

return vars
}

Expand Down Expand Up @@ -219,9 +212,20 @@ func (p TemplateLogPlugin) GetTaskLogs(input Input) (Output, error) {
})
}

// Compile the potential dynamic log links
dynamicLogRegex := regexp.MustCompile(`(?i){{\s*.taskConfig[\.$]([a-zA-Z_]+)\s*}}`)

for _, dynamicLogLinkType := range getDynamicLogLinkTypes(input.TaskTemplate) {
for _, dynamicTemplateURI := range p.DynamicTemplateURIs {
if p.Name == dynamicLogLinkType {
for _, match := range dynamicLogRegex.FindAllStringSubmatch(dynamicTemplateURI, -1) {
if len(match) > 1 {
value := input.TaskTemplate.GetConfig()[match[1]]
if value != "" {
templateVars = append(templateVars, TemplateVar{MustCreateDynamicLogRegex(match[1]), value})
}
}
}
taskLogs = append(taskLogs, &core.TaskLog{
Uri: replaceAll(dynamicTemplateURI, templateVars),
Name: p.DisplayName + input.LogName,
Expand Down
105 changes: 26 additions & 79 deletions flyteplugins/go/tasks/pluginmachinery/tasklog/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,26 +83,6 @@ func Test_Input_templateVars(t *testing.T) {
PodUnixFinishTime: 12345,
}

flyinBase := Input{
HostName: "my-host",
PodName: "my-pod",
PodUID: "my-pod-uid",
Namespace: "my-namespace",
ContainerName: "my-container",
ContainerID: "docker://containerID",
LogName: "main_logs",
PodRFC3339StartTime: "1970-01-01T01:02:03+01:00",
PodRFC3339FinishTime: "1970-01-01T04:25:45+01:00",
PodUnixStartTime: 123,
PodUnixFinishTime: 12345,
TaskTemplate: &core.TaskTemplate{
Config: map[string]string{
"link_type": "vscode",
"port": "1234",
},
},
}

tests := []struct {
name string
baseVars Input
Expand Down Expand Up @@ -193,38 +173,6 @@ func Test_Input_templateVars(t *testing.T) {
},
nil,
},
{
"flyin happy path",
flyinBase,
nil,
nil,
[]TemplateVar{
{testRegexes.Port, "1234"},
},
nil,
},
{
"flyin and pod happy path",
flyinBase,
nil,
[]TemplateVar{
{defaultRegexes.LogName, "main_logs"},
{defaultRegexes.PodName, "my-pod"},
{defaultRegexes.PodUID, "my-pod-uid"},
{defaultRegexes.Namespace, "my-namespace"},
{defaultRegexes.ContainerName, "my-container"},
{defaultRegexes.ContainerID, "containerID"},
{defaultRegexes.Hostname, "my-host"},
{defaultRegexes.PodRFC3339StartTime, "1970-01-01T01:02:03+01:00"},
{defaultRegexes.PodRFC3339FinishTime, "1970-01-01T04:25:45+01:00"},
{defaultRegexes.PodUnixStartTime, "123"},
{defaultRegexes.PodUnixFinishTime, "12345"},
{testRegexes.LinkType, "vscode"},
{testRegexes.Port, "1234"},
},
nil,
nil,
},
{
"pod with port not affected",
podBase,
Expand Down Expand Up @@ -530,33 +478,6 @@ func TestTemplateLogPlugin(t *testing.T) {
},
},
},
// {
// "flyin - default port",
// TemplateLogPlugin{
// Name: "vscode",
// Scheme: TemplateSchemeDynamic,
// DynamicTemplateURIs: []TemplateURI{"vscode://flyin:{{ .taskConfig.port }}/{{ .podName }}"},
// MessageFormat: core.TaskLog_JSON,
// },
// args{
// input: Input{
// PodName: "my-pod-name",
// TaskTemplate: &core.TaskTemplate{
// Config: map[string]string{
// "link_type": "vscode",
// },
// },
// },
// },
// Output{
// TaskLogs: []*core.TaskLog{
// {
// Uri: "vscode://flyin:8080/my-pod-name",
// MessageFormat: core.TaskLog_JSON,
// },
// },
// },
// },
{
"flyin - no link_type in task template",
TemplateLogPlugin{
Expand All @@ -574,6 +495,32 @@ func TestTemplateLogPlugin(t *testing.T) {
TaskLogs: []*core.TaskLog{},
},
},
{
"kubernetes",
TemplateLogPlugin{
TemplateURIs: []TemplateURI{"https://dashboard.k8s.net/#!/log/{{.namespace}}/{{.podName}}/pod?namespace={{.namespace}}"},
MessageFormat: core.TaskLog_JSON,
},
args{
input: Input{
PodName: "flyteexamples-development-task-name",
PodUID: "pod-uid",
Namespace: "flyteexamples-development",
ContainerName: "ignore",
ContainerID: "ignore",
LogName: "main_logs",
PodRFC3339StartTime: "1970-01-01T01:02:03+01:00",
PodRFC3339FinishTime: "1970-01-01T04:25:45+01:00",
PodUnixStartTime: 123,
PodUnixFinishTime: 12345,
},
},
Output{TaskLogs: []*core.TaskLog{{
Uri: "https://dashboard.k8s.net/#!/log/flyteexamples-development/flyteexamples-development-task-name/pod?namespace=flyteexamples-development",
MessageFormat: core.TaskLog_JSON,
Name: "main_logs",
}}},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down

0 comments on commit 634c17c

Please sign in to comment.