Skip to content

Commit

Permalink
implement labels
Browse files Browse the repository at this point in the history
  • Loading branch information
concaf committed Apr 12, 2017
1 parent 1a8121e commit 0e5a26e
Show file tree
Hide file tree
Showing 6 changed files with 246 additions and 13 deletions.
17 changes: 16 additions & 1 deletion pkg/encoding/v1/encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,19 @@ func (raw *EnvVariable) UnmarshalYAML(unmarshal func(interface{}) error) error {
return nil
}

type Labels object.Labels

func (lb *Labels) UnmarshalYAML(unmarshal func(interface{}) error) error {
labelMap := make(map[string]string)
if err := unmarshal(&labelMap); err != nil {
return err
}

*lb = Labels(labelMap)

return nil
}

type ImageRef string

// FIXME: implement ImageRef unmarshalling
Expand Down Expand Up @@ -209,6 +222,7 @@ type Service struct {
Name ResourceName `yaml:"name"`
Containers []Container `yaml:"containers"`
Replicas *int32 `yaml:"replicas,omitempty"`
Labels Labels `yaml:"labels,omitempty"`
}

func (s *Service) UnmarshalYAML(unmarshal func(interface{}) error) error {
Expand Down Expand Up @@ -341,7 +355,8 @@ func (d *Decoder) Decode(data []byte) (*object.OpenCompose, error) {
// convert services
for _, s := range v1.Services {
os := object.Service{
Name: string(s.Name),
Name: string(s.Name),
Labels: object.Labels(s.Labels),
}

os.Replicas = s.Replicas
Expand Down
65 changes: 65 additions & 0 deletions pkg/encoding/v1/encoding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,35 @@ 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 {
Expand Down Expand Up @@ -658,6 +687,42 @@ services:
},
},
},
{
true,
`
version: 0.1-dev
services:
- name: helloworld
replicas: 2
containers:
- image: tomaskral/nonroot-nginx
labels:
key1: value1
key2: value2
key3:
key4: value4
`,
&object.OpenCompose{
Version: Version,
Services: []object.Service{
{
Name: "helloworld",
Replicas: goutil.Int32Addr(2),
Containers: []object.Container{
{
Image: "tomaskral/nonroot-nginx",
},
},
Labels: object.Labels{
"key1": "value1",
"key2": "value2",
"key3": "",
"key4": "value4",
},
},
},
},
},
}

for _, tt := range tests {
Expand Down
3 changes: 3 additions & 0 deletions pkg/object/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ type EnvVariable struct {
Value string
}

type Labels map[string]string

type Container struct {
Image string
Environment []EnvVariable
Expand All @@ -36,6 +38,7 @@ type Service struct {
Name string
Containers []Container
Replicas *int32
Labels Labels
}

type Volume struct {
Expand Down
40 changes: 28 additions & 12 deletions pkg/transform/kubernetes/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"

"github.com/redhat-developer/opencompose/pkg/object"
"github.com/redhat-developer/opencompose/pkg/util"
_ "k8s.io/client-go/pkg/api/install"
api_v1 "k8s.io/client-go/pkg/api/v1"
_ "k8s.io/client-go/pkg/apis/extensions/install"
Expand All @@ -19,12 +20,16 @@ func (t *Transformer) CreateServices(o *object.Service) ([]runtime.Object, error
result := []runtime.Object{}

Service := func() *api_v1.Service {
serviceLabels := map[string]string(o.Labels)
return &api_v1.Service{
ObjectMeta: api_v1.ObjectMeta{
Name: o.Name,
Labels: map[string]string{
"service": o.Name,
},
Labels: *util.MergeMaps(
&map[string]string{
"service": o.Name,
},
&serviceLabels,
),
},
Spec: api_v1.ServiceSpec{
Selector: map[string]string{
Expand Down Expand Up @@ -80,13 +85,17 @@ func (t *Transformer) CreateServices(o *object.Service) ([]runtime.Object, error
// Create k8s ingresses for OpenCompose service
func (t *Transformer) CreateIngresses(o *object.Service) ([]runtime.Object, error) {
result := []runtime.Object{}
serviceLabels := map[string]string(o.Labels)

i := &ext_v1beta1.Ingress{
ObjectMeta: api_v1.ObjectMeta{
Name: o.Name,
Labels: map[string]string{
"service": o.Name,
},
Labels: *util.MergeMaps(
&map[string]string{
"service": o.Name,
},
&serviceLabels,
),
},
}

Expand Down Expand Up @@ -141,13 +150,17 @@ func (t *Transformer) CreateIngresses(o *object.Service) ([]runtime.Object, erro
// Create k8s deployments for OpenCompose service
func (t *Transformer) CreateDeployments(o *object.Service) ([]runtime.Object, error) {
result := []runtime.Object{}
serviceLabels := map[string]string(o.Labels)

d := &ext_v1beta1.Deployment{
ObjectMeta: api_v1.ObjectMeta{
Name: o.Name,
Labels: map[string]string{
"service": o.Name,
},
Labels: *util.MergeMaps(
&map[string]string{
"service": o.Name,
},
&serviceLabels,
),
},
Spec: ext_v1beta1.DeploymentSpec{
Strategy: ext_v1beta1.DeploymentStrategy{
Expand All @@ -158,9 +171,12 @@ func (t *Transformer) CreateDeployments(o *object.Service) ([]runtime.Object, er
},
Template: api_v1.PodTemplateSpec{
ObjectMeta: api_v1.ObjectMeta{
Labels: map[string]string{
"service": o.Name,
},
Labels: *util.MergeMaps(
&map[string]string{
"service": o.Name,
},
&serviceLabels,
),
},
Spec: api_v1.PodSpec{},
},
Expand Down
13 changes: 13 additions & 0 deletions pkg/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,16 @@ func FetchURLWithRetries(url string, attempts int, duration time.Duration) ([]by

return data, err
}

// 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
// TODO: add to docs about use with caution bits
func MergeMaps(maps ...*map[string]string) *map[string]string {
mergedMap := make(map[string]string)
for _, m := range maps {
for k, v := range *m {
mergedMap[k] = v
}
}
return &mergedMap
}
121 changes: 121 additions & 0 deletions pkg/util/util_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package util

import (
"reflect"
"testing"
"time"
)
Expand Down Expand Up @@ -64,3 +65,123 @@ func TestFetchURLWithRetries(t *testing.T) {
})
}
}

func TestMergeMaps(t *testing.T) {
no_map1 := map[string]string{
"key1": "value1",
"key2": "value2",
"key3": "value3",
}
no_map2 := map[string]string{
"key4": "value4",
"key5": "value5",
"key6": "value6",
}
no_map3 := map[string]string{
"key7": "value7",
"key8": "value8",
"key9": "value9",
}
o_map1 := map[string]string{
"key1": "value1",
"key2": "value2",
"key3": "value3",
}
o_map2 := map[string]string{
"key4": "value4",
"key2": "value5",
"key1": "value6",
}
o_map3 := map[string]string{
"key4": "value7",
"key5": "value8",
"key6": "value9",
}
no_map12 := map[string]string{
"key1": "value1",
"key2": "value2",
"key3": "value3",
"key4": "value4",
"key5": "value5",
"key6": "value6",
}
no_map123 := map[string]string{
"key1": "value1",
"key2": "value2",
"key3": "value3",
"key4": "value4",
"key5": "value5",
"key6": "value6",
"key7": "value7",
"key8": "value8",
"key9": "value9",
}
o_map12 := map[string]string{
"key3": "value3",
"key4": "value4",
"key2": "value5",
"key1": "value6",
}
o_map123 := map[string]string{
"key3": "value3",
"key2": "value5",
"key1": "value6",
"key4": "value7",
"key5": "value8",
"key6": "value9",
}
o_map231 := map[string]string{
"key4": "value7",
"key5": "value8",
"key6": "value9",
"key1": "value1",
"key2": "value2",
"key3": "value3",
}

tests := []struct {
name string
input []*map[string]string
output *map[string]string
}{
{
"passing 1 map",
[]*map[string]string{&no_map1},
&no_map1,
},
{
"merging 2 non overlapping maps",
[]*map[string]string{&no_map1, &no_map2},
&no_map12,
},
{
"merging 3 non overlapping maps",
[]*map[string]string{&no_map1, &no_map2, &no_map3},
&no_map123,
},
{
"merging 2 overlapping maps",
[]*map[string]string{&o_map1, &o_map2},
&o_map12,
},
{
"merging 3 overlapping maps",
[]*map[string]string{&o_map1, &o_map2, &o_map3},
&o_map123,
},
{
"merging 3 overlapping maps in a different order",
[]*map[string]string{&o_map2, &o_map3, &o_map1},
&o_map231,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
mergedMap := MergeMaps(tt.input...)
if !reflect.DeepEqual(*tt.output, *mergedMap) {
t.Fatalf("The expected output - %v - is different than the resulting merged map - %v", *tt.output, *mergedMap)
}
})
}
}

0 comments on commit 0e5a26e

Please sign in to comment.