github.imxd.top/hashicorp/consul@v1.4.5/agent/proxyprocess/proxy.go (about) 1 // Package proxy contains logic for agent interaction with proxies, 2 // primarily "managed" proxies. Managed proxies are proxy processes for 3 // Connect-compatible endpoints that Consul owns and controls the lifecycle 4 // for. 5 // 6 // This package does not contain the built-in proxy for Connect. The source 7 // for that is available in the "connect/proxy" package. 8 package proxyprocess 9 10 import ( 11 "github.com/hashicorp/consul/agent/structs" 12 ) 13 14 const ( 15 // EnvProxyID is the name of the environment variable that is set for 16 // managed proxies containing the proxy service ID. This is required along 17 // with the token to make API requests related to the proxy. 18 EnvProxyID = "CONNECT_PROXY_ID" 19 20 // EnvProxyToken is the name of the environment variable that is passed 21 // to managed proxies containing the proxy token. 22 EnvProxyToken = "CONNECT_PROXY_TOKEN" 23 24 // EnvSidecarFor is the name of the environment variable that is set for 25 // sidecar proxies containing the service ID of their target on the local 26 // agent 27 EnvSidecarFor = "CONNECT_SIDECAR_FOR" 28 ) 29 30 // Proxy is the interface implemented by all types of managed proxies. 31 // 32 // Calls to all the functions on this interface must be concurrency safe. 33 // Please read the documentation carefully on top of each function for expected 34 // behavior. 35 // 36 // Whenever a new proxy type is implemented, please also update proxyExecMode 37 // and newProxyFromMode and newProxy to support the new proxy. 38 type Proxy interface { 39 // Start starts the proxy. If an error is returned then the managed 40 // proxy registration is rejected. Therefore, this should only fail if 41 // the configuration of the proxy itself is irrecoverable, and should 42 // retry starting for other failures. 43 // 44 // Starting an already-started proxy should not return an error. 45 Start() error 46 47 // Stop stops the proxy and disallows it from ever being started again. 48 // This should also clean up any resources used by this Proxy. 49 // 50 // If the proxy is not started yet, this should not return an error, but 51 // it should disallow Start from working again. If the proxy is already 52 // stopped, this should not return an error. 53 Stop() error 54 55 // Close should clean up any resources associated with this proxy but 56 // keep it running in the background. Only one of Close or Stop can be 57 // called. 58 Close() error 59 60 // Equal returns true if the argument is equal to the proxy being called. 61 // This is called by the manager to determine if a change in configuration 62 // results in a proxy that needs to be restarted or not. If Equal returns 63 // false, then the manager will stop the old proxy and start the new one. 64 // If Equal returns true, the old proxy will remain running and the new 65 // one will be ignored. 66 Equal(Proxy) bool 67 68 // MarshalSnapshot returns the state that will be stored in a snapshot 69 // so that Consul can recover the proxy process after a restart. The 70 // result should only contain primitive values and containers (lists/maps). 71 // 72 // MarshalSnapshot does NOT need to store the following fields, since they 73 // are part of the manager snapshot and will be automatically restored 74 // for any proxies: proxy ID. 75 // 76 // UnmarshalSnapshot is called to restore the receiving Proxy from its 77 // marshaled state. If UnmarshalSnapshot returns an error, the snapshot 78 // is ignored and the marshaled snapshot will be lost. The manager will 79 // log. 80 // 81 // This should save/restore enough state to be able to regain management 82 // of a proxy process as well as to perform the Equal method above. The 83 // Equal method will be called when a local state sync happens to determine 84 // if the recovered process should be restarted or not. 85 MarshalSnapshot() map[string]interface{} 86 UnmarshalSnapshot(map[string]interface{}) error 87 } 88 89 // proxyExecMode returns the ProxyExecMode for a Proxy instance. 90 func proxyExecMode(p Proxy) structs.ProxyExecMode { 91 switch p.(type) { 92 case *Daemon: 93 return structs.ProxyExecModeDaemon 94 95 case *Noop: 96 return structs.ProxyExecModeTest 97 98 default: 99 return structs.ProxyExecModeUnspecified 100 } 101 }