github.com/rish1988/moby@v25.0.2+incompatible/plugin/defs.go (about)

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