plugin.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. package types
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. )
  6. // PluginInstallOptions holds parameters to install a plugin.
  7. type PluginInstallOptions struct {
  8. Disabled bool
  9. AcceptAllPermissions bool
  10. RegistryAuth string // RegistryAuth is the base64 encoded credentials for the registry
  11. PrivilegeFunc RequestPrivilegeFunc
  12. AcceptPermissionsFunc func(PluginPrivileges) (bool, error)
  13. }
  14. // PluginConfig represents the values of settings potentially modifiable by a user
  15. type PluginConfig struct {
  16. Mounts []PluginMount
  17. Env []string
  18. Args []string
  19. Devices []PluginDevice
  20. }
  21. // Plugin represents a Docker plugin for the remote API
  22. type Plugin struct {
  23. ID string `json:"Id,omitempty"`
  24. Name string
  25. Tag string
  26. // Enabled is true when the plugin is running, is false when the plugin is not running, only installed.
  27. Enabled bool
  28. Config PluginConfig
  29. Manifest PluginManifest
  30. }
  31. // PluginsListResponse contains the response for the remote API
  32. type PluginsListResponse []*Plugin
  33. const (
  34. authzDriver = "AuthzDriver"
  35. graphDriver = "GraphDriver"
  36. ipamDriver = "IpamDriver"
  37. networkDriver = "NetworkDriver"
  38. volumeDriver = "VolumeDriver"
  39. )
  40. // PluginInterfaceType represents a type that a plugin implements.
  41. type PluginInterfaceType struct {
  42. Prefix string // This is always "docker"
  43. Capability string // Capability should be validated against the above list.
  44. Version string // Plugin API version. Depends on the capability
  45. }
  46. // UnmarshalJSON implements json.Unmarshaler for PluginInterfaceType
  47. func (t *PluginInterfaceType) UnmarshalJSON(p []byte) error {
  48. versionIndex := len(p)
  49. prefixIndex := 0
  50. if len(p) < 2 || p[0] != '"' || p[len(p)-1] != '"' {
  51. return fmt.Errorf("%q is not a plugin interface type", p)
  52. }
  53. p = p[1 : len(p)-1]
  54. loop:
  55. for i, b := range p {
  56. switch b {
  57. case '.':
  58. prefixIndex = i
  59. case '/':
  60. versionIndex = i
  61. break loop
  62. }
  63. }
  64. t.Prefix = string(p[:prefixIndex])
  65. t.Capability = string(p[prefixIndex+1 : versionIndex])
  66. if versionIndex < len(p) {
  67. t.Version = string(p[versionIndex+1:])
  68. }
  69. return nil
  70. }
  71. // MarshalJSON implements json.Marshaler for PluginInterfaceType
  72. func (t *PluginInterfaceType) MarshalJSON() ([]byte, error) {
  73. return json.Marshal(t.String())
  74. }
  75. // String implements fmt.Stringer for PluginInterfaceType
  76. func (t PluginInterfaceType) String() string {
  77. return fmt.Sprintf("%s.%s/%s", t.Prefix, t.Capability, t.Version)
  78. }
  79. // PluginInterface describes the interface between Docker and plugin
  80. type PluginInterface struct {
  81. Types []PluginInterfaceType
  82. Socket string
  83. }
  84. // PluginSetting is to be embedded in other structs, if they are supposed to be
  85. // modifiable by the user.
  86. type PluginSetting struct {
  87. Name string
  88. Description string
  89. Settable []string
  90. }
  91. // PluginNetwork represents the network configuration for a plugin
  92. type PluginNetwork struct {
  93. Type string
  94. }
  95. // PluginMount represents the mount configuration for a plugin
  96. type PluginMount struct {
  97. PluginSetting
  98. Source *string
  99. Destination string
  100. Type string
  101. Options []string
  102. }
  103. // PluginEnv represents an environment variable for a plugin
  104. type PluginEnv struct {
  105. PluginSetting
  106. Value *string
  107. }
  108. // PluginArgs represents the command line arguments for a plugin
  109. type PluginArgs struct {
  110. PluginSetting
  111. Value []string
  112. }
  113. // PluginDevice represents a device for a plugin
  114. type PluginDevice struct {
  115. PluginSetting
  116. Path *string
  117. }
  118. // PluginUser represents the user for the plugin's process
  119. type PluginUser struct {
  120. UID uint32 `json:"Uid,omitempty"`
  121. GID uint32 `json:"Gid,omitempty"`
  122. }
  123. // PluginManifest represents the manifest of a plugin
  124. type PluginManifest struct {
  125. ManifestVersion string
  126. Description string
  127. Documentation string
  128. Interface PluginInterface
  129. Entrypoint []string
  130. Workdir string
  131. User PluginUser `json:",omitempty"`
  132. Network PluginNetwork
  133. Capabilities []string
  134. Mounts []PluginMount
  135. Devices []PluginDevice
  136. Env []PluginEnv
  137. Args PluginArgs
  138. }
  139. // PluginPrivilege describes a permission the user has to accept
  140. // upon installing a plugin.
  141. type PluginPrivilege struct {
  142. Name string
  143. Description string
  144. Value []string
  145. }
  146. // PluginPrivileges is a list of PluginPrivilege
  147. type PluginPrivileges []PluginPrivilege