github.com/hernad/nomad@v1.6.112/helper/pluginutils/loader/instance.go (about)

     1  // Copyright (c) HashiCorp, Inc.
     2  // SPDX-License-Identifier: MPL-2.0
     3  
     4  package loader
     5  
     6  import plugin "github.com/hashicorp/go-plugin"
     7  
     8  // PluginInstance wraps an instance of a plugin. If the plugin is external, it
     9  // provides methods to retrieve the ReattachConfig and to kill the plugin.
    10  type PluginInstance interface {
    11  	// Internal returns if the plugin is internal
    12  	Internal() bool
    13  
    14  	// Kill kills the plugin if it is external. It is safe to call on internal
    15  	// plugins.
    16  	Kill()
    17  
    18  	// ReattachConfig returns the ReattachConfig and whether the plugin is internal
    19  	// or not. If the second return value is false, no ReattachConfig is
    20  	// possible to return.
    21  	ReattachConfig() (config *plugin.ReattachConfig, canReattach bool)
    22  
    23  	// Plugin returns the wrapped plugin instance.
    24  	Plugin() interface{}
    25  
    26  	// Exited returns whether the plugin has exited
    27  	Exited() bool
    28  
    29  	// ApiVersion returns the API version to be used with the plugin
    30  	ApiVersion() string
    31  }
    32  
    33  // internalPluginInstance wraps an internal plugin
    34  type internalPluginInstance struct {
    35  	instance   interface{}
    36  	apiVersion string
    37  	killFn     func()
    38  }
    39  
    40  func (p *internalPluginInstance) Internal() bool { return true }
    41  func (p *internalPluginInstance) Kill()          { p.killFn() }
    42  
    43  func (p *internalPluginInstance) ReattachConfig() (*plugin.ReattachConfig, bool) { return nil, false }
    44  func (p *internalPluginInstance) Plugin() interface{}                            { return p.instance }
    45  func (p *internalPluginInstance) Exited() bool                                   { return false }
    46  func (p *internalPluginInstance) ApiVersion() string                             { return p.apiVersion }
    47  
    48  // externalPluginInstance wraps an external plugin
    49  type externalPluginInstance struct {
    50  	client     *plugin.Client
    51  	instance   interface{}
    52  	apiVersion string
    53  }
    54  
    55  func (p *externalPluginInstance) Internal() bool      { return false }
    56  func (p *externalPluginInstance) Plugin() interface{} { return p.instance }
    57  func (p *externalPluginInstance) Exited() bool        { return p.client.Exited() }
    58  func (p *externalPluginInstance) ApiVersion() string  { return p.apiVersion }
    59  
    60  func (p *externalPluginInstance) ReattachConfig() (*plugin.ReattachConfig, bool) {
    61  	return p.client.ReattachConfig(), true
    62  }
    63  
    64  func (p *externalPluginInstance) Kill() {
    65  	p.client.Kill()
    66  }