Skip to content

Commit

Permalink
Merge pull request containerd#10401 from samuelkarp/nri-panic
Browse files Browse the repository at this point in the history
cri: ensure NRI API never has nil CRI
  • Loading branch information
samuelkarp authored Jul 1, 2024
2 parents 37e0f54 + 10aec35 commit ebcbbe5
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 13 deletions.
6 changes: 3 additions & 3 deletions internal/cri/nri/nri_api_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,10 @@ type API struct {
nri nri.API
}

func NewAPI(nri nri.API) *API {
func NewAPI(nri nri.API, cri CRIImplementation) *API {
return &API{
nri: nri,
cri: cri,
}
}

Expand All @@ -58,12 +59,11 @@ func (a *API) IsDisabled() bool {

func (a *API) IsEnabled() bool { return !a.IsDisabled() }

func (a *API) Register(cri CRIImplementation) error {
func (a *API) Register() error {
if a.IsDisabled() {
return nil
}

a.cri = cri
nri.RegisterDomain(a)

return a.nri.Start()
Expand Down
4 changes: 2 additions & 2 deletions internal/cri/nri/nri_api_other.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ import (
type API struct {
}

func NewAPI(nri.API) *API {
func NewAPI(nri.API, CRIImplementation) *API {
return nil
}

func (a *API) Register(CRIImplementation) error {
func (a *API) Register() error {
return nil
}

Expand Down
8 changes: 5 additions & 3 deletions internal/cri/server/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import (
"k8s.io/kubelet/pkg/cri/streaming"

apitypes "github.com/containerd/containerd/api/types"

containerd "github.com/containerd/containerd/v2/client"
"github.com/containerd/containerd/v2/core/introspection"
_ "github.com/containerd/containerd/v2/core/runtime" // for typeurl init
Expand All @@ -50,6 +51,7 @@ import (
snapshotstore "github.com/containerd/containerd/v2/internal/cri/store/snapshot"
ctrdutil "github.com/containerd/containerd/v2/internal/cri/util"
"github.com/containerd/containerd/v2/internal/eventq"
nriservice "github.com/containerd/containerd/v2/internal/nri"
"github.com/containerd/containerd/v2/internal/registrar"
"github.com/containerd/containerd/v2/pkg/oci"
osinterface "github.com/containerd/containerd/v2/pkg/os"
Expand Down Expand Up @@ -164,7 +166,7 @@ type CRIServiceOptions struct {

StreamingConfig streaming.Config

NRI *nri.API
NRI nriservice.API

// SandboxControllers is a map of all the loaded sandbox controllers
SandboxControllers map[string]sandbox.Controller
Expand Down Expand Up @@ -236,7 +238,7 @@ func NewCRIService(options *CRIServiceOptions) (CRIService, runtime.RuntimeServi
}
}

c.nri = options.NRI
c.nri = nri.NewAPI(options.NRI, &criImplementation{c})

c.runtimeHandlers, err = c.introspectRuntimeHandlers(ctx)
if err != nil {
Expand Down Expand Up @@ -297,7 +299,7 @@ func (c *criService) Run(ready func()) error {
}()

// register CRI domain with NRI
if err := c.nri.Register(&criImplementation{c}); err != nil {
if err := c.nri.Register(); err != nil {
return fmt.Errorf("failed to set up NRI for CRI service: %w", err)
}

Expand Down
2 changes: 1 addition & 1 deletion internal/nri/nri.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ type API interface {
// IsEnabled returns true if the NRI interface is enabled and initialized.
IsEnabled() bool

// Start start the NRI interface, allowing external NRI plugins to
// Start starts the NRI interface, allowing external NRI plugins to
// connect, register, and hook themselves into the lifecycle events
// of pods and containers.
Start() error
Expand Down
6 changes: 2 additions & 4 deletions plugins/cri/cri.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ import (
criconfig "github.com/containerd/containerd/v2/internal/cri/config"
"github.com/containerd/containerd/v2/internal/cri/constants"
"github.com/containerd/containerd/v2/internal/cri/instrument"
"github.com/containerd/containerd/v2/internal/cri/nri"
"github.com/containerd/containerd/v2/internal/cri/server"
nriservice "github.com/containerd/containerd/v2/internal/nri"
"github.com/containerd/containerd/v2/plugins"
Expand Down Expand Up @@ -212,7 +211,7 @@ func (c criGRPCServerWithTCP) RegisterTCP(s *grpc.Server) error {
}

// Get the NRI plugin, and set up our NRI API for it.
func getNRIAPI(ic *plugin.InitContext) *nri.API {
func getNRIAPI(ic *plugin.InitContext) nriservice.API {
const (
pluginType = plugins.NRIApiPlugin
pluginName = "nri"
Expand All @@ -234,8 +233,7 @@ func getNRIAPI(ic *plugin.InitContext) *nri.API {
}

log.G(ctx).Info("using experimental NRI integration - disable nri plugin to prevent this")

return nri.NewAPI(api)
return api
}

func getSandboxControllers(ic *plugin.InitContext) (map[string]sandbox.Controller, error) {
Expand Down

0 comments on commit ebcbbe5

Please sign in to comment.