2023-08-11 13:12:13 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: BUSL-1.1
|
|
|
|
|
2023-07-17 22:15:22 +00:00
|
|
|
package tfgen
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/hashicorp/consul/testing/deployer/topology"
|
|
|
|
)
|
|
|
|
|
|
|
|
type terraformPod struct {
|
|
|
|
PodName string
|
|
|
|
Node *topology.Node
|
|
|
|
Ports []int
|
|
|
|
Labels map[string]string
|
|
|
|
TLSVolumeName string
|
|
|
|
DNSAddress string
|
|
|
|
DockerNetworkName string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *Generator) generateNodeContainers(
|
|
|
|
step Step,
|
|
|
|
cluster *topology.Cluster,
|
|
|
|
node *topology.Node,
|
|
|
|
) ([]Resource, error) {
|
|
|
|
if node.Disabled {
|
|
|
|
return nil, fmt.Errorf("cannot generate containers for a disabled node")
|
|
|
|
}
|
|
|
|
|
|
|
|
pod := terraformPod{
|
|
|
|
PodName: node.PodName(),
|
|
|
|
Node: node,
|
|
|
|
Labels: map[string]string{
|
|
|
|
"consulcluster-topology-id": g.topology.ID,
|
|
|
|
"consulcluster-cluster-name": node.Cluster,
|
|
|
|
},
|
|
|
|
TLSVolumeName: cluster.TLSVolumeName,
|
|
|
|
DNSAddress: "8.8.8.8",
|
|
|
|
}
|
|
|
|
|
|
|
|
cluster, ok := g.topology.Clusters[node.Cluster]
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("no such cluster: %s", node.Cluster)
|
|
|
|
}
|
|
|
|
|
|
|
|
net, ok := g.topology.Networks[cluster.NetworkName]
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("no local network: %s", cluster.NetworkName)
|
|
|
|
}
|
|
|
|
if net.DNSAddress != "" {
|
|
|
|
pod.DNSAddress = net.DNSAddress
|
|
|
|
}
|
|
|
|
pod.DockerNetworkName = net.DockerName
|
|
|
|
|
2023-09-06 23:46:34 +00:00
|
|
|
containers := []Resource{}
|
2023-07-17 22:15:22 +00:00
|
|
|
|
|
|
|
if node.IsAgent() {
|
|
|
|
switch {
|
|
|
|
case node.IsServer() && step.StartServers(),
|
|
|
|
!node.IsServer() && step.StartAgents():
|
2023-09-06 23:46:34 +00:00
|
|
|
containers = append(containers, Eval(tfConsulT, struct {
|
|
|
|
terraformPod
|
|
|
|
ImageResource string
|
|
|
|
HCL string
|
|
|
|
EnterpriseLicense string
|
|
|
|
}{
|
|
|
|
terraformPod: pod,
|
|
|
|
ImageResource: DockerImageResourceName(node.Images.Consul),
|
2023-11-02 19:25:48 +00:00
|
|
|
HCL: g.generateAgentHCL(node, cluster.EnableV2 && node.IsServer()),
|
2023-09-06 23:46:34 +00:00
|
|
|
EnterpriseLicense: g.license,
|
|
|
|
}))
|
2023-07-17 22:15:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-10 19:22:06 +00:00
|
|
|
wrkContainers := []Resource{}
|
|
|
|
for _, wrk := range node.SortedWorkloads() {
|
|
|
|
token := g.sec.ReadWorkloadToken(node.Cluster, wrk.ID)
|
2023-09-06 23:46:34 +00:00
|
|
|
switch {
|
2023-11-10 19:22:06 +00:00
|
|
|
case wrk.IsMeshGateway && !node.IsDataplane():
|
|
|
|
wrkContainers = append(wrkContainers, Eval(tfMeshGatewayT, struct {
|
2023-09-06 23:46:34 +00:00
|
|
|
terraformPod
|
|
|
|
ImageResource string
|
|
|
|
Enterprise bool
|
2023-11-10 19:22:06 +00:00
|
|
|
Workload *topology.Workload
|
2023-09-06 23:46:34 +00:00
|
|
|
Token string
|
|
|
|
}{
|
|
|
|
terraformPod: pod,
|
|
|
|
ImageResource: DockerImageResourceName(node.Images.EnvoyConsulImage()),
|
|
|
|
Enterprise: cluster.Enterprise,
|
2023-11-10 19:22:06 +00:00
|
|
|
Workload: wrk,
|
2023-09-06 23:46:34 +00:00
|
|
|
Token: token,
|
|
|
|
}))
|
2023-11-10 19:22:06 +00:00
|
|
|
case wrk.IsMeshGateway && node.IsDataplane():
|
|
|
|
wrkContainers = append(wrkContainers, Eval(tfMeshGatewayDataplaneT, &struct {
|
2023-09-06 23:46:34 +00:00
|
|
|
terraformPod
|
|
|
|
ImageResource string
|
|
|
|
Enterprise bool
|
2023-11-10 19:22:06 +00:00
|
|
|
Workload *topology.Workload
|
2023-09-06 23:46:34 +00:00
|
|
|
Token string
|
|
|
|
}{
|
|
|
|
terraformPod: pod,
|
|
|
|
ImageResource: DockerImageResourceName(node.Images.LocalDataplaneImage()),
|
|
|
|
Enterprise: cluster.Enterprise,
|
2023-11-10 19:22:06 +00:00
|
|
|
Workload: wrk,
|
2023-09-06 23:46:34 +00:00
|
|
|
Token: token,
|
|
|
|
}))
|
|
|
|
|
2023-11-10 19:22:06 +00:00
|
|
|
case !wrk.IsMeshGateway:
|
|
|
|
wrkContainers = append(wrkContainers, Eval(tfAppT, struct {
|
2023-09-06 23:46:34 +00:00
|
|
|
terraformPod
|
|
|
|
ImageResource string
|
2023-11-10 19:22:06 +00:00
|
|
|
Workload *topology.Workload
|
2023-09-06 23:46:34 +00:00
|
|
|
}{
|
|
|
|
terraformPod: pod,
|
2023-11-10 19:22:06 +00:00
|
|
|
ImageResource: DockerImageResourceName(wrk.Image),
|
|
|
|
Workload: wrk,
|
2023-09-06 23:46:34 +00:00
|
|
|
}))
|
|
|
|
|
2023-11-10 19:22:06 +00:00
|
|
|
if wrk.DisableServiceMesh {
|
2023-09-06 23:46:34 +00:00
|
|
|
break
|
2023-07-17 22:15:22 +00:00
|
|
|
}
|
|
|
|
|
2023-09-06 23:46:34 +00:00
|
|
|
tmpl := tfAppSidecarT
|
|
|
|
var img string
|
|
|
|
if node.IsDataplane() {
|
|
|
|
tmpl = tfAppDataplaneT
|
2023-11-10 19:22:06 +00:00
|
|
|
if wrk.EnableTransparentProxy {
|
2023-11-02 21:13:16 +00:00
|
|
|
img = DockerImageResourceName(node.Images.LocalDataplaneTProxyImage())
|
|
|
|
} else {
|
|
|
|
img = DockerImageResourceName(node.Images.LocalDataplaneImage())
|
|
|
|
}
|
2023-09-06 23:46:34 +00:00
|
|
|
} else {
|
|
|
|
img = DockerImageResourceName(node.Images.EnvoyConsulImage())
|
2023-07-17 22:15:22 +00:00
|
|
|
}
|
2023-11-10 19:22:06 +00:00
|
|
|
wrkContainers = append(wrkContainers, Eval(tmpl, struct {
|
2023-09-06 23:46:34 +00:00
|
|
|
terraformPod
|
|
|
|
ImageResource string
|
2023-11-10 19:22:06 +00:00
|
|
|
Workload *topology.Workload
|
2023-09-06 23:46:34 +00:00
|
|
|
Token string
|
|
|
|
Enterprise bool
|
|
|
|
}{
|
|
|
|
terraformPod: pod,
|
|
|
|
ImageResource: img,
|
2023-11-10 19:22:06 +00:00
|
|
|
Workload: wrk,
|
2023-09-06 23:46:34 +00:00
|
|
|
Token: token,
|
|
|
|
Enterprise: cluster.Enterprise,
|
|
|
|
}))
|
|
|
|
}
|
2023-07-17 22:15:22 +00:00
|
|
|
|
2023-09-06 23:46:34 +00:00
|
|
|
if step.StartServices() {
|
2023-11-10 19:22:06 +00:00
|
|
|
containers = append(containers, wrkContainers...)
|
2023-07-17 22:15:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait until the very end to render the pod so we know all of the ports.
|
|
|
|
pod.Ports = node.SortedPorts()
|
|
|
|
|
|
|
|
// pod placeholder container
|
|
|
|
containers = append(containers, Eval(tfPauseT, &pod))
|
|
|
|
|
|
|
|
return containers, nil
|
|
|
|
}
|