2023-03-28 22:48:58 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
2023-08-11 13:12:13 +00:00
|
|
|
// SPDX-License-Identifier: BUSL-1.1
|
2023-03-28 22:48:58 +00:00
|
|
|
|
2022-11-01 19:03:23 +00:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2023-01-20 22:02:44 +00:00
|
|
|
"io"
|
2023-01-11 21:34:27 +00:00
|
|
|
"path/filepath"
|
|
|
|
"strconv"
|
2022-11-01 19:03:23 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/testcontainers/testcontainers-go"
|
|
|
|
"github.com/testcontainers/testcontainers-go/wait"
|
|
|
|
|
2023-01-04 20:28:15 +00:00
|
|
|
"github.com/hashicorp/consul/api"
|
2023-01-11 21:34:27 +00:00
|
|
|
|
|
|
|
libcluster "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"
|
|
|
|
)
|
|
|
|
|
|
|
|
// gatewayContainer
|
|
|
|
type gatewayContainer struct {
|
2023-02-24 20:57:44 +00:00
|
|
|
ctx context.Context
|
|
|
|
container testcontainers.Container
|
|
|
|
ip string
|
|
|
|
port int
|
|
|
|
adminPort int
|
|
|
|
serviceName string
|
|
|
|
portMappings map[int]int
|
2022-11-01 19:03:23 +00:00
|
|
|
}
|
|
|
|
|
2023-01-11 21:34:27 +00:00
|
|
|
var _ Service = (*gatewayContainer)(nil)
|
|
|
|
|
2023-02-14 22:22:09 +00:00
|
|
|
func (g gatewayContainer) 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 gatewayContainer) Export(partition, peer string, client *api.Client) error {
|
|
|
|
return fmt.Errorf("gatewayContainer export unimplemented")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g gatewayContainer) GetAddr() (string, int) {
|
|
|
|
return g.ip, g.port
|
|
|
|
}
|
|
|
|
|
2023-02-22 15:22:25 +00:00
|
|
|
func (g gatewayContainer) GetAddrs() (string, []int) {
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
|
2023-01-20 22:02:44 +00:00
|
|
|
func (g gatewayContainer) GetLogs() (string, error) {
|
|
|
|
rc, err := g.container.Logs(context.Background())
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("could not get logs for gateway 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 gateway service %s: %w", g.GetServiceName(), err)
|
|
|
|
}
|
|
|
|
return string(out), nil
|
|
|
|
}
|
|
|
|
|
2022-11-01 19:03:23 +00:00
|
|
|
func (g gatewayContainer) 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 gatewayContainer) GetServiceName() string {
|
|
|
|
return g.serviceName
|
2023-01-11 21:34:27 +00:00
|
|
|
}
|
|
|
|
|
2022-11-01 19:03:23 +00:00
|
|
|
func (g gatewayContainer) Start() error {
|
|
|
|
if g.container == nil {
|
|
|
|
return fmt.Errorf("container has not been initialized")
|
|
|
|
}
|
|
|
|
return g.container.Start(context.Background())
|
|
|
|
}
|
|
|
|
|
2023-02-15 15:26:43 +00:00
|
|
|
func (g gatewayContainer) Stop() error {
|
|
|
|
if g.container == nil {
|
|
|
|
return fmt.Errorf("container has not been initialized")
|
|
|
|
}
|
|
|
|
return g.container.Stop(context.Background(), nil)
|
|
|
|
}
|
|
|
|
|
2022-11-01 19:03:23 +00:00
|
|
|
func (c gatewayContainer) Terminate() error {
|
2023-05-11 22:49:49 +00:00
|
|
|
return libcluster.TerminateContainer(c.ctx, c.container, true)
|
2022-11-01 19:03:23 +00:00
|
|
|
}
|
|
|
|
|
2023-01-20 22:02:44 +00:00
|
|
|
func (g gatewayContainer) GetAdminAddr() (string, int) {
|
|
|
|
return "localhost", g.adminPort
|
2023-01-04 20:28:15 +00:00
|
|
|
}
|
|
|
|
|
2023-02-24 20:57:44 +00:00
|
|
|
func (g gatewayContainer) GetPort(port int) (int, error) {
|
|
|
|
p, ok := g.portMappings[port]
|
|
|
|
if !ok {
|
|
|
|
return 0, fmt.Errorf("port does not exist")
|
|
|
|
}
|
|
|
|
return p, nil
|
|
|
|
}
|
|
|
|
|
2023-01-27 16:25:48 +00:00
|
|
|
func (g gatewayContainer) Restart() error {
|
2023-02-01 15:48:54 +00:00
|
|
|
_, err := g.container.State(g.ctx)
|
2023-01-27 16:25:48 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error get gateway state %s", err)
|
|
|
|
}
|
|
|
|
|
2023-02-13 19:09:12 +00:00
|
|
|
fmt.Printf("Stopping container: %s\n", g.GetName())
|
2023-02-01 15:48:54 +00:00
|
|
|
err = g.container.Stop(g.ctx, nil)
|
2023-01-27 16:25:48 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error stop gateway %s", err)
|
|
|
|
}
|
2023-02-13 19:09:12 +00:00
|
|
|
|
|
|
|
fmt.Printf("Starting container: %s\n", g.GetName())
|
2023-02-01 15:48:54 +00:00
|
|
|
err = g.container.Start(g.ctx)
|
2023-01-27 16:25:48 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error start gateway %s", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-02-01 15:48:54 +00:00
|
|
|
func (g gatewayContainer) GetStatus() (string, error) {
|
|
|
|
state, err := g.container.State(g.ctx)
|
|
|
|
return state.Status, err
|
|
|
|
}
|
|
|
|
|
2023-03-15 17:51:36 +00:00
|
|
|
type GatewayConfig struct {
|
|
|
|
Name string
|
|
|
|
Kind string
|
|
|
|
Namespace string
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewGatewayService(ctx context.Context, gwCfg GatewayConfig, node libcluster.Agent, ports ...int) (Service, error) {
|
2023-03-29 16:51:21 +00:00
|
|
|
return NewGatewayServiceReg(ctx, gwCfg, node, true, ports...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewGatewayServiceReg(ctx context.Context, gwCfg GatewayConfig, node libcluster.Agent, doRegister bool, ports ...int) (Service, 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-gateway-%s", node.GetDatacenter(), gwCfg.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-01-27 15:19:10 +00:00
|
|
|
adminPort, err := node.ClaimAdminPort()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-03-29 16:51:21 +00:00
|
|
|
cmd := []string{
|
|
|
|
"consul", "connect", "envoy",
|
|
|
|
fmt.Sprintf("-gateway=%s", gwCfg.Kind),
|
|
|
|
"-service", gwCfg.Name,
|
|
|
|
"-namespace", gwCfg.Namespace,
|
|
|
|
"-address", "{{ GetInterfaceIP \"eth0\" }}:8443",
|
|
|
|
"-admin-bind", fmt.Sprintf("0.0.0.0:%d", adminPort),
|
|
|
|
}
|
|
|
|
if doRegister {
|
|
|
|
cmd = append(cmd, "-register")
|
|
|
|
}
|
|
|
|
cmd = append(cmd, "--")
|
|
|
|
envoyArgs := []string{
|
|
|
|
"--log-level", envoyLogLevel,
|
|
|
|
}
|
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,
|
|
|
|
Env: make(map[string]string),
|
|
|
|
Cmd: append(cmd, envoyArgs...),
|
2022-11-01 19:03:23 +00:00
|
|
|
}
|
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 (
|
|
|
|
portStr = "8443"
|
|
|
|
adminPortStr = strconv.Itoa(adminPort)
|
|
|
|
)
|
|
|
|
|
2023-02-24 20:57:44 +00:00
|
|
|
extraPorts := []string{}
|
|
|
|
for _, port := range ports {
|
|
|
|
extraPorts = append(extraPorts, strconv.Itoa(port))
|
|
|
|
}
|
|
|
|
|
2023-05-11 22:49:49 +00:00
|
|
|
info, err := libcluster.LaunchContainerOnNode(ctx, node, req, append(
|
2023-02-24 20:57:44 +00:00
|
|
|
extraPorts,
|
2023-01-11 21:34:27 +00:00
|
|
|
portStr,
|
|
|
|
adminPortStr,
|
2023-02-24 20:57:44 +00:00
|
|
|
))
|
2023-01-11 21:34:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2022-11-01 19:03:23 +00:00
|
|
|
}
|
|
|
|
|
2023-02-24 20:57:44 +00:00
|
|
|
portMappings := make(map[int]int)
|
|
|
|
for _, port := range ports {
|
|
|
|
portMappings[port] = info.MappedPorts[strconv.Itoa(port)].Int()
|
|
|
|
}
|
|
|
|
|
2023-01-11 21:34:27 +00:00
|
|
|
out := &gatewayContainer{
|
2023-02-24 20:57:44 +00:00
|
|
|
ctx: ctx,
|
|
|
|
container: info.Container,
|
|
|
|
ip: info.IP,
|
|
|
|
port: info.MappedPorts[portStr].Int(),
|
|
|
|
adminPort: info.MappedPorts[adminPortStr].Int(),
|
2023-03-15 17:51:36 +00:00
|
|
|
serviceName: gwCfg.Name,
|
2023-02-24 20:57:44 +00:00
|
|
|
portMappings: portMappings,
|
2022-11-01 19:03:23 +00:00
|
|
|
}
|
|
|
|
|
2023-01-11 21:34:27 +00:00
|
|
|
return out, nil
|
2022-11-01 19:03:23 +00:00
|
|
|
}
|