Skip to content

Commit

Permalink
Add simplified endpoint declaration (#1454)
Browse files Browse the repository at this point in the history
* Add simplified endpoint declaration

Signed-off-by: Javier López Barba <javier@okteto.com>

* Add test for uncovered case

Signed-off-by: Javier López Barba <javier@okteto.com>
  • Loading branch information
jLopezbarb committed Apr 28, 2021
1 parent 948c1a3 commit f9cbb5a
Show file tree
Hide file tree
Showing 3 changed files with 167 additions and 2 deletions.
4 changes: 3 additions & 1 deletion pkg/model/stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ type Stack struct {
Volumes map[string]*VolumeSpec `yaml:"volumes,omitempty"`
Namespace string `yaml:"namespace,omitempty"`
Services map[string]*Service `yaml:"services,omitempty"`
Endpoints map[string]Endpoint `yaml:"endpoints,omitempty"`
Endpoints EndpointSpec `yaml:"endpoints,omitempty"`
}

//Service represents an okteto stack service
Expand Down Expand Up @@ -112,6 +112,8 @@ type Port struct {
Protocol apiv1.Protocol
}

type EndpointSpec map[string]Endpoint

//Endpoints represents an okteto stack ingress
type Endpoint struct {
Labels Labels `json:"labels,omitempty" yaml:"labels,omitempty"`
Expand Down
28 changes: 27 additions & 1 deletion pkg/model/stack_serializer.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ type StackRaw struct {
Name string `yaml:"name"`
Namespace string `yaml:"namespace,omitempty"`
Services map[string]*ServiceRaw `yaml:"services,omitempty"`
Endpoints map[string]Endpoint `yaml:"endpoints,omitempty"`
Endpoints EndpointSpec `yaml:"endpoints,omitempty"`
Volumes map[string]*VolumeTopLevel `yaml:"volumes,omitempty"`

// Docker-compose not implemented
Expand Down Expand Up @@ -197,6 +197,10 @@ func (s *Stack) UnmarshalYAML(unmarshal func(interface{}) error) error {
s.Namespace = stackRaw.Namespace

s.Endpoints = stackRaw.Endpoints
if endpoint, ok := s.Endpoints[""]; ok {
s.Endpoints[s.Name] = endpoint
delete(s.Endpoints, "")
}

volumes := make(map[string]*VolumeSpec, 0)
for volumeName, v := range stackRaw.Volumes {
Expand Down Expand Up @@ -471,6 +475,28 @@ func (r DeployComposeResources) toServiceResources() ServiceResources {
return resources
}

func (endpoint *EndpointSpec) UnmarshalYAML(unmarshal func(interface{}) error) error {
result := make(EndpointSpec)
if endpoint == nil {
*endpoint = result
}
var directRule Endpoint
err := unmarshal(&directRule)
if err == nil {
result[""] = directRule
*endpoint = result
return nil
}
var expandedAnnotation map[string]Endpoint
err = unmarshal(&expandedAnnotation)
if err != nil {
return err
}

*endpoint = expandedAnnotation
return nil
}

// UnmarshalYAML Implements the Unmarshaler interface of the yaml pkg.
func (s *StackResources) UnmarshalYAML(unmarshal func(interface{}) error) error {
type stackResources StackResources // prevent recursion
Expand Down
137 changes: 137 additions & 0 deletions pkg/model/stack_serializer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -646,6 +646,142 @@ func Test_restartFile(t *testing.T) {
}
}

func Test_endpoints(t *testing.T) {
tests := []struct {
name string
manifest []byte
expected EndpointSpec
}{
{
name: "rule with name",
manifest: []byte(`name: test
services:
app:
ports:
- 9213
image: okteto/vote:1
endpoints:
app:
- path: /
service: app
port: 9213`),
expected: EndpointSpec{
"app": Endpoint{
Annotations: make(Annotations),
Labels: make(Labels),
Rules: []EndpointRule{
{
Service: "app",
Path: "/",
Port: 9213,
},
},
},
},
},
{
name: "rule with name and annotations/labels",
manifest: []byte(`name: test
services:
app:
ports:
- 9213
image: okteto/vote:1
endpoints:
app:
annotations:
key: value
labels:
key: value
rules:
- path: /
service: app
port: 9213`),
expected: EndpointSpec{
"app": Endpoint{
Annotations: Annotations{"key": "value"},
Labels: Labels{"key": "value"},
Rules: []EndpointRule{
{
Service: "app",
Path: "/",
Port: 9213,
},
},
},
},
},
{
name: "direct rules with labels/annotations",
manifest: []byte(`name: test
services:
app:
ports:
- 9213
image: okteto/vote:1
endpoints:
annotations:
key: value
labels:
key: value
rules:
- path: /
service: app
port: 9213`),
expected: EndpointSpec{
"test": Endpoint{
Annotations: Annotations{"key": "value"},
Labels: Labels{"key": "value"},
Rules: []EndpointRule{
{
Service: "app",
Path: "/",
Port: 9213,
},
},
},
},
},
{
name: "direct rules",
manifest: []byte(`name: test
services:
app:
ports:
- 9213
image: okteto/vote:1
endpoints:
- path: /
service: app
port: 9213`),
expected: EndpointSpec{
"test": Endpoint{
Annotations: make(Annotations),
Labels: make(Labels),
Rules: []EndpointRule{
{
Service: "app",
Path: "/",
Port: 9213,
},
},
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
s, err := ReadStack(tt.manifest, false)
if err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(tt.expected, s.Endpoints) {
t.Fatalf("Expected %v, but got %v", tt.expected, s.Endpoints)
}
})
}
}

func Test_validateEnvFiles(t *testing.T) {
tests := []struct {
name string
Expand Down Expand Up @@ -679,6 +815,7 @@ func Test_validateEnvFiles(t *testing.T) {
if err != nil {
t.Fatal(err)
}

if svc, ok := s.Services["app"]; ok {
if !reflect.DeepEqual(tt.EnvFiles, svc.EnvFiles) {
t.Fatalf("expected %v but got %v", tt.EnvFiles, svc.EnvFiles)
Expand Down

0 comments on commit f9cbb5a

Please sign in to comment.