Skip to content

Commit

Permalink
feat(server): dissociate ports from environment
Browse files Browse the repository at this point in the history
Signed-off-by: Quentin Guidée <[email protected]>
  • Loading branch information
quentinguidee committed Mar 5, 2024
1 parent fa5956a commit 91e4c41
Show file tree
Hide file tree
Showing 7 changed files with 128 additions and 25 deletions.
2 changes: 1 addition & 1 deletion server/apps/containers/adapter/runner_docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ func (a runnerDockerAdapter) Start(
WithEnv(env).
WithCaps(caps).
WithSysctls(sysctls).
WithPorts(ports, env).
WithPorts(ports).
WithVolumes(volumes).
WithCommand(c.Command).
Build()
Expand Down
12 changes: 5 additions & 7 deletions server/apps/containers/core/service/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func (s *containerService) CreateContainer(ctx context.Context, opts types.Creat

env []types.TemplateEnv
caps []string
ports = map[string]string{}
ports []types.TemplatePort
volumes = map[string]string{}
sysctls = map[string]string{}
)
Expand All @@ -122,12 +122,10 @@ func (s *containerService) CreateContainer(ctx context.Context, opts types.Creat
cmd = template.Methods.Docker.Cmd

env = template.Env
ports = template.Ports
if template.Methods.Docker.Capabilities != nil {
caps = *template.Methods.Docker.Capabilities
}
if template.Methods.Docker.Ports != nil {
ports = *template.Methods.Docker.Ports
}
if template.Methods.Docker.Volumes != nil {
volumes = *template.Methods.Docker.Volumes
}
Expand Down Expand Up @@ -194,12 +192,12 @@ func (s *containerService) CreateContainer(ctx context.Context, opts types.Creat
}

// Set default ports
for in, out := range ports {
for _, p := range ports {
err = s.ports.CreatePort(ctx, types.Port{
ID: uuid.New(),
ContainerID: id,
In: in,
Out: out,
In: p.Port,
Out: p.Port,
})
if err != nil {
return nil, err
Expand Down
7 changes: 2 additions & 5 deletions server/apps/containers/core/types/builder/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,10 @@ func (b *ContainerBuilder) WithSysctls(sysctls types.Sysctls) *ContainerBuilder
return b
}

func (b *ContainerBuilder) WithPorts(ports types.Ports, env types.EnvVariables) *ContainerBuilder {
func (b *ContainerBuilder) WithPorts(ports types.Ports) *ContainerBuilder {
var all []string
for _, p := range ports {
out := env.Get(p.Out)
if out == "" {
continue
}
out := p.Out
in := p.In
all = append(all, out+":"+in)
}
Expand Down
4 changes: 2 additions & 2 deletions server/apps/containers/core/types/ports.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ type (
Port struct {
ID uuid.UUID `json:"id" db:"id" example:"7e63ced7-4f4e-4b79-95ca-62930866f7bc"`
ContainerID uuid.UUID `json:"container_id" db:"container_id" example:"d1fb743c-f937-4f3d-95b9-1a8475464591"`
In string `json:"in" db:"internal_port" example:"5432"` // Port in the container
Out string `json:"out" db:"external_port" example:"DB_PORT"` // Port exposed
In string `json:"in" db:"internal_port" example:"5432"` // Port in the container
Out string `json:"out" db:"external_port" example:"5432"` // Port exposed
}
)
44 changes: 41 additions & 3 deletions server/apps/containers/core/types/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ import (
"github.com/vertex-center/vlog"
)

const MaxSupportedVersion Version = 2
const MaxSupportedVersion Version = 3

var ErrTemplateNotFound = errors.NotFoundf("template")

type Version int

type TemplateVersioning struct {
// Version of the template format used.
Version Version `yaml:"version" json:"version" example:"2"`
Version Version `yaml:"version" json:"version" example:"3"`
}

type Template struct {
Expand Down Expand Up @@ -49,6 +49,9 @@ type Template struct {
// Databases defines all databases used by the template.
Databases map[string]DatabaseEnvironment `yaml:"databases,omitempty" json:"databases,omitempty"`

// Ports defines all ports that should be exposed by the container.
Ports []TemplatePort `yaml:"ports,omitempty" json:"ports,omitempty"`

// URLs defines all template urls.
URLs []URL `yaml:"urls,omitempty" json:"urls,omitempty"`

Expand Down Expand Up @@ -87,7 +90,28 @@ func (s *TemplateV1) Upgrade() *TemplateV2 {

type TemplateV2 Template

func (s *TemplateV2) Upgrade() *Template {
// Upgrade TemplateV2 to TemplateV3.
// Ports are now an array in the root of the template.
func (s *TemplateV2) Upgrade() *TemplateV3 {
s.Version = 3
envs := make([]TemplateEnv, 0)
for _, env := range s.Env {
if env.Type == "port" {
s.Ports = append(s.Ports, TemplatePort{
Name: env.Name,
Port: env.Default,
})
} else {
envs = append(envs, env)
}
}
s.Env = envs
return (*TemplateV3)(s)
}

type TemplateV3 Template

func (s *TemplateV3) Upgrade() *Template {
return (*Template)(s)
}

Expand All @@ -114,6 +138,8 @@ func (s *Template) UnmarshalYAML(unmarshal func(interface{}) error) error {
tmpl = &TemplateV1{}
case 2:
tmpl = &TemplateV2{}
case 3:
tmpl = &TemplateV3{}
}
err = unmarshal(tmpl)
if err != nil {
Expand All @@ -128,6 +154,9 @@ func (s *Template) UnmarshalYAML(unmarshal func(interface{}) error) error {
fallthrough
case 2:
tmpl = tmpl.(*TemplateV2).Upgrade()
fallthrough
case 3:
tmpl = tmpl.(*TemplateV3).Upgrade()
}

if serv, ok := tmpl.(*Template); ok {
Expand Down Expand Up @@ -212,6 +241,14 @@ type TemplateEnv struct {
Description string `yaml:"description" json:"description" example:"The port where the server will listen."`
}

type TemplatePort struct {
// Name is the name displayed to the used describing this port.
Name string `yaml:"name" json:"name" example:"Server Port"`

// Port is the port where this port is supposed to be.
Port string `yaml:"port" json:"port" example:"3000"`
}

type TemplateDependency struct{}

type TemplateClone struct {
Expand All @@ -230,6 +267,7 @@ type TemplateMethodDocker struct {

// Ports is a map containing docker port as a key, and output port as a value.
// The output port is automatically adjusted with PORT environment variables.
// Deprecated: Use the root Ports variable instead.
Ports *map[string]string `yaml:"ports,omitempty" json:"ports,omitempty"`

// Volumes is a map containing output folder as a key, and input folder from Docker
Expand Down
60 changes: 53 additions & 7 deletions server/apps/containers/core/types/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"testing"

"github.com/stretchr/testify/suite"
"gopkg.in/yaml.v3"
)

type TemplateTestSuite struct {
Expand All @@ -15,7 +14,7 @@ func TestTemplateTestSuite(t *testing.T) {
suite.Run(t, new(TemplateTestSuite))
}

func (suite *TemplateTestSuite) TestTemplateUpgrade() {
func (suite *TemplateTestSuite) TestTemplateUpgradeV2() {
t := TemplateV1{
TemplateVersioning: TemplateVersioning{
Version: 1,
Expand Down Expand Up @@ -50,12 +49,8 @@ func (suite *TemplateTestSuite) TestTemplateUpgrade() {
},
}

bytes, err := yaml.Marshal(&t)
suite.Require().NoError(err)
template := t.Upgrade()

var template Template
err = yaml.Unmarshal(bytes, &template)
suite.Require().NoError(err)
suite.Equal(2, int(template.Version))
suite.Equal(&map[string]string{
"22": "PORT_22",
Expand All @@ -70,3 +65,54 @@ func (suite *TemplateTestSuite) TestTemplateUpgrade() {
},
}, template.URLs)
}

func (suite *TemplateTestSuite) TestTemplateUpgradeV3() {
t := TemplateV2{
TemplateVersioning: TemplateVersioning{
Version: 2,
},
Env: []TemplateEnv{
{
Type: "port",
Name: "PORT_22",
Default: "22",
},
{
Type: "port",
Name: "PORT_80",
Default: "80",
},
},
URLs: []URL{
{
Port: "PORT_22",
},
{
Port: "PORT_80",
},
},
Methods: TemplateMethods{
Docker: &TemplateMethodDocker{
Ports: &map[string]string{
"22": "PORT_22",
"80": "PORT_80",
},
},
},
}

template := t.Upgrade()

suite.Equal(3, int(template.Version))
suite.Equal([]TemplatePort{
{
Name: "PORT_22",
Port: "22",
},
{
Name: "PORT_80",
Port: "80",
},
}, template.Ports)
suite.Empty(template.Env)
}
24 changes: 24 additions & 0 deletions server/apps/containers/database/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ var Migrations = []vsql.Migration{
// 0.16
&v1{}, // Rename service_id to template_id
&v2{}, // Make template_id nullable
&v3{}, // Move external port from env variable to ports table
}

type v1 struct{}
Expand Down Expand Up @@ -39,3 +40,26 @@ func (m *v2) Up(tx *sqlx.Tx) error {
`)
return err
}

type v3 struct{}

func (m *v3) Up(tx *sqlx.Tx) error {
_, err := tx.Exec(`
UPDATE ports
SET external_port = (
SELECT value AS external_port
FROM env_variables
WHERE type = 'port' AND container_id = ports.container_id AND default_value = ports.internal_port
)
WHERE true
`)
if err != nil {
return err
}

_, err = tx.Exec(`
DELETE FROM env_variables
WHERE type = 'port'
`)
return err
}

0 comments on commit 91e4c41

Please sign in to comment.