Skip to content

Commit

Permalink
Strip empty DNS entries to avoid parse errors when creating sandbox D…
Browse files Browse the repository at this point in the history
…NS in Moby
  • Loading branch information
IvanRibakov committed Apr 5, 2024
1 parent 2658c37 commit dc1eef6
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
13 changes: 13 additions & 0 deletions pkg/compose/create.go
Expand Up @@ -94,6 +94,8 @@ func (s *composeService) create(ctx context.Context, project *types.Project, opt
return err
}

prepareServiceDNS(project)

if err := s.ensureProjectVolumes(ctx, project); err != nil {
return err
}
Expand Down Expand Up @@ -140,6 +142,17 @@ func (s *composeService) ensureNetworks(ctx context.Context, networks types.Netw
return nil
}

func prepareServiceDNS(project *types.Project) {
for k, service := range project.Services {
trimmedDNS := utils.Remove(service.DNS, "")
if len(trimmedDNS) != len(service.DNS) {
logrus.Warnf("The %q service has one or more empty DNS entries. Skipping them.", k)
}
service.DNS = trimmedDNS
project.Services[k] = service
}
}

func (s *composeService) ensureProjectVolumes(ctx context.Context, project *types.Project) error {
for k, volume := range project.Volumes {
volume.Labels = volume.Labels.Add(api.VolumeLabel, k)
Expand Down
28 changes: 28 additions & 0 deletions pkg/compose/create_test.go
Expand Up @@ -275,3 +275,31 @@ func TestDefaultNetworkSettings(t *testing.T) {
assert.Check(t, cmp.Nil(networkConfig))
})
}

func TestPrepareServiceDNS(t *testing.T) {
t.Run("returns DNS list without empty strings", func(t *testing.T) {
const (
serviceName = "myService"
dns1 = "dns-1"
dns2 = "dns-2"
)
project := composetypes.Project{
Services: composetypes.Services{
serviceName: composetypes.ServiceConfig{
DNS: []string{
dns1,
"",
dns2,
"",
},
},
},
}

prepareServiceDNS(&project)
dnsConfig := project.Services[serviceName].DNS
assert.Equal(t, len(dnsConfig), 2)
assert.Equal(t, dnsConfig[0], dns1)
assert.Equal(t, dnsConfig[1], dns2)
})
}

0 comments on commit dc1eef6

Please sign in to comment.