Skip to content

pkg/deviceplugin: move to grpc.NewClient() #1748

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/gpu_plugin/rm/gpu_plugin_resource_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func (w *mockPodResources) Get(ctx context.Context,
}

func newMockResourceManager(pods []v1.Pod) ResourceManager {
client, err := grpc.Dial("fake", grpc.WithTransportCredentials(insecure.NewCredentials()))
client, err := grpc.NewClient("fake", grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
fmt.Fprintf(os.Stderr, "failed to create client: %v\n", err)

Expand Down
48 changes: 28 additions & 20 deletions pkg/deviceplugin/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/fsnotify/fsnotify"
"github.com/pkg/errors"
"google.golang.org/grpc"
"google.golang.org/grpc/connectivity"
"google.golang.org/grpc/credentials/insecure"

"k8s.io/klog/v2"
Expand Down Expand Up @@ -326,15 +327,9 @@ func watchFile(file string) error {
}

func (srv *server) registerWithKubelet(kubeletSocket, pluginEndPoint, resourceName string) error {
ctx := context.Background()

conn, err := grpc.DialContext(ctx, kubeletSocket,
grpc.WithTransportCredentials(insecure.NewCredentials()),
grpc.WithContextDialer(func(ctx context.Context, addr string) (net.Conn, error) {
return (&net.Dialer{}).DialContext(ctx, "unix", addr)
}))
conn, err := grpc.NewClient(filepath.Join("unix://", kubeletSocket), grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
return errors.Wrap(err, "Cannot connect to kubelet service")
return errors.Wrap(err, "Cannot create a gRPC client")
}

defer conn.Close()
Expand All @@ -347,7 +342,7 @@ func (srv *server) registerWithKubelet(kubeletSocket, pluginEndPoint, resourceNa
Options: srv.getDevicePluginOptions(),
}

_, err = client.Register(ctx, reqt)
_, err = client.Register(context.Background(), reqt)
if err != nil {
return errors.Wrap(err, "Cannot register to kubelet service")
}
Expand All @@ -358,20 +353,33 @@ func (srv *server) registerWithKubelet(kubeletSocket, pluginEndPoint, resourceNa
// waitForServer checks if grpc server is alive
// by making grpc blocking connection to the server socket.
func waitForServer(socket string, timeout time.Duration) error {
conn, err := grpc.NewClient(filepath.Join("unix://", socket), grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
return errors.Wrap(err, "Cannot create a gRPC client")
}

defer conn.Close()

ctx, cancel := context.WithTimeout(context.Background(), timeout)

defer cancel()

conn, err := grpc.DialContext(ctx, socket,
grpc.WithTransportCredentials(insecure.NewCredentials()),
grpc.WithBlock(),
grpc.WithContextDialer(func(ctx context.Context, addr string) (net.Conn, error) {
return (&net.Dialer{}).DialContext(ctx, "unix", addr)
}),
)
if conn != nil {
_ = conn.Close()
}
// A blocking dial blocks until the clientConn is ready. Based
// on grpc-go's DialContext() that moved to use NewClient() but
// marked DialContext() deprecated.
for {
state := conn.GetState()
if state == connectivity.Idle {
conn.Connect()
}

if state == connectivity.Ready {
return nil
}

return errors.Wrapf(err, "Failed dial context at %s", socket)
if !conn.WaitForStateChange(ctx, state) {
// ctx got timeout or canceled.
return errors.Wrapf(ctx.Err(), "Failed dial context at %s", socket)
}
}
}
18 changes: 6 additions & 12 deletions pkg/deviceplugin/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"net"
"os"
"path"
"path/filepath"
"reflect"
"sync"
"testing"
Expand Down Expand Up @@ -111,7 +112,7 @@ func (k *kubeletStub) start() error {
return waitForServer(k.socket, 10*time.Second)
}

func TestRegisterWithKublet(t *testing.T) {
func TestRegisterWithKubelet(t *testing.T) {
pluginSocket := path.Join(devicePluginPath, pluginEndpoint)

srv := newTestServer()
Expand Down Expand Up @@ -180,11 +181,8 @@ func TestSetupAndServe(t *testing.T) {

ctx := context.Background()

conn, err := grpc.DialContext(ctx, pluginSocket,
grpc.WithTransportCredentials(insecure.NewCredentials()),
grpc.WithContextDialer(func(ctx context.Context, addr string) (net.Conn, error) {
return (&net.Dialer{}).DialContext(ctx, "unix", addr)
}))
conn, err := grpc.NewClient(filepath.Join("unix://", pluginSocket),
grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
t.Fatalf("Failed to get connection: %+v", err)
}
Expand Down Expand Up @@ -231,12 +229,8 @@ func TestSetupAndServe(t *testing.T) {
time.Sleep(1 * time.Second)
}

conn, err = grpc.DialContext(ctx, pluginSocket,
grpc.WithTransportCredentials(insecure.NewCredentials()),
grpc.WithContextDialer(func(ctx context.Context, addr string) (net.Conn, error) {
return (&net.Dialer{}).DialContext(ctx, "unix", addr)
}))

conn, err = grpc.NewClient(filepath.Join("unix://", pluginSocket),
grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
t.Fatalf("Failed to get connection: %+v", err)
}
Expand Down