update: updated logic to filter workflow templates by labels. It now supports AND so you can filter by multiple labels

This commit is contained in:
Andrey Melnikov
2020-08-09 21:04:50 -07:00
parent 8586639a6c
commit 3c3a514983
3 changed files with 19 additions and 9 deletions

View File

@@ -1,6 +1,7 @@
package v1
import (
"encoding/json"
"fmt"
"strings"
"time"
@@ -96,3 +97,16 @@ func LabelFromString(value string) (label *Label, err error) {
return label, nil
}
// LabelsToJSONString converts an array of labels to a json string representing an object
// where the keys are the label keys and the values are the label values
func LabelsToJSONString(labels []*Label) (string, error) {
labelMap := LabelsToMapping(labels...)
resultBytes, err := json.Marshal(labelMap)
if err != nil {
return "", err
}
return string(resultBytes), nil
}

View File

@@ -409,14 +409,11 @@ func (c *Client) selectWorkflowTemplatesQuery(namespace string, paginator *pagin
OrderBy("wt.created_at DESC")
if filter != nil && len(filter.Labels) > 0 {
sb = sb.Join("labels l ON wt.id = l.resource_id AND l.resource = 'workflow_template'")
// Note: multiple labels are not yet supported. The below will not correctly AND label selection.
for _, lbl := range filter.Labels {
sb = sb.Where(sq.Eq{
"l.key": lbl.Key,
"l.value": lbl.Value,
})
labelsJSON, err := LabelsToJSONString(filter.Labels)
if err != nil {
log.Printf("[error] %v", err)
} else {
sb = sb.Where("wt.labels @> ?", labelsJSON)
}
}
sb = *paginator.ApplyToSelect(&sb)

View File

@@ -136,7 +136,6 @@ func (s *WorkflowTemplateServer) CloneWorkflowTemplate(ctx context.Context, req
}
//Verify the cloned template name doesn't exist already
templatesCount, err := client.CountWorkflowTemplatesByName(req.Name, req.Name, nil)
if err != nil {
return nil, err