diff --git a/pkg/encoding/v1/encoding_test.go b/pkg/encoding/v1/encoding_test.go index 5846af20..0f6b2a4e 100644 --- a/pkg/encoding/v1/encoding_test.go +++ b/pkg/encoding/v1/encoding_test.go @@ -265,6 +265,53 @@ func TestEnvVariable_UnmarshalYAML(t *testing.T) { } } +func TestLabels_UnmarshalYAML(t *testing.T) { + tests := []struct { + name string + Succeed bool + RawLabels string + Labels *Labels + }{ + { + "Providing valid label strings", + true, + ` +key1: value1 +key2: value2 +key3: +key4: value4 +`, + &Labels{ + "key1": "value1", + "key2": "value2", + "key3": "", + "key4": "value4", + }, + }, + } + + for _, tt := range tests { + var labels Labels + err := yaml.Unmarshal([]byte(tt.RawLabels), &labels) + if err != nil { + if tt.Succeed { + t.Errorf("Failed to unmarshal %#v; error %#v", tt.RawLabels, err) + } + continue + } + + if !tt.Succeed { + t.Errorf("Expected %#v to fail!", tt.RawLabels) + continue + } + + if !reflect.DeepEqual(labels, *tt.Labels) { + t.Errorf("Expected %#v, got %#v", *tt.Labels, labels) + continue + } + } +} + func TestService_UnmarshalYAML(t *testing.T) { tests := []struct { @@ -319,35 +366,6 @@ containers: }, }, }, - - { - "Providing valid label strings", - true, - ` -name: frontend -containers: -- image: tomaskral/kompose-demo-frontend:test -labels: - key1: value1 - key2: value2 - key3: - key4: value4 -`, - &Service{ - Name: "frontend", - Containers: []Container{ - { - Image: "tomaskral/kompose-demo-frontend:test", - }, - }, - Labels: Labels{ - "key1": "value1", - "key2": "value2", - "key3": "", - "key4": "value4", - }, - }, - }, } for _, test := range tests { diff --git a/pkg/transform/kubernetes/kubernetes.go b/pkg/transform/kubernetes/kubernetes.go index 7c97c250..970d8252 100644 --- a/pkg/transform/kubernetes/kubernetes.go +++ b/pkg/transform/kubernetes/kubernetes.go @@ -25,10 +25,12 @@ func (t *Transformer) CreateServices(o *object.Service) ([]runtime.Object, error ObjectMeta: api_v1.ObjectMeta{ Name: o.Name, Labels: *util.MergeMaps( + // The map containing `"service": o.Name` should always be + // passed later to avoid being overridden by util.MergeMaps() + &serviceLabels, &map[string]string{ "service": o.Name, }, - &serviceLabels, ), }, Spec: api_v1.ServiceSpec{ @@ -91,10 +93,12 @@ func (t *Transformer) CreateIngresses(o *object.Service) ([]runtime.Object, erro ObjectMeta: api_v1.ObjectMeta{ Name: o.Name, Labels: *util.MergeMaps( + // The map containing `"service": o.Name` should always be + // passed later to avoid being overridden by util.MergeMaps() + &serviceLabels, &map[string]string{ "service": o.Name, }, - &serviceLabels, ), }, } @@ -156,10 +160,12 @@ func (t *Transformer) CreateDeployments(o *object.Service) ([]runtime.Object, er ObjectMeta: api_v1.ObjectMeta{ Name: o.Name, Labels: *util.MergeMaps( + // The map containing `"service": o.Name` should always be + // passed later to avoid being overridden by util.MergeMaps() + &serviceLabels, &map[string]string{ "service": o.Name, }, - &serviceLabels, ), }, Spec: ext_v1beta1.DeploymentSpec{ @@ -172,10 +178,12 @@ func (t *Transformer) CreateDeployments(o *object.Service) ([]runtime.Object, er Template: api_v1.PodTemplateSpec{ ObjectMeta: api_v1.ObjectMeta{ Labels: *util.MergeMaps( + // The map containing `"service": o.Name` should always be + // passed later to avoid being overridden by util.MergeMaps() + &serviceLabels, &map[string]string{ "service": o.Name, }, - &serviceLabels, ), }, Spec: api_v1.PodSpec{}, diff --git a/pkg/util/util.go b/pkg/util/util.go index 99ccc7a5..ed42b269 100644 --- a/pkg/util/util.go +++ b/pkg/util/util.go @@ -71,7 +71,7 @@ func FetchURLWithRetries(url string, attempts int, duration time.Duration) ([]by } // MergeMaps will merge the given maps, but it does not check for conflicts. -// In case of conflicting keys, the map that is provided later wins +// In case of conflicting keys, the map that is provided later wins. // TODO: add to docs about use with caution bits func MergeMaps(maps ...*map[string]string) *map[string]string { mergedMap := make(map[string]string)