-
Notifications
You must be signed in to change notification settings - Fork 2
/
clinet.go
208 lines (172 loc) · 5.7 KB
/
clinet.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
// Copyright 2019 Intel Corporation. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package client
import (
"fmt"
"github.com/sealyun-market/image-cri-shim/pkg/utils"
"net"
"os"
"syscall"
"time"
"google.golang.org/grpc"
"google.golang.org/grpc/connectivity"
api "k8s.io/cri-api/pkg/apis/runtime/v1alpha2"
)
// DialNotifyFn is a function to call after a successful net.Dial[Timeout]().
type DialNotifyFn func(string, int, int, os.FileMode, error)
// Options contains the configurable options of our CRI client.
type Options struct {
// ImageSocket is the socket path for the CRI image service.
ImageSocket string
// RuntimeSocket is the socket path for the CRI runtime service.
RuntimeSocket string
// DialNotify is an optional function to notify after net.Dial returns for a socket.
DialNotify DialNotifyFn
}
// ConnectOptions contains options for connecting to the server.
type ConnectOptions struct {
// Wait indicates whether Connect() should wait (indefinitely) for the server.
Wait bool
// Reconnect indicates whether CheckConnection() should attempt to Connect().
Reconnect bool
}
// Client is the interface we expose to our CRI client.
type Client interface {
// Connect tries to connect the client to the specified image and runtime services.
Connect(ConnectOptions) error
// Close closes any existing client connections.
Close()
// CheckConnection checks if we have (un-Close()'d as opposed to working) connections.
CheckConnection(ConnectOptions) error
// HasRuntimeService checks if the client is configured with runtime services.
HasRuntimeService() bool
// HasImageService checks if the client is configured with image services.
HasImageService() bool
// We expose full image and runtime client services.
api.ImageServiceClient
}
// client is the implementation of Client.
type client struct {
api.ImageServiceClient
options Options // client options
icc *grpc.ClientConn // our gRPC connection to the image service
}
const (
// DontConnect is used to mark a socket to not be connected.
DontConnect = "-"
)
// NewClient creates a new client instance.
func NewClient(options Options) (Client, error) {
if options.ImageSocket == DontConnect && options.RuntimeSocket == DontConnect {
return nil, clientError("neither image nor runtime socket specified")
}
c := &client{
options: options,
}
return c, nil
}
// Connect attempts to establish gRPC client connections to the configured services.
func (c *client) Connect(options ConnectOptions) error {
var err error
kind, socket := "image services", c.options.ImageSocket
if c.icc, err = c.connect(kind, socket, options); err != nil {
return err
}
if c.icc != nil {
c.ImageServiceClient = api.NewImageServiceClient(c.icc)
}
return nil
}
// Close any open service connection.
func (c *client) Close() {
if c.icc != nil {
c.icc.Close()
}
c.icc = nil
}
// Check if the connecton to CRI services is up, try to reconnect if requested.
func (c *client) CheckConnection(options ConnectOptions) error {
if c.icc == nil || c.icc.GetState() == connectivity.Ready {
return nil
}
c.Close()
if options.Reconnect {
if err := c.Connect(ConnectOptions{Wait: false}); err == nil {
return nil
}
}
return clientError("client connections are down")
}
// HasRuntimeService checks if the client is configured with runtime services.
func (c *client) HasRuntimeService() bool {
return c.options.RuntimeSocket != "" && c.options.RuntimeSocket != DontConnect
}
// HasImageService checks if the client is configured with image services.
func (c *client) HasImageService() bool {
return c.options.ImageSocket != "" && c.options.ImageSocket != DontConnect
}
// connect attempts to create a gRPC client connection to the given socket.
func (c *client) connect(kind, socket string, options ConnectOptions) (*grpc.ClientConn, error) {
var cc *grpc.ClientConn
var err error
if socket == DontConnect {
return nil, nil
}
dialOpts := []grpc.DialOption{
grpc.WithInsecure(),
grpc.WithBlock(),
grpc.FailOnNonTempDialError(true),
grpc.WithDialer(func(socket string, timeout time.Duration) (net.Conn, error) {
conn, err := net.DialTimeout("unix", socket, timeout)
if err != nil {
return conn, err
}
c.dialNotify(socket)
return conn, err
}),
}
if options.Wait {
if err = utils.WaitForServer(socket, -1, dialOpts, &cc); err != nil {
return nil, clientError("failed to connect to %s: %v", kind, err)
}
} else {
if cc, err = grpc.Dial(socket, dialOpts...); err != nil {
return nil, clientError("failed to connect to %s: %v", kind, err)
}
}
return cc, nil
}
func (c *client) dialNotify(socket string) {
if c.options.DialNotify == nil {
return
}
info, err := os.Stat(socket)
if err != nil {
c.options.DialNotify(socket, -1, -1, 0, err)
return
}
st, ok := info.Sys().(*syscall.Stat_t)
if !ok {
err := clientError("failed to stat socket %q: %v", socket, err)
c.options.DialNotify(socket, -1, -1, 0, err)
return
}
uid, gid := int(st.Uid), int(st.Gid)
mode := info.Mode() & os.ModePerm
c.options.DialNotify(socket, uid, gid, mode, nil)
}
// Return a formatted client-specific error.
func clientError(format string, args ...interface{}) error {
return fmt.Errorf("cri/client: "+format, args...)
}