Skip to content

Commit 44ac008

Browse files
tarasposm-kunz
andauthored
feat(job-run): Add volumes-from parameter (#309)
* add volumes-from option * Add tests * Update jobs.md --------- Co-authored-by: Martin Tomisch <martin@kunzmanns.com>
1 parent 7071cff commit 44ac008

File tree

4 files changed

+133
-11
lines changed

4 files changed

+133
-11
lines changed

cli/config_test.go

+117-1
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
package cli
22

33
import (
4+
"encoding/json"
45
"testing"
56

67
defaults "github.com/mcuadros/go-defaults"
78
"github.com/mcuadros/ofelia/core"
89
"github.com/mcuadros/ofelia/middlewares"
910
. "gopkg.in/check.v1"
11+
gcfg "gopkg.in/gcfg.v1"
1012
)
1113

1214
func Test(t *testing.T) { TestingT(t) }
@@ -68,6 +70,76 @@ func (s *SuiteConfig) TestExecJobBuild(c *C) {
6870
c.Assert(j.Middlewares(), HasLen, 1)
6971
}
7072

73+
func (s *SuiteConfig) TestConfigIni(c *C) {
74+
testcases := []struct {
75+
Ini string
76+
ExpectedConfig Config
77+
Comment string
78+
}{
79+
{
80+
Ini: `
81+
[job-exec "foo"]
82+
schedule = @every 10s
83+
command = echo \"foo\"
84+
`,
85+
ExpectedConfig: Config{
86+
ExecJobs: map[string]*ExecJobConfig{
87+
"foo": {ExecJob: core.ExecJob{BareJob: core.BareJob{
88+
Schedule: "@every 10s",
89+
Command: `echo "foo"`,
90+
}}},
91+
},
92+
},
93+
Comment: "Test job-exec",
94+
},
95+
{
96+
Ini: `
97+
[job-run "foo"]
98+
schedule = @every 10s
99+
environment = "KEY1=value1"
100+
Environment = "KEY2=value2"
101+
`,
102+
ExpectedConfig: Config{
103+
RunJobs: map[string]*RunJobConfig{
104+
"foo": {RunJob: core.RunJob{BareJob: core.BareJob{
105+
Schedule: "@every 10s",
106+
},
107+
Environment: []string{"KEY1=value1", "KEY2=value2"},
108+
}},
109+
},
110+
},
111+
Comment: "Test job-run with Env Variables",
112+
},
113+
{
114+
Ini: `
115+
[job-run "foo"]
116+
schedule = @every 10s
117+
volumes-from = "volume1"
118+
volumes-from = "volume2"
119+
`,
120+
ExpectedConfig: Config{
121+
RunJobs: map[string]*RunJobConfig{
122+
"foo": {RunJob: core.RunJob{BareJob: core.BareJob{
123+
Schedule: "@every 10s",
124+
},
125+
VolumesFrom: []string{"volume1", "volume2"},
126+
}},
127+
},
128+
},
129+
Comment: "Test job-run with Env Variables",
130+
},
131+
}
132+
133+
for _, t := range testcases {
134+
conf := Config{}
135+
err := gcfg.ReadStringInto(&conf, t.Ini)
136+
c.Assert(err, IsNil)
137+
if !c.Check(conf, DeepEquals, t.ExpectedConfig) {
138+
c.Errorf("Test %q\nExpected %s, but got %s", t.Comment, toJSON(t.ExpectedConfig), toJSON(conf))
139+
}
140+
}
141+
}
142+
71143
func (s *SuiteConfig) TestLabelsConfig(c *C) {
72144
testcases := []struct {
73145
Labels map[string]map[string]string
@@ -285,12 +357,56 @@ func (s *SuiteConfig) TestLabelsConfig(c *C) {
285357
},
286358
Comment: "Test run job with environment variables",
287359
},
360+
{
361+
Labels: map[string]map[string]string{
362+
"some": {
363+
requiredLabel: "true",
364+
serviceLabel: "true",
365+
labelPrefix + "." + jobRun + ".job1.schedule": "schedule1",
366+
labelPrefix + "." + jobRun + ".job1.command": "command1",
367+
labelPrefix + "." + jobRun + ".job1.volumes-from": "test123",
368+
labelPrefix + "." + jobRun + ".job2.schedule": "schedule2",
369+
labelPrefix + "." + jobRun + ".job2.command": "command2",
370+
labelPrefix + "." + jobRun + ".job2.volumes-from": `["test321", "test456"]`,
371+
},
372+
},
373+
ExpectedConfig: Config{
374+
RunJobs: map[string]*RunJobConfig{
375+
"job1": {
376+
RunJob: core.RunJob{
377+
BareJob: core.BareJob{
378+
Schedule: "schedule1",
379+
Command: "command1",
380+
},
381+
VolumesFrom: []string{"test123"},
382+
},
383+
},
384+
"job2": {
385+
RunJob: core.RunJob{
386+
BareJob: core.BareJob{
387+
Schedule: "schedule2",
388+
Command: "command2",
389+
},
390+
VolumesFrom: []string{"test321", "test456"},
391+
},
392+
},
393+
},
394+
},
395+
Comment: "Test run job with volumes-from",
396+
},
288397
}
289398

290399
for _, t := range testcases {
291400
var conf = Config{}
292401
err := conf.buildFromDockerLabels(t.Labels)
293402
c.Assert(err, IsNil)
294-
c.Assert(conf, DeepEquals, t.ExpectedConfig)
403+
if !c.Check(conf, DeepEquals, t.ExpectedConfig) {
404+
c.Errorf("Test %q\nExpected %s, but got %s", t.Comment, toJSON(t.ExpectedConfig), toJSON(conf))
405+
}
295406
}
296407
}
408+
409+
func toJSON(any interface{}) string {
410+
b, _ := json.MarshalIndent(any, "", " ")
411+
return string(b)
412+
}

cli/docker-labels.go

+2-8
Original file line numberDiff line numberDiff line change
@@ -174,19 +174,13 @@ func (c *Config) buildFromDockerLabels(labels map[string]map[string]string) erro
174174
}
175175

176176
func setJobParam(params map[string]interface{}, paramName, paramVal string) {
177-
switch paramName {
178-
case "volume":
177+
switch strings.ToLower(paramName) {
178+
case "volume", "environment", "volumes-from":
179179
arr := []string{} // allow providing JSON arr of volume mounts
180180
if err := json.Unmarshal([]byte(paramVal), &arr); err == nil {
181181
params[paramName] = arr
182182
return
183183
}
184-
case "environment":
185-
arr := []string{} // allow providing JSON arr of env keyvalues
186-
if err := json.Unmarshal([]byte(paramVal), &arr); err == nil {
187-
params[paramName] = arr
188-
return
189-
}
190184
}
191185

192186
params[paramName] = paramVal

core/runjob.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ type RunJob struct {
3434
Hostname string
3535
Container string
3636
Volume []string
37+
VolumesFrom []string `gcfg:"volumes-from" mapstructure:"volumes-from,"`
3738
Environment []string
3839

3940
containerID string
@@ -176,7 +177,8 @@ func (j *RunJob) buildContainer() (*docker.Container, error) {
176177
},
177178
NetworkingConfig: &docker.NetworkingConfig{},
178179
HostConfig: &docker.HostConfig{
179-
Binds: j.Volume,
180+
Binds: j.Volume,
181+
VolumesFrom: j.VolumesFrom,
180182
},
181183
})
182184

docs/jobs.md

+11-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
- [job-local](#job-local)
66
- [job-service-run](#job-service-run)
77

8+
9+
>[!IMPORTANT]
10+
>Configuration keys are not case sensitive
11+
812
## Job-exec
913

1014
This job is executed inside a running container. Similar to `docker exec`
@@ -114,10 +118,16 @@ This job can be used in 2 situations:
114118
- **INI config**: `Volume` setting can be provided multiple times for multiple mounts.
115119
- **Labels config**: multiple mounts has to be provided as JSON array: `["/test/tmp:/test/tmp:ro", "/test/tmp:/test/tmp:rw"]`
116120
- *default*: Optional field, no default.
121+
- **Volumes-From**
122+
- *description*: Use the volumes from another container.
123+
- *value*: The name of the container, from which the volumes will be used.
124+
- **INI config**: setting can be provided multiple times for multiple mounts.
125+
- **Labels config**: multiple mounts has to be provided as JSON array: `["container-foo", "bar-container"]`
126+
- *default*: Optional field, no default.
117127
- **Environment**
118128
- *description*: Environment variables you want to set in the running container.
119129
- *value*: Same format as used with `-e` flag within `docker run`. For example: `FOO=bar`
120-
- **INI config**: `Environment` setting can be provided multiple times for multiple environment variables.
130+
- **INI config**: setting can be provided multiple times for multiple environment variables.
121131
- **Labels config**: multiple environment variables has to be provided as JSON array: `["FOO=bar", "BAZ=qux"]`
122132
- *default*: Optional field, no default.
123133

0 commit comments

Comments
 (0)