2022-04-25 14:41:36 +00:00
|
|
|
package node
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"strconv"
|
|
|
|
"time"
|
|
|
|
|
2022-06-28 14:50:13 +00:00
|
|
|
dockercontainer "github.com/docker/docker/api/types/container"
|
2022-04-25 14:41:36 +00:00
|
|
|
"github.com/docker/docker/pkg/ioutils"
|
|
|
|
"github.com/hashicorp/consul/api"
|
2022-06-24 15:26:17 +00:00
|
|
|
"github.com/hashicorp/hcl"
|
2022-04-25 14:41:36 +00:00
|
|
|
"github.com/testcontainers/testcontainers-go"
|
|
|
|
"github.com/testcontainers/testcontainers-go/wait"
|
|
|
|
|
2022-10-19 14:36:35 +00:00
|
|
|
"github.com/hashicorp/consul/test/integration/consul-container/libs/utils"
|
2022-04-25 14:41:36 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const bootLogLine = "Consul agent running"
|
|
|
|
const disableRYUKEnv = "TESTCONTAINERS_RYUK_DISABLED"
|
|
|
|
|
|
|
|
// consulContainerNode implements the Node interface by running a Consul node
|
|
|
|
// in a container.
|
|
|
|
type consulContainerNode struct {
|
|
|
|
ctx context.Context
|
|
|
|
client *api.Client
|
2022-06-28 14:50:13 +00:00
|
|
|
pod testcontainers.Container
|
2022-04-25 14:41:36 +00:00
|
|
|
container testcontainers.Container
|
|
|
|
ip string
|
|
|
|
port int
|
2022-05-18 19:52:26 +00:00
|
|
|
config Config
|
2022-06-28 14:50:13 +00:00
|
|
|
podReq testcontainers.ContainerRequest
|
|
|
|
consulReq testcontainers.ContainerRequest
|
2022-05-18 19:52:26 +00:00
|
|
|
dataDir string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *consulContainerNode) GetConfig() Config {
|
|
|
|
return c.config
|
|
|
|
}
|
|
|
|
|
2022-06-28 14:50:13 +00:00
|
|
|
func startContainer(ctx context.Context, req testcontainers.ContainerRequest) (testcontainers.Container, error) {
|
|
|
|
return testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
|
2022-05-18 19:52:26 +00:00
|
|
|
ContainerRequest: req,
|
|
|
|
Started: true,
|
|
|
|
})
|
2022-04-25 14:41:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewConsulContainer starts a Consul node in a container with the given config.
|
|
|
|
func NewConsulContainer(ctx context.Context, config Config) (Node, error) {
|
2022-04-25 15:58:29 +00:00
|
|
|
license, err := readLicense()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-04-25 14:41:36 +00:00
|
|
|
name := utils.RandName("consul-")
|
2022-05-18 19:52:26 +00:00
|
|
|
|
|
|
|
tmpDirData, err := ioutils.TempDir("", name)
|
2022-04-25 14:41:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-05-18 19:52:26 +00:00
|
|
|
err = os.Chmod(tmpDirData, 0777)
|
2022-04-25 14:41:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-05-18 19:52:26 +00:00
|
|
|
|
2022-06-24 15:26:17 +00:00
|
|
|
pc, err := readSomeConfigFileFields(config.HCL)
|
2022-04-25 14:41:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-06-24 15:26:17 +00:00
|
|
|
|
|
|
|
configFile, err := createConfigFile(config.HCL)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2022-04-25 14:41:36 +00:00
|
|
|
}
|
2022-06-24 15:26:17 +00:00
|
|
|
|
2022-06-28 14:50:13 +00:00
|
|
|
podReq, consulReq := newContainerRequest(config, name, configFile, tmpDirData, license)
|
|
|
|
|
|
|
|
podContainer, err := startContainer(ctx, podReq)
|
2022-04-25 14:41:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-06-28 14:50:13 +00:00
|
|
|
localIP, err := podContainer.Host(ctx)
|
|
|
|
if err != nil {
|
2022-06-24 15:26:17 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-06-28 14:50:13 +00:00
|
|
|
mappedPort, err := podContainer.MappedPort(ctx, "8500")
|
2022-04-25 14:41:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-06-28 14:50:13 +00:00
|
|
|
ip, err := podContainer.ContainerIP(ctx)
|
2022-04-25 14:41:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-06-28 14:50:13 +00:00
|
|
|
consulContainer, err := startContainer(ctx, consulReq)
|
2022-04-25 14:41:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-06-28 14:50:13 +00:00
|
|
|
if err := consulContainer.StartLogProducer(ctx); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
consulContainer.FollowOutput(&NodeLogConsumer{
|
|
|
|
Prefix: pc.NodeName,
|
|
|
|
})
|
|
|
|
|
2022-04-25 14:41:36 +00:00
|
|
|
uri := fmt.Sprintf("http://%s:%s", localIP, mappedPort.Port())
|
|
|
|
apiConfig := api.DefaultConfig()
|
|
|
|
apiConfig.Address = uri
|
2022-06-24 15:26:17 +00:00
|
|
|
apiClient, err := api.NewClient(apiConfig)
|
2022-04-25 14:41:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-06-24 15:26:17 +00:00
|
|
|
|
|
|
|
return &consulContainerNode{
|
|
|
|
config: config,
|
2022-06-28 14:50:13 +00:00
|
|
|
pod: podContainer,
|
|
|
|
container: consulContainer,
|
2022-06-24 15:26:17 +00:00
|
|
|
ip: ip,
|
|
|
|
port: mappedPort.Int(),
|
|
|
|
client: apiClient,
|
|
|
|
ctx: ctx,
|
2022-06-28 14:50:13 +00:00
|
|
|
podReq: podReq,
|
|
|
|
consulReq: consulReq,
|
2022-06-24 15:26:17 +00:00
|
|
|
dataDir: tmpDirData,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2022-06-28 14:50:13 +00:00
|
|
|
const pauseImage = "k8s.gcr.io/pause:3.3"
|
|
|
|
|
|
|
|
func newContainerRequest(config Config, name, configFile, dataDir, license string) (podRequest, consulRequest testcontainers.ContainerRequest) {
|
2022-06-24 15:26:17 +00:00
|
|
|
skipReaper := isRYUKDisabled()
|
|
|
|
|
2022-06-28 14:50:13 +00:00
|
|
|
pod := testcontainers.ContainerRequest{
|
|
|
|
Image: pauseImage,
|
2022-06-24 15:26:17 +00:00
|
|
|
AutoRemove: false,
|
2022-06-28 14:50:13 +00:00
|
|
|
Name: name + "-pod",
|
|
|
|
SkipReaper: skipReaper,
|
|
|
|
ExposedPorts: []string{"8500/tcp"},
|
|
|
|
}
|
|
|
|
|
|
|
|
app := testcontainers.ContainerRequest{
|
|
|
|
NetworkMode: dockercontainer.NetworkMode("container:" + name + "-pod"),
|
|
|
|
Image: consulImage + ":" + config.Version,
|
|
|
|
WaitingFor: wait.ForLog(bootLogLine).WithStartupTimeout(10 * time.Second),
|
|
|
|
AutoRemove: false,
|
|
|
|
Name: name,
|
2022-06-24 15:26:17 +00:00
|
|
|
Mounts: []testcontainers.ContainerMount{
|
|
|
|
{Source: testcontainers.DockerBindMountSource{HostPath: configFile}, Target: "/consul/config/config.hcl"},
|
|
|
|
{Source: testcontainers.DockerBindMountSource{HostPath: dataDir}, Target: "/consul/data"},
|
|
|
|
},
|
|
|
|
Cmd: config.Cmd,
|
|
|
|
SkipReaper: skipReaper,
|
|
|
|
Env: map[string]string{"CONSUL_LICENSE": license},
|
|
|
|
}
|
2022-06-28 14:50:13 +00:00
|
|
|
|
|
|
|
return pod, app
|
2022-04-25 14:41:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetClient returns an API client that can be used to communicate with the Node.
|
|
|
|
func (c *consulContainerNode) GetClient() *api.Client {
|
|
|
|
return c.client
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetAddr return the network address associated with the Node.
|
|
|
|
func (c *consulContainerNode) GetAddr() (string, int) {
|
|
|
|
return c.ip, c.port
|
|
|
|
}
|
|
|
|
|
2022-05-18 19:52:26 +00:00
|
|
|
func (c *consulContainerNode) Upgrade(ctx context.Context, config Config) error {
|
2022-06-24 15:26:17 +00:00
|
|
|
pc, err := readSomeConfigFileFields(config.HCL)
|
2022-05-18 19:52:26 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-06-24 15:26:17 +00:00
|
|
|
|
|
|
|
file, err := createConfigFile(config.HCL)
|
2022-05-18 19:52:26 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-06-24 15:26:17 +00:00
|
|
|
|
2022-06-28 14:50:13 +00:00
|
|
|
// We'll keep the same pod.
|
|
|
|
_, consulReq2 := newContainerRequest(
|
2022-06-24 15:26:17 +00:00
|
|
|
config,
|
2022-06-28 14:50:13 +00:00
|
|
|
c.consulReq.Name,
|
2022-06-24 15:26:17 +00:00
|
|
|
file,
|
|
|
|
c.dataDir,
|
|
|
|
"",
|
|
|
|
)
|
2022-06-28 14:50:13 +00:00
|
|
|
consulReq2.Env = c.consulReq.Env // copy license
|
2022-06-24 15:26:17 +00:00
|
|
|
|
|
|
|
_ = c.container.StopLogProducer()
|
|
|
|
if err := c.container.Terminate(ctx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-06-28 14:50:13 +00:00
|
|
|
c.consulReq = consulReq2
|
2022-06-24 15:26:17 +00:00
|
|
|
|
2022-06-28 14:50:13 +00:00
|
|
|
container, err := startContainer(ctx, c.consulReq)
|
2022-05-18 19:52:26 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-06-24 15:26:17 +00:00
|
|
|
if err := container.StartLogProducer(ctx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
container.FollowOutput(&NodeLogConsumer{
|
|
|
|
Prefix: pc.NodeName,
|
|
|
|
})
|
|
|
|
|
2022-05-18 19:52:26 +00:00
|
|
|
c.container = container
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-04-25 14:41:36 +00:00
|
|
|
// Terminate attempts to terminate the container. On failure, an error will be
|
|
|
|
// returned and the reaper process (RYUK) will handle cleanup.
|
|
|
|
func (c *consulContainerNode) Terminate() error {
|
2022-06-24 15:26:17 +00:00
|
|
|
if c.container == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
err := c.container.StopLogProducer()
|
|
|
|
|
|
|
|
if err1 := c.container.Terminate(c.ctx); err == nil {
|
|
|
|
err = err1
|
|
|
|
}
|
|
|
|
|
|
|
|
c.container = nil
|
|
|
|
|
|
|
|
return err
|
2022-04-25 14:41:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// isRYUKDisabled returns whether the reaper process (RYUK) has been disabled
|
|
|
|
// by an environment variable.
|
|
|
|
//
|
|
|
|
// https://github.com/testcontainers/moby-ryuk
|
|
|
|
func isRYUKDisabled() bool {
|
|
|
|
skipReaperStr := os.Getenv(disableRYUKEnv)
|
|
|
|
skipReaper, err := strconv.ParseBool(skipReaperStr)
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return skipReaper
|
|
|
|
}
|
2022-04-25 15:58:29 +00:00
|
|
|
|
|
|
|
func readLicense() (string, error) {
|
|
|
|
license := os.Getenv("CONSUL_LICENSE")
|
|
|
|
if license == "" {
|
|
|
|
licensePath := os.Getenv("CONSUL_LICENSE_PATH")
|
|
|
|
if licensePath != "" {
|
|
|
|
licenseBytes, err := os.ReadFile(licensePath)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
license = string(licenseBytes)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return license, nil
|
|
|
|
}
|
2022-05-18 19:52:26 +00:00
|
|
|
|
|
|
|
func createConfigFile(HCL string) (string, error) {
|
|
|
|
tmpDir, err := ioutils.TempDir("", "consul-container-test-config")
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
err = os.Chmod(tmpDir, 0777)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
err = os.Mkdir(tmpDir+"/config", 0777)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
configFile := tmpDir + "/config/config.hcl"
|
|
|
|
err = os.WriteFile(configFile, []byte(HCL), 0644)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return configFile, nil
|
|
|
|
}
|
2022-06-24 15:26:17 +00:00
|
|
|
|
|
|
|
type parsedConfig struct {
|
|
|
|
NodeName string `hcl:"node_name"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func readSomeConfigFileFields(HCL string) (parsedConfig, error) {
|
|
|
|
var pc parsedConfig
|
|
|
|
if err := hcl.Decode(&pc, HCL); err != nil {
|
|
|
|
return pc, fmt.Errorf("Failed to parse config file: %w", err)
|
|
|
|
}
|
|
|
|
return pc, nil
|
|
|
|
}
|