Skip to content

Commit fc89337

Browse files
authored
Merge pull request #100 from arangodb/ready-probe
Added LivenessProbe & Readiness probe
2 parents 7048b67 + 13a5cc8 commit fc89337

File tree

8 files changed

+138
-11
lines changed

8 files changed

+138
-11
lines changed

main.go

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ import (
4848
"github.com/arangodb/kube-arangodb/pkg/operator"
4949
"github.com/arangodb/kube-arangodb/pkg/util/constants"
5050
"github.com/arangodb/kube-arangodb/pkg/util/k8sutil"
51+
"github.com/arangodb/kube-arangodb/pkg/util/probe"
5152
"github.com/arangodb/kube-arangodb/pkg/util/retry"
5253
)
5354

@@ -80,6 +81,8 @@ var (
8081
enableStorage bool // Run deployment operator
8182
createCRD bool
8283
}
84+
deploymentProbe probe.Probe
85+
storageProbe probe.Probe
8386
)
8487

8588
func init() {
@@ -135,7 +138,9 @@ func cmdMainRun(cmd *cobra.Command, args []string) {
135138
cliLog.Fatal().Err(err).Msg("Failed to get hostname")
136139
}
137140

138-
//http.HandleFunc(probe.HTTPReadyzEndpoint, probe.ReadyzHandler)
141+
http.HandleFunc("/health", probe.LivenessHandler)
142+
http.HandleFunc("/ready/deployment", deploymentProbe.ReadyHandler)
143+
http.HandleFunc("/ready/storage", storageProbe.ReadyHandler)
139144
http.Handle("/metrics", prometheus.Handler())
140145
listenAddr := net.JoinHostPort(server.host, strconv.Itoa(server.port))
141146
go http.ListenAndServe(listenAddr, nil)
@@ -186,11 +191,13 @@ func newOperatorConfigAndDeps(id, namespace, name string) (operator.Config, oper
186191
CreateCRD: operatorOptions.createCRD,
187192
}
188193
deps := operator.Dependencies{
189-
LogService: logService,
190-
KubeCli: kubecli,
191-
KubeExtCli: kubeExtCli,
192-
CRCli: crCli,
193-
EventRecorder: eventRecorder,
194+
LogService: logService,
195+
KubeCli: kubecli,
196+
KubeExtCli: kubeExtCli,
197+
CRCli: crCli,
198+
EventRecorder: eventRecorder,
199+
DeploymentProbe: &deploymentProbe,
200+
StorageProbe: &storageProbe,
194201
}
195202

196203
return cfg, deps, nil

manifests/templates/deployment/deployment.yaml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ metadata:
66
namespace: {{ .Deployment.Operator.Namespace }}
77
spec:
88
replicas: 1
9+
strategy:
10+
type: Recreate
911
template:
1012
metadata:
1113
labels:
@@ -26,3 +28,18 @@ spec:
2628
valueFrom:
2729
fieldRef:
2830
fieldPath: metadata.name
31+
ports:
32+
- name: metrics
33+
containerPort: 8528
34+
livenessProbe:
35+
httpGet:
36+
path: /health
37+
port: 8528
38+
initialDelaySeconds: 5
39+
periodSeconds: 10
40+
readinessProbe:
41+
httpGet:
42+
path: /ready/deployment
43+
port: 8528
44+
initialDelaySeconds: 5
45+
periodSeconds: 10

manifests/templates/storage/deployment.yaml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ metadata:
1414
namespace: {{ .Storage.Operator.Namespace }}
1515
spec:
1616
replicas: 1
17+
strategy:
18+
type: Recreate
1719
template:
1820
metadata:
1921
labels:
@@ -35,3 +37,19 @@ spec:
3537
valueFrom:
3638
fieldRef:
3739
fieldPath: metadata.name
40+
ports:
41+
- name: metrics
42+
containerPort: 8528
43+
livenessProbe:
44+
httpGet:
45+
path: /health
46+
port: 8528
47+
initialDelaySeconds: 5
48+
periodSeconds: 10
49+
readinessProbe:
50+
httpGet:
51+
path: /ready/storage
52+
port: 8528
53+
initialDelaySeconds: 5
54+
periodSeconds: 10
55+

pkg/operator/operator.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import (
3939
"github.com/arangodb/kube-arangodb/pkg/generated/clientset/versioned"
4040
"github.com/arangodb/kube-arangodb/pkg/logging"
4141
"github.com/arangodb/kube-arangodb/pkg/storage"
42+
"github.com/arangodb/kube-arangodb/pkg/util/probe"
4243
)
4344

4445
const (
@@ -71,11 +72,13 @@ type Config struct {
7172
}
7273

7374
type Dependencies struct {
74-
LogService logging.Service
75-
KubeCli kubernetes.Interface
76-
KubeExtCli apiextensionsclient.Interface
77-
CRCli versioned.Interface
78-
EventRecorder record.EventRecorder
75+
LogService logging.Service
76+
KubeCli kubernetes.Interface
77+
KubeExtCli apiextensionsclient.Interface
78+
CRCli versioned.Interface
79+
EventRecorder record.EventRecorder
80+
DeploymentProbe *probe.Probe
81+
StorageProbe *probe.Probe
7982
}
8083

8184
// NewOperator instantiates a new operator from given config & dependencies.

pkg/operator/operator_deployment.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ func (o *Operator) runDeployments(stop <-chan struct{}) {
5858
DeleteFunc: o.onDeleteArangoDeployment,
5959
}, cache.Indexers{})
6060

61+
o.Dependencies.DeploymentProbe.SetReady()
6162
informer.Run(stop)
6263
}
6364

pkg/operator/operator_local_storage.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ func (o *Operator) runLocalStorages(stop <-chan struct{}) {
5858
DeleteFunc: o.onDeleteArangoLocalStorage,
5959
}, cache.Indexers{})
6060

61+
o.Dependencies.StorageProbe.SetReady()
6162
informer.Run(stop)
6263
}
6364

pkg/util/probe/health.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
//
2+
// DISCLAIMER
3+
//
4+
// Copyright 2018 ArangoDB GmbH, Cologne, Germany
5+
//
6+
// Licensed under the Apache License, Version 2.0 (the "License");
7+
// you may not use this file except in compliance with the License.
8+
// You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing, software
13+
// distributed under the License is distributed on an "AS IS" BASIS,
14+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
// See the License for the specific language governing permissions and
16+
// limitations under the License.
17+
//
18+
// Copyright holder is ArangoDB GmbH, Cologne, Germany
19+
//
20+
// Author Ewout Prangsma
21+
//
22+
23+
package probe
24+
25+
import (
26+
"net/http"
27+
)
28+
29+
// LivenessHandler writes back the HTTP status code 200 to indicate a healthy operator.
30+
func LivenessHandler(w http.ResponseWriter, r *http.Request) {
31+
w.WriteHeader(http.StatusOK)
32+
}

pkg/util/probe/ready.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
//
2+
// DISCLAIMER
3+
//
4+
// Copyright 2018 ArangoDB GmbH, Cologne, Germany
5+
//
6+
// Licensed under the Apache License, Version 2.0 (the "License");
7+
// you may not use this file except in compliance with the License.
8+
// You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing, software
13+
// distributed under the License is distributed on an "AS IS" BASIS,
14+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
// See the License for the specific language governing permissions and
16+
// limitations under the License.
17+
//
18+
// Copyright holder is ArangoDB GmbH, Cologne, Germany
19+
//
20+
// Author Ewout Prangsma
21+
//
22+
23+
package probe
24+
25+
import (
26+
"net/http"
27+
"sync/atomic"
28+
)
29+
30+
// Probe wraps a readiness probe handler.
31+
type Probe struct {
32+
ready int32
33+
}
34+
35+
// SetReady marks the probe as ready.
36+
func (p *Probe) SetReady() {
37+
atomic.StoreInt32(&p.ready, 1)
38+
}
39+
40+
// ReadyHandler writes back the HTTP status code 200 if the operator is ready, and 500 otherwise.
41+
func (p *Probe) ReadyHandler(w http.ResponseWriter, r *http.Request) {
42+
isReady := atomic.LoadInt32(&p.ready) != 0
43+
if isReady {
44+
w.WriteHeader(http.StatusOK)
45+
} else {
46+
w.WriteHeader(http.StatusInternalServerError)
47+
}
48+
}

0 commit comments

Comments
 (0)