depends_on with ports, cross-namespace DNS

This commit is contained in:
stackops
2026-04-09 17:00:02 +03:00
parent 03dd339d6f
commit 9314e1f7ba
2 changed files with 19 additions and 3 deletions

View File

@@ -306,11 +306,20 @@ func buildEnvVars(sf *schema.Stackfile) []envVar {
} }
// depends_on — inject service URLs via k8s DNS. // depends_on — inject service URLs via k8s DNS.
for _, svc := range sf.DependsOn.Services { // Sorted for stable output.
svcNames := make([]string, 0, len(sf.DependsOn.Services))
for name := range sf.DependsOn.Services {
svcNames = append(svcNames, name)
}
sort.Strings(svcNames)
for _, svc := range svcNames {
port := sf.DependsOn.Services[svc]
envKey := fmt.Sprintf("%s_URL", strings.ToUpper(strings.ReplaceAll(svc, "-", "_"))) envKey := fmt.Sprintf("%s_URL", strings.ToUpper(strings.ReplaceAll(svc, "-", "_")))
// Use <service>.<namespace>.svc for cross-namespace discovery.
// Convention: namespace = service name (StackOps creates namespace per project).
envs = append(envs, envVar{ envs = append(envs, envVar{
Name: envKey, Name: envKey,
Value: fmt.Sprintf("http://%s", svc), Value: fmt.Sprintf("http://%s.%s.svc:%d", svc, svc, port),
}) })
} }

View File

@@ -67,8 +67,15 @@ type SecretRef struct {
Key string `toml:"key"` Key string `toml:"key"`
} }
// DependsOnConfig maps service names to their ports.
// Example: [depends_on.services]
//
// auth-api = 8080
// notification-api = 3000
//
// Injects: AUTH_API_URL=http://auth-api:8080
type DependsOnConfig struct { type DependsOnConfig struct {
Services []string `toml:"services"` Services map[string]int `toml:"services"`
} }
// EnvOverride holds fields that override the base config for a named environment. // EnvOverride holds fields that override the base config for a named environment.