Skip to content
This repository has been archived by the owner on May 11, 2022. It is now read-only.

Commit

Permalink
complete specification of app.toml
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilsk committed Jul 19, 2020
1 parent 7b15aa7 commit c984d54
Show file tree
Hide file tree
Showing 9 changed files with 290 additions and 155 deletions.
94 changes: 57 additions & 37 deletions internal/model/merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,15 @@ func (crons *Crons) Merge(src Crons) {
copied := *crons
copied = append(copied, src...)
sort.Sort(copied)
j := 0
shift := 0
for i := 1; i < len(copied); i++ {
if copied[j].Name == copied[i].Name {
if copied[shift].Name == copied[i].Name {
continue
}
j++
copied[j] = copied[i]
shift++
copied[shift] = copied[i]
}
*crons = copied[:j+1]
*crons = copied[:shift+1]
}

func (deps *Dependencies) Merge(src Dependencies) {
Expand All @@ -58,15 +58,15 @@ func (deps *Dependencies) Merge(src Dependencies) {
copied := *deps
copied = append(copied, src...)
sort.Sort(copied)
j := 0
shift := 0
for i := 1; i < len(copied); i++ {
if copied[j].Name == copied[i].Name {
if copied[shift].Name == copied[i].Name {
continue
}
j++
copied[j] = copied[i]
shift++
copied[shift] = copied[i]
}
*deps = copied[:j+1]
*deps = copied[:shift+1]
}

func (engine *Engine) Merge(src *Engine) {
Expand Down Expand Up @@ -111,15 +111,15 @@ func (exec *Executable) Merge(src Executable) {
copied := *exec
copied = append(copied, src...)
sort.Sort(copied)
j := 0
shift := 0
for i := 1; i < len(copied); i++ {
if copied[j].Name == copied[i].Name {
if copied[shift].Name == copied[i].Name {
continue
}
j++
copied[j] = copied[i]
shift++
copied[shift] = copied[i]
}
*exec = copied[:j+1]
*exec = copied[:shift+1]
}

func (logger *Logger) Merge(src *Logger) {
Expand All @@ -140,15 +140,15 @@ func (proxies *Proxies) Merge(src Proxies) {
copied := *proxies
copied = append(copied, src...)
sort.Sort(copied)
j := 0
shift := 0
for i := 1; i < len(copied); i++ {
if copied[j].Name == copied[i].Name {
if copied[shift].Name == copied[i].Name {
continue
}
j++
copied[j] = copied[i]
shift++
copied[shift] = copied[i]
}
*proxies = copied[:j+1]
*proxies = copied[:shift+1]
}

func (queues *Queues) Merge(src Queues) {
Expand All @@ -159,15 +159,15 @@ func (queues *Queues) Merge(src Queues) {
copied := *queues
copied = append(copied, src...)
sort.Sort(copied)
j := 0
shift := 0
for i := 1; i < len(copied); i++ {
if copied[j].Name == copied[i].Name {
if copied[shift].Name == copied[i].Name {
continue
}
j++
copied[j] = copied[i]
shift++
copied[shift] = copied[i]
}
*queues = copied[:j+1]
*queues = copied[:shift+1]
}

func (resource *Resource) Merge(src *Resource) {
Expand Down Expand Up @@ -199,6 +199,19 @@ func (resources *Resources) Merge(src *Resources) {
resources.Limits.Merge(src.Limits)
}

func (sftp *SFTP) Merge(src *SFTP) {
if sftp == nil || src == nil {
return
}

if src.Size != "" {
sftp.Size = src.Size
}
if src.Enabled != nil {
sftp.Enabled = src.Enabled
}
}

func (spec *Specification) Merge(src *Specification) {
if spec == nil || src == nil {
return
Expand Down Expand Up @@ -245,21 +258,28 @@ func (spec *Specification) Merge(src *Specification) {
spec.Executable.Merge(src.Executable)
spec.Proxies.Merge(src.Proxies)
spec.Queues.Merge(src.Queues)
spec.Sphinxes.Merge(src.Sphinxes)
spec.Workers.Merge(src.Workers)
spec.EnvVars.Merge(src.EnvVars)
}

func (sftp *SFTP) Merge(src *SFTP) {
if sftp == nil || src == nil {
func (sphinxes *Sphinxes) Merge(src Sphinxes) {
if sphinxes == nil || len(src) == 0 {
return
}

if src.Size != "" {
sftp.Size = src.Size
}
if src.Enabled != nil {
sftp.Enabled = src.Enabled
copied := *sphinxes
copied = append(copied, src...)
sort.Sort(copied)
shift := 0
for i := 1; i < len(copied); i++ {
if copied[shift].Name == copied[i].Name {
continue
}
shift++
copied[shift] = copied[i]
}
*sphinxes = copied[:shift+1]
}

func (workers *Workers) Merge(src Workers) {
Expand All @@ -270,13 +290,13 @@ func (workers *Workers) Merge(src Workers) {
copied := *workers
copied = append(copied, src...)
sort.Sort(copied)
j := 0
shift := 0
for i := 1; i < len(copied); i++ {
if copied[j].Name == copied[i].Name {
if copied[shift].Name == copied[i].Name {
continue
}
j++
copied[j] = copied[i]
shift++
copied[shift] = copied[i]
}
*workers = copied[:j+1]
*workers = copied[:shift+1]
}
13 changes: 9 additions & 4 deletions internal/model/merge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,15 +117,20 @@ func TestMerge(t *testing.T) {
assert.NotPanics(t, func() { resources.Merge(&Resources{Requests: &Resource{CPU: 1}}) })
assert.Nil(t, resources)
})
t.Run("nil sftp", func(t *testing.T) {
var sftp *SFTP
assert.NotPanics(t, func() { sftp.Merge(&SFTP{Size: "small"}) })
assert.Nil(t, sftp)
})
t.Run("nil specification", func(t *testing.T) {
var spec *Specification
assert.NotPanics(t, func() { spec.Merge(&Specification{Name: "test"}) })
assert.Nil(t, spec)
})
t.Run("nil sftp", func(t *testing.T) {
var sftp *SFTP
assert.NotPanics(t, func() { sftp.Merge(&SFTP{Size: "small"}) })
assert.Nil(t, sftp)
t.Run("nil sphinxes", func(t *testing.T) {
var sphinxes *Sphinxes
assert.NotPanics(t, func() { sphinxes.Merge(Sphinxes{{Name: "test"}}) })
assert.Nil(t, sphinxes)
})
t.Run("nil workers", func(t *testing.T) {
var workers *Workers
Expand Down
44 changes: 30 additions & 14 deletions internal/model/specification.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ type Exec struct {
Name string `toml:"name,omitempty"`
Replicas uint `toml:"replicas,omitempty"`
Command string `toml:"command,omitempty"`
Port int `toml:"service-port,omitempty"`
Port uint `toml:"service-port,omitempty"`
Size string `toml:"size,omitempty"`
RedinessProbe string `toml:"readiness-probe-command,omitempty"`
LivenessProbe string `toml:"liveness-probe-command,omitempty"`
Expand All @@ -61,7 +61,9 @@ func (exec Executable) Swap(i, j int) { exec[i], exec[j] = exec[j], exec[i]

type Host struct {
Name string `toml:"host,omitempty"`
AgentPort uint `toml:"agent_port,omitempty"`
Connections uint `toml:"connections,omitempty"`
MaxConns uint `toml:"maxconn,omitempty"`
Weight uint `toml:"weight,omitempty"`
Backup bool `toml:"backup,omitempty"`
}
Expand Down Expand Up @@ -101,15 +103,20 @@ func (queues Queues) Less(i, j int) bool { return queues[i].Name < queues[j].Nam
func (queues Queues) Swap(i, j int) { queues[i], queues[j] = queues[j], queues[i] }

type Resource struct {
CPU int `toml:"cpu,omitempty"`
Memory int `toml:"memory,omitempty"`
CPU uint `toml:"cpu,omitempty"`
Memory uint `toml:"memory,omitempty"`
}

type Resources struct {
Requests *Resource `toml:"requests,omitempty"`
Limits *Resource `toml:"limits,omitempty"`
}

type SFTP struct {
Size string `toml:"size,omitempty"`
Enabled *bool `toml:"enabled,omitempty"`
}

type Specification struct {
Name string `toml:"name,omitempty"`
Description string `toml:"kind,omitempty"`
Expand All @@ -125,24 +132,33 @@ type Specification struct {
Executable Executable `toml:"executable,omitempty"`
Proxies Proxies `toml:"proxy,omitempty"`
Queues Queues `toml:"queues,omitempty"`
Sphinxes Sphinxes `toml:"sphinx,omitempty"`
Workers Workers `toml:"workers,omitempty"`
EnvVars EnvironmentVariables `toml:"env_vars,omitempty"`
}

type SFTP struct {
Size string `toml:"size,omitempty"`
Enabled *bool `toml:"enabled,omitempty"`
type Sphinx struct {
Name string `toml:"name,omitepmty"`
Enabled *bool `toml:"enabled,omitepmty"`
Haproxy string `toml:"haproxy_tag,omitempty"`
Hosts Hosts `toml:"hosts,omitempty"`
}

type Sphinxes []Sphinx

func (sphinxes Sphinxes) Len() int { return len(sphinxes) }
func (sphinxes Sphinxes) Less(i, j int) bool { return sphinxes[i].Name < sphinxes[j].Name }
func (sphinxes Sphinxes) Swap(i, j int) { sphinxes[i], sphinxes[j] = sphinxes[j], sphinxes[i] }

type Worker struct {
Name string `toml:"name,omitempty"`
Enabled *bool `toml:"enabled,omitempty"`
Replicas uint `toml:"replicas,omitempty"`
Command string `toml:"command,omitempty"`
Commands []string `toml:"commands,omitempty"`
Size string `toml:"size,omitempty"`
LivenessProbe string `toml:"liveness-probe-command,omitempty"`
Resources Resources `toml:"resources,omitempty"`
Name string `toml:"name,omitempty"`
Enabled *bool `toml:"enabled,omitempty"`
Replicas uint `toml:"replicas,omitempty"`
Command string `toml:"command,omitempty"`
Commands []string `toml:"commands,omitempty"`
Size string `toml:"size,omitempty"`
LivenessProbe string `toml:"liveness-probe-command,omitempty"`
Resources *Resources `toml:"resources,omitempty"`
}

type Workers []Worker
Expand Down
38 changes: 38 additions & 0 deletions internal/model/specification_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,44 @@ func TestSorting(t *testing.T) {
})
}
})
t.Run("Sphinxes", func(t *testing.T) {
tests := map[string]struct {
input Sphinxes
expected Sphinxes
}{
"sorted": {
input: Sphinxes{
{Name: "a"},
{Name: "b"},
{Name: "c"},
},
expected: Sphinxes{
{Name: "a"},
{Name: "b"},
{Name: "c"},
},
},
"unsorted": {
input: Sphinxes{
{Name: "b"},
{Name: "c"},
{Name: "a"},
},
expected: Sphinxes{
{Name: "a"},
{Name: "b"},
{Name: "c"},
},
},
}

for name, test := range tests {
t.Run(name, func(t *testing.T) {
sort.Sort(test.input)
assert.Equal(t, test.expected, test.input)
})
}
})
t.Run("Workers", func(t *testing.T) {
tests := map[string]struct {
input Workers
Expand Down
27 changes: 26 additions & 1 deletion internal/model/testdata/app.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
description = "Some awesome service"
host = "http://www.example.com"
kind = "business"
name = "service"
replicas = 1
Expand Down Expand Up @@ -41,6 +40,7 @@ replicas = 1
[envs]

[envs.perf]
host = "http://www.example.com"

[[envs.perf.crons]]
enabled = false
Expand Down Expand Up @@ -75,6 +75,10 @@ replicas = 1
[envs.perf.sftp]
enabled = false

[[envs.perf.sphinx]]
enabled = false
name = "search"

[envs.prod]

[envs.prod.engine]
Expand Down Expand Up @@ -169,9 +173,30 @@ replicas = 1
schema = "item.landed.on.mars"

[[queues]]
aliases = ["item.landed.report.1", "item.landed.report.2", "item.landed.report.3"]
dlq = ["5m", "10m", "30m"]
schema = "item.landed.on.moon"

[sftp]
enabled = true
size = "small"

[[sphinx]]
enabled = true
name = "search"

[[sphinx.hosts]]
host = "search01:9306"

[[sphinx.hosts]]
host = "search02:9306"

[[sphinx]]
enabled = true
name = "standby"

[[sphinx.hosts]]
host = "search03:9306"

[[sphinx.hosts]]
host = "search04:9306"
Loading

0 comments on commit c984d54

Please sign in to comment.