Skip to content

dns fixup in build args #776

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

Merged
merged 10 commits into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 38 additions & 14 deletions src/pkg/cli/compose/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,33 @@ import (
compose "github.com/compose-spec/compose-go/v2/types"
)

type serviceNameReplacer struct {
client client.Client
nonReplaceServiceNameRegex *regexp.Regexp
serviceName string
serviceNameRegex *regexp.Regexp
}

func (s *serviceNameReplacer) replaceServiceNameWithDNS(key, value string, showWarnings bool) string {
val := value
if s.serviceNameRegex != nil {
// Replace service names with their actual DNS names; TODO: support public names too
val = s.serviceNameRegex.ReplaceAllStringFunc(value, func(serviceName string) string {
return s.client.ServiceDNS(NormalizeServiceName(serviceName))
})

if showWarnings {
if val != value {
term.Warnf("service %q: service name was fixed up: environment variable %q assigned value %q", s.serviceName, key, val)
} else if s.nonReplaceServiceNameRegex != nil && s.nonReplaceServiceNameRegex.MatchString(value) {
term.Warnf("service %q: service name(s) in the environment variable %q were not updated, only references to other services with port mode set to 'host' will be fixed-up", s.serviceName, key)
}
}
}

return val
}

func ConvertServices(ctx context.Context, c client.Client, serviceConfigs compose.Services, upload UploadMode) ([]*defangv1.Service, error) {
// Create a regexp to detect private service names in environment variable values
var serviceNames []string
Expand Down Expand Up @@ -48,6 +75,14 @@ func ConvertServices(ctx context.Context, c client.Client, serviceConfigs compos
//
var services []*defangv1.Service
for _, svccfg := range serviceConfigs {

svcNameReplacer := &serviceNameReplacer{
client: c,
nonReplaceServiceNameRegex: nonReplaceServiceNameRegex,
serviceName: svccfg.Name,
serviceNameRegex: serviceNameRegex,
}

var healthcheck *defangv1.HealthCheck
if svccfg.HealthCheck != nil && len(svccfg.HealthCheck.Test) > 0 && !svccfg.HealthCheck.Disable {
healthcheck = &defangv1.HealthCheck{
Expand Down Expand Up @@ -115,7 +150,8 @@ func ConvertServices(ctx context.Context, c client.Client, serviceConfigs compos
term.Warnf("service %q: skipping unset build argument %q", svccfg.Name, key)
continue
}
build.Args[key] = *value

build.Args[key] = svcNameReplacer.replaceServiceNameWithDNS(key, *value, false)
}
}
}
Expand Down Expand Up @@ -146,19 +182,7 @@ func ConvertServices(ctx context.Context, c client.Client, serviceConfigs compos
continue
}

val := *value
if serviceNameRegex != nil {
// Replace service names with their actual DNS names; TODO: support public names too
val = serviceNameRegex.ReplaceAllStringFunc(*value, func(serviceName string) string {
return c.ServiceDNS(NormalizeServiceName(serviceName))
})
if val != *value {
term.Warnf("service %q: service name was fixed up: environment variable %q assigned value %q", svccfg.Name, key, val)
} else if nonReplaceServiceNameRegex != nil && nonReplaceServiceNameRegex.MatchString(*value) {
term.Warnf("service %q: service name(s) in the environment variable %q were not fixed up, only services with port mode set to 'host' will be fixed up", svccfg.Name, key)
}
}
envs[key] = val
envs[key] = svcNameReplacer.replaceServiceNameWithDNS(key, *value, true)
}

// Add unset environment variables as "secrets"
Expand Down
2 changes: 1 addition & 1 deletion src/tests/fixupenv/compose.yaml.warnings
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
! service "ui": missing memory reservation; using provider-specific defaults. Specify deploy.resources.reservations.memory to avoid out-of-memory errors
! service "ui": service name was fixed up: environment variable "API_URL" assigned value "http://mock-mistral:8000"
! service "use-bad-service": missing memory reservation; using provider-specific defaults. Specify deploy.resources.reservations.memory to avoid out-of-memory errors
! service "use-bad-service": service name(s) in the environment variable "DB_URL" were not fixed up, only services with port mode set to 'host' will be fixed up
! service "use-bad-service": service name(s) in the environment variable "DB_URL" were not updated, only references to other services with port mode set to 'host' will be fixed-up
! service name "Mistral" was normalized to "mistral"
Loading