Skip to content

Commit

Permalink
populate name if one service specified, add tests
Browse files Browse the repository at this point in the history
If only one root level service is defined, then the resulting
Kubernetes service will have the name populated as the root level
name field.

The same is being done in case of root level persistent volume.

Also, tests have been added for this behavior.

Fixes kedgeproject#33
  • Loading branch information
concaf committed Jun 20, 2017
1 parent 9917534 commit 0ab70e0
Show file tree
Hide file tree
Showing 8 changed files with 197 additions and 0 deletions.
44 changes: 44 additions & 0 deletions pkg/encoding/encoding_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package encoding

import (
"testing"

"github.com/surajssd/kapp/pkg/encoding/fixtures"
"github.com/surajssd/kapp/pkg/spec"

"reflect"

"github.com/davecgh/go-spew/spew"
)

func TestDecode(t *testing.T) {
tests := []struct {
Name string
Data []byte
App *spec.App
}{
{
Name: "One container mentioned in the spec",
Data: fixtures.SingleContainer,
App: &fixtures.SingleContainerApp,
},
{
Name: "One persistent volume mentioned in the spec",
Data: fixtures.SinglePersistentVolume,
App: &fixtures.SinglePersistentVolumeApp,
},
}

for _, test := range tests {
t.Run(test.Name, func(t *testing.T) {
app, err := Decode(test.Data)
if err != nil {
t.Fatalf("Unable to run Decode(), and error occurred: %v", err)
}

if !reflect.DeepEqual(test.App, app) {
t.Fatalf("Expected:\n%v\nGot:\n%v", spew.Sprint(test.App), spew.Sprint(app))
}
})
}
}
10 changes: 10 additions & 0 deletions pkg/encoding/fixtures/single_container.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package fixtures

var SingleContainer []byte = []byte(
`name: test
containers:
- image: nginx
services:
- ports:
- port: 8080
`)
28 changes: 28 additions & 0 deletions pkg/encoding/fixtures/single_container_app.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package fixtures

import (
"github.com/surajssd/kapp/pkg/spec"
api_v1 "k8s.io/client-go/pkg/api/v1"
)

var SingleContainerApp spec.App = spec.App{
Name: "test",
Containers: []spec.Container{
{
Container: api_v1.Container{
Image: "nginx",
},
},
},
Services: []spec.ServiceSpecMod{
{
Ports: []spec.ServicePortMod{
{
ServicePort: api_v1.ServicePort{
Port: 8080,
},
},
},
},
},
}
12 changes: 12 additions & 0 deletions pkg/encoding/fixtures/single_persistent_volume.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package fixtures

var SinglePersistentVolume []byte = []byte(
`name: test
containers:
- image: nginx
services:
- ports:
- port: 8080
persistentVolumes:
- size: 500Mi
`)
33 changes: 33 additions & 0 deletions pkg/encoding/fixtures/single_persistent_volume_app.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package fixtures

import (
"github.com/surajssd/kapp/pkg/spec"
api_v1 "k8s.io/client-go/pkg/api/v1"
)

var SinglePersistentVolumeApp spec.App = spec.App{
Name: "test",
Containers: []spec.Container{
{
Container: api_v1.Container{
Image: "nginx",
},
},
},
Services: []spec.ServiceSpecMod{
{
Ports: []spec.ServicePortMod{
{
ServicePort: api_v1.ServicePort{
Port: 8080,
},
},
},
},
},
PersistentVolumes: []spec.PersistentVolume{
{
Size: "500Mi",
},
},
}
16 changes: 16 additions & 0 deletions pkg/transform/fixtures/single_container_object.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package fixtures

import api_v1 "k8s.io/client-go/pkg/api/v1"

var SingleContainerService *api_v1.Service = &api_v1.Service{
ObjectMeta: api_v1.ObjectMeta{
Name: "test",
},
Spec: api_v1.ServiceSpec{
Ports: []api_v1.ServicePort{
{
Port: 8080,
},
},
},
}
14 changes: 14 additions & 0 deletions pkg/transform/kubernetes/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,13 @@ func createServices(app *spec.App) ([]runtime.Object, error) {
if len(svc.Spec.Selector) == 0 {
svc.Spec.Selector = app.Labels
}
if s.Name == "" {
if len(app.Services) == 1 {
svc.ObjectMeta.Name = app.Name
} else {
return nil, errors.New("More than one service mentioned, please specify name for each service")
}
}
svcs = append(svcs, svc)

// Generate ingress if "endpoint" is mentioned in app.Services.Ports[].Endpoint
Expand Down Expand Up @@ -289,6 +296,13 @@ func CreateK8sObjects(app *spec.App) ([]runtime.Object, error) {
if err != nil {
return nil, errors.Wrapf(err, "app %q", app.Name)
}
if v.Name == "" {
if len(app.PersistentVolumes) == 1 {
pvc.Name = app.Name
} else {
return nil, errors.New("More than one persistent volume mentioned, please specify name for each one")
}
}
pvcs = append(pvcs, pvc)
}
if err := populateVolumes(app); err != nil {
Expand Down
40 changes: 40 additions & 0 deletions pkg/transform/kubernetes/kubernetes_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package kubernetes

import (
"testing"

"reflect"

encodingFixtures "github.com/surajssd/kapp/pkg/encoding/fixtures"
"github.com/surajssd/kapp/pkg/spec"
transformFixtures "github.com/surajssd/kapp/pkg/transform/fixtures"
"k8s.io/client-go/pkg/runtime"
)

func TestCreateServices(t *testing.T) {
tests := []struct {
Name string
App *spec.App
Objects []runtime.Object
}{
{
"Single container specified",
&encodingFixtures.SingleContainerApp,
append(make([]runtime.Object, 0), transformFixtures.SingleContainerService),
},
}

for _, test := range tests {
t.Run(test.Name, func(t *testing.T) {
object, err := createServices(test.App)
if err != nil {
t.Fatalf("Creating services failed: %v", err)
}
if !reflect.DeepEqual(test.Objects, object) {
t.Fatalf("Expected:\n%v\nGot:\n%v", test.Objects, object)
}
})
}
}

// TODO: add test for auto naming of single persistent volume

0 comments on commit 0ab70e0

Please sign in to comment.