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
|
|
|
|
2023-01-11 21:34:27 +00:00
|
|
|
package cluster
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-07-21 01:30:22 +00:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2023-05-31 17:18:00 +00:00
|
|
|
"io"
|
2023-01-11 21:34:27 +00:00
|
|
|
|
2023-07-21 01:30:22 +00:00
|
|
|
jsonpatch "github.com/evanphx/json-patch"
|
2023-10-11 20:39:09 +00:00
|
|
|
agentconfig "github.com/hashicorp/consul/agent/config"
|
|
|
|
"github.com/hashicorp/consul/api"
|
|
|
|
"github.com/hashicorp/consul/lib/decode"
|
2023-07-21 01:30:22 +00:00
|
|
|
"github.com/hashicorp/hcl"
|
|
|
|
"github.com/mitchellh/mapstructure"
|
2023-01-11 21:34:27 +00:00
|
|
|
"github.com/testcontainers/testcontainers-go"
|
2023-06-16 20:29:50 +00:00
|
|
|
"google.golang.org/grpc"
|
2023-01-11 21:34:27 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/consul/test/integration/consul-container/libs/utils"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Agent represent a Consul agent abstraction
|
|
|
|
type Agent interface {
|
|
|
|
GetIP() string
|
|
|
|
GetClient() *api.Client
|
2023-02-07 19:13:19 +00:00
|
|
|
NewClient(string, bool) (*api.Client, error)
|
2023-01-11 21:34:27 +00:00
|
|
|
GetName() string
|
2023-02-07 19:13:19 +00:00
|
|
|
GetAgentName() string
|
2023-03-06 18:28:02 +00:00
|
|
|
GetPartition() string
|
2023-01-11 21:34:27 +00:00
|
|
|
GetPod() testcontainers.Container
|
2023-05-31 17:18:00 +00:00
|
|
|
Logs(context.Context) (io.ReadCloser, error)
|
2023-01-27 15:19:10 +00:00
|
|
|
ClaimAdminPort() (int, error)
|
2023-01-11 21:34:27 +00:00
|
|
|
GetConfig() Config
|
|
|
|
GetInfo() AgentInfo
|
|
|
|
GetDatacenter() string
|
2023-02-24 20:57:44 +00:00
|
|
|
GetNetwork() string
|
2023-01-11 21:34:27 +00:00
|
|
|
IsServer() bool
|
|
|
|
RegisterTermination(func() error)
|
|
|
|
Terminate() error
|
2023-01-30 14:49:52 +00:00
|
|
|
TerminateAndRetainPod(bool) error
|
2023-01-11 21:34:27 +00:00
|
|
|
Upgrade(ctx context.Context, config Config) error
|
2023-02-07 19:13:19 +00:00
|
|
|
Exec(ctx context.Context, cmd []string) (string, error)
|
2023-01-11 21:34:27 +00:00
|
|
|
DataDir() string
|
2023-06-16 20:29:50 +00:00
|
|
|
GetGRPCConn() *grpc.ClientConn
|
2023-10-03 14:55:31 +00:00
|
|
|
GetAPIClientConfig() api.Config
|
2023-01-11 21:34:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Config is a set of configurations required to create a Agent
|
|
|
|
//
|
|
|
|
// Constructed by (Builder).ToAgentConfig()
|
|
|
|
type Config struct {
|
2023-04-12 22:00:56 +00:00
|
|
|
// NodeName is set for the consul agent name and container name
|
|
|
|
// Equivalent to the -node command-line flag.
|
2023-07-04 15:09:17 +00:00
|
|
|
// If empty, a random name will be generated
|
2023-04-12 22:00:56 +00:00
|
|
|
NodeName string
|
|
|
|
// NodeID is used to configure node_id in agent config file
|
|
|
|
// Equivalent to the -node-id command-line flag.
|
2023-07-04 15:09:17 +00:00
|
|
|
// If empty, a random name will be generated
|
2023-04-12 22:00:56 +00:00
|
|
|
NodeID string
|
|
|
|
|
|
|
|
// ExternalDataDir is data directory to copy consul data from, if set.
|
|
|
|
// This directory contains subdirectories like raft, serf, services
|
|
|
|
ExternalDataDir string
|
|
|
|
|
2023-01-11 21:34:27 +00:00
|
|
|
ScratchDir string
|
|
|
|
CertVolume string
|
|
|
|
CACert string
|
|
|
|
JSON string
|
|
|
|
ConfigBuilder *ConfigBuilder
|
|
|
|
Image string
|
|
|
|
Version string
|
|
|
|
Cmd []string
|
2023-01-19 15:43:33 +00:00
|
|
|
LogConsumer testcontainers.LogConsumer
|
2023-01-11 21:34:27 +00:00
|
|
|
|
|
|
|
// service defaults
|
|
|
|
UseAPIWithTLS bool // TODO
|
|
|
|
UseGRPCWithTLS bool
|
2023-02-07 19:13:19 +00:00
|
|
|
|
2023-10-11 20:39:09 +00:00
|
|
|
ACLEnabled bool
|
|
|
|
TokenBootstrap string
|
2023-01-11 21:34:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Config) DockerImage() string {
|
|
|
|
return utils.DockerImage(c.Image, c.Version)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clone copies everything. It is the caller's job to replace fields that
|
|
|
|
// should be unique.
|
|
|
|
func (c Config) Clone() Config {
|
|
|
|
c2 := c
|
|
|
|
if c.Cmd != nil {
|
2023-07-04 15:09:17 +00:00
|
|
|
copy(c2.Cmd, c.Cmd)
|
2023-01-11 21:34:27 +00:00
|
|
|
}
|
|
|
|
return c2
|
|
|
|
}
|
|
|
|
|
2023-07-21 01:30:22 +00:00
|
|
|
type decodeTarget struct {
|
|
|
|
agentconfig.Config `mapstructure:",squash"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// MutatebyAgentConfig mutates config by applying the fields in the input hclConfig
|
|
|
|
// Note that the precedence order is config > hclConfig, because user provider hclConfig
|
|
|
|
// may not work with the testing environment, e.g., data dir, agent name, etc.
|
|
|
|
// Currently only hcl config is allowed
|
|
|
|
func (c *Config) MutatebyAgentConfig(hclConfig string) error {
|
|
|
|
rawConfigJson, err := convertHcl2Json(hclConfig)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error converting to Json: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Merge 2 json
|
|
|
|
mergedConfigJosn, err := jsonpatch.MergePatch([]byte(rawConfigJson), []byte(c.JSON))
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error merging configurations: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
c.JSON = string(mergedConfigJosn)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-01-11 21:34:27 +00:00
|
|
|
// TODO: refactor away
|
|
|
|
type AgentInfo struct {
|
|
|
|
CACertFile string
|
|
|
|
UseTLSForAPI bool
|
|
|
|
UseTLSForGRPC bool
|
2023-04-18 13:49:53 +00:00
|
|
|
DebugURI string
|
2023-01-11 21:34:27 +00:00
|
|
|
}
|
2023-07-21 01:30:22 +00:00
|
|
|
|
|
|
|
func convertHcl2Json(in string) (string, error) {
|
|
|
|
var raw map[string]interface{}
|
|
|
|
err := hcl.Decode(&raw, in)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
var target decodeTarget
|
|
|
|
var md mapstructure.Metadata
|
|
|
|
d, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{
|
|
|
|
DecodeHook: mapstructure.ComposeDecodeHookFunc(
|
|
|
|
// decode.HookWeakDecodeFromSlice is only necessary when reading from
|
|
|
|
// an HCL config file. In the future we could omit it when reading from
|
|
|
|
// JSON configs. It is left here for now to maintain backwards compat
|
|
|
|
// for the unlikely scenario that someone is using malformed JSON configs
|
|
|
|
// and expecting this behaviour to correct their config.
|
|
|
|
decode.HookWeakDecodeFromSlice,
|
|
|
|
decode.HookTranslateKeys,
|
|
|
|
),
|
|
|
|
Metadata: &md,
|
|
|
|
Result: &target,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
if err := d.Decode(raw); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
rawjson, err := json.MarshalIndent(target, "", " ")
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return string(rawjson), nil
|
|
|
|
}
|