@@ -23,6 +23,7 @@ import (
2323 "net/http"
2424 "os"
2525 "path/filepath"
26+ "sync"
2627 "time"
2728
2829 "github.com/containerd/containerd"
@@ -49,6 +50,9 @@ import (
4950 "github.com/containerd/containerd/pkg/registrar"
5051)
5152
53+ // defaultNetworkPlugin is used for the default CNI configuration
54+ const defaultNetworkPlugin = "default"
55+
5256// grpcServices are all the grpc services provided by cri containerd.
5357type grpcServices interface {
5458 runtime.RuntimeServiceServer
@@ -92,7 +96,7 @@ type criService struct {
9296 // snapshotStore stores information of all snapshots.
9397 snapshotStore * snapshotstore.Store
9498 // netPlugin is used to setup and teardown network when run/stop pod sandbox.
95- netPlugin cni.CNI
99+ netPlugin map [ string ] cni.CNI
96100 // client is an instance of the containerd client
97101 client * containerd.Client
98102 // streamServer is the streaming server serves container streaming request.
@@ -104,7 +108,7 @@ type criService struct {
104108 initialized atomic.Bool
105109 // cniNetConfMonitor is used to reload cni network conf if there is
106110 // any valid fs change events from cni network conf dir.
107- cniNetConfMonitor * cniNetConfSyncer
111+ cniNetConfMonitor map [ string ] * cniNetConfSyncer
108112 // baseOCISpecs contains cached OCI specs loaded via `Runtime.BaseRuntimeSpec`
109113 baseOCISpecs map [string ]* oci.Spec
110114 // allCaps is the list of the capabilities.
@@ -127,6 +131,7 @@ func NewCRIService(config criconfig.Config, client *containerd.Client) (CRIServi
127131 sandboxNameIndex : registrar .NewRegistrar (),
128132 containerNameIndex : registrar .NewRegistrar (),
129133 initialized : atomic .NewBool (false ),
134+ netPlugin : make (map [string ]cni.CNI ),
130135 }
131136
132137 if client .SnapshotService (c .config .ContainerdConfig .Snapshotter ) == nil {
@@ -148,9 +153,21 @@ func NewCRIService(config criconfig.Config, client *containerd.Client) (CRIServi
148153
149154 c .eventMonitor = newEventMonitor (c )
150155
151- c .cniNetConfMonitor , err = newCNINetConfSyncer (c .config .NetworkPluginConfDir , c .netPlugin , c .cniLoadOptions ())
152- if err != nil {
153- return nil , errors .Wrap (err , "failed to create cni conf monitor" )
156+ c .cniNetConfMonitor = make (map [string ]* cniNetConfSyncer )
157+ for name , i := range c .netPlugin {
158+ path := c .config .NetworkPluginConfDir
159+ if name != defaultNetworkPlugin {
160+ if rc , ok := c .config .Runtimes [name ]; ok {
161+ path = rc .NetworkPluginConfDir
162+ }
163+ }
164+ if path != "" {
165+ m , err := newCNINetConfSyncer (path , i , c .cniLoadOptions ())
166+ if err != nil {
167+ return nil , errors .Wrapf (err , "failed to create cni conf monitor for %s" , name )
168+ }
169+ c .cniNetConfMonitor [name ] = m
170+ }
154171 }
155172
156173 // Preload base OCI specs
@@ -200,12 +217,20 @@ func (c *criService) Run() error {
200217 )
201218 snapshotsSyncer .start ()
202219
203- // Start CNI network conf syncer
204- logrus .Info ("Start cni network conf syncer" )
205- cniNetConfMonitorErrCh := make (chan error , 1 )
220+ // Start CNI network conf syncers
221+ cniNetConfMonitorErrCh := make (chan error , len (c .cniNetConfMonitor ))
222+ var netSyncGroup sync.WaitGroup
223+ for name , h := range c .cniNetConfMonitor {
224+ netSyncGroup .Add (1 )
225+ logrus .Infof ("Start cni network conf syncer for %s" , name )
226+ go func (h * cniNetConfSyncer ) {
227+ cniNetConfMonitorErrCh <- h .syncLoop ()
228+ netSyncGroup .Done ()
229+ }(h )
230+ }
206231 go func () {
207- defer close ( cniNetConfMonitorErrCh )
208- cniNetConfMonitorErrCh <- c . cniNetConfMonitor . syncLoop ( )
232+ netSyncGroup . Wait ( )
233+ close ( cniNetConfMonitorErrCh )
209234 }()
210235
211236 // Start streaming server.
@@ -272,8 +297,10 @@ func (c *criService) Run() error {
272297// TODO(random-liu): Make close synchronous.
273298func (c * criService ) Close () error {
274299 logrus .Info ("Stop CRI service" )
275- if err := c .cniNetConfMonitor .stop (); err != nil {
276- logrus .WithError (err ).Error ("failed to stop cni network conf monitor" )
300+ for name , h := range c .cniNetConfMonitor {
301+ if err := h .stop (); err != nil {
302+ logrus .WithError (err ).Errorf ("failed to stop cni network conf monitor for %s" , name )
303+ }
277304 }
278305 c .eventMonitor .stop ()
279306 if err := c .streamServer .Stop (); err != nil {
0 commit comments