Skip to content

Commit d1835e8

Browse files
authored
Merge pull request kubernetes-retired#3 from rrati/grpc-tools
Added grpc client and server modules
2 parents 784bb4a + 27c5ed8 commit d1835e8

File tree

4 files changed

+367
-0
lines changed

4 files changed

+367
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module github.com/kubernetes-sigs/container-object-storage-interface-provisioner-sidecar
2+
3+
go 1.14
4+
5+
require (
6+
google.golang.org/grpc v1.34.0 // indirect
7+
k8s.io/klog v1.0.0 // indirect
8+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
2+
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
3+
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
4+
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
5+
github.com/cncf/udpa/go v0.0.0-20200629203442-efcf912fb354/go.mod h1:WmhPx2Nbnhtbo57+VJT5O0JRkEi1Wbu0z5j0R8u5Hbk=
6+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
7+
github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
8+
github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
9+
github.com/envoyproxy/go-control-plane v0.9.7/go.mod h1:cwu0lG7PUMfa9snN8LXBig5ynNVH9qI8YYLbd1fK2po=
10+
github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
11+
github.com/go-logr/logr v0.1.0/go.mod h1:ixOQHD9gLJUVQQ2ZOR7zLEifBX6tGkNJF4QyIY7sIas=
12+
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
13+
github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=
14+
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
15+
github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
16+
github.com/golang/protobuf v1.4.0-rc.1/go.mod h1:ceaxUfeHdC40wWswd/P6IGgMaK3YpKi5j83Wpe3EHw8=
17+
github.com/golang/protobuf v1.4.0-rc.1.0.20200221234624-67d41d38c208/go.mod h1:xKAWHe0F5eneWXFV3EuXVDTCmh+JuBKY0li0aMyXATA=
18+
github.com/golang/protobuf v1.4.0-rc.2/go.mod h1:LlEzMj4AhA7rCAGe4KMBDvJI+AwstrUpVNzEA03Pprs=
19+
github.com/golang/protobuf v1.4.0-rc.4.0.20200313231945-b860323f09d0/go.mod h1:WU3c8KckQ9AFe+yFwt9sWVRKCVIyN9cPHBJSNnbL67w=
20+
github.com/golang/protobuf v1.4.0/go.mod h1:jodUvKwWbYaEsadDk5Fwe5c77LiNKVO9IDvqG2KuDX0=
21+
github.com/golang/protobuf v1.4.1/go.mod h1:U8fpvMrcmy5pZrNK1lt4xCsGvpyWQ/VVv6QDs8UjoX8=
22+
github.com/golang/protobuf v1.4.2 h1:+Z5KGCizgyZCbGh1KZqA0fcLLkwbsjIzS4aV2v7wJX0=
23+
github.com/golang/protobuf v1.4.2/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI=
24+
github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=
25+
github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
26+
github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
27+
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
28+
github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
29+
github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
30+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
31+
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
32+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
33+
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
34+
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
35+
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
36+
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
37+
golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU=
38+
golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
39+
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
40+
golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
41+
golang.org/x/net v0.0.0-20190213061140-3a22650c66bd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
42+
golang.org/x/net v0.0.0-20190311183353-d8887717615a h1:oWX7TPOiFAMXLq8o0ikBYfCJVlRHBcsciT5bXOrH628=
43+
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
44+
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
45+
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
46+
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
47+
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
48+
golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
49+
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
50+
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
51+
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
52+
golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
53+
golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY=
54+
golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
55+
golang.org/x/tools v0.0.0-20190524140312-2c0ae7006135/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q=
56+
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
57+
google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM=
58+
google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4=
59+
google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc=
60+
google.golang.org/genproto v0.0.0-20190819201941-24fa4b261c55/go.mod h1:DMBHOl98Agz4BDEuKkezgsaosCRResVns1a3J2ZsMNc=
61+
google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013 h1:+kGHl1aib/qcwaRi1CbqBZ1rk19r85MNUf8HaBghugY=
62+
google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013/go.mod h1:NbSheEEYHJ7i3ixzK3sjbqSGDJWnxyFXZblF3eUsNvo=
63+
google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c=
64+
google.golang.org/grpc v1.23.0/go.mod h1:Y5yQAOtifL1yxbo5wqy6BxZv8vAUGQwXBOALyacEbxg=
65+
google.golang.org/grpc v1.25.1/go.mod h1:c3i+UQWmh7LiEpx4sFZnkU36qjEYZ0imhYfXVyQciAY=
66+
google.golang.org/grpc v1.27.0/go.mod h1:qbnxyOmOxrQa7FizSgH+ReBfzJrCY1pSN7KXBS8abTk=
67+
google.golang.org/grpc v1.34.0 h1:raiipEjMOIC/TO2AvyTxP25XFdLxNIBwzDh3FM3XztI=
68+
google.golang.org/grpc v1.34.0/go.mod h1:WotjhfgOW/POjDeRt8vscBtXq+2VjORFy659qA51WJ8=
69+
google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8=
70+
google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0=
71+
google.golang.org/protobuf v0.0.0-20200228230310-ab0ca4ff8a60/go.mod h1:cfTl7dwQJ+fmap5saPgwCLgHXTUD7jkjRqWcaiX5VyM=
72+
google.golang.org/protobuf v1.20.1-0.20200309200217-e05f789c0967/go.mod h1:A+miEFZTKqfCUM6K7xSMQL9OKL/b6hQv+e19PK+JZNE=
73+
google.golang.org/protobuf v1.21.0/go.mod h1:47Nbq4nVaFHyn7ilMalzfO3qCViNmqZ2kzikPIcrTAo=
74+
google.golang.org/protobuf v1.22.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU=
75+
google.golang.org/protobuf v1.23.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU=
76+
google.golang.org/protobuf v1.23.1-0.20200526195155-81db48ad09cc/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU=
77+
google.golang.org/protobuf v1.25.0 h1:Ejskq+SyPohKW+1uil0JJMtmHCgJPJ/qWTxr8qp+R4c=
78+
google.golang.org/protobuf v1.25.0/go.mod h1:9JNX74DMeImyA3h4bdi1ymwjUzf21/xIlbajtzgsN7c=
79+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
80+
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
81+
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
82+
honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
83+
k8s.io/klog v1.0.0 h1:Pt+yjF5aB1xDSVbau4VsWe+dQNzA0qv1LlXdC2dF6Q8=
84+
k8s.io/klog v1.0.0/go.mod h1:4Bi6QPql/J/LkTDqv7R/cd3hPo4k2DG6Ptcz060Ez5I=
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
/*
2+
Copyright 2020 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package grpcclient
18+
19+
import (
20+
"context"
21+
"errors"
22+
"net"
23+
"strings"
24+
"time"
25+
26+
"google.golang.org/grpc"
27+
28+
"k8s.io/klog"
29+
)
30+
31+
type options struct {
32+
reconnect func() bool
33+
}
34+
35+
// Option is the type of all optional parameters for Connect.
36+
type Option func(o *options)
37+
38+
type GRPCClient struct {
39+
serverAddress string
40+
dialOptions []grpc.DialOption
41+
}
42+
43+
// NewGRPCClient creates a new GRPCClient
44+
func NewGRPCClient(address string, dialOptions []grpc.DialOption, connectOptions []Option) (*GRPCClient, error) {
45+
var o options
46+
for _, option := range connectOptions {
47+
option(&o)
48+
}
49+
50+
dialOptions = append(dialOptions,
51+
grpc.WithInsecure(), // Don't use TLS, it's usually local Unix domain socket in a container.
52+
grpc.WithBackoffMaxDelay(time.Second), // Retry every second after failure.
53+
grpc.WithBlock(), // Block until connection succeeds.
54+
)
55+
56+
unixPrefix := "unix://"
57+
if strings.HasPrefix(address, "tcp://") {
58+
address = address[6:]
59+
}
60+
if strings.HasPrefix(address, "/") {
61+
// It looks like filesystem path.
62+
address = unixPrefix + address
63+
}
64+
65+
if strings.HasPrefix(address, unixPrefix) {
66+
// state variables for the custom dialer
67+
haveConnected := false
68+
lostConnection := false
69+
reconnect := true
70+
71+
dialOptions = append(dialOptions, grpc.WithDialer(func(addr string, timeout time.Duration) (net.Conn, error) {
72+
if haveConnected && !lostConnection {
73+
// We have detected a loss of connection for the first time. Decide what to do...
74+
// Record this once. TODO (?): log at regular time intervals.
75+
klog.Errorf("Lost connection to %s.", address)
76+
// Inform caller and let it decide? Default is to reconnect.
77+
if o.reconnect != nil {
78+
reconnect = o.reconnect()
79+
}
80+
lostConnection = true
81+
}
82+
if !reconnect {
83+
return nil, errors.New("connection lost, reconnecting disabled")
84+
}
85+
conn, err := net.DialTimeout("unix", address[len(unixPrefix):], timeout)
86+
if err == nil {
87+
// Connection restablished.
88+
haveConnected = true
89+
lostConnection = false
90+
}
91+
return conn, err
92+
}))
93+
} else if o.reconnect != nil {
94+
return nil, errors.New("OnConnectionLoss callback only supported for unix:// addresses")
95+
}
96+
97+
return &GRPCClient{serverAddress: address, dialOptions: dialOptions}, nil
98+
}
99+
100+
// Connect connects to the grpc server
101+
func (c *GRPCClient) ConnectWithLogging(interval time.Duration) (*grpc.ClientConn, error) {
102+
klog.Infof("Connecting to %s", c.serverAddress)
103+
104+
grpcLogFunc := func(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
105+
klog.V(5).Infof("GRPC call: %s", method)
106+
klog.V(5).Infof("GRPC request: %s", req)
107+
err := invoker(ctx, method, req, reply, cc, opts...)
108+
klog.V(5).Infof("GRPC response: %s", reply)
109+
klog.V(5).Infof("GRPC error: %v", err)
110+
return err
111+
}
112+
113+
// Log all messages
114+
c.dialOptions = append(c.dialOptions, grpc.WithChainUnaryInterceptor(grpcLogFunc))
115+
116+
// Connect in background.
117+
var conn *grpc.ClientConn
118+
var err error
119+
ready := make(chan bool)
120+
go func() {
121+
conn, err = grpc.Dial(c.serverAddress, c.dialOptions...)
122+
close(ready)
123+
}()
124+
125+
// Log error every connectionLoggingInterval
126+
ticker := time.NewTicker(interval)
127+
defer ticker.Stop()
128+
129+
// Wait until Dial() succeeds.
130+
for {
131+
select {
132+
case <-ticker.C:
133+
klog.Warningf("Still connecting to %s", c.serverAddress)
134+
135+
case <-ready:
136+
return conn, err
137+
}
138+
}
139+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
/*
2+
Copyright 2020 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package grpcserver
18+
19+
import (
20+
"fmt"
21+
"net"
22+
"os"
23+
"strings"
24+
"sync"
25+
26+
"github.com/kubernetes-csi/csi-lib-utils/protosanitizer"
27+
"golang.org/x/net/context"
28+
"google.golang.org/grpc"
29+
30+
osi "github.com/kubernetes-sigs/container-object-storage-interface-spec"
31+
32+
"k8s.io/klog"
33+
)
34+
35+
// Defines Non blocking GRPC server interfaces
36+
type NonBlockingGRPCServer interface {
37+
// Start services at the endpoint
38+
Start(endpoint string, cds osi.ProvisionerServer)
39+
// Waits for the service to stop
40+
Wait()
41+
// Stops the service gracefully
42+
Stop()
43+
// Stops the service forcefully
44+
ForceStop()
45+
}
46+
47+
func NewNonBlockingGRPCServer() NonBlockingGRPCServer {
48+
return &nonBlockingGRPCServer{}
49+
}
50+
51+
// NonBlocking server
52+
type nonBlockingGRPCServer struct {
53+
wg sync.WaitGroup
54+
server *grpc.Server
55+
}
56+
57+
func logGRPC(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
58+
klog.Infof("GRPC call: %s", info.FullMethod)
59+
klog.Infof("GRPC request: %s", protosanitizer.StripSecrets(req))
60+
resp, err := handler(ctx, req)
61+
if err != nil {
62+
klog.Errorf("GRPC error: %v", err)
63+
} else {
64+
klog.Infof("GRPC response: %s", protosanitizer.StripSecrets(resp))
65+
}
66+
return resp, err
67+
}
68+
69+
func ParseEndpoint(ep string) (string, string, error) {
70+
if strings.HasPrefix(strings.ToLower(ep), "unix://") || strings.HasPrefix(strings.ToLower(ep), "tcp://") {
71+
s := strings.SplitN(ep, "://", 2)
72+
if s[1] != "" {
73+
return s[0], s[1], nil
74+
}
75+
}
76+
return "", "", fmt.Errorf("Invalid endpoint: %v", ep)
77+
}
78+
79+
func (s *nonBlockingGRPCServer) Start(endpoint string, cds osi.ProvisionerServer) {
80+
81+
s.wg.Add(1)
82+
83+
go s.serve(endpoint, cds)
84+
85+
return
86+
}
87+
88+
func (s *nonBlockingGRPCServer) Wait() {
89+
s.wg.Wait()
90+
}
91+
92+
func (s *nonBlockingGRPCServer) Stop() {
93+
s.server.GracefulStop()
94+
}
95+
96+
func (s *nonBlockingGRPCServer) ForceStop() {
97+
s.server.Stop()
98+
}
99+
100+
func (s *nonBlockingGRPCServer) serve(endpoint string, driver osi.ProvisionerServer) {
101+
102+
proto, addr, err := ParseEndpoint(endpoint)
103+
if err != nil {
104+
klog.Fatal(err.Error())
105+
}
106+
107+
if proto == "unix" {
108+
addr = "/" + addr
109+
if err := os.Remove(addr); err != nil && !os.IsNotExist(err) {
110+
klog.Fatalf("Failed to remove %s, error: %s", addr, err.Error())
111+
}
112+
}
113+
klog.Infof("Create listener on %s", endpoint)
114+
115+
//listener, err := net.Listen("tcp", fmt.Sprintf(":%d", osi-port))
116+
listener, err := net.Listen(proto, addr)
117+
if err != nil {
118+
klog.Fatalf("Failed to listen: %v", err)
119+
}
120+
121+
opts := []grpc.ServerOption{
122+
grpc.UnaryInterceptor(logGRPC),
123+
}
124+
server := grpc.NewServer(opts...)
125+
s.server = server
126+
127+
klog.Infof("Registering CosiControllerServer")
128+
if driver != nil {
129+
osi.RegisterProvisionerServer(server, driver)
130+
}
131+
132+
klog.Infof("Listening for connections on address: %#v", listener.Addr())
133+
134+
server.Serve(listener)
135+
136+
}

0 commit comments

Comments
 (0)