-
Notifications
You must be signed in to change notification settings - Fork 4.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
connect: expose an API endpoint to compile the discovery chain
- Loading branch information
Showing
10 changed files
with
183 additions
and
111 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package consul | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
|
||
metrics "github.com/armon/go-metrics" | ||
"github.com/hashicorp/consul/acl" | ||
"github.com/hashicorp/consul/agent/consul/discoverychain" | ||
"github.com/hashicorp/consul/agent/consul/state" | ||
"github.com/hashicorp/consul/agent/structs" | ||
memdb "github.com/hashicorp/go-memdb" | ||
) | ||
|
||
type DiscoveryChain struct { | ||
srv *Server | ||
} | ||
|
||
func (c *DiscoveryChain) Get(args *structs.DiscoveryChainRequest, reply *structs.DiscoveryChainResponse) error { | ||
if done, err := c.srv.forward("DiscoveryChain.Get", args, args, reply); done { | ||
return err | ||
} | ||
defer metrics.MeasureSince([]string{"discoverychain", "get"}, time.Now()) | ||
|
||
// Fetch the ACL token, if any. | ||
rule, err := c.srv.ResolveToken(args.Token) | ||
if err != nil { | ||
return err | ||
} | ||
if rule != nil && !rule.ServiceRead(args.Name) { | ||
return acl.ErrPermissionDenied | ||
} | ||
|
||
if args.Name == "" { | ||
return fmt.Errorf("Must provide service name") | ||
} | ||
|
||
evalDC := args.EvaluateInDatacenter | ||
if evalDC == "" { | ||
evalDC = c.srv.config.Datacenter | ||
} | ||
|
||
evalNS := args.EvaluateInNamespace | ||
if evalNS == "" { | ||
// TODO(namespaces) pull from something else? | ||
evalNS = "default" | ||
} | ||
|
||
return c.srv.blockingQuery( | ||
&args.QueryOptions, | ||
&reply.QueryMeta, | ||
func(ws memdb.WatchSet, state *state.Store) error { | ||
index, entries, err := state.ReadDiscoveryChainConfigEntries(ws, args.Name) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// Then we compile it into something useful. | ||
chain, err := discoverychain.Compile(discoverychain.CompileRequest{ | ||
ServiceName: args.Name, | ||
CurrentNamespace: evalNS, | ||
CurrentDatacenter: evalDC, | ||
OverrideMeshGateway: args.OverrideMeshGateway, | ||
OverrideProtocol: args.OverrideProtocol, | ||
OverrideConnectTimeout: args.OverrideConnectTimeout, | ||
Entries: entries, | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
reply.Index = index | ||
reply.Entries = entries.Flatten() | ||
reply.Chain = chain | ||
|
||
return nil | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package agent | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"strings" | ||
"time" | ||
|
||
"github.com/hashicorp/consul/agent/structs" | ||
) | ||
|
||
func (s *HTTPServer) ConnectDiscoveryChainGet(resp http.ResponseWriter, req *http.Request) (interface{}, error) { | ||
var args structs.DiscoveryChainRequest | ||
if done := s.parse(resp, req, &args.Datacenter, &args.QueryOptions); done { | ||
return nil, nil | ||
} | ||
|
||
args.Name = strings.TrimPrefix(req.URL.Path, "/v1/connect/discovery-chain/") | ||
if args.Name == "" { | ||
resp.WriteHeader(http.StatusBadRequest) | ||
fmt.Fprint(resp, "Missing chain name") | ||
return nil, nil | ||
} | ||
|
||
args.EvaluateInDatacenter = req.URL.Query().Get("eval_dc") | ||
// TODO(namespaces): args.EvaluateInNamespace = req.URL.Query().Get("eval_namespace") | ||
|
||
overrideMeshGatewayMode := req.URL.Query().Get("override_mesh_gateway_mode") | ||
if overrideMeshGatewayMode != "" { | ||
mode, err := structs.ValidateMeshGatewayMode(overrideMeshGatewayMode) | ||
if err != nil { | ||
resp.WriteHeader(http.StatusBadRequest) | ||
fmt.Fprint(resp, "Invalid override_mesh_gateway_mode parameter") | ||
return nil, nil | ||
} | ||
args.OverrideMeshGateway.Mode = mode | ||
} | ||
|
||
args.OverrideProtocol = req.URL.Query().Get("override_protocol") | ||
overrideTimeoutString := req.URL.Query().Get("override_connect_timeout") | ||
if overrideTimeoutString != "" { | ||
d, err := time.ParseDuration(overrideTimeoutString) | ||
if err != nil { | ||
resp.WriteHeader(http.StatusBadRequest) | ||
fmt.Fprint(resp, "Invalid override_connect_timeout parameter") | ||
return nil, nil | ||
} | ||
args.OverrideConnectTimeout = d | ||
} | ||
|
||
// Make the RPC request | ||
var out structs.DiscoveryChainResponse | ||
defer setMeta(resp, &out.QueryMeta) | ||
|
||
if err := s.agent.RPC("DiscoveryChain.Get", &args, &out); err != nil { | ||
return nil, err | ||
} | ||
|
||
apiOut := apiDiscoveryChainResponse{ | ||
Chain: out.Chain, | ||
Entries: out.Entries, | ||
} | ||
|
||
return apiOut, nil | ||
} | ||
|
||
type apiDiscoveryChainResponse struct { | ||
Chain *structs.CompiledDiscoveryChain | ||
Entries []structs.ConfigEntry | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters