defs.go 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. package plugin // import "github.com/docker/docker/plugin"
  2. import (
  3. "strings"
  4. "sync"
  5. "github.com/docker/docker/pkg/plugins"
  6. v2 "github.com/docker/docker/plugin/v2"
  7. specs "github.com/opencontainers/runtime-spec/specs-go"
  8. )
  9. // Store manages the plugin inventory in memory and on-disk
  10. type Store struct {
  11. sync.RWMutex
  12. plugins map[string]*v2.Plugin
  13. specOpts map[string][]SpecOpt
  14. /* handlers are necessary for transition path of legacy plugins
  15. * to the new model. Legacy plugins use Handle() for registering an
  16. * activation callback.*/
  17. handlers map[string][]func(string, *plugins.Client)
  18. }
  19. // NewStore creates a Store.
  20. func NewStore() *Store {
  21. return &Store{
  22. plugins: make(map[string]*v2.Plugin),
  23. specOpts: make(map[string][]SpecOpt),
  24. handlers: make(map[string][]func(string, *plugins.Client)),
  25. }
  26. }
  27. // SpecOpt is used for subsystems that need to modify the runtime spec of a plugin
  28. type SpecOpt func(*specs.Spec)
  29. // CreateOpt is used to configure specific plugin details when created
  30. type CreateOpt func(p *v2.Plugin)
  31. // WithSwarmService is a CreateOpt that flags the passed in a plugin as a plugin
  32. // managed by swarm
  33. func WithSwarmService(id string) CreateOpt {
  34. return func(p *v2.Plugin) {
  35. p.SwarmServiceID = id
  36. }
  37. }
  38. // WithEnv is a CreateOpt that passes the user-provided environment variables
  39. // to the plugin container, de-duplicating variables with the same names case
  40. // sensitively and only appends valid key=value pairs
  41. func WithEnv(env []string) CreateOpt {
  42. return func(p *v2.Plugin) {
  43. effectiveEnv := make(map[string]string)
  44. for _, penv := range p.PluginObj.Config.Env {
  45. if penv.Value != nil {
  46. effectiveEnv[penv.Name] = *penv.Value
  47. }
  48. }
  49. for _, line := range env {
  50. if k, v, ok := strings.Cut(line, "="); ok {
  51. effectiveEnv[k] = v
  52. }
  53. }
  54. p.PluginObj.Settings.Env = make([]string, 0, len(effectiveEnv))
  55. for key, value := range effectiveEnv {
  56. p.PluginObj.Settings.Env = append(p.PluginObj.Settings.Env, key+"="+value)
  57. }
  58. }
  59. }
  60. // WithSpecMounts is a SpecOpt which appends the provided mounts to the runtime spec
  61. func WithSpecMounts(mounts []specs.Mount) SpecOpt {
  62. return func(s *specs.Spec) {
  63. s.Mounts = append(s.Mounts, mounts...)
  64. }
  65. }