Skip to content

Commit

Permalink
Create multiple endpoints instead of setting public (#1485)
Browse files Browse the repository at this point in the history
* Create multiple endpoints for not public svcs

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

* Generate endpoints if more than two ports exposed

Signed-off-by: Javier López Barba <javier@okteto.com>
  • Loading branch information
jLopezbarb committed May 7, 2021
1 parent dd512f6 commit 6848092
Show file tree
Hide file tree
Showing 2 changed files with 189 additions and 4 deletions.
40 changes: 37 additions & 3 deletions pkg/model/stack_serializer.go
Expand Up @@ -204,6 +204,9 @@ func (s *Stack) UnmarshalYAML(unmarshal func(interface{}) error) error {
delete(s.Endpoints, "")
}

if len(s.Endpoints) == 0 {
s.Endpoints = getEndpointsFromPorts(stackRaw.Services)
}
volumes := make(map[string]*VolumeSpec)
for volumeName, v := range stackRaw.Volumes {
result := VolumeSpec{}
Expand Down Expand Up @@ -240,6 +243,40 @@ func (s *Stack) UnmarshalYAML(unmarshal func(interface{}) error) error {
return nil
}

func getEndpointsFromPorts(services map[string]*ServiceRaw) EndpointSpec {
endpoints := make(EndpointSpec)
for svcName, svc := range services {
accessiblePorts := getAccessiblePorts(svc.Ports)
if len(accessiblePorts) >= 2 {
for _, p := range svc.Ports {
if p.HostPort != 0 {
endpointName := fmt.Sprintf("%s-%d", svcName, p.HostPort)
endpoints[endpointName] = Endpoint{
Rules: []EndpointRule{
{
Path: "/",
Service: svcName,
Port: p.ContainerPort,
},
},
}
}
}
}
}
return endpoints
}

func getAccessiblePorts(ports []PortRaw) []PortRaw {
accessiblePorts := make([]PortRaw, 0)
for _, p := range ports {
if p.HostPort != 0 && !IsSkippablePort(p.ContainerPort) {
accessiblePorts = append(accessiblePorts, p)
}
}
return accessiblePorts
}

func (serviceRaw *ServiceRaw) ToService(svcName string, stack *Stack) (*Service, error) {
svc := &Service{}
var err error
Expand Down Expand Up @@ -292,9 +329,6 @@ func (serviceRaw *ServiceRaw) ToService(svcName string, stack *Stack) (*Service,
svc.Public = serviceRaw.Public

for _, p := range serviceRaw.Ports {
if p.HostPort != 0 {
svc.Public = true
}
svc.Ports = append(svc.Ports, Port{Port: p.ContainerPort, Protocol: p.Protocol})
}

Expand Down
153 changes: 152 additions & 1 deletion pkg/model/stack_serializer_test.go
Expand Up @@ -549,7 +549,7 @@ func Test_validateIngressCreationPorts(t *testing.T) {
{
name: "not-public-port-but-with-assignation",
manifest: []byte("services:\n app:\n ports:\n - 9213:9213\n image: okteto/vote:1"),
isPublic: true,
isPublic: false,
},
{
name: "mysql-port-forwarding",
Expand Down Expand Up @@ -896,3 +896,154 @@ func Test_Environment(t *testing.T) {
})
}
}

func Test_MultipleEndpoints(t *testing.T) {
tests := []struct {
name string
manifest []byte
expectedStack *Stack
}{
{
name: "no-ports",
manifest: []byte("services:\n app:\n image: okteto/vote:1"),
expectedStack: &Stack{
Services: map[string]*Service{
"app": {Image: "okteto/vote:1"},
},
Endpoints: EndpointSpec{},
},
},
{
name: "one-port-that-should-not-be-skipped",
manifest: []byte("services:\n app:\n image: okteto/vote:1\n ports:\n - 8080:8080"),
expectedStack: &Stack{
Services: map[string]*Service{
"app": {Image: "okteto/vote:1"},
},
Endpoints: EndpointSpec{},
},
},
{
name: "two-port-that-should-not-be-skipped",
manifest: []byte("services:\n app:\n image: okteto/vote:1\n ports:\n - 8080:8080\n - 80:8081"),
expectedStack: &Stack{
Services: map[string]*Service{
"app": {Image: "okteto/vote:1"},
},
Endpoints: EndpointSpec{
"app-8080": Endpoint{
Rules: []EndpointRule{
{
Path: "/",
Service: "app",
Port: 8080,
},
},
},
"app-80": Endpoint{
Rules: []EndpointRule{
{
Path: "/",
Service: "app",
Port: 8081,
},
},
},
},
},
},
{
name: "one-port-that-should-be-skipped",
manifest: []byte("services:\n app:\n image: okteto/vote:1\n ports:\n - 3306:3306"),
expectedStack: &Stack{
Services: map[string]*Service{
"app": {Image: "okteto/vote:1"},
},
Endpoints: EndpointSpec{},
},
},
{
name: "two-ports-one-skippable-and-one-not",
manifest: []byte("services:\n app:\n image: okteto/vote:1\n ports:\n - 8080:8080\n - 3306:3306"),
expectedStack: &Stack{
Services: map[string]*Service{
"app": {Image: "okteto/vote:1"},
},
Endpoints: EndpointSpec{},
},
},
{
name: "three-ports-one-skippable-and-two-not",
manifest: []byte("services:\n app:\n image: okteto/vote:1\n ports:\n - 8080:8080\n - 80:8081\n - 3306:3306"),
expectedStack: &Stack{
Services: map[string]*Service{
"app": {Image: "okteto/vote:1"},
},
Endpoints: EndpointSpec{
"app-8080": Endpoint{
Rules: []EndpointRule{
{
Path: "/",
Service: "app",
Port: 8080,
},
},
},
"app-80": Endpoint{
Rules: []EndpointRule{
{
Path: "/",
Service: "app",
Port: 8081,
},
},
},
},
},
},
{
name: "two-ports-not-skippable",
manifest: []byte("services:\n app:\n image: okteto/vote:1\n ports:\n - 8080:8080\n - 8081:8081"),
expectedStack: &Stack{
Services: map[string]*Service{
"app": {Image: "okteto/vote:1"},
},
Endpoints: EndpointSpec{
"app-8080": Endpoint{
Rules: []EndpointRule{
{
Path: "/",
Service: "app",
Port: 8080,
},
},
},
"app-8081": Endpoint{
Rules: []EndpointRule{
{
Path: "/",
Service: "app",
Port: 8081,
},
},
},
},
},
},
}
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 len(s.Endpoints) != len(tt.expectedStack.Endpoints) {
t.Fatal("The number of created endpoints is incorrect")
}
if !reflect.DeepEqual(s.Endpoints, tt.expectedStack.Endpoints) {
t.Fatal("The endpoints have not been created properly")
}
})
}
}

0 comments on commit 6848092

Please sign in to comment.