This repository has been archived by the owner on Dec 1, 2022. It is now read-only.
forked from knative/serving
-
Notifications
You must be signed in to change notification settings - Fork 16
/
probe.go
135 lines (113 loc) · 3.68 KB
/
probe.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*
Copyright 2019 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 readiness
import (
"fmt"
"os"
"time"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/util/wait"
"knative.dev/serving/pkg/queue/health"
)
const (
aggressiveProbeTimeout = 100 * time.Millisecond
// PollTimeout is set equal to the queue-proxy's ExecProbe timeout to take
// advantage of the full window
PollTimeout = 10 * time.Second
retryInterval = 50 * time.Millisecond
)
// Probe wraps a corev1.Probe along with a count of consecutive, successful probes
type Probe struct {
*corev1.Probe
count int32
}
// NewProbe returns a pointer a new Probe
func NewProbe(v1p *corev1.Probe) *Probe {
return &Probe{
Probe: v1p,
}
}
// IsAggressive indicates whether the Knative probe with aggressive retries should be used.
func (p *Probe) IsAggressive() bool {
return p.PeriodSeconds == 0
}
// ProbeContainer executes the defined Probe against the user-container
func (p *Probe) ProbeContainer() bool {
var err error
switch {
case p.HTTPGet != nil:
err = p.httpProbe()
case p.TCPSocket != nil:
err = p.tcpProbe()
case p.Exec != nil:
// Should never be reachable. Exec probes to be translated to
// TCP probes when container is built.
// Using Fprintf for a concise error message in the event log.
fmt.Fprintln(os.Stderr, "exec probe not supported")
return false
default:
// Using Fprintf for a concise error message in the event log.
fmt.Fprintln(os.Stderr, "no probe found")
return false
}
if err != nil {
// Using Fprintf for a concise error message in the event log.
fmt.Fprint(os.Stderr, err.Error())
return false
}
return true
}
func (p *Probe) doProbe(probe func(time.Duration) error) error {
if p.IsAggressive() {
return wait.PollImmediate(retryInterval, PollTimeout, func() (bool, error) {
if tcpErr := probe(aggressiveProbeTimeout); tcpErr != nil {
// reset count of consecutive successes to zero
p.count = 0
return false, nil
}
p.count++
// return success if count of consecutive successes is equal to or greater
// than the probe's SuccessThreshold.
return p.Count() >= p.SuccessThreshold, nil
})
}
return probe(time.Duration(p.TimeoutSeconds) * time.Second)
}
// tcpProbe function executes TCP probe once if its standard probe
// otherwise TCP probe polls condition function which returns true
// if the probe count is greater than success threshold and false if TCP probe fails
func (p *Probe) tcpProbe() error {
config := health.TCPProbeConfigOptions{
Address: p.TCPSocket.Host + ":" + p.TCPSocket.Port.String(),
}
return p.doProbe(func(to time.Duration) error {
config.SocketTimeout = to
return health.TCPProbe(config)
})
}
// httpProbe function executes HTTP probe once if its standard probe
// otherwise HTTP probe polls condition function which returns true
// if the probe count is greater than success threshold and false if HTTP probe fails
func (p *Probe) httpProbe() error {
config := health.HTTPProbeConfigOptions{
HTTPGetAction: p.HTTPGet,
}
return p.doProbe(func(to time.Duration) error {
config.Timeout = to
return health.HTTPProbe(config)
})
}
// Count function fetches current probe count
func (p *Probe) Count() int32 {
return p.count
}