Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Strip empty DNS entries to avoid Moby parse errors when creating sandbox DNS #11691

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 13 additions & 0 deletions pkg/compose/create.go
Expand Up @@ -99,6 +99,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 @@ -141,6 +143,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 @@ -327,3 +327,31 @@ func TestCreateEndpointSettings(t *testing.T) {
IPv6Gateway: "fdb4:7a7f:373a:3f0c::42",
}))
}

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)
})
}