Skip to content

Commit

Permalink
fix(tilt-prepare): adding container metrics port only if missing
Browse files Browse the repository at this point in the history
  • Loading branch information
prometherion committed Aug 25, 2023
1 parent 3cbf341 commit 58ea650
Showing 1 changed file with 19 additions and 6 deletions.
25 changes: 19 additions & 6 deletions hack/tools/internal/tilt-prepare/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -834,12 +834,15 @@ func prepareWorkload(name, prefix, binaryName, containerName string, objs []unst
}
finalArgs = append(finalArgs, a)
}

container.Ports = append(container.Ports, corev1.ContainerPort{
Name: "metrics",
ContainerPort: 8080,
Protocol: "TCP",
})
// tilt-prepare should add the metrics port if and only if it's missing:
// as best practices with controller-runtime, it's named metrics and listening on port 8080.
if !containerHasMetricsPort(container.Ports) {
container.Ports = append(container.Ports, corev1.ContainerPort{
Name: "metrics",
ContainerPort: 8080,
Protocol: corev1.ProtocolTCP,
})
}

container.Command = cmd
container.Args = finalArgs
Expand All @@ -848,6 +851,16 @@ func prepareWorkload(name, prefix, binaryName, containerName string, objs []unst
})
}

func containerHasMetricsPort(ports []corev1.ContainerPort) bool {
for _, port := range ports {
if port.ContainerPort == 8080 && port.Name == "metrics" && port.Protocol == corev1.ProtocolTCP {
return true
}
}

return false
}

type updateDeploymentFunction func(deployment *appsv1.Deployment)

// updateDeployment passes all (typed) deployments to the updateDeploymentFunction
Expand Down

0 comments on commit 58ea650

Please sign in to comment.