services.go 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /*
  2. Copyright The containerd Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package containerd
  14. import (
  15. "fmt"
  16. containersapi "github.com/containerd/containerd/api/services/containers/v1"
  17. "github.com/containerd/containerd/api/services/diff/v1"
  18. imagesapi "github.com/containerd/containerd/api/services/images/v1"
  19. introspectionapi "github.com/containerd/containerd/api/services/introspection/v1"
  20. namespacesapi "github.com/containerd/containerd/api/services/namespaces/v1"
  21. "github.com/containerd/containerd/api/services/tasks/v1"
  22. "github.com/containerd/containerd/containers"
  23. "github.com/containerd/containerd/content"
  24. "github.com/containerd/containerd/images"
  25. "github.com/containerd/containerd/leases"
  26. "github.com/containerd/containerd/namespaces"
  27. "github.com/containerd/containerd/plugin"
  28. "github.com/containerd/containerd/sandbox"
  29. srv "github.com/containerd/containerd/services"
  30. "github.com/containerd/containerd/services/introspection"
  31. "github.com/containerd/containerd/snapshots"
  32. )
  33. type services struct {
  34. contentStore content.Store
  35. imageStore images.Store
  36. containerStore containers.Store
  37. namespaceStore namespaces.Store
  38. snapshotters map[string]snapshots.Snapshotter
  39. taskService tasks.TasksClient
  40. diffService DiffService
  41. eventService EventService
  42. leasesService leases.Manager
  43. introspectionService introspection.Service
  44. sandboxStore sandbox.Store
  45. sandboxController sandbox.Controller
  46. }
  47. // ServicesOpt allows callers to set options on the services
  48. type ServicesOpt func(c *services)
  49. // WithContentStore sets the content store.
  50. func WithContentStore(contentStore content.Store) ServicesOpt {
  51. return func(s *services) {
  52. s.contentStore = contentStore
  53. }
  54. }
  55. // WithImageClient sets the image service to use using an images client.
  56. func WithImageClient(imageService imagesapi.ImagesClient) ServicesOpt {
  57. return func(s *services) {
  58. s.imageStore = NewImageStoreFromClient(imageService)
  59. }
  60. }
  61. // WithImageStore sets the image store.
  62. func WithImageStore(imageStore images.Store) ServicesOpt {
  63. return func(s *services) {
  64. s.imageStore = imageStore
  65. }
  66. }
  67. // WithSnapshotters sets the snapshotters.
  68. func WithSnapshotters(snapshotters map[string]snapshots.Snapshotter) ServicesOpt {
  69. return func(s *services) {
  70. s.snapshotters = make(map[string]snapshots.Snapshotter)
  71. for n, sn := range snapshotters {
  72. s.snapshotters[n] = sn
  73. }
  74. }
  75. }
  76. // WithContainerClient sets the container service to use using a containers client.
  77. func WithContainerClient(containerService containersapi.ContainersClient) ServicesOpt {
  78. return func(s *services) {
  79. s.containerStore = NewRemoteContainerStore(containerService)
  80. }
  81. }
  82. // WithContainerStore sets the container store.
  83. func WithContainerStore(containerStore containers.Store) ServicesOpt {
  84. return func(s *services) {
  85. s.containerStore = containerStore
  86. }
  87. }
  88. // WithTaskClient sets the task service to use from a tasks client.
  89. func WithTaskClient(taskService tasks.TasksClient) ServicesOpt {
  90. return func(s *services) {
  91. s.taskService = taskService
  92. }
  93. }
  94. // WithDiffClient sets the diff service to use from a diff client.
  95. func WithDiffClient(diffService diff.DiffClient) ServicesOpt {
  96. return func(s *services) {
  97. s.diffService = NewDiffServiceFromClient(diffService)
  98. }
  99. }
  100. // WithDiffService sets the diff store.
  101. func WithDiffService(diffService DiffService) ServicesOpt {
  102. return func(s *services) {
  103. s.diffService = diffService
  104. }
  105. }
  106. // WithEventService sets the event service.
  107. func WithEventService(eventService EventService) ServicesOpt {
  108. return func(s *services) {
  109. s.eventService = eventService
  110. }
  111. }
  112. // WithNamespaceClient sets the namespace service using a namespaces client.
  113. func WithNamespaceClient(namespaceService namespacesapi.NamespacesClient) ServicesOpt {
  114. return func(s *services) {
  115. s.namespaceStore = NewNamespaceStoreFromClient(namespaceService)
  116. }
  117. }
  118. // WithNamespaceService sets the namespace service.
  119. func WithNamespaceService(namespaceService namespaces.Store) ServicesOpt {
  120. return func(s *services) {
  121. s.namespaceStore = namespaceService
  122. }
  123. }
  124. // WithLeasesService sets the lease service.
  125. func WithLeasesService(leasesService leases.Manager) ServicesOpt {
  126. return func(s *services) {
  127. s.leasesService = leasesService
  128. }
  129. }
  130. // WithIntrospectionClient sets the introspection service using an introspection client.
  131. func WithIntrospectionClient(in introspectionapi.IntrospectionClient) ServicesOpt {
  132. return func(s *services) {
  133. s.introspectionService = introspection.NewIntrospectionServiceFromClient(in)
  134. }
  135. }
  136. // WithIntrospectionService sets the introspection service.
  137. func WithIntrospectionService(in introspection.Service) ServicesOpt {
  138. return func(s *services) {
  139. s.introspectionService = in
  140. }
  141. }
  142. // WithSandboxStore sets the sandbox store.
  143. func WithSandboxStore(client sandbox.Store) ServicesOpt {
  144. return func(s *services) {
  145. s.sandboxStore = client
  146. }
  147. }
  148. // WithSandboxController sets the sandbox controller.
  149. func WithSandboxController(client sandbox.Controller) ServicesOpt {
  150. return func(s *services) {
  151. s.sandboxController = client
  152. }
  153. }
  154. // WithInMemoryServices is suitable for cases when there is need to use containerd's client from
  155. // another (in-memory) containerd plugin (such as CRI).
  156. func WithInMemoryServices(ic *plugin.InitContext) ClientOpt {
  157. return func(c *clientOpts) error {
  158. var opts []ServicesOpt
  159. for t, fn := range map[plugin.Type]func(interface{}) ServicesOpt{
  160. plugin.EventPlugin: func(i interface{}) ServicesOpt {
  161. return WithEventService(i.(EventService))
  162. },
  163. plugin.LeasePlugin: func(i interface{}) ServicesOpt {
  164. return WithLeasesService(i.(leases.Manager))
  165. },
  166. plugin.SandboxStorePlugin: func(i interface{}) ServicesOpt {
  167. return WithSandboxStore(i.(sandbox.Store))
  168. },
  169. plugin.SandboxControllerPlugin: func(i interface{}) ServicesOpt {
  170. return WithSandboxController(i.(sandbox.Controller))
  171. },
  172. } {
  173. i, err := ic.Get(t)
  174. if err != nil {
  175. return fmt.Errorf("failed to get %q plugin: %w", t, err)
  176. }
  177. opts = append(opts, fn(i))
  178. }
  179. plugins, err := ic.GetByType(plugin.ServicePlugin)
  180. if err != nil {
  181. return fmt.Errorf("failed to get service plugin: %w", err)
  182. }
  183. for s, fn := range map[string]func(interface{}) ServicesOpt{
  184. srv.ContentService: func(s interface{}) ServicesOpt {
  185. return WithContentStore(s.(content.Store))
  186. },
  187. srv.ImagesService: func(s interface{}) ServicesOpt {
  188. return WithImageClient(s.(imagesapi.ImagesClient))
  189. },
  190. srv.SnapshotsService: func(s interface{}) ServicesOpt {
  191. return WithSnapshotters(s.(map[string]snapshots.Snapshotter))
  192. },
  193. srv.ContainersService: func(s interface{}) ServicesOpt {
  194. return WithContainerClient(s.(containersapi.ContainersClient))
  195. },
  196. srv.TasksService: func(s interface{}) ServicesOpt {
  197. return WithTaskClient(s.(tasks.TasksClient))
  198. },
  199. srv.DiffService: func(s interface{}) ServicesOpt {
  200. return WithDiffClient(s.(diff.DiffClient))
  201. },
  202. srv.NamespacesService: func(s interface{}) ServicesOpt {
  203. return WithNamespaceClient(s.(namespacesapi.NamespacesClient))
  204. },
  205. srv.IntrospectionService: func(s interface{}) ServicesOpt {
  206. return WithIntrospectionClient(s.(introspectionapi.IntrospectionClient))
  207. },
  208. } {
  209. p := plugins[s]
  210. if p == nil {
  211. return fmt.Errorf("service %q not found", s)
  212. }
  213. i, err := p.Instance()
  214. if err != nil {
  215. return fmt.Errorf("failed to get instance of service %q: %w", s, err)
  216. }
  217. if i == nil {
  218. return fmt.Errorf("instance of service %q not found", s)
  219. }
  220. opts = append(opts, fn(i))
  221. }
  222. c.services = &services{}
  223. for _, o := range opts {
  224. o(c.services)
  225. }
  226. return nil
  227. }
  228. }