Skip to content

Commit

Permalink
add missing probes
Browse files Browse the repository at this point in the history
  • Loading branch information
skonto committed Dec 19, 2022
1 parent 35abde0 commit c8a61fd
Show file tree
Hide file tree
Showing 8 changed files with 154 additions and 12 deletions.
7 changes: 6 additions & 1 deletion cmd/autoscaler-hpa/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ limitations under the License.
package main

import (
"knative.dev/pkg/signals"
"knative.dev/serving/pkg/networking"

// The set of controllers this controller process runs.
"knative.dev/serving/pkg/reconciler/autoscaling/hpa"

Expand All @@ -25,5 +28,7 @@ import (
)

func main() {
sharedmain.Main("hpaautoscaler", hpa.NewController)
ctx := signals.NewContext()
networking.StartHealthCheck(ctx)
sharedmain.MainWithContext(ctx, "hpaautoscaler", hpa.NewController)
}
1 change: 1 addition & 0 deletions cmd/controller/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,5 +62,6 @@ func main() {

labelName := networking.ServingCertName + secretLabelNamePostfix
ctx := filteredFactory.WithSelectors(signals.NewContext(), labelName)
networking.StartHealthCheck(ctx)
sharedmain.MainWithContext(ctx, "controller", ctors...)
}
12 changes: 2 additions & 10 deletions cmd/default-domain/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"flag"
"fmt"
"net"
"net/http"
"os"
"strings"
"time"
Expand All @@ -40,10 +39,10 @@ import (
netv1alpha1 "knative.dev/networking/pkg/apis/networking/v1alpha1"
netclient "knative.dev/networking/pkg/client/clientset/versioned"
netcfg "knative.dev/networking/pkg/config"
netprobe "knative.dev/networking/pkg/http/probe"
"knative.dev/pkg/logging"
"knative.dev/pkg/signals"
"knative.dev/pkg/system"
servingnetworking "knative.dev/serving/pkg/networking"
routecfg "knative.dev/serving/pkg/reconciler/route/config"
)

Expand Down Expand Up @@ -188,13 +187,7 @@ func main() {
return
}

// Start an HTTP Server
h := netprobe.NewHandler(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusOK)
}))
//nolinlt:gosec https://github.com/knative/serving/issues/13439
server := http.Server{Addr: ":8080", Handler: h, ReadHeaderTimeout: time.Minute}
go server.ListenAndServe()
servingnetworking.StartHealthCheck(ctx)

// Determine the address of the gateway service.
address, err := findGatewayAddress(ctx, kubeClient, client)
Expand Down Expand Up @@ -226,5 +219,4 @@ func main() {
}

logger.Info("Updated default domain to: ", domain)
server.Shutdown(context.Background())
}
7 changes: 6 additions & 1 deletion cmd/domain-mapping/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ limitations under the License.
package main

import (
"knative.dev/pkg/signals"
"knative.dev/serving/pkg/networking"

// The set of controllers this controller process runs.
"knative.dev/serving/pkg/reconciler/domainmapping"

Expand All @@ -25,5 +28,7 @@ import (
)

func main() {
sharedmain.Main("domainmapping", domainmapping.NewController)
ctx := signals.NewContext()
networking.StartHealthCheck(ctx)
sharedmain.MainWithContext(ctx, "domainmapping", domainmapping.NewController)
}
17 changes: 17 additions & 0 deletions config/core/deployments/controller.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,28 @@ spec:
seccompProfile:
type: RuntimeDefault

livenessProbe:
httpGet:
path: /health
port: probes
scheme: HTTP
periodSeconds: 5
failureThreshold: 5
readinessProbe:
httpGet:
path: /readiness
port: probes
scheme: HTTP
periodSeconds: 5
failureThreshold: 5

ports:
- name: metrics
containerPort: 9090
- name: profiling
containerPort: 8008
- name: probes
containerPort: 8080

---
apiVersion: v1
Expand Down
17 changes: 17 additions & 0 deletions config/core/deployments/domainmapping-controller.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,25 @@ spec:
seccompProfile:
type: RuntimeDefault

livenessProbe:
httpGet:
path: /health
port: probes
scheme: HTTP
periodSeconds: 5
failureThreshold: 5
readinessProbe:
httpGet:
path: /readiness
port: probes
scheme: HTTP
periodSeconds: 5
failureThreshold: 5

ports:
- name: metrics
containerPort: 9090
- name: profiling
containerPort: 8008
- name: probes
containerPort: 8080
17 changes: 17 additions & 0 deletions config/hpa-autoscaling/controller.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,28 @@ spec:
seccompProfile:
type: RuntimeDefault

livenessProbe:
httpGet:
path: /health
port: probes
scheme: HTTP
periodSeconds: 5
failureThreshold: 5
readinessProbe:
httpGet:
path: /readiness
port: probes
scheme: HTTP
periodSeconds: 5
failureThreshold: 5

ports:
- name: metrics
containerPort: 9090
- name: profiling
containerPort: 8008
- name: probes
containerPort: 8080

---
apiVersion: v1
Expand Down
88 changes: 88 additions & 0 deletions pkg/networking/health_check.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
Copyright 2022 The Knative Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package networking

import (
"context"
"errors"
"log"
"net/http"
"os"
"sync"
"time"

netheader "knative.dev/networking/pkg/http/header"
)

func StartHealthCheck(ctx context.Context) {
port := os.Getenv("PROBES_PORT")
if port == "" {
port = "8080"
}
handler := healthHandler{HealthCheck: newHealthCheck(ctx)}

// sets up liveness and readiness probes.
server := http.Server{ReadHeaderTimeout: time.Minute, Handler: &handler, Addr: ":" + port}

go func() {
go func() {
<-ctx.Done()
_ = server.Shutdown(ctx)
}()

// start the web server on port and accept requests
log.Printf("Readiness and health check server listening on port %s", port)

if err := server.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
log.Fatal(err)
}
}()
}

func newHealthCheck(sigCtx context.Context) func() error {
once := sync.Once{}
return func() error {
select {
// When we get SIGTERM (sigCtx done), let readiness probes start failing.
case <-sigCtx.Done():
once.Do(func() {
log.Println("Signal context canceled")
})
return errors.New("received SIGTERM from kubelet")
default:
return nil
}
}
}

// healthHandler handles responding to kubelet probes with a provided health check.
type healthHandler struct {
HealthCheck func() error
}

func (h *healthHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if netheader.IsKubeletProbe(r) {
if err := h.HealthCheck(); err != nil {
log.Println("Healthcheck failed: ", err.Error())
http.Error(w, err.Error(), http.StatusInternalServerError)
} else {
w.WriteHeader(http.StatusOK)
}
return
}
http.Error(w, "Unexpected request", http.StatusBadRequest)
}

0 comments on commit c8a61fd

Please sign in to comment.