Skip to content

Add retry mechanism to re-connect device plugin from kubelet #131922

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
51 changes: 40 additions & 11 deletions pkg/kubelet/cm/devicemanager/plugin/v1beta1/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"

"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/klog/v2"
api "k8s.io/kubelet/pkg/apis/deviceplugin/v1beta1"
)
Expand All @@ -51,6 +52,7 @@ type client struct {
grpc *grpc.ClientConn
handler ClientHandler
client api.DevicePluginClient
backoff wait.Backoff
}

// NewPluginClient returns an initialized device plugin client.
Expand All @@ -59,6 +61,13 @@ func NewPluginClient(r string, socketPath string, h ClientHandler) Client {
resource: r,
socket: socketPath,
handler: h,
backoff: wait.Backoff{
Duration: 1 * time.Second,
Factor: 2.0,
Jitter: 0.1,
Steps: 5,
Cap: 30 * time.Second,
},
}
}

Expand All @@ -78,20 +87,40 @@ func (c *client) Connect() error {

// Run is for running the device plugin gRPC client.
func (c *client) Run() {
stream, err := c.client.ListAndWatch(context.Background(), &api.Empty{})
if err != nil {
klog.ErrorS(err, "ListAndWatch ended unexpectedly for device plugin", "resource", c.resource)
return
}

for {
response, err := stream.Recv()
var lastErr error
err := wait.ExponentialBackoff(c.backoff, func() (bool, error) {
stream, err := c.client.ListAndWatch(context.Background(), &api.Empty{})
if err != nil {
lastErr = err
klog.ErrorS(err, "ListAndWatch ended unexpectedly for device plugin", "resource", c.resource)
return
if err := c.Disconnect(); err != nil {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be nice to know a real reason of failure. Is it a connection drop or something else? If it's something else, then we probably shouldn't force disconnect.

klog.ErrorS(err, "Failed to disconnect", "resource", c.resource)
}
if err := c.Connect(); err != nil {
klog.ErrorS(err, "Failed to reconnect", "resource", c.resource)
return false, nil
}
return false, nil
}
lastErr = nil

for {
response, err := stream.Recv()
if err != nil {
klog.ErrorS(err, "ListAndWatch ended unexpectedly for device plugin", "resource", c.resource)
return false, nil
}
klog.V(2).InfoS("State pushed for device plugin", "resource", c.resource, "resourceCapacity", len(response.Devices))
c.handler.PluginListAndWatchReceiver(c.resource, response)
}
})

if err != nil {
if err == wait.ErrWaitTimeout {
klog.ErrorS(lastErr, "Max retries reached, giving up", "resource", c.resource)
} else {
klog.ErrorS(err, "Unexpected error occurred", "resource", c.resource)
}
klog.V(2).InfoS("State pushed for device plugin", "resource", c.resource, "resourceCapacity", len(response.Devices))
c.handler.PluginListAndWatchReceiver(c.resource, response)
}
}

Expand Down