defs.go 2.2 KB

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