func()

in plugins/internal/plugin/device_plugin.go [233:278]


func (p *DevicePlugin) RegisterWithKubelet() error {

	// Set a 60 second timeout when attempting to connect to the Kubelet
	ctxConnect, cancelConnect := context.WithTimeout(context.Background(), time.Minute)
	defer cancelConnect()

	// Create a dialler that treats the Kubelet's endpoint as a Unix socket rather than a TCP address
	dialler := grpc.WithContextDialer(func(ctx context.Context, address string) (net.Conn, error) {
		return (&net.Dialer{}).DialContext(ctx, "unix", address)
	})

	// Attempt to connect to the Kubelet's gRPC service using the socket path for Windows
	p.logger.Infow("Connecting to the Kubelet", "endpoint", pluginapi.KubeletSocketWindows)
	conn, err := grpc.DialContext(
		ctxConnect,
		pluginapi.KubeletSocketWindows,
		grpc.WithBlock(),
		grpc.WithTransportCredentials(insecure.NewCredentials()),
		dialler,
	)
	if err != nil {
		return fmt.Errorf("failed to connect to the Kubelet's gRPC service: %v", err)
	}
	defer conn.Close()

	// Prepare a registration request
	request := &pluginapi.RegisterRequest{
		Version:      pluginapi.Version,
		Endpoint:     filepath.Base(p.endpoint),
		ResourceName: p.resourceName,
	}

	// Set a 60 second timeout when attempting to register with the Kubelet
	ctxRegister, cancelRegister := context.WithTimeout(context.Background(), time.Minute)
	defer cancelRegister()

	// Create a registration client and attempt to send our registration request
	p.logger.Infow("Sending registration request to the Kubelet", "request", request)
	client := pluginapi.NewRegistrationClient(conn)
	if _, err := client.Register(ctxRegister, request); err != nil {
		return fmt.Errorf("failed to register the device plugin with the Kubelet: %v", err)
	}

	p.logger.Info("Successfully registered the device plugin with the Kubelet")
	return nil
}