Skip to content

Commit 8c7ff3d

Browse files
committed
Update probes for OpenFaaS CE
Signed-off-by: Alex Ellis (OpenFaaS Ltd) <[email protected]>
1 parent 4193656 commit 8c7ff3d

File tree

3 files changed

+70
-114
lines changed

3 files changed

+70
-114
lines changed

pkg/controller/deployment_test.go

Lines changed: 69 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ func Test_GracePeriodFromWriteTimeout(t *testing.T) {
7171
}
7272
}
7373

74-
func Test_newDeployment(t *testing.T) {
74+
func Test_newDeployment_withHTTPProbe(t *testing.T) {
7575
function := &faasv1.Function{
7676
ObjectMeta: metav1.ObjectMeta{
7777
Name: "kubesec",
@@ -80,29 +80,26 @@ func Test_newDeployment(t *testing.T) {
8080
Name: "kubesec",
8181
Image: "docker.io/kubesec/kubesec",
8282
Annotations: &map[string]string{
83-
"com.openfaas.serviceaccount": "kubesec",
84-
"com.openfaas.health.http.initialDelay": "2m",
85-
"com.openfaas.health.http.path": "/healthz",
83+
"com.openfaas.serviceaccount": "kubesec",
8684
},
8785
ReadOnlyRootFilesystem: true,
8886
},
8987
}
90-
91-
factory := NewFunctionFactory(fake.NewSimpleClientset(),
92-
k8s.DeploymentConfig{
93-
HTTPProbe: false,
94-
SetNonRootUser: true,
95-
LivenessProbe: &k8s.ProbeConfig{
96-
PeriodSeconds: 1,
97-
TimeoutSeconds: 3,
98-
InitialDelaySeconds: 0,
99-
},
100-
ReadinessProbe: &k8s.ProbeConfig{
101-
PeriodSeconds: 1,
102-
TimeoutSeconds: 3,
103-
InitialDelaySeconds: 0,
104-
},
105-
})
88+
k8sConfig := k8s.DeploymentConfig{
89+
HTTPProbe: true,
90+
SetNonRootUser: true,
91+
LivenessProbe: &k8s.ProbeConfig{
92+
PeriodSeconds: 1,
93+
TimeoutSeconds: 3,
94+
InitialDelaySeconds: 0,
95+
},
96+
ReadinessProbe: &k8s.ProbeConfig{
97+
PeriodSeconds: 1,
98+
TimeoutSeconds: 3,
99+
InitialDelaySeconds: 0,
100+
},
101+
}
102+
factory := NewFunctionFactory(fake.NewSimpleClientset(), k8sConfig)
106103

107104
secrets := map[string]*corev1.Secret{}
108105

@@ -113,12 +110,16 @@ func Test_newDeployment(t *testing.T) {
113110
t.Fail()
114111
}
115112

116-
if deployment.Spec.Template.Spec.Containers[0].ReadinessProbe.HTTPGet.Path != "/healthz" {
117-
t.Errorf("Readiness probe should have HTTPGet handler set to %s", "/healthz")
113+
if deployment.Spec.Template.Spec.Containers[0].ReadinessProbe.HTTPGet == nil {
114+
t.Fatalf("ReadinessProbe's HTTPGet should not be nil")
115+
}
116+
117+
if deployment.Spec.Template.Spec.Containers[0].ReadinessProbe.HTTPGet.Path != "/_/health" {
118+
t.Errorf("Readiness probe should have HTTPGet handler set to %s", "/_/health")
118119
t.Fail()
119120
}
120121

121-
if deployment.Spec.Template.Spec.Containers[0].LivenessProbe.InitialDelaySeconds != 120 {
122+
if deployment.Spec.Template.Spec.Containers[0].LivenessProbe.InitialDelaySeconds != k8sConfig.ReadinessProbe.InitialDelaySeconds {
122123
t.Errorf("Liveness probe should have initial delay seconds set to %s", "2m")
123124
t.Fail()
124125
}
@@ -134,6 +135,51 @@ func Test_newDeployment(t *testing.T) {
134135
}
135136
}
136137

138+
func Test_newDeployment_withExecProbe(t *testing.T) {
139+
function := &faasv1.Function{
140+
ObjectMeta: metav1.ObjectMeta{
141+
Name: "kubesec",
142+
},
143+
Spec: faasv1.FunctionSpec{
144+
Name: "kubesec",
145+
Image: "docker.io/kubesec/kubesec",
146+
Annotations: &map[string]string{
147+
"com.openfaas.serviceaccount": "kubesec",
148+
},
149+
ReadOnlyRootFilesystem: true,
150+
},
151+
}
152+
k8sConfig := k8s.DeploymentConfig{
153+
HTTPProbe: false,
154+
SetNonRootUser: true,
155+
LivenessProbe: &k8s.ProbeConfig{
156+
PeriodSeconds: 1,
157+
TimeoutSeconds: 3,
158+
InitialDelaySeconds: 0,
159+
},
160+
ReadinessProbe: &k8s.ProbeConfig{
161+
PeriodSeconds: 1,
162+
TimeoutSeconds: 3,
163+
InitialDelaySeconds: 0,
164+
},
165+
}
166+
167+
factory := NewFunctionFactory(fake.NewSimpleClientset(), k8sConfig)
168+
169+
secrets := map[string]*corev1.Secret{}
170+
171+
deployment := newDeployment(function, nil, secrets, factory)
172+
173+
if deployment.Spec.Template.Spec.ServiceAccountName != "kubesec" {
174+
t.Errorf("ServiceAccountName should be %s", "kubesec")
175+
t.Fail()
176+
}
177+
178+
if deployment.Spec.Template.Spec.Containers[0].ReadinessProbe.HTTPGet != nil {
179+
t.Fatalf("ReadinessProbe's HTTPGet should be nil due to exec probe")
180+
}
181+
}
182+
137183
func Test_newDeployment_PrometheusScrape_NotOverridden(t *testing.T) {
138184
function := &faasv1.Function{
139185
ObjectMeta: metav1.ObjectMeta{

pkg/k8s/probes.go

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,15 @@
44
package k8s
55

66
import (
7-
"fmt"
87
"path/filepath"
9-
"time"
108

119
types "github.com/openfaas/faas-provider/types"
1210
corev1 "k8s.io/api/core/v1"
1311
"k8s.io/apimachinery/pkg/util/intstr"
1412
)
1513

1614
const (
17-
ProbePath = "com.openfaas.health.http.path"
18-
ProbePathValue = "/_/health"
19-
ProbeInitialDelay = "com.openfaas.health.http.initialDelay"
15+
ProbePathValue = "/_/health"
2016
)
2117

2218
type FunctionProbes struct {
@@ -31,20 +27,6 @@ func (f *FunctionFactory) MakeProbes(r types.FunctionDeployment) (*FunctionProbe
3127
httpPath := ProbePathValue
3228
initialDelaySeconds := int32(f.Config.LivenessProbe.InitialDelaySeconds)
3329

34-
if r.Annotations != nil {
35-
annotations := *r.Annotations
36-
if path, ok := annotations[ProbePath]; ok {
37-
httpPath = path
38-
}
39-
if delay, ok := annotations[ProbeInitialDelay]; ok {
40-
d, err := time.ParseDuration(delay)
41-
if err != nil {
42-
return nil, fmt.Errorf("invalid %s duration format: %v", ProbeInitialDelay, err)
43-
}
44-
initialDelaySeconds = int32(d.Seconds())
45-
}
46-
}
47-
4830
if f.Config.HTTPProbe || httpPath != ProbePathValue {
4931
handler = corev1.Handler{
5032
HTTPGet: &corev1.HTTPGetAction{

pkg/k8s/probes_test.go

Lines changed: 0 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -55,75 +55,3 @@ func Test_makeProbes_useHTTPProbe(t *testing.T) {
5555
t.Fail()
5656
}
5757
}
58-
59-
func Test_makeProbes_useCustomHTTPProbe(t *testing.T) {
60-
f := mockFactory()
61-
customPath := "/healthz"
62-
request := types.FunctionDeployment{
63-
Service: "testfunc",
64-
ReadOnlyRootFilesystem: false,
65-
Annotations: &map[string]string{
66-
ProbePath: customPath,
67-
},
68-
}
69-
70-
probes, err := f.MakeProbes(request)
71-
if err != nil {
72-
t.Fatal(err)
73-
}
74-
75-
if probes.Readiness.HTTPGet == nil {
76-
t.Errorf("Readiness probe should have had HTTPGet handler")
77-
t.Fail()
78-
}
79-
if probes.Readiness.HTTPGet.Path != customPath {
80-
t.Errorf("Readiness probe should have had HTTPGet handler set to %s", customPath)
81-
t.Fail()
82-
}
83-
84-
if probes.Liveness.HTTPGet == nil {
85-
t.Errorf("Liveness probe should have had HTTPGet handler")
86-
t.Fail()
87-
}
88-
if probes.Liveness.HTTPGet.Path != customPath {
89-
t.Errorf("Readiness probe should have had HTTPGet handler set to %s", customPath)
90-
t.Fail()
91-
}
92-
}
93-
94-
func Test_makeProbes_useCustomDurationHTTPProbe(t *testing.T) {
95-
f := mockFactory()
96-
f.Config.HTTPProbe = true
97-
customDelay := "2m"
98-
99-
request := types.FunctionDeployment{
100-
Service: "testfunc",
101-
ReadOnlyRootFilesystem: false,
102-
Annotations: &map[string]string{
103-
ProbeInitialDelay: customDelay,
104-
},
105-
}
106-
107-
probes, err := f.MakeProbes(request)
108-
if err != nil {
109-
t.Fatal(err)
110-
}
111-
112-
if probes.Readiness.HTTPGet == nil {
113-
t.Errorf("Readiness probe should have had HTTPGet handler")
114-
t.Fail()
115-
}
116-
if probes.Readiness.InitialDelaySeconds != 120 {
117-
t.Errorf("Readiness probe should have initial delay seconds set to %s", customDelay)
118-
t.Fail()
119-
}
120-
121-
if probes.Liveness.HTTPGet == nil {
122-
t.Errorf("Liveness probe should have had HTTPGet handler")
123-
t.Fail()
124-
}
125-
if probes.Liveness.InitialDelaySeconds != 120 {
126-
t.Errorf("Readiness probe should have had HTTPGet handler set to %s", customDelay)
127-
t.Fail()
128-
}
129-
}

0 commit comments

Comments
 (0)