Skip to content

Commit

Permalink
Merge pull request #40 from meltred/bind
Browse files Browse the repository at this point in the history
Bind
  • Loading branch information
KunalSin9h committed Dec 14, 2023
2 parents 3534028 + f9abb35 commit 4860594
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 8 deletions.
35 changes: 30 additions & 5 deletions spec/dockerSwarm.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package spec

import (
"bufio"
"errors"
"fmt"
"os"
"os/user"
Expand Down Expand Up @@ -85,11 +86,15 @@ func (d *DockerSwarm) GetServiceSpec(appName string) ([]swarm.ServiceSpec, error
}

for _, envFile := range spec.EnvFile {
log.Info("Using environment variable from files", "file", envFile)

envVars, err := getEnvVars(envFile)
if err != nil {
return []swarm.ServiceSpec{}, err
}

log.Info("Found environment from file", "count", len(envVars))

for k, v := range envVars {
targetSpec.TaskTemplate.ContainerSpec.Env = append(targetSpec.TaskTemplate.ContainerSpec.Env, k+"="+v)
}
Expand All @@ -100,17 +105,37 @@ func (d *DockerSwarm) GetServiceSpec(appName string) ([]swarm.ServiceSpec, error
}

for _, m := range spec.Volumes {
tokens := strings.Split(m, ":")
tokens := strings.SplitN(m, ":", 2)
if len(tokens) != 2 {
log.Error("Volumes are not split on : in 2", "tokens", tokens)
os.Exit(1)
return []swarm.ServiceSpec{}, errors.New("invalid volumes")
}

key := tokens[0]
value := tokens[1]

volumeType := mount.TypeVolume // volume mount

// checking for Bind mounts
if strings.HasPrefix(key, ".") ||
strings.HasPrefix(key, "~") ||
strings.HasPrefix(key, "/") {
absPath, err := normalizeFilePath(key)
if err != nil {
return []swarm.ServiceSpec{}, err
}

key = absPath
volumeType = mount.TypeBind
}

targetSpec.TaskTemplate.ContainerSpec.Mounts = append(targetSpec.TaskTemplate.ContainerSpec.Mounts, mount.Mount{
Type: mount.TypeVolume,
Source: tokens[0],
Target: tokens[1],
Type: volumeType,
Source: key,
Target: value,
})

log.Info("Using volume", "key", key, "value", value)
}

if spec.Deploy.Mode == "replicated" {
Expand Down
17 changes: 14 additions & 3 deletions spec/dockerSwarm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ import (
)

func TestNormalizeFilePath(t *testing.T) {
testCase := []string{
testCaseNeg := []string{
".env",
"file.txt",
"~/.service.yaml",
}

for _, file := range testCase {
for _, file := range testCaseNeg {
res, err := normalizeFilePath(file)
if err != nil {
t.Error(err.Error())
Expand All @@ -23,10 +23,21 @@ func TestNormalizeFilePath(t *testing.T) {
//
// so the length should not equal to expanded result
if len(res) == len(file) {
t.Log("file does not example", file)
t.Log("file does not expanded", file)
t.Fail()
}
}

homePath := "/home/user/secret/something"
res, err := normalizeFilePath(homePath)
if err != nil {
t.Error(err.Error())
}

if res != homePath {
t.Log("file expanded even if the path is home", "path", homePath)
t.Fail()
}
}

func TestGetEnvVars(t *testing.T) {
Expand Down

0 comments on commit 4860594

Please sign in to comment.