Skip to content

Commit 62d39be

Browse files
committed
livenessprobe should use CSI connection lib
1 parent 781623f commit 62d39be

File tree

3 files changed

+11
-45
lines changed

3 files changed

+11
-45
lines changed

cmd/livenessprobe_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func createMockServer(t *testing.T) (
5353

5454
// Create a client connection to it
5555
addr := drv.Address()
56-
csiConn, err := connection.NewConnection(addr, 10)
56+
csiConn, err := connection.NewConnection(addr)
5757
if err != nil {
5858
return nil, nil, nil, nil, nil, nil, err
5959
}

cmd/main.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ const (
3636
// Command line flags
3737
var (
3838
// kubeconfig = flag.String("kubeconfig", "", "Absolute path to the kubeconfig file. Required only when running out of cluster.")
39-
connectionTimeout = flag.Duration("connection-timeout", 30*time.Second, "Timeout for waiting for CSI driver socket in seconds.")
39+
connectionTimeout = flag.Duration("connection-timeout", 0, "The --connection-timeout flag is deprecated")
4040
csiAddress = flag.String("csi-address", "/run/csi/socket", "Address of the CSI driver socket.")
4141
healthzPort = flag.String("health-port", "9808", "TCP ports for listening healthz requests")
4242
)
@@ -56,10 +56,8 @@ func runProbe(ctx context.Context, csiConn connection.CSIConnection) error {
5656
}
5757

5858
func getCSIConnection() (connection.CSIConnection, error) {
59-
// Connect to CSI.
6059
klog.Infof("Attempting to open a gRPC connection with: %s", *csiAddress)
61-
csiConn, err := connection.NewConnection(*csiAddress, *connectionTimeout)
62-
return csiConn, err
60+
return connection.NewConnection(*csiAddress)
6361
}
6462

6563
func checkHealth(w http.ResponseWriter, req *http.Request) {
@@ -72,7 +70,7 @@ func checkHealth(w http.ResponseWriter, req *http.Request) {
7270
return
7371
}
7472
defer csiConn.Close()
75-
ctx, cancel := context.WithTimeout(context.Background(), *connectionTimeout)
73+
ctx, cancel := context.WithCancel(context.Background())
7674
defer cancel()
7775
if err := runProbe(ctx, csiConn); err != nil {
7876
w.WriteHeader(http.StatusInternalServerError)
@@ -90,6 +88,10 @@ func main() {
9088
flag.Set("logtostderr", "true")
9189
flag.Parse()
9290

91+
if *connectionTimeout != 0 {
92+
klog.Warning("--connection-timeout is deprecated and will have no effect")
93+
}
94+
9395
addr := net.JoinHostPort("0.0.0.0", *healthzPort)
9496
http.HandleFunc("/healthz", checkHealth)
9597
klog.Infof("Serving requests to /healthz on: %s", addr)

pkg/connection/connection.go

Lines changed: 3 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,11 @@ package connection
1919
import (
2020
"context"
2121
"fmt"
22-
"net"
23-
"strings"
24-
"time"
2522

2623
"github.com/container-storage-interface/spec/lib/go/csi"
24+
"github.com/kubernetes-csi/csi-lib-utils/connection"
2725
"github.com/kubernetes-csi/csi-lib-utils/protosanitizer"
2826
"google.golang.org/grpc"
29-
"google.golang.org/grpc/connectivity"
3027
"k8s.io/klog"
3128
)
3229

@@ -53,9 +50,8 @@ var (
5350
)
5451

5552
// NewConnection establishes new connection to CSI driver
56-
func NewConnection(
57-
address string, timeout time.Duration) (CSIConnection, error) {
58-
conn, err := connect(address, timeout)
53+
func NewConnection(address string) (CSIConnection, error) {
54+
conn, err := connection.Connect(address)
5955
if err != nil {
6056
return nil, err
6157
}
@@ -64,38 +60,6 @@ func NewConnection(
6460
}, nil
6561
}
6662

67-
func connect(address string, timeout time.Duration) (*grpc.ClientConn, error) {
68-
klog.Infof("Connecting to %s", address)
69-
dialOptions := []grpc.DialOption{
70-
grpc.WithInsecure(),
71-
grpc.WithBackoffMaxDelay(time.Second),
72-
grpc.WithUnaryInterceptor(logGRPC),
73-
}
74-
if strings.HasPrefix(address, "/") {
75-
dialOptions = append(dialOptions, grpc.WithDialer(func(addr string, timeout time.Duration) (net.Conn, error) {
76-
return net.DialTimeout("unix", addr, timeout)
77-
}))
78-
}
79-
conn, err := grpc.Dial(address, dialOptions...)
80-
81-
if err != nil {
82-
return nil, err
83-
}
84-
ctx, cancel := context.WithTimeout(context.Background(), timeout)
85-
defer cancel()
86-
for {
87-
if !conn.WaitForStateChange(ctx, conn.GetState()) {
88-
klog.Infof("Connection timed out")
89-
return conn, nil // return nil, subsequent GetPluginInfo will show the real connection error
90-
}
91-
if conn.GetState() == connectivity.Ready {
92-
klog.Infof("Connected")
93-
return conn, nil
94-
}
95-
klog.Infof("Still trying, connection is %s", conn.GetState())
96-
}
97-
}
98-
9963
func (c *csiConnection) GetDriverName(ctx context.Context) (string, error) {
10064
client := csi.NewIdentityClient(c.conn)
10165

0 commit comments

Comments
 (0)