Skip to content

Commit ac44520

Browse files
authored
Merge pull request #8 from sbezverk/web_server_liveness_probe
Refactor of liveness probe
2 parents d69f4b8 + ee935d6 commit ac44520

File tree

3 files changed

+88
-67
lines changed

3 files changed

+88
-67
lines changed

cmd/main.go

+44-26
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ package main
1919
import (
2020
"context"
2121
"flag"
22-
"os"
22+
"net"
23+
"net/http"
2324
"time"
2425

2526
"github.com/golang/glog"
@@ -37,51 +38,68 @@ var (
3738
// kubeconfig = flag.String("kubeconfig", "", "Absolute path to the kubeconfig file. Required only when running out of cluster.")
3839
connectionTimeout = flag.Duration("connection-timeout", 30*time.Second, "Timeout for waiting for CSI driver socket in seconds.")
3940
csiAddress = flag.String("csi-address", "/run/csi/socket", "Address of the CSI driver socket.")
41+
healthzPort = flag.String("health-port", "9808", "TCP ports for listening healthz requests")
4042
)
4143

42-
func main() {
43-
flag.Set("logtostderr", "true")
44-
flag.Parse()
44+
func runProbe(ctx context.Context) error {
4545

4646
// Connect to CSI.
47-
glog.V(1).Infof("Attempting to open a gRPC connection with: %q", csiAddress)
47+
glog.Infof("Attempting to open a gRPC connection with: %s", *csiAddress)
4848
csiConn, err := connection.NewConnection(*csiAddress, *connectionTimeout)
4949
if err != nil {
50-
glog.Error(err.Error())
51-
os.Exit(1)
50+
return err
5251
}
5352

5453
// Get CSI driver name.
55-
glog.V(1).Infof("Calling CSI driver to discover driver name.")
56-
ctx, cancel := context.WithTimeout(context.Background(), csiTimeout)
57-
defer cancel()
54+
glog.Infof("Calling CSI driver to discover driver name.")
5855
csiDriverName, err := csiConn.GetDriverName(ctx)
5956
if err != nil {
60-
glog.Error(err.Error())
61-
os.Exit(1)
57+
return err
6258
}
63-
glog.V(2).Infof("CSI driver name: %q", csiDriverName)
59+
glog.Infof("CSI driver name: %q", csiDriverName)
6460

6561
// Get CSI Driver Node ID
66-
glog.V(1).Infof("Calling CSI driver to discover node ID.")
67-
ctx, cancel = context.WithTimeout(context.Background(), csiTimeout)
68-
defer cancel()
62+
glog.Infof("Calling CSI driver to discover node ID.")
6963
csiDriverNodeID, err := csiConn.NodeGetId(ctx)
7064
if err != nil {
71-
glog.Error(err.Error())
72-
os.Exit(1)
65+
return err
7366
}
74-
glog.V(2).Infof("CSI driver node ID: %q", csiDriverNodeID)
67+
glog.Infof("CSI driver node ID: %q", csiDriverNodeID)
7568

7669
// Sending Probe request
77-
glog.V(1).Infof("Sending probe request to CSI driver.")
78-
ctx, cancel = context.WithTimeout(context.Background(), csiTimeout)
70+
glog.Infof("Sending probe request to CSI driver.")
71+
if err := csiConn.LivenessProbe(ctx); err != nil {
72+
return err
73+
}
74+
return nil
75+
}
76+
77+
func chekcHealth(w http.ResponseWriter, req *http.Request) {
78+
79+
glog.Infof("Request: %s from: %s\n", req.URL.Path, req.RemoteAddr)
80+
ctx, cancel := context.WithTimeout(context.Background(), *connectionTimeout)
7981
defer cancel()
80-
err = csiConn.LivenessProbe(ctx)
82+
err := runProbe(ctx)
83+
if err != nil {
84+
w.WriteHeader(http.StatusInternalServerError)
85+
w.Write([]byte(err.Error()))
86+
glog.Infof("Health check failed with: %v.", err)
87+
} else {
88+
w.WriteHeader(http.StatusOK)
89+
w.Write([]byte(`ok`))
90+
glog.Infof("Health check succeeded.")
91+
}
92+
}
93+
94+
func main() {
95+
flag.Set("logtostderr", "true")
96+
flag.Parse()
97+
98+
addr := net.JoinHostPort("0.0.0.0", *healthzPort)
99+
http.HandleFunc("/healthz", chekcHealth)
100+
glog.Infof("Serving requests to /healthz on: %s", addr)
101+
err := http.ListenAndServe(addr, nil)
81102
if err != nil {
82-
glog.Error(err.Error())
83-
os.Exit(1)
103+
glog.Fatalf("failed to start http server with error: %v", err)
84104
}
85-
// Liveness Probe was sucessfuly
86-
os.Exit(0)
87105
}

deployment/kubernetes/hostpath-with-livenessprobe.yaml

+36-33
Original file line numberDiff line numberDiff line change
@@ -102,22 +102,23 @@ kind: Pod
102102
metadata:
103103
labels:
104104
app: hostpath-driver
105-
name: csi-pod
105+
name: csi-hostpath-plugin
106106
namespace: default
107107
spec:
108108
serviceAccount: csi-service-account
109109
containers:
110-
- args:
110+
- name: external-provisioner
111+
args:
111112
- --v=5
112113
- --provisioner=csi-hostpath
113114
- --csi-address=/csi/csi.sock
114115
image: quay.io/k8scsi/csi-provisioner:v0.2.0
115116
imagePullPolicy: Always
116-
name: external-provisioner
117117
volumeMounts:
118118
- mountPath: /csi
119119
name: socket-dir
120-
- args:
120+
- name: driver-registrar
121+
args:
121122
- --v=5
122123
- --csi-address=/csi/csi.sock
123124
env:
@@ -128,23 +129,46 @@ spec:
128129
fieldPath: spec.nodeName
129130
image: quay.io/k8scsi/driver-registrar:v0.2.0
130131
imagePullPolicy: Always
131-
name: driver-registrar
132132
volumeMounts:
133133
- mountPath: /csi
134134
name: socket-dir
135-
- args:
135+
- name: external-attacher
136+
args:
136137
- --v=5
137138
- --csi-address=$(ADDRESS)
138139
env:
139140
- name: ADDRESS
140141
value: /csi/csi.sock
141142
image: quay.io/k8scsi/csi-attacher:v0.2.0
142143
imagePullPolicy: Always
143-
name: external-attacher
144144
volumeMounts:
145145
- mountPath: /csi
146146
name: socket-dir
147-
- args:
147+
- name: hostpath-driver
148+
image: quay.io/k8scsi/hostpathplugin:v0.2.0
149+
imagePullPolicy: Always
150+
securityContext:
151+
privileged: true
152+
ports:
153+
- containerPort: 9808
154+
name: healthz
155+
protocol: TCP
156+
livenessProbe:
157+
failureThreshold: 5
158+
httpGet:
159+
path: /healthz
160+
port: healthz
161+
initialDelaySeconds: 10
162+
timeoutSeconds: 3
163+
periodSeconds: 2
164+
failureThreshold: 1
165+
volumeMounts:
166+
- mountPath: /csi
167+
name: socket-dir
168+
- mountPath: /var/lib/kubelet/pods
169+
mountPropagation: Bidirectional
170+
name: mountpoint-dir
171+
args:
148172
- --v=5
149173
- --endpoint=$(CSI_ENDPOINT)
150174
- --nodeid=$(KUBE_NODE_NAME)
@@ -156,36 +180,15 @@ spec:
156180
fieldRef:
157181
apiVersion: v1
158182
fieldPath: spec.nodeName
159-
image: quay.io/k8scsi/hostpathplugin:v0.2.0
160-
imagePullPolicy: Always
161-
name: hostpath-driver
162-
securityContext:
163-
privileged: true
164-
volumeMounts:
165-
- mountPath: /csi
166-
name: socket-dir
167-
- mountPath: /var/lib/kubelet/pods
168-
mountPropagation: Bidirectional
169-
name: mountpoint-dir
170183
- name: liveness-probe
171-
image: quay.io/k8scsi/livenessprobe:v0.2.0
172184
imagePullPolicy: Always
173-
command: ["/bin/sh"]
174-
args: ["-c", "while true; do sleep 10;done"]
175-
livenessProbe:
176-
exec:
177-
command:
178-
- ./livenessprobe
179-
- --v=6
180-
- --csi-address=/csi/csi.sock
181-
- --connection-timeout=3s
182-
initialDelaySeconds: 10
183-
timeoutSeconds: 3
184-
periodSeconds: 2
185-
failureThreshold: 1
186185
volumeMounts:
187186
- mountPath: /csi
188187
name: socket-dir
188+
image: quay.io/k8scsi/livenessprobe:v0.2.0
189+
args:
190+
- --csi-address=/csi/csi.sock
191+
- --connection-timeout=3s
189192
volumes:
190193
- hostPath:
191194
path: /var/lib/kubelet/plugins/csi-hostpath

pkg/connection/connection.go

+8-8
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ func NewConnection(
6767
}
6868

6969
func connect(address string, timeout time.Duration) (*grpc.ClientConn, error) {
70-
glog.V(2).Infof("Connecting to %s", address)
70+
glog.Infof("Connecting to %s", address)
7171
dialOptions := []grpc.DialOption{
7272
grpc.WithInsecure(),
7373
grpc.WithBackoffMaxDelay(time.Second),
@@ -87,14 +87,14 @@ func connect(address string, timeout time.Duration) (*grpc.ClientConn, error) {
8787
defer cancel()
8888
for {
8989
if !conn.WaitForStateChange(ctx, conn.GetState()) {
90-
glog.V(4).Infof("Connection timed out")
90+
glog.Infof("Connection timed out")
9191
return conn, nil // return nil, subsequent GetPluginInfo will show the real connection error
9292
}
9393
if conn.GetState() == connectivity.Ready {
94-
glog.V(3).Infof("Connected")
94+
glog.Infof("Connected")
9595
return conn, nil
9696
}
97-
glog.V(4).Infof("Still trying, connection is %s", conn.GetState())
97+
glog.Infof("Still trying, connection is %s", conn.GetState())
9898
}
9999
}
100100

@@ -147,10 +147,10 @@ func (c *csiConnection) Close() error {
147147
}
148148

149149
func logGRPC(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
150-
glog.V(5).Infof("GRPC call: %s", method)
151-
glog.V(5).Infof("GRPC request: %+v", req)
150+
glog.Infof("GRPC call: %s", method)
151+
glog.Infof("GRPC request: %+v", req)
152152
err := invoker(ctx, method, req, reply, cc, opts...)
153-
glog.V(5).Infof("GRPC response: %+v", reply)
154-
glog.V(5).Infof("GRPC error: %v", err)
153+
glog.Infof("GRPC response: %+v", reply)
154+
glog.Infof("GRPC error: %v", err)
155155
return err
156156
}

0 commit comments

Comments
 (0)