From 9a3fc6a2b4eb1e4428bd93ae5f101f5b44a7d47b Mon Sep 17 00:00:00 2001 From: Patrice Ferlet Date: Wed, 3 Apr 2024 22:22:48 +0200 Subject: [PATCH] Fix "depends_on" check If "depends_on" is set, we need to ensure that the target service has got declared ports. It's necessary, at this time, to ensure the target is started (with an initContainer) As soon as Kubernetes proposes a better check, we will be able to fix this requirement. --- generator/deployment.go | 6 +++++- generator/generator.go | 2 +- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/generator/deployment.go b/generator/deployment.go index 394ed46..eff2b84 100644 --- a/generator/deployment.go +++ b/generator/deployment.go @@ -91,11 +91,15 @@ func NewDeployment(service types.ServiceConfig, chart *HelmChart) *Deployment { } // DependsOn adds a initContainer to the deployment that will wait for the service to be up. -func (d *Deployment) DependsOn(to *Deployment) error { +func (d *Deployment) DependsOn(to *Deployment, servicename string) error { // Add a initContainer with busybox:latest using netcat to check if the service is up // it will wait until the service responds to all ports for _, container := range to.Spec.Template.Spec.Containers { commands := []string{} + if len(container.Ports) == 0 { + utils.Warn("No ports found for service ", servicename, ". You should declare a port in the service or use "+LABEL_PORTS+" label.") + os.Exit(1) + } for _, port := range container.Ports { command := fmt.Sprintf("until nc -z %s %d; do\n sleep 1;\ndone", to.Name, port.ContainerPort) commands = append(commands, command) diff --git a/generator/generator.go b/generator/generator.go index 869f9a8..c646d31 100644 --- a/generator/generator.go +++ b/generator/generator.go @@ -148,7 +148,7 @@ func Generate(project *types.Project) (*HelmChart, error) { for _, s := range project.Services { for _, d := range s.GetDependencies() { if dep, ok := deployments[d]; ok { - deployments[s.Name].DependsOn(dep) + deployments[s.Name].DependsOn(dep, d) } else { log.Printf("service %[1]s depends on %[2]s, but %[2]s is not defined", s.Name, d) }