2018-04-25 21:21:03 +00:00
|
|
|
// Package proxy contains logic for agent interaction with proxies,
|
|
|
|
// primarily "managed" proxies. Managed proxies are proxy processes for
|
|
|
|
// Connect-compatible endpoints that Consul owns and controls the lifecycle
|
|
|
|
// for.
|
|
|
|
//
|
|
|
|
// This package does not contain the built-in proxy for Connect. The source
|
|
|
|
// for that is available in the "connect/proxy" package.
|
2018-09-06 10:50:38 +00:00
|
|
|
package proxyprocess
|
2018-04-25 21:21:03 +00:00
|
|
|
|
2018-05-04 00:44:54 +00:00
|
|
|
import (
|
|
|
|
"github.com/hashicorp/consul/agent/structs"
|
|
|
|
)
|
|
|
|
|
2018-05-06 15:41:25 +00:00
|
|
|
const (
|
2018-06-06 20:04:19 +00:00
|
|
|
// EnvProxyID is the name of the environment variable that is set for
|
2018-05-06 15:41:25 +00:00
|
|
|
// managed proxies containing the proxy service ID. This is required along
|
|
|
|
// with the token to make API requests related to the proxy.
|
2018-06-06 20:04:19 +00:00
|
|
|
EnvProxyID = "CONNECT_PROXY_ID"
|
2018-05-06 15:41:25 +00:00
|
|
|
|
|
|
|
// EnvProxyToken is the name of the environment variable that is passed
|
|
|
|
// to managed proxies containing the proxy token.
|
|
|
|
EnvProxyToken = "CONNECT_PROXY_TOKEN"
|
2018-09-27 14:00:51 +00:00
|
|
|
|
|
|
|
// EnvSidecarFor is the name of the environment variable that is set for
|
|
|
|
// sidecar proxies containing the service ID of their target on the local
|
|
|
|
// agent
|
|
|
|
EnvSidecarFor = "CONNECT_SIDECAR_FOR"
|
2018-05-06 15:41:25 +00:00
|
|
|
)
|
2018-04-27 04:11:56 +00:00
|
|
|
|
|
|
|
// Proxy is the interface implemented by all types of managed proxies.
|
|
|
|
//
|
|
|
|
// Calls to all the functions on this interface must be concurrency safe.
|
|
|
|
// Please read the documentation carefully on top of each function for expected
|
|
|
|
// behavior.
|
2018-05-04 00:44:54 +00:00
|
|
|
//
|
|
|
|
// Whenever a new proxy type is implemented, please also update proxyExecMode
|
|
|
|
// and newProxyFromMode and newProxy to support the new proxy.
|
2018-04-27 04:11:56 +00:00
|
|
|
type Proxy interface {
|
|
|
|
// Start starts the proxy. If an error is returned then the managed
|
|
|
|
// proxy registration is rejected. Therefore, this should only fail if
|
|
|
|
// the configuration of the proxy itself is irrecoverable, and should
|
|
|
|
// retry starting for other failures.
|
2018-04-27 17:44:16 +00:00
|
|
|
//
|
|
|
|
// Starting an already-started proxy should not return an error.
|
2018-04-27 04:11:56 +00:00
|
|
|
Start() error
|
|
|
|
|
2018-04-27 17:44:16 +00:00
|
|
|
// Stop stops the proxy and disallows it from ever being started again.
|
2018-06-05 12:22:32 +00:00
|
|
|
// This should also clean up any resources used by this Proxy.
|
2018-04-27 17:44:16 +00:00
|
|
|
//
|
|
|
|
// If the proxy is not started yet, this should not return an error, but
|
|
|
|
// it should disallow Start from working again. If the proxy is already
|
|
|
|
// stopped, this should not return an error.
|
2018-04-27 04:11:56 +00:00
|
|
|
Stop() error
|
2018-05-02 18:02:58 +00:00
|
|
|
|
2018-06-05 12:22:32 +00:00
|
|
|
// Close should clean up any resources associated with this proxy but
|
|
|
|
// keep it running in the background. Only one of Close or Stop can be
|
|
|
|
// called.
|
|
|
|
Close() error
|
|
|
|
|
2018-05-02 18:02:58 +00:00
|
|
|
// Equal returns true if the argument is equal to the proxy being called.
|
|
|
|
// This is called by the manager to determine if a change in configuration
|
|
|
|
// results in a proxy that needs to be restarted or not. If Equal returns
|
|
|
|
// false, then the manager will stop the old proxy and start the new one.
|
|
|
|
// If Equal returns true, the old proxy will remain running and the new
|
|
|
|
// one will be ignored.
|
|
|
|
Equal(Proxy) bool
|
2018-05-03 22:46:00 +00:00
|
|
|
|
|
|
|
// MarshalSnapshot returns the state that will be stored in a snapshot
|
|
|
|
// so that Consul can recover the proxy process after a restart. The
|
|
|
|
// result should only contain primitive values and containers (lists/maps).
|
|
|
|
//
|
2018-05-06 15:41:25 +00:00
|
|
|
// MarshalSnapshot does NOT need to store the following fields, since they
|
|
|
|
// are part of the manager snapshot and will be automatically restored
|
|
|
|
// for any proxies: proxy ID.
|
|
|
|
//
|
2018-05-03 22:46:00 +00:00
|
|
|
// UnmarshalSnapshot is called to restore the receiving Proxy from its
|
2019-03-06 17:13:28 +00:00
|
|
|
// marshaled state. If UnmarshalSnapshot returns an error, the snapshot
|
|
|
|
// is ignored and the marshaled snapshot will be lost. The manager will
|
2018-05-03 22:46:00 +00:00
|
|
|
// log.
|
|
|
|
//
|
|
|
|
// This should save/restore enough state to be able to regain management
|
|
|
|
// of a proxy process as well as to perform the Equal method above. The
|
|
|
|
// Equal method will be called when a local state sync happens to determine
|
|
|
|
// if the recovered process should be restarted or not.
|
|
|
|
MarshalSnapshot() map[string]interface{}
|
|
|
|
UnmarshalSnapshot(map[string]interface{}) error
|
2018-04-27 04:11:56 +00:00
|
|
|
}
|
2018-05-04 00:44:54 +00:00
|
|
|
|
|
|
|
// proxyExecMode returns the ProxyExecMode for a Proxy instance.
|
|
|
|
func proxyExecMode(p Proxy) structs.ProxyExecMode {
|
|
|
|
switch p.(type) {
|
|
|
|
case *Daemon:
|
|
|
|
return structs.ProxyExecModeDaemon
|
|
|
|
|
|
|
|
case *Noop:
|
|
|
|
return structs.ProxyExecModeTest
|
|
|
|
|
|
|
|
default:
|
|
|
|
return structs.ProxyExecModeUnspecified
|
|
|
|
}
|
|
|
|
}
|