From c1e6b2775159a26e50aeb4caa58243018136f605 Mon Sep 17 00:00:00 2001 From: Suhas Karanth Date: Fri, 24 Feb 2023 16:05:34 +0530 Subject: [PATCH] fix: Sink asset labels to Compass (#475) Labels were not being sent to Compass if no labels were specified in the sink configuration for Compass. --- plugins/sinks/compass/sink.go | 28 ++++++++++++++-------------- plugins/sinks/compass/sink_test.go | 13 ++++++++++--- 2 files changed, 24 insertions(+), 17 deletions(-) diff --git a/plugins/sinks/compass/sink.go b/plugins/sinks/compass/sink.go index bea9b255c..ac49652a9 100644 --- a/plugins/sinks/compass/sink.go +++ b/plugins/sinks/compass/sink.go @@ -226,27 +226,27 @@ func (s *Sink) buildOwners(asset *v1beta2.Asset) (owners []Owner) { return } -func (s *Sink) buildLabels(asset *v1beta2.Asset) (labels map[string]string, err error) { - if s.config.Labels == nil { - return +func (s *Sink) buildLabels(asset *v1beta2.Asset) (map[string]string, error) { + total := len(s.config.Labels) + len(asset.Labels) + if total == 0 { + return nil, nil } - labels = asset.GetLabels() - if labels == nil { - labels = make(map[string]string) + labels := make(map[string]string, total) + for k, v := range asset.Labels { + labels[k] = v } + for key, template := range s.config.Labels { - var value string - value, err = s.buildLabelValue(template, asset) + value, err := s.buildLabelValue(template, asset) if err != nil { - err = errors.Wrapf(err, "could not find \"%s\"", template) - return + return nil, errors.Wrapf(err, "could not find %q", template) } labels[key] = value } - return + return labels, nil } func (s *Sink) buildLabelValue(template string, asset *v1beta2.Asset) (value string, err error) { @@ -271,12 +271,12 @@ func (s *Sink) getLabelValueFromProperties(field1 string, field2 string, asset * attr := utils.GetAttributes(asset) v, ok := attr[field2] if !ok { - err = fmt.Errorf("could not find \"%s\" field on attributes", field2) + err = fmt.Errorf("could not find %q field on attributes", field2) return } value, ok = v.(string) if !ok { - err = fmt.Errorf("\"%s\" field is not a string", field2) + err = fmt.Errorf("%q field is not a string", field2) return } return @@ -289,7 +289,7 @@ func (s *Sink) getLabelValueFromProperties(field1 string, field2 string, asset * var ok bool value, ok = labels[field2] if !ok { - err = fmt.Errorf("could not find \"%s\" from labels", field2) + err = fmt.Errorf("could not find %q from labels", field2) return } diff --git a/plugins/sinks/compass/sink_test.go b/plugins/sinks/compass/sink_test.go index 2e6b987d7..32265f8bb 100644 --- a/plugins/sinks/compass/sink_test.go +++ b/plugins/sinks/compass/sink_test.go @@ -16,13 +16,12 @@ import ( "github.com/stretchr/testify/require" "google.golang.org/protobuf/types/known/anypb" - testUtils "github.com/odpf/meteor/test/utils" - "github.com/odpf/meteor/utils" - "github.com/odpf/meteor/models" v1beta2 "github.com/odpf/meteor/models/odpf/assets/v1beta2" "github.com/odpf/meteor/plugins" "github.com/odpf/meteor/plugins/sinks/compass" + testUtils "github.com/odpf/meteor/test/utils" + "github.com/odpf/meteor/utils" "github.com/stretchr/testify/assert" ) @@ -213,6 +212,10 @@ func TestSink(t *testing.T) { }, }, }), + Labels: map[string]string{ + "labelA": "valueLabelA", + "labelB": "valueLabelB", + }, }, config: map[string]interface{}{ "host": host, @@ -236,6 +239,10 @@ func TestSink(t *testing.T) { }, }, }, + Labels: map[string]string{ + "labelA": "valueLabelA", + "labelB": "valueLabelB", + }, }, }, },