diff --git a/playground/local_runner.go b/playground/local_runner.go index de3bc72..5cc4527 100644 --- a/playground/local_runner.go +++ b/playground/local_runner.go @@ -632,6 +632,11 @@ func (d *LocalRunner) toDockerComposeService(s *Service) (map[string]interface{} // create the bind volumes var createdVolumes []string for localPath, volume := range s.VolumesMapped { + if volume.HostPath != "" { + volumes[volume.HostPath] = localPath + continue + } + dockerVolumeName := d.createVolumeName(s.Name, volume.Name) if volume.IsLocal { diff --git a/playground/manifest.go b/playground/manifest.go index eaa4129..109acea 100644 --- a/playground/manifest.go +++ b/playground/manifest.go @@ -453,8 +453,9 @@ type Service struct { } type VolumeMapped struct { - Name string - IsLocal bool + Name string + IsLocal bool + HostPath string } type DependsOnCondition string @@ -607,6 +608,16 @@ func (s *Service) WithVolume(name, localPath string, isLocalTri ...bool) *Servic return s } +func (s *Service) WithHostVolume(hostPath, containerPath string) *Service { + if s.VolumesMapped == nil { + s.VolumesMapped = make(map[string]*VolumeMapped) + } + s.VolumesMapped[containerPath] = &VolumeMapped{ + HostPath: hostPath, + } + return s +} + func (s *Service) WithArtifact(localPath, artifactName string) *Service { if s.FilesMapped == nil { s.FilesMapped = make(map[string]string) diff --git a/playground/recipe_yaml.go b/playground/recipe_yaml.go index e6379a6..c99273f 100644 --- a/playground/recipe_yaml.go +++ b/playground/recipe_yaml.go @@ -116,8 +116,9 @@ type YAMLServiceConfig struct { } type YAMLVolumeMappedConfig struct { - Name string `yaml:"name"` - IsLocal bool `yaml:"is_local"` + Name string `yaml:"name"` + IsLocal bool `yaml:"is_local"` + HostPath string `yaml:"host_path"` } // YAMLReleaseConfig specifies a GitHub release to download @@ -481,9 +482,7 @@ func applyServiceOverrides(svc *Service, config *YAMLServiceConfig, root *Compon applyFilesToService(svc, config.Files) } if config.Volumes != nil { - for containerPath, volumeMapping := range config.Volumes { - svc.WithVolume(volumeMapping.Name, containerPath, volumeMapping.IsLocal) - } + applyVolumesToService(svc, config.Volumes) } if config.DependsOn != nil { applyDependsOn(svc, config.DependsOn, root) @@ -573,7 +572,17 @@ func yamlReleaseToRelease(cfg *YAMLReleaseConfig) *release { } } -// applyFilesToService maps files to a service +// applyVolumesToService maps volumes to a service +func applyVolumesToService(svc *Service, volumes map[string]*YAMLVolumeMappedConfig) { + for containerPath, volumeMapping := range volumes { + if volumeMapping.HostPath != "" { + svc.WithHostVolume(volumeMapping.HostPath, containerPath) + } else { + svc.WithVolume(volumeMapping.Name, containerPath, volumeMapping.IsLocal) + } + } +} + func applyFilesToService(svc *Service, files map[string]string) { for containerPath, fileSource := range files { var artifactName string @@ -662,9 +671,7 @@ func createServiceFromConfig(name string, config *YAMLServiceConfig, root *Compo applyFilesToService(svc, config.Files) } if config.Volumes != nil { - for containerPath, volumeMapping := range config.Volumes { - svc.WithVolume(volumeMapping.Name, containerPath, volumeMapping.IsLocal) - } + applyVolumesToService(svc, config.Volumes) } if config.DependsOn != nil { applyDependsOn(svc, config.DependsOn, root) diff --git a/playground/recipe_yaml_test.go b/playground/recipe_yaml_test.go index b5ee8f0..ff08666 100644 --- a/playground/recipe_yaml_test.go +++ b/playground/recipe_yaml_test.go @@ -417,6 +417,22 @@ func TestCreateServiceFromConfig_WithVolumes(t *testing.T) { require.Equal(t, "myvolume", svc.VolumesMapped["/data"].Name) } +func TestCreateServiceFromConfig_WithHostPathVolume(t *testing.T) { + config := &YAMLServiceConfig{ + Image: "test-image", + Volumes: map[string]*YAMLVolumeMappedConfig{"/var/run/docker.sock": { + HostPath: "/var/run/docker.sock", + }}, + } + + svc := createServiceFromConfig("my-service", config, nil, "") + + require.NotNil(t, svc.VolumesMapped) + require.Equal(t, "/var/run/docker.sock", svc.VolumesMapped["/var/run/docker.sock"].HostPath) + require.Empty(t, svc.VolumesMapped["/var/run/docker.sock"].Name) + require.False(t, svc.VolumesMapped["/var/run/docker.sock"].IsLocal) +} + func TestParseYAMLRecipe(t *testing.T) { tmpDir, err := os.MkdirTemp("", "recipe-test") require.NoError(t, err)