Skip to content

Commit

Permalink
pkg/adaptation: add support for extra ttrpc options.
Browse files Browse the repository at this point in the history
Add WithTTRPCOptions() which can be used to pass extra client
and server options to ttrpc.

Signed-off-by: Krisztian Litkey <krisztian.litkey@intel.com>
  • Loading branch information
klihub committed Jan 30, 2024
1 parent 6f5a4d2 commit c4e2f81
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
12 changes: 12 additions & 0 deletions pkg/adaptation/adaptation.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (

"github.com/containerd/nri/pkg/api"
"github.com/containerd/nri/pkg/log"
"github.com/containerd/ttrpc"
)

const (
Expand Down Expand Up @@ -60,6 +61,8 @@ type Adaptation struct {
dontListen bool
syncFn SyncFn
updateFn UpdateFn
clientOpts []ttrpc.ClientOpts
serverOpts []ttrpc.ServerOpt
listener net.Listener
plugins []*plugin
}
Expand Down Expand Up @@ -104,6 +107,15 @@ func WithDisabledExternalConnections() Option {
}
}

// WithTTRPCOptions sets extra client and server options to use for ttrpc.
func WithTTRPCOptions(clientOpts []ttrpc.ClientOpts, serverOpts []ttrpc.ServerOpt) Option {
return func(r *Adaptation) error {
r.clientOpts = append(r.clientOpts, clientOpts...)
r.serverOpts = append(r.serverOpts, serverOpts...)
return nil
}
}

// New creates a new NRI Runtime.
func New(name, version string, syncFn SyncFn, updateFn UpdateFn, opts ...Option) (*Adaptation, error) {
var err error
Expand Down
18 changes: 11 additions & 7 deletions pkg/adaptation/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,20 +198,24 @@ func (p *plugin) connect(conn stdnet.Conn) (retErr error) {
if err != nil {
return fmt.Errorf("failed to mux plugin connection for plugin %q: %w", p.name(), err)
}
rpcc := ttrpc.NewClient(pconn, ttrpc.WithOnClose(
func() {
log.Infof(noCtx, "connection to plugin %q closed", p.name())
close(p.closeC)
p.close()
}))

clientOpts := []ttrpc.ClientOpts{
ttrpc.WithOnClose(
func() {
log.Infof(noCtx, "connection to plugin %q closed", p.name())
close(p.closeC)
p.close()
}),
}
rpcc := ttrpc.NewClient(pconn, append(clientOpts, p.r.clientOpts...)...)
defer func() {
if retErr != nil {
rpcc.Close()
}
}()
stub := api.NewPluginClient(rpcc)

rpcs, err := ttrpc.NewServer()
rpcs, err := ttrpc.NewServer(p.r.serverOpts...)
if err != nil {
return fmt.Errorf("failed to create ttrpc server for plugin %q: %w", p.name(), err)
}
Expand Down

0 comments on commit c4e2f81

Please sign in to comment.