2023-03-28 22:48:58 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2022-11-01 19:03:23 +00:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-02-24 20:57:44 +00:00
|
|
|
"errors"
|
2022-11-01 19:03:23 +00:00
|
|
|
"fmt"
|
2023-01-20 22:02:44 +00:00
|
|
|
"io"
|
2023-01-11 21:34:27 +00:00
|
|
|
"path/filepath"
|
|
|
|
"strconv"
|
2023-04-26 16:49:38 +00:00
|
|
|
"strings"
|
2022-11-01 19:03:23 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/testcontainers/testcontainers-go"
|
|
|
|
"github.com/testcontainers/testcontainers-go/wait"
|
|
|
|
|
2023-01-11 21:34:27 +00:00
|
|
|
"github.com/hashicorp/consul/api"
|
|
|
|
|
|
|
|
"github.com/hashicorp/consul/test/integration/consul-container/libs/cluster"
|
2022-11-01 19:03:23 +00:00
|
|
|
"github.com/hashicorp/consul/test/integration/consul-container/libs/utils"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ConnectContainer
|
|
|
|
type ConnectContainer struct {
|
2023-02-14 22:22:09 +00:00
|
|
|
ctx context.Context
|
|
|
|
container testcontainers.Container
|
|
|
|
ip string
|
2023-02-22 15:22:25 +00:00
|
|
|
appPort []int
|
2023-02-14 22:22:09 +00:00
|
|
|
externalAdminPort int
|
|
|
|
internalAdminPort int
|
|
|
|
mappedPublicPort int
|
|
|
|
serviceName string
|
2022-11-01 19:03:23 +00:00
|
|
|
}
|
|
|
|
|
2023-01-11 21:34:27 +00:00
|
|
|
var _ Service = (*ConnectContainer)(nil)
|
|
|
|
|
2023-02-14 22:22:09 +00:00
|
|
|
func (g ConnectContainer) Exec(ctx context.Context, cmd []string) (string, error) {
|
|
|
|
exitCode, reader, err := g.container.Exec(ctx, cmd)
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("exec with error %s", err)
|
|
|
|
}
|
|
|
|
if exitCode != 0 {
|
|
|
|
return "", fmt.Errorf("exec with exit code %d", exitCode)
|
|
|
|
}
|
|
|
|
buf, err := io.ReadAll(reader)
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("error reading from exec output: %w", err)
|
|
|
|
}
|
|
|
|
return string(buf), nil
|
|
|
|
}
|
|
|
|
|
2023-01-20 22:02:44 +00:00
|
|
|
func (g ConnectContainer) Export(partition, peer string, client *api.Client) error {
|
|
|
|
return fmt.Errorf("ConnectContainer export unimplemented")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g ConnectContainer) GetAddr() (string, int) {
|
2023-02-22 15:22:25 +00:00
|
|
|
return g.ip, g.appPort[0]
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g ConnectContainer) GetAddrs() (string, []int) {
|
2023-01-20 22:02:44 +00:00
|
|
|
return g.ip, g.appPort
|
|
|
|
}
|
|
|
|
|
2023-02-24 20:57:44 +00:00
|
|
|
func (g ConnectContainer) GetPort(port int) (int, error) {
|
|
|
|
return 0, errors.New("not implemented")
|
|
|
|
}
|
|
|
|
|
2023-01-27 16:25:48 +00:00
|
|
|
func (g ConnectContainer) Restart() error {
|
2023-02-06 18:09:44 +00:00
|
|
|
_, err := g.GetStatus()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error fetching sidecar container state %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Printf("Stopping container: %s\n", g.GetName())
|
|
|
|
err = g.container.Stop(g.ctx, nil)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error stopping sidecar container %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Printf("Starting container: %s\n", g.GetName())
|
|
|
|
err = g.container.Start(g.ctx)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error starting sidecar container %s", err)
|
|
|
|
}
|
|
|
|
return nil
|
2023-01-27 16:25:48 +00:00
|
|
|
}
|
|
|
|
|
2023-01-20 22:02:44 +00:00
|
|
|
func (g ConnectContainer) GetLogs() (string, error) {
|
|
|
|
rc, err := g.container.Logs(context.Background())
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("could not get logs for connect service %s: %w", g.GetServiceName(), err)
|
|
|
|
}
|
|
|
|
defer rc.Close()
|
|
|
|
|
|
|
|
out, err := io.ReadAll(rc)
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("could not read from logs for connect service %s: %w", g.GetServiceName(), err)
|
|
|
|
}
|
|
|
|
return string(out), nil
|
|
|
|
}
|
|
|
|
|
2022-11-01 19:03:23 +00:00
|
|
|
func (g ConnectContainer) GetName() string {
|
|
|
|
name, err := g.container.Name(g.ctx)
|
|
|
|
if err != nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return name
|
|
|
|
}
|
|
|
|
|
2023-01-20 22:02:44 +00:00
|
|
|
func (g ConnectContainer) GetServiceName() string {
|
|
|
|
return g.serviceName
|
2022-11-01 19:03:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (g ConnectContainer) Start() error {
|
|
|
|
if g.container == nil {
|
|
|
|
return fmt.Errorf("container has not been initialized")
|
|
|
|
}
|
2023-02-01 15:48:54 +00:00
|
|
|
return g.container.Start(g.ctx)
|
2022-11-01 19:03:23 +00:00
|
|
|
}
|
|
|
|
|
2023-02-15 15:26:43 +00:00
|
|
|
func (g ConnectContainer) Stop() error {
|
|
|
|
if g.container == nil {
|
|
|
|
return fmt.Errorf("container has not been initialized")
|
|
|
|
}
|
|
|
|
return g.container.Stop(context.Background(), nil)
|
|
|
|
}
|
|
|
|
|
2023-02-01 15:48:54 +00:00
|
|
|
func (g ConnectContainer) Terminate() error {
|
|
|
|
return cluster.TerminateContainer(g.ctx, g.container, true)
|
2022-11-01 19:03:23 +00:00
|
|
|
}
|
|
|
|
|
2023-02-14 22:22:09 +00:00
|
|
|
func (g ConnectContainer) GetInternalAdminAddr() (string, int) {
|
|
|
|
return "localhost", g.internalAdminPort
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetAdminAddr returns the external admin port
|
2023-01-20 22:02:44 +00:00
|
|
|
func (g ConnectContainer) GetAdminAddr() (string, int) {
|
2023-02-14 22:22:09 +00:00
|
|
|
return "localhost", g.externalAdminPort
|
2023-01-04 20:28:15 +00:00
|
|
|
}
|
|
|
|
|
2023-02-01 15:48:54 +00:00
|
|
|
func (g ConnectContainer) GetStatus() (string, error) {
|
|
|
|
state, err := g.container.State(g.ctx)
|
|
|
|
return state.Status, err
|
|
|
|
}
|
|
|
|
|
2023-03-15 17:51:36 +00:00
|
|
|
type SidecarConfig struct {
|
2023-04-26 16:49:38 +00:00
|
|
|
Name string
|
|
|
|
ServiceID string
|
|
|
|
Namespace string
|
|
|
|
EnableTProxy bool
|
2023-03-15 17:51:36 +00:00
|
|
|
}
|
|
|
|
|
2023-01-04 20:28:15 +00:00
|
|
|
// NewConnectService returns a container that runs envoy sidecar, launched by
|
|
|
|
// "consul connect envoy", for service name (serviceName) on the specified
|
2023-01-11 21:34:27 +00:00
|
|
|
// node. The container exposes port serviceBindPort and envoy admin port
|
|
|
|
// (19000) by mapping them onto host ports. The container's name has a prefix
|
2023-01-04 20:28:15 +00:00
|
|
|
// combining datacenter and name.
|
2023-03-15 17:51:36 +00:00
|
|
|
func NewConnectService(ctx context.Context, sidecarCfg SidecarConfig, serviceBindPorts []int, node cluster.Agent) (*ConnectContainer, error) {
|
2023-01-11 21:34:27 +00:00
|
|
|
nodeConfig := node.GetConfig()
|
|
|
|
if nodeConfig.ScratchDir == "" {
|
|
|
|
return nil, fmt.Errorf("node ScratchDir is required")
|
|
|
|
}
|
|
|
|
|
2023-03-15 17:51:36 +00:00
|
|
|
namePrefix := fmt.Sprintf("%s-service-connect-%s", node.GetDatacenter(), sidecarCfg.Name)
|
2022-11-01 19:03:23 +00:00
|
|
|
containerName := utils.RandName(namePrefix)
|
|
|
|
|
2023-01-17 22:16:55 +00:00
|
|
|
agentConfig := node.GetConfig()
|
2023-02-14 22:22:09 +00:00
|
|
|
internalAdminPort, err := node.ClaimAdminPort()
|
2023-01-27 15:19:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-11-01 19:03:23 +00:00
|
|
|
|
2023-05-11 22:49:49 +00:00
|
|
|
fmt.Println("agent image name", agentConfig.DockerImage())
|
|
|
|
imageVersion := utils.SideCarVersion(agentConfig.DockerImage())
|
2022-11-01 19:03:23 +00:00
|
|
|
req := testcontainers.ContainerRequest{
|
2023-05-11 22:49:49 +00:00
|
|
|
Image: fmt.Sprintf("consul-envoy:%s", imageVersion),
|
|
|
|
WaitingFor: wait.ForLog("").WithStartupTimeout(100 * time.Second),
|
|
|
|
AutoRemove: false,
|
|
|
|
Name: containerName,
|
2022-11-01 19:03:23 +00:00
|
|
|
Cmd: []string{
|
|
|
|
"consul", "connect", "envoy",
|
2023-03-15 17:51:36 +00:00
|
|
|
"-sidecar-for", sidecarCfg.ServiceID,
|
2023-02-14 22:22:09 +00:00
|
|
|
"-admin-bind", fmt.Sprintf("0.0.0.0:%d", internalAdminPort),
|
2023-03-15 17:51:36 +00:00
|
|
|
"-namespace", sidecarCfg.Namespace,
|
2022-11-01 19:03:23 +00:00
|
|
|
"--",
|
2023-01-11 21:34:27 +00:00
|
|
|
"--log-level", envoyLogLevel,
|
2022-11-01 19:03:23 +00:00
|
|
|
},
|
2023-01-11 21:34:27 +00:00
|
|
|
Env: make(map[string]string),
|
2022-11-01 19:03:23 +00:00
|
|
|
}
|
2023-01-11 21:34:27 +00:00
|
|
|
|
2023-04-26 16:49:38 +00:00
|
|
|
if sidecarCfg.EnableTProxy {
|
|
|
|
req.Entrypoint = []string{"/bin/tproxy-startup.sh"}
|
|
|
|
req.Env["REDIRECT_TRAFFIC_ARGS"] = strings.Join(
|
|
|
|
[]string{
|
|
|
|
"-exclude-inbound-port", fmt.Sprint(internalAdminPort),
|
|
|
|
"-exclude-inbound-port", "8300",
|
|
|
|
"-exclude-inbound-port", "8301",
|
|
|
|
"-exclude-inbound-port", "8302",
|
|
|
|
"-exclude-inbound-port", "8500",
|
|
|
|
"-exclude-inbound-port", "8502",
|
|
|
|
"-exclude-inbound-port", "8600",
|
|
|
|
"-consul-dns-ip", "127.0.0.1",
|
|
|
|
"-consul-dns-port", "8600",
|
|
|
|
"-proxy-id", fmt.Sprintf("%s-sidecar-proxy", sidecarCfg.ServiceID),
|
|
|
|
},
|
|
|
|
" ",
|
|
|
|
)
|
|
|
|
req.CapAdd = append(req.CapAdd, "NET_ADMIN")
|
|
|
|
}
|
|
|
|
|
2023-01-11 21:34:27 +00:00
|
|
|
nodeInfo := node.GetInfo()
|
|
|
|
if nodeInfo.UseTLSForAPI || nodeInfo.UseTLSForGRPC {
|
|
|
|
req.Mounts = append(req.Mounts, testcontainers.ContainerMount{
|
|
|
|
Source: testcontainers.DockerBindMountSource{
|
|
|
|
// See cluster.NewConsulContainer for this info
|
|
|
|
HostPath: filepath.Join(nodeConfig.ScratchDir, "ca.pem"),
|
|
|
|
},
|
|
|
|
Target: "/ca.pem",
|
|
|
|
ReadOnly: true,
|
|
|
|
})
|
2022-11-01 19:03:23 +00:00
|
|
|
}
|
|
|
|
|
2023-01-11 21:34:27 +00:00
|
|
|
if nodeInfo.UseTLSForAPI {
|
|
|
|
req.Env["CONSUL_HTTP_ADDR"] = fmt.Sprintf("https://127.0.0.1:%d", 8501)
|
|
|
|
req.Env["CONSUL_HTTP_SSL"] = "1"
|
|
|
|
req.Env["CONSUL_CACERT"] = "/ca.pem"
|
|
|
|
} else {
|
|
|
|
req.Env["CONSUL_HTTP_ADDR"] = fmt.Sprintf("http://127.0.0.1:%d", 8500)
|
2022-11-01 19:03:23 +00:00
|
|
|
}
|
|
|
|
|
2023-01-11 21:34:27 +00:00
|
|
|
if nodeInfo.UseTLSForGRPC {
|
|
|
|
req.Env["CONSUL_GRPC_ADDR"] = fmt.Sprintf("https://127.0.0.1:%d", 8503)
|
|
|
|
req.Env["CONSUL_GRPC_CACERT"] = "/ca.pem"
|
|
|
|
} else {
|
|
|
|
req.Env["CONSUL_GRPC_ADDR"] = fmt.Sprintf("http://127.0.0.1:%d", 8502)
|
2022-11-01 19:03:23 +00:00
|
|
|
}
|
2023-01-11 21:34:27 +00:00
|
|
|
|
|
|
|
var (
|
2023-02-22 15:22:25 +00:00
|
|
|
appPortStrs []string
|
2023-02-14 22:22:09 +00:00
|
|
|
adminPortStr = strconv.Itoa(internalAdminPort)
|
2023-01-11 21:34:27 +00:00
|
|
|
)
|
|
|
|
|
2023-02-22 15:22:25 +00:00
|
|
|
for _, port := range serviceBindPorts {
|
|
|
|
appPortStrs = append(appPortStrs, strconv.Itoa(port))
|
|
|
|
}
|
|
|
|
|
|
|
|
// expose the app ports and the envoy adminPortStr on the agent container
|
|
|
|
exposedPorts := make([]string, len(appPortStrs))
|
|
|
|
copy(exposedPorts, appPortStrs)
|
|
|
|
exposedPorts = append(exposedPorts, adminPortStr)
|
|
|
|
info, err := cluster.LaunchContainerOnNode(ctx, node, req, exposedPorts)
|
2022-11-01 19:03:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-01-11 21:34:27 +00:00
|
|
|
out := &ConnectContainer{
|
2023-02-14 22:22:09 +00:00
|
|
|
ctx: ctx,
|
|
|
|
container: info.Container,
|
|
|
|
ip: info.IP,
|
|
|
|
externalAdminPort: info.MappedPorts[adminPortStr].Int(),
|
|
|
|
internalAdminPort: internalAdminPort,
|
2023-03-15 17:51:36 +00:00
|
|
|
serviceName: sidecarCfg.Name,
|
2022-11-01 19:03:23 +00:00
|
|
|
}
|
|
|
|
|
2023-02-22 15:22:25 +00:00
|
|
|
for _, port := range appPortStrs {
|
|
|
|
out.appPort = append(out.appPort, info.MappedPorts[port].Int())
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Printf("NewConnectService: name %s, mapped App Port %d, service bind port %v\n",
|
2023-03-15 17:51:36 +00:00
|
|
|
sidecarCfg.ServiceID, out.appPort, serviceBindPorts)
|
2023-01-30 14:49:52 +00:00
|
|
|
fmt.Printf("NewConnectService sidecar: name %s, mapped admin port %d, admin port %d\n",
|
2023-03-15 17:51:36 +00:00
|
|
|
sidecarCfg.Name, out.externalAdminPort, internalAdminPort)
|
2023-01-04 20:28:15 +00:00
|
|
|
|
2023-01-11 21:34:27 +00:00
|
|
|
return out, nil
|
2022-11-01 19:03:23 +00:00
|
|
|
}
|