Skip to content

Commit be68373

Browse files
committed
fix(tilt-prepare): adding container metrics port only if missing
1 parent 3cbf341 commit be68373

File tree

1 file changed

+19
-6
lines changed
  • hack/tools/internal/tilt-prepare

1 file changed

+19
-6
lines changed

hack/tools/internal/tilt-prepare/main.go

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -834,12 +834,15 @@ func prepareWorkload(name, prefix, binaryName, containerName string, objs []unst
834834
}
835835
finalArgs = append(finalArgs, a)
836836
}
837-
838-
container.Ports = append(container.Ports, corev1.ContainerPort{
839-
Name: "metrics",
840-
ContainerPort: 8080,
841-
Protocol: "TCP",
842-
})
837+
// tilt-prepare should add the metrics port if and only if it's missing:
838+
// as best practices with controller-runtime, it's named metrics and listening on port 8080.
839+
if !containerHasMetricsPort(container.Ports) {
840+
container.Ports = append(container.Ports, corev1.ContainerPort{
841+
Name: "metrics",
842+
ContainerPort: 8080,
843+
Protocol: corev1.ProtocolTCP,
844+
})
845+
}
843846

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

854+
func containerHasMetricsPort(ports []corev1.ContainerPort) bool {
855+
for _, port := range ports {
856+
if port.ContainerPort == 8080 {
857+
return true
858+
}
859+
}
860+
861+
return false
862+
}
863+
851864
type updateDeploymentFunction func(deployment *appsv1.Deployment)
852865

853866
// updateDeployment passes all (typed) deployments to the updateDeploymentFunction

0 commit comments

Comments
 (0)