Skip to content

Commit

Permalink
Fix non named error (#1536)
Browse files Browse the repository at this point in the history
* Creates services before deployments/stfs

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

* Fix tests

Signed-off-by: Javier López Barba <javier@okteto.com>
  • Loading branch information
jLopezbarb committed May 17, 2021
1 parent 14edbee commit 6cc0e9b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
17 changes: 16 additions & 1 deletion pkg/model/stack_serializer.go
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ func (serviceRaw *ServiceRaw) ToService(svcName string, stack *Stack) (*Service,

svc.Volumes, svc.VolumeMounts = splitVolumesByType(serviceRaw.Volumes, stack)
for _, volume := range svc.VolumeMounts {
if volume.LocalPath != "" && !(!FileExists(volume.LocalPath) || !strings.HasPrefix(volume.LocalPath, "/")) {
if !isNamedVolumeDeclared(volume) {
return nil, fmt.Errorf("Named volume '%s' is used in service '%s' but no declaration was found in the volumes section.", volume.ToString(), svcName)
}
}
Expand All @@ -402,6 +402,21 @@ func (serviceRaw *ServiceRaw) ToService(svcName string, stack *Stack) (*Service,
return svc, nil
}

func isNamedVolumeDeclared(volume StackVolume) bool {
if volume.LocalPath != "" {
if strings.HasPrefix(volume.LocalPath, "/") {
return true
}
if strings.HasPrefix(volume.LocalPath, "./") {
return true
}
if FileExists(volume.LocalPath) {
return true
}
}
return false
}

func splitVolumesByType(volumes []StackVolume, s *Stack) ([]StackVolume, []StackVolume) {
topLevelVolumes := make([]StackVolume, 0)
mountedVolumes := make([]StackVolume, 0)
Expand Down
8 changes: 7 additions & 1 deletion pkg/model/stack_serializer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,12 @@ func Test_validateVolumesUnmarshalling(t *testing.T) {
create: false,
expectedError: false,
},
{
name: "absolute path",
manifest: []byte("services:\n app:\n image: okteto/vote:1\n volumes:\n - /var/run/docker.sock:/var/run/docker.sock"),
create: false,
expectedError: false,
},
{
name: "volume-relative-path-found",
manifest: []byte("services:\n app:\n volumes: \n - test-volume-relative-path-found:/var/lib/redpanda/data\n image: okteto/vote:1\n"),
Expand All @@ -549,7 +555,7 @@ func Test_validateVolumesUnmarshalling(t *testing.T) {
name: "volume-relative-path-not-found",
manifest: []byte("services:\n app:\n volumes: \n - test:/var/lib/redpanda/data\n image: okteto/vote:1\n"),
create: false,
expectedError: false,
expectedError: true,
},
{
name: "pv",
Expand Down

0 comments on commit 6cc0e9b

Please sign in to comment.