@@ -19,6 +19,7 @@ package server
1919import (
2020 "context"
2121 "encoding/json"
22+ "errors"
2223 "fmt"
2324 "io"
2425 "net/http"
@@ -139,20 +140,9 @@ func NewCRIService(config criconfig.Config, client *containerd.Client, nri *nri.
139140 return nil , fmt .Errorf ("failed to find snapshotter %q" , config .ContainerdConfig .Snapshotter )
140141 }
141142
142- imageFSPath := imageFSPath (config .ContainerdRootDir , config .ContainerdConfig .Snapshotter )
143- log .L .Infof ("Get image filesystem path %q" , imageFSPath )
144-
145- // TODO: expose this as a separate containerd plugin.
146- imageService , err := images .NewService (config , imageFSPath , client )
147- if err != nil {
148- return nil , fmt .Errorf ("unable to create CRI image service: %w" , err )
149- }
150-
151143 c := & criService {
152- imageService : imageService ,
153144 config : config ,
154145 client : client ,
155- imageFSPath : imageFSPath ,
156146 os : osinterface.RealOS {},
157147 sandboxStore : sandboxstore .NewStore (labels ),
158148 containerStore : containerstore .NewStore (labels ),
@@ -165,6 +155,23 @@ func NewCRIService(config criconfig.Config, client *containerd.Client, nri *nri.
165155 // TODO: figure out a proper channel size.
166156 c .containerEventsChan = make (chan runtime.ContainerEventResponse , 1000 )
167157
158+ if client .SnapshotService (c .config .ContainerdConfig .Snapshotter ) == nil {
159+ return nil , fmt .Errorf ("failed to find snapshotter %q" , c .config .ContainerdConfig .Snapshotter )
160+ }
161+
162+ c .imageFSPath = imageFSPath (
163+ config .ContainerdRootDir ,
164+ config .ContainerdConfig .Snapshotter ,
165+ client ,
166+ )
167+ log .L .Infof ("Get image filesystem path %q" , c .imageFSPath )
168+
169+ // TODO: expose this as a separate containerd plugin.
170+ c .imageService , err = images .NewService (config , c .imageFSPath , client )
171+ if err != nil {
172+ return nil , fmt .Errorf ("unable to create CRI image service: %w" , err )
173+ }
174+
168175 if err := c .initPlatform (); err != nil {
169176 return nil , fmt .Errorf ("initialize platform: %w" , err )
170177 }
@@ -201,7 +208,7 @@ func NewCRIService(config criconfig.Config, client *containerd.Client, nri *nri.
201208 }
202209
203210 // Load all sandbox controllers(pod sandbox controller and remote shim controller)
204- c .sandboxControllers [criconfig .ModePodSandbox ] = podsandbox .New (config , client , c .sandboxStore , c .os , c , imageService , c .baseOCISpecs )
211+ c .sandboxControllers [criconfig .ModePodSandbox ] = podsandbox .New (config , client , c .sandboxStore , c .os , c , c . imageService , c .baseOCISpecs )
205212 c .sandboxControllers [criconfig .ModeShim ] = client .SandboxController ()
206213
207214 c .nri = nri
@@ -349,8 +356,40 @@ func (c *criService) register(s *grpc.Server) error {
349356
350357// imageFSPath returns containerd image filesystem path.
351358// Note that if containerd changes directory layout, we also needs to change this.
352- func imageFSPath (rootDir , snapshotter string ) string {
353- return filepath .Join (rootDir , plugins .SnapshotPlugin .String ()+ "." + snapshotter )
359+ func imageFSPath (rootDir , snapshotter string , client * containerd.Client ) string {
360+ introspection := func () (string , error ) {
361+ filters := []string {fmt .Sprintf ("type==%s, id==%s" , plugins .SnapshotPlugin , snapshotter )}
362+ in := client .IntrospectionService ()
363+
364+ resp , err := in .Plugins (context .Background (), filters )
365+ if err != nil {
366+ return "" , err
367+ }
368+
369+ if len (resp .Plugins ) <= 0 {
370+ return "" , fmt .Errorf ("inspection service could not find snapshotter %s plugin" , snapshotter )
371+ }
372+
373+ sn := resp .Plugins [0 ]
374+ if root , ok := sn .Exports [plugins .SnapshotterRootDir ]; ok {
375+ return root , nil
376+ }
377+ return "" , errors .New ("snapshotter does not export root path" )
378+ }
379+
380+ var imageFSPath string
381+ path , err := introspection ()
382+ if err != nil {
383+ log .L .WithError (err ).WithField ("snapshotter" , snapshotter ).Warn ("snapshotter doesn't export root path" )
384+ imageFSPath = filepath .Join (
385+ rootDir ,
386+ plugins .SnapshotPlugin .String ()+ "." + snapshotter ,
387+ )
388+ } else {
389+ imageFSPath = path
390+ }
391+
392+ return imageFSPath
354393}
355394
356395func loadOCISpec (filename string ) (* oci.Spec , error ) {
0 commit comments