Skip to content

Commit

Permalink
implement labels
Browse files Browse the repository at this point in the history
  • Loading branch information
concaf committed Apr 6, 2017
1 parent d7d01b7 commit 33ca615
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 10 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 @@ -208,6 +221,7 @@ func (c *Container) UnmarshalYAML(unmarshal func(interface{}) error) error {
type Service struct {
Name ResourceName `yaml:"name"`
Containers []Container `yaml:"containers"`
Labels Labels `yaml:"labels,omitempty"`
}

func (s *Service) UnmarshalYAML(unmarshal func(interface{}) error) error {
Expand Down Expand Up @@ -340,7 +354,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),
}

// convert containers
Expand Down
3 changes: 3 additions & 0 deletions pkg/object/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ type EnvVariable struct {
Value string
}

type Labels map[string]string

type Container struct {
Image string
Environment []EnvVariable
Expand All @@ -33,6 +35,7 @@ type Container struct {
type Service struct {
Name string
Containers []Container
Labels Labels
}

type Volume struct {
Expand Down
31 changes: 22 additions & 9 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 Down
12 changes: 12 additions & 0 deletions pkg/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,15 @@ 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
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
}

0 comments on commit 33ca615

Please sign in to comment.