Skip to content

Commit

Permalink
Use PORT instead of HEALTHCHECK_PORT for serving container
Browse files Browse the repository at this point in the history
  • Loading branch information
mgencur committed May 2, 2024
1 parent 94da517 commit 4c56f35
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ func TestMultiContainerReadiness(t *testing.T) {
ContainerPort: 8881,
}},
Env: []corev1.EnvVar{
{Name: "HEALTHCHECK_PORT", Value: "8881"},
// A port in the next container to forward requests to.
{Name: "FORWARD_PORT", Value: "8882"},
},
Expand All @@ -75,7 +74,7 @@ func TestMultiContainerReadiness(t *testing.T) {
}, { // Sidecar with readiness probe.
Image: pkgTest.ImagePath(names.Sidecars[0]),
Env: []corev1.EnvVar{
{Name: "HEALTHCHECK_PORT", Value: "8882"},
{Name: "PORT", Value: "8882"},
{Name: "FORWARD_PORT", Value: "8883"},
},
ReadinessProbe: &corev1.Probe{
Expand All @@ -88,7 +87,7 @@ func TestMultiContainerReadiness(t *testing.T) {
}, { // Sidecar with liveness probe.
Image: pkgTest.ImagePath(names.Sidecars[1]),
Env: []corev1.EnvVar{
{Name: "HEALTHCHECK_PORT", Value: "8883"},
{Name: "PORT", Value: "8883"},
{Name: "FORWARD_PORT", Value: "8884"},
},
LivenessProbe: &corev1.Probe{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,14 @@ import (
"log"
"net/http"
"os"
"strconv"

"knative.dev/serving/test"
)

const (
defaultPort = "8080"
)

func handler(w http.ResponseWriter, r *http.Request) {
log.Println("serving container received a request.")
res, err := http.Get(os.ExpandEnv("http://localhost:$FORWARD_PORT"))
Expand All @@ -43,10 +46,13 @@ func handler(w http.ResponseWriter, r *http.Request) {

func main() {
flag.Parse()
var port int
if env := os.Getenv("HEALTHCHECK_PORT"); env != "" {
port, _ = strconv.Atoi(env)
log.Printf("serving container started on port %d", getPort())
test.ListenAndServeGracefully(":"+getPort(), handler)
}

func getPort() string {
if port := os.Getenv("PORT"); port != "" {
return port
}
log.Printf("serving container started on port %d", port)
test.ListenAndServeGracefully(":"+strconv.Itoa(port), handler)
return defaultPort
}

0 comments on commit 4c56f35

Please sign in to comment.