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 sprawl
|
|
|
|
|
|
|
|
import (
|
2023-11-02 19:25:48 +00:00
|
|
|
"context"
|
2023-07-17 22:15:22 +00:00
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
|
2023-11-02 19:25:48 +00:00
|
|
|
"google.golang.org/protobuf/proto"
|
|
|
|
"google.golang.org/protobuf/types/known/anypb"
|
2023-07-17 22:15:22 +00:00
|
|
|
|
2024-05-21 19:52:19 +00:00
|
|
|
"github.com/hashicorp/consul/api"
|
|
|
|
"github.com/hashicorp/consul/proto-public/pbresource"
|
2023-07-17 22:15:22 +00:00
|
|
|
"github.com/hashicorp/consul/testing/deployer/topology"
|
|
|
|
"github.com/hashicorp/consul/testing/deployer/util"
|
|
|
|
)
|
|
|
|
|
2023-11-07 22:29:13 +00:00
|
|
|
// registerAllServicesToAgents registers services in agent-ful mode
|
2023-07-17 22:15:22 +00:00
|
|
|
func (s *Sprawl) registerAllServicesToAgents() error {
|
|
|
|
for _, cluster := range s.topology.Clusters {
|
|
|
|
if err := s.registerServicesToAgents(cluster); err != nil {
|
|
|
|
return fmt.Errorf("registerServicesToAgents[%s]: %w", cluster.Name, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-11-07 22:29:13 +00:00
|
|
|
func (s *Sprawl) syncAllServicesForDataplaneInstances() error {
|
2023-07-17 22:15:22 +00:00
|
|
|
for _, cluster := range s.topology.Clusters {
|
2023-11-10 19:22:06 +00:00
|
|
|
if err := s.syncWorkloadsForDataplaneInstances(cluster); err != nil {
|
|
|
|
return fmt.Errorf("syncWorkloadsForDataplaneInstances[%s]: %w", cluster.Name, err)
|
2023-07-17 22:15:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Sprawl) registerServicesToAgents(cluster *topology.Cluster) error {
|
|
|
|
for _, node := range cluster.Nodes {
|
2023-11-10 19:22:06 +00:00
|
|
|
if !node.RunsWorkloads() || len(node.Workloads) == 0 || node.Disabled {
|
2023-07-17 22:15:22 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if !node.IsAgent() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
agentClient, err := util.ProxyAPIClient(
|
|
|
|
node.LocalProxyPort(),
|
|
|
|
node.LocalAddress(),
|
|
|
|
8500,
|
|
|
|
"", /*token will be in request*/
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-11-10 19:22:06 +00:00
|
|
|
for _, wrk := range node.Workloads {
|
|
|
|
if err := s.registerAgentService(agentClient, cluster, node, wrk); err != nil {
|
2023-07-17 22:15:22 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Sprawl) registerAgentService(
|
|
|
|
agentClient *api.Client,
|
|
|
|
cluster *topology.Cluster,
|
|
|
|
node *topology.Node,
|
2023-11-10 19:22:06 +00:00
|
|
|
wrk *topology.Workload,
|
2023-07-17 22:15:22 +00:00
|
|
|
) error {
|
|
|
|
if !node.IsAgent() {
|
|
|
|
panic("called wrong method type")
|
|
|
|
}
|
|
|
|
|
2023-11-10 19:22:06 +00:00
|
|
|
if wrk.IsMeshGateway {
|
2023-11-07 22:29:13 +00:00
|
|
|
return nil // handled at startup time for agent-ful, but won't be for agent-less
|
2023-07-17 22:15:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
logger = s.logger.With("cluster", cluster.Name)
|
|
|
|
)
|
|
|
|
|
|
|
|
reg := &api.AgentServiceRegistration{
|
2023-11-10 19:22:06 +00:00
|
|
|
ID: wrk.ID.Name,
|
|
|
|
Name: wrk.ID.Name,
|
|
|
|
Port: wrk.Port,
|
|
|
|
Meta: wrk.Meta,
|
2023-07-17 22:15:22 +00:00
|
|
|
}
|
|
|
|
if cluster.Enterprise {
|
2023-11-10 19:22:06 +00:00
|
|
|
reg.Namespace = wrk.ID.Namespace
|
|
|
|
reg.Partition = wrk.ID.Partition
|
2023-07-17 22:15:22 +00:00
|
|
|
}
|
|
|
|
|
2023-11-10 19:22:06 +00:00
|
|
|
if !wrk.DisableServiceMesh {
|
2023-07-17 22:15:22 +00:00
|
|
|
var upstreams []api.Upstream
|
2024-05-21 19:52:19 +00:00
|
|
|
for _, us := range wrk.Upstreams {
|
2023-07-17 22:15:22 +00:00
|
|
|
uAPI := api.Upstream{
|
2024-05-21 19:52:19 +00:00
|
|
|
DestinationPeer: us.Peer,
|
|
|
|
DestinationName: us.ID.Name,
|
|
|
|
LocalBindAddress: us.LocalAddress,
|
|
|
|
LocalBindPort: us.LocalPort,
|
2023-07-17 22:15:22 +00:00
|
|
|
// Config map[string]interface{} `json:",omitempty" bexpr:"-"`
|
|
|
|
// MeshGateway MeshGatewayConfig `json:",omitempty"`
|
|
|
|
}
|
|
|
|
if cluster.Enterprise {
|
2024-05-21 19:52:19 +00:00
|
|
|
uAPI.DestinationNamespace = us.ID.Namespace
|
|
|
|
if us.Peer == "" {
|
|
|
|
uAPI.DestinationPartition = us.ID.Partition
|
2023-07-17 22:15:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
upstreams = append(upstreams, uAPI)
|
|
|
|
}
|
|
|
|
reg.Connect = &api.AgentServiceConnect{
|
|
|
|
SidecarService: &api.AgentServiceRegistration{
|
|
|
|
Proxy: &api.AgentServiceConnectProxyConfig{
|
|
|
|
Upstreams: upstreams,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch {
|
2023-11-10 19:22:06 +00:00
|
|
|
case wrk.CheckTCP != "":
|
2023-07-17 22:15:22 +00:00
|
|
|
chk := &api.AgentServiceCheck{
|
|
|
|
Name: "up",
|
2023-11-10 19:22:06 +00:00
|
|
|
TCP: wrk.CheckTCP,
|
2023-07-17 22:15:22 +00:00
|
|
|
Interval: "5s",
|
|
|
|
Timeout: "1s",
|
|
|
|
}
|
|
|
|
reg.Checks = append(reg.Checks, chk)
|
2023-11-10 19:22:06 +00:00
|
|
|
case wrk.CheckHTTP != "":
|
2023-07-17 22:15:22 +00:00
|
|
|
chk := &api.AgentServiceCheck{
|
|
|
|
Name: "up",
|
2023-11-10 19:22:06 +00:00
|
|
|
HTTP: wrk.CheckHTTP,
|
2023-07-17 22:15:22 +00:00
|
|
|
Method: "GET",
|
|
|
|
Interval: "5s",
|
|
|
|
Timeout: "1s",
|
|
|
|
}
|
|
|
|
reg.Checks = append(reg.Checks, chk)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Switch token for every request.
|
|
|
|
hdr := make(http.Header)
|
2023-11-10 19:22:06 +00:00
|
|
|
hdr.Set("X-Consul-Token", s.secrets.ReadWorkloadToken(cluster.Name, wrk.ID))
|
2023-07-17 22:15:22 +00:00
|
|
|
agentClient.SetHeaders(hdr)
|
|
|
|
|
|
|
|
RETRY:
|
|
|
|
if err := agentClient.Agent().ServiceRegister(reg); err != nil {
|
|
|
|
if isACLNotFound(err) {
|
|
|
|
time.Sleep(50 * time.Millisecond)
|
|
|
|
goto RETRY
|
|
|
|
}
|
2023-11-10 19:22:06 +00:00
|
|
|
return fmt.Errorf("failed to register workload %q to node %q: %w", wrk.ID, node.ID(), err)
|
2023-07-17 22:15:22 +00:00
|
|
|
}
|
|
|
|
|
2023-11-10 19:22:06 +00:00
|
|
|
logger.Debug("registered workload to client agent",
|
|
|
|
"workload", wrk.ID.Name,
|
2023-07-17 22:15:22 +00:00
|
|
|
"node", node.Name,
|
2023-11-10 19:22:06 +00:00
|
|
|
"namespace", wrk.ID.Namespace,
|
|
|
|
"partition", wrk.ID.Partition,
|
2023-07-17 22:15:22 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-11-10 19:22:06 +00:00
|
|
|
// syncWorkloadsForDataplaneInstances register/deregister services in the given cluster
|
|
|
|
func (s *Sprawl) syncWorkloadsForDataplaneInstances(cluster *topology.Cluster) error {
|
|
|
|
// registerWorkloadToNode is called when node is not disabled
|
|
|
|
registerWorkloadToNode := func(node *topology.Node, wrk *topology.Workload) error {
|
2024-05-21 19:52:19 +00:00
|
|
|
if err := s.registerCatalogServiceV1(cluster, node, wrk); err != nil {
|
|
|
|
return fmt.Errorf("error registering service: %w", err)
|
|
|
|
}
|
|
|
|
if !wrk.DisableServiceMesh {
|
|
|
|
if err := s.registerCatalogSidecarServiceV1(cluster, node, wrk); err != nil {
|
|
|
|
return fmt.Errorf("error registering sidecar service: %w", err)
|
2023-07-17 22:15:22 +00:00
|
|
|
}
|
|
|
|
}
|
2023-11-07 22:29:13 +00:00
|
|
|
return nil
|
2023-07-17 22:15:22 +00:00
|
|
|
}
|
|
|
|
|
2023-11-10 19:22:06 +00:00
|
|
|
// deregisterWorkloadFromNode is called when node is disabled
|
|
|
|
deregisterWorkloadFromNode := func(node *topology.Node, wrk *topology.Workload) error {
|
2024-05-21 19:52:19 +00:00
|
|
|
if err := s.deregisterCatalogServiceV1(cluster, node, wrk); err != nil {
|
|
|
|
return fmt.Errorf("error deregistering service: %w", err)
|
|
|
|
}
|
|
|
|
if !wrk.DisableServiceMesh {
|
|
|
|
if err := s.deregisterCatalogSidecarServiceV1(cluster, node, wrk); err != nil {
|
|
|
|
return fmt.Errorf("error deregistering sidecar service: %w", err)
|
2023-11-07 22:29:13 +00:00
|
|
|
}
|
2023-11-02 19:25:48 +00:00
|
|
|
}
|
2023-11-07 22:29:13 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-11-10 19:22:06 +00:00
|
|
|
var syncWorkload func(node *topology.Node, wrk *topology.Workload) error
|
2023-11-07 22:29:13 +00:00
|
|
|
|
|
|
|
for _, node := range cluster.Nodes {
|
2023-11-10 19:22:06 +00:00
|
|
|
if !node.RunsWorkloads() || len(node.Workloads) == 0 {
|
2023-11-07 22:29:13 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if !node.IsDataplane() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Register virtual node service first if node is not disabled
|
|
|
|
if !node.Disabled {
|
|
|
|
if err := s.registerCatalogNode(cluster, node); err != nil {
|
|
|
|
return fmt.Errorf("error registering virtual node: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Register/deregister services on the node
|
2023-11-10 19:22:06 +00:00
|
|
|
for _, wrk := range node.Workloads {
|
2023-11-07 22:29:13 +00:00
|
|
|
if !node.Disabled {
|
2023-11-10 19:22:06 +00:00
|
|
|
syncWorkload = registerWorkloadToNode
|
2023-11-07 22:29:13 +00:00
|
|
|
} else {
|
2023-11-10 19:22:06 +00:00
|
|
|
syncWorkload = deregisterWorkloadFromNode
|
2023-11-07 22:29:13 +00:00
|
|
|
}
|
2023-11-15 16:26:26 +00:00
|
|
|
if err := syncWorkload(node, wrk); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-11-07 22:29:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Deregister the virtual node if node is disabled
|
|
|
|
if node.Disabled {
|
|
|
|
if err := s.deregisterCatalogNode(cluster, node); err != nil {
|
|
|
|
return fmt.Errorf("error deregistering virtual node: %w", err)
|
|
|
|
}
|
2023-11-02 19:25:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-17 22:15:22 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Sprawl) registerCatalogNode(
|
|
|
|
cluster *topology.Cluster,
|
|
|
|
node *topology.Node,
|
2023-11-02 19:25:48 +00:00
|
|
|
) error {
|
|
|
|
return s.registerCatalogNodeV1(cluster, node)
|
|
|
|
}
|
|
|
|
|
2023-11-07 22:29:13 +00:00
|
|
|
func (s *Sprawl) deregisterCatalogNode(
|
|
|
|
cluster *topology.Cluster,
|
|
|
|
node *topology.Node,
|
|
|
|
) error {
|
|
|
|
return s.deregisterCatalogNodeV1(cluster, node)
|
|
|
|
}
|
|
|
|
|
2023-11-02 19:25:48 +00:00
|
|
|
func (s *Sprawl) writeResource(cluster *topology.Cluster, res *pbresource.Resource) (*pbresource.Resource, error) {
|
|
|
|
var (
|
|
|
|
client = s.getResourceClient(cluster.Name)
|
|
|
|
logger = s.logger.With("cluster", cluster.Name)
|
|
|
|
)
|
|
|
|
|
|
|
|
ctx := s.getManagementTokenContext(context.Background(), cluster.Name)
|
|
|
|
RETRY:
|
|
|
|
wrote, err := client.Write(ctx, &pbresource.WriteRequest{
|
|
|
|
Resource: res,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
if isACLNotFound(err) { // TODO: is this right for v2?
|
|
|
|
time.Sleep(50 * time.Millisecond)
|
|
|
|
goto RETRY
|
|
|
|
}
|
|
|
|
return nil, fmt.Errorf("error creating resource %s: %w", util.IDToString(res.Id), err)
|
|
|
|
}
|
|
|
|
|
|
|
|
logger.Info("resource upserted", "id", util.IDToString(res.Id))
|
|
|
|
return wrote.Resource, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Sprawl) registerCatalogNodeV1(
|
|
|
|
cluster *topology.Cluster,
|
|
|
|
node *topology.Node,
|
2023-07-17 22:15:22 +00:00
|
|
|
) error {
|
|
|
|
if !node.IsDataplane() {
|
|
|
|
panic("called wrong method type")
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
client = s.clients[cluster.Name]
|
|
|
|
logger = s.logger.With("cluster", cluster.Name)
|
|
|
|
)
|
|
|
|
|
|
|
|
reg := &api.CatalogRegistration{
|
|
|
|
Node: node.PodName(),
|
|
|
|
Address: node.LocalAddress(),
|
|
|
|
NodeMeta: map[string]string{
|
|
|
|
"dataplane-faux": "1",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
if cluster.Enterprise {
|
|
|
|
reg.Partition = node.Partition
|
|
|
|
}
|
|
|
|
|
|
|
|
// register synthetic node
|
|
|
|
RETRY:
|
|
|
|
if _, err := client.Catalog().Register(reg, nil); err != nil {
|
|
|
|
if isACLNotFound(err) {
|
|
|
|
time.Sleep(50 * time.Millisecond)
|
|
|
|
goto RETRY
|
|
|
|
}
|
|
|
|
return fmt.Errorf("error registering virtual node %s: %w", node.ID(), err)
|
|
|
|
}
|
|
|
|
|
2023-10-03 15:06:50 +00:00
|
|
|
logger.Debug("virtual node created",
|
2023-07-17 22:15:22 +00:00
|
|
|
"node", node.ID(),
|
|
|
|
)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-11-07 22:29:13 +00:00
|
|
|
func (s *Sprawl) deregisterCatalogNodeV1(
|
|
|
|
cluster *topology.Cluster,
|
|
|
|
node *topology.Node,
|
|
|
|
) error {
|
|
|
|
if !node.IsDataplane() {
|
|
|
|
panic("called wrong method type")
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
client = s.clients[cluster.Name]
|
|
|
|
logger = s.logger.With("cluster", cluster.Name)
|
|
|
|
)
|
|
|
|
|
|
|
|
dereg := &api.CatalogDeregistration{
|
|
|
|
Node: node.PodName(),
|
|
|
|
Address: node.LocalAddress(),
|
|
|
|
}
|
|
|
|
if cluster.Enterprise {
|
|
|
|
dereg.Partition = node.Partition
|
|
|
|
}
|
|
|
|
|
|
|
|
// deregister synthetic node
|
|
|
|
RETRY:
|
|
|
|
if _, err := client.Catalog().Deregister(dereg, nil); err != nil {
|
|
|
|
if isACLNotFound(err) {
|
|
|
|
time.Sleep(50 * time.Millisecond)
|
|
|
|
goto RETRY
|
|
|
|
}
|
|
|
|
return fmt.Errorf("error deregistering virtual node %s: %w", node.ID(), err)
|
|
|
|
}
|
|
|
|
|
|
|
|
logger.Info("virtual node removed",
|
|
|
|
"node", node.ID(),
|
|
|
|
)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Sprawl) deregisterCatalogServiceV1(
|
|
|
|
cluster *topology.Cluster,
|
|
|
|
node *topology.Node,
|
2023-11-10 19:22:06 +00:00
|
|
|
wrk *topology.Workload,
|
2023-11-07 22:29:13 +00:00
|
|
|
) error {
|
|
|
|
if !node.IsDataplane() {
|
|
|
|
panic("called wrong method type")
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
client = s.clients[cluster.Name]
|
|
|
|
logger = s.logger.With("cluster", cluster.Name)
|
|
|
|
)
|
|
|
|
|
|
|
|
dereg := &api.CatalogDeregistration{
|
|
|
|
Node: node.PodName(),
|
2023-11-10 19:22:06 +00:00
|
|
|
ServiceID: wrk.ID.Name,
|
2023-11-07 22:29:13 +00:00
|
|
|
}
|
|
|
|
RETRY:
|
|
|
|
if _, err := client.Catalog().Deregister(dereg, nil); err != nil {
|
|
|
|
if isACLNotFound(err) {
|
|
|
|
time.Sleep(50 * time.Millisecond)
|
|
|
|
goto RETRY
|
|
|
|
}
|
2023-11-10 19:22:06 +00:00
|
|
|
return fmt.Errorf("error deregistering service %s at node %s: %w", wrk.ID, node.ID(), err)
|
2023-11-07 22:29:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
logger.Info("dataplane service removed",
|
2023-11-10 19:22:06 +00:00
|
|
|
"service", wrk.ID,
|
2023-11-07 22:29:13 +00:00
|
|
|
"node", node.ID(),
|
|
|
|
)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-11-02 19:25:48 +00:00
|
|
|
func (s *Sprawl) registerCatalogServiceV1(
|
2023-07-17 22:15:22 +00:00
|
|
|
cluster *topology.Cluster,
|
|
|
|
node *topology.Node,
|
2023-11-10 19:22:06 +00:00
|
|
|
wrk *topology.Workload,
|
2023-07-17 22:15:22 +00:00
|
|
|
) error {
|
|
|
|
if !node.IsDataplane() {
|
|
|
|
panic("called wrong method type")
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
client = s.clients[cluster.Name]
|
|
|
|
logger = s.logger.With("cluster", cluster.Name)
|
|
|
|
)
|
|
|
|
|
2023-11-10 19:22:06 +00:00
|
|
|
reg := workloadToCatalogRegistration(cluster, node, wrk)
|
2023-07-17 22:15:22 +00:00
|
|
|
|
|
|
|
RETRY:
|
|
|
|
if _, err := client.Catalog().Register(reg, nil); err != nil {
|
|
|
|
if isACLNotFound(err) {
|
|
|
|
time.Sleep(50 * time.Millisecond)
|
|
|
|
goto RETRY
|
|
|
|
}
|
2023-11-10 19:22:06 +00:00
|
|
|
return fmt.Errorf("error registering service %s to node %s: %w", wrk.ID, node.ID(), err)
|
2023-07-17 22:15:22 +00:00
|
|
|
}
|
|
|
|
|
2023-10-03 15:06:50 +00:00
|
|
|
logger.Debug("dataplane service created",
|
2023-11-10 19:22:06 +00:00
|
|
|
"service", wrk.ID,
|
2023-07-17 22:15:22 +00:00
|
|
|
"node", node.ID(),
|
|
|
|
)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-11-07 22:29:13 +00:00
|
|
|
func (s *Sprawl) deregisterCatalogSidecarServiceV1(
|
|
|
|
cluster *topology.Cluster,
|
|
|
|
node *topology.Node,
|
2023-11-10 19:22:06 +00:00
|
|
|
wrk *topology.Workload,
|
2023-11-07 22:29:13 +00:00
|
|
|
) error {
|
|
|
|
if !node.IsDataplane() {
|
|
|
|
panic("called wrong method type")
|
|
|
|
}
|
2023-11-10 19:22:06 +00:00
|
|
|
if wrk.DisableServiceMesh {
|
2023-11-07 22:29:13 +00:00
|
|
|
panic("not valid")
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
client = s.clients[cluster.Name]
|
|
|
|
logger = s.logger.With("cluster", cluster.Name)
|
|
|
|
)
|
|
|
|
|
2023-11-10 19:22:06 +00:00
|
|
|
pid := wrk.ID
|
2023-11-07 22:29:13 +00:00
|
|
|
pid.Name += "-sidecar-proxy"
|
|
|
|
dereg := &api.CatalogDeregistration{
|
|
|
|
Node: node.PodName(),
|
|
|
|
ServiceID: pid.Name,
|
|
|
|
}
|
|
|
|
|
|
|
|
RETRY:
|
|
|
|
if _, err := client.Catalog().Deregister(dereg, nil); err != nil {
|
|
|
|
if isACLNotFound(err) {
|
|
|
|
time.Sleep(50 * time.Millisecond)
|
|
|
|
goto RETRY
|
|
|
|
}
|
2023-11-10 19:22:06 +00:00
|
|
|
return fmt.Errorf("error deregistering service %s to node %s: %w", wrk.ID, node.ID(), err)
|
2023-11-07 22:29:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
logger.Info("dataplane sidecar service removed",
|
|
|
|
"service", pid,
|
|
|
|
"node", node.ID(),
|
|
|
|
)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-11-02 19:25:48 +00:00
|
|
|
func (s *Sprawl) registerCatalogSidecarServiceV1(
|
2023-07-17 22:15:22 +00:00
|
|
|
cluster *topology.Cluster,
|
|
|
|
node *topology.Node,
|
2023-11-10 19:22:06 +00:00
|
|
|
wrk *topology.Workload,
|
2023-07-17 22:15:22 +00:00
|
|
|
) error {
|
|
|
|
if !node.IsDataplane() {
|
|
|
|
panic("called wrong method type")
|
|
|
|
}
|
2023-11-10 19:22:06 +00:00
|
|
|
if wrk.DisableServiceMesh {
|
2023-07-17 22:15:22 +00:00
|
|
|
panic("not valid")
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
client = s.clients[cluster.Name]
|
|
|
|
logger = s.logger.With("cluster", cluster.Name)
|
|
|
|
)
|
|
|
|
|
2023-11-10 19:22:06 +00:00
|
|
|
pid, reg := workloadToSidecarCatalogRegistration(cluster, node, wrk)
|
2023-07-17 22:15:22 +00:00
|
|
|
RETRY:
|
|
|
|
if _, err := client.Catalog().Register(reg, nil); err != nil {
|
|
|
|
if isACLNotFound(err) {
|
|
|
|
time.Sleep(50 * time.Millisecond)
|
|
|
|
goto RETRY
|
|
|
|
}
|
2023-11-10 19:22:06 +00:00
|
|
|
return fmt.Errorf("error registering service %s to node %s: %w", wrk.ID, node.ID(), err)
|
2023-07-17 22:15:22 +00:00
|
|
|
}
|
|
|
|
|
2023-10-03 15:06:50 +00:00
|
|
|
logger.Debug("dataplane sidecar service created",
|
2023-07-17 22:15:22 +00:00
|
|
|
"service", pid,
|
|
|
|
"node", node.ID(),
|
|
|
|
)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-11-02 19:25:48 +00:00
|
|
|
type Resource[V proto.Message] struct {
|
|
|
|
Resource *pbresource.Resource
|
|
|
|
Data V
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Resource[V]) Build() (*pbresource.Resource, error) {
|
|
|
|
anyData, err := anypb.New(r.Data)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
r.Resource.Data = anyData
|
|
|
|
return r.Resource, nil
|
|
|
|
}
|
|
|
|
|
2023-11-10 19:22:06 +00:00
|
|
|
func workloadToCatalogRegistration(
|
2023-07-17 22:15:22 +00:00
|
|
|
cluster *topology.Cluster,
|
|
|
|
node *topology.Node,
|
2023-11-10 19:22:06 +00:00
|
|
|
wrk *topology.Workload,
|
2023-07-17 22:15:22 +00:00
|
|
|
) *api.CatalogRegistration {
|
|
|
|
reg := &api.CatalogRegistration{
|
|
|
|
Node: node.PodName(),
|
|
|
|
SkipNodeUpdate: true,
|
|
|
|
Service: &api.AgentService{
|
|
|
|
Kind: api.ServiceKindTypical,
|
2023-11-10 19:22:06 +00:00
|
|
|
ID: wrk.ID.Name,
|
|
|
|
Service: wrk.ID.Name,
|
|
|
|
Meta: wrk.Meta,
|
|
|
|
Port: wrk.Port,
|
2023-07-17 22:15:22 +00:00
|
|
|
Address: node.LocalAddress(),
|
|
|
|
},
|
|
|
|
}
|
2023-11-10 19:22:06 +00:00
|
|
|
if wrk.IsMeshGateway {
|
2023-09-06 23:46:34 +00:00
|
|
|
reg.Service.Kind = api.ServiceKindMeshGateway
|
|
|
|
reg.Service.Proxy = &api.AgentServiceConnectProxyConfig{
|
|
|
|
Config: map[string]interface{}{
|
|
|
|
"envoy_gateway_no_default_bind": true,
|
|
|
|
"envoy_gateway_bind_tagged_addresses": true,
|
|
|
|
},
|
|
|
|
MeshGateway: api.MeshGatewayConfig{
|
|
|
|
Mode: api.MeshGatewayModeLocal,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
2023-07-17 22:15:22 +00:00
|
|
|
if node.HasPublicAddress() {
|
|
|
|
reg.TaggedAddresses = map[string]string{
|
|
|
|
"lan": node.LocalAddress(),
|
|
|
|
"lan_ipv4": node.LocalAddress(),
|
|
|
|
"wan": node.PublicAddress(),
|
|
|
|
"wan_ipv4": node.PublicAddress(),
|
|
|
|
}
|
2023-09-06 23:46:34 +00:00
|
|
|
// TODO: not sure what the difference is between these, but with just the
|
|
|
|
// top-level set, it appeared to not get set in either :/
|
|
|
|
reg.Service.TaggedAddresses = map[string]api.ServiceAddress{
|
|
|
|
"lan": {
|
|
|
|
Address: node.LocalAddress(),
|
2023-11-10 19:22:06 +00:00
|
|
|
Port: wrk.Port,
|
2023-09-06 23:46:34 +00:00
|
|
|
},
|
|
|
|
"lan_ipv4": {
|
|
|
|
Address: node.LocalAddress(),
|
2023-11-10 19:22:06 +00:00
|
|
|
Port: wrk.Port,
|
2023-09-06 23:46:34 +00:00
|
|
|
},
|
|
|
|
"wan": {
|
|
|
|
Address: node.PublicAddress(),
|
2023-11-10 19:22:06 +00:00
|
|
|
Port: wrk.Port,
|
2023-09-06 23:46:34 +00:00
|
|
|
},
|
|
|
|
"wan_ipv4": {
|
|
|
|
Address: node.PublicAddress(),
|
2023-11-10 19:22:06 +00:00
|
|
|
Port: wrk.Port,
|
2023-09-06 23:46:34 +00:00
|
|
|
},
|
|
|
|
}
|
2023-07-17 22:15:22 +00:00
|
|
|
}
|
|
|
|
if cluster.Enterprise {
|
2023-11-10 19:22:06 +00:00
|
|
|
reg.Partition = wrk.ID.Partition
|
|
|
|
reg.Service.Namespace = wrk.ID.Namespace
|
|
|
|
reg.Service.Partition = wrk.ID.Partition
|
2023-07-17 22:15:22 +00:00
|
|
|
}
|
|
|
|
|
2023-11-10 19:22:06 +00:00
|
|
|
if wrk.HasCheck() {
|
2023-07-17 22:15:22 +00:00
|
|
|
chk := &api.HealthCheck{
|
|
|
|
Name: "external sync",
|
|
|
|
// Type: "external-sync",
|
|
|
|
Status: "passing", // TODO
|
2023-11-10 19:22:06 +00:00
|
|
|
ServiceID: wrk.ID.Name,
|
|
|
|
ServiceName: wrk.ID.Name,
|
2023-07-17 22:15:22 +00:00
|
|
|
Output: "",
|
|
|
|
}
|
|
|
|
if cluster.Enterprise {
|
2023-11-10 19:22:06 +00:00
|
|
|
chk.Namespace = wrk.ID.Namespace
|
|
|
|
chk.Partition = wrk.ID.Partition
|
2023-07-17 22:15:22 +00:00
|
|
|
}
|
|
|
|
switch {
|
2023-11-10 19:22:06 +00:00
|
|
|
case wrk.CheckTCP != "":
|
|
|
|
chk.Definition.TCP = wrk.CheckTCP
|
|
|
|
case wrk.CheckHTTP != "":
|
|
|
|
chk.Definition.HTTP = wrk.CheckHTTP
|
2023-07-17 22:15:22 +00:00
|
|
|
chk.Definition.Method = "GET"
|
|
|
|
}
|
|
|
|
reg.Checks = append(reg.Checks, chk)
|
|
|
|
}
|
|
|
|
return reg
|
|
|
|
}
|
|
|
|
|
2023-11-10 19:22:06 +00:00
|
|
|
func workloadToSidecarCatalogRegistration(
|
2023-07-17 22:15:22 +00:00
|
|
|
cluster *topology.Cluster,
|
|
|
|
node *topology.Node,
|
2023-11-10 19:22:06 +00:00
|
|
|
wrk *topology.Workload,
|
|
|
|
) (topology.ID, *api.CatalogRegistration) {
|
|
|
|
pid := wrk.ID
|
2023-07-17 22:15:22 +00:00
|
|
|
pid.Name += "-sidecar-proxy"
|
|
|
|
reg := &api.CatalogRegistration{
|
|
|
|
Node: node.PodName(),
|
|
|
|
SkipNodeUpdate: true,
|
|
|
|
Service: &api.AgentService{
|
|
|
|
Kind: api.ServiceKindConnectProxy,
|
|
|
|
ID: pid.Name,
|
|
|
|
Service: pid.Name,
|
2023-11-10 19:22:06 +00:00
|
|
|
Meta: wrk.Meta,
|
|
|
|
Port: wrk.EnvoyPublicListenerPort,
|
2023-07-17 22:15:22 +00:00
|
|
|
Address: node.LocalAddress(),
|
|
|
|
Proxy: &api.AgentServiceConnectProxyConfig{
|
2023-11-10 19:22:06 +00:00
|
|
|
DestinationServiceName: wrk.ID.Name,
|
|
|
|
DestinationServiceID: wrk.ID.Name,
|
|
|
|
LocalServicePort: wrk.Port,
|
2023-07-17 22:15:22 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
Checks: []*api.HealthCheck{{
|
|
|
|
Name: "external sync",
|
|
|
|
// Type: "external-sync",
|
|
|
|
Status: "passing", // TODO
|
|
|
|
ServiceID: pid.Name,
|
|
|
|
ServiceName: pid.Name,
|
|
|
|
Definition: api.HealthCheckDefinition{
|
2023-11-10 19:22:06 +00:00
|
|
|
TCP: fmt.Sprintf("%s:%d", node.LocalAddress(), wrk.EnvoyPublicListenerPort),
|
2023-07-17 22:15:22 +00:00
|
|
|
},
|
|
|
|
Output: "",
|
|
|
|
}},
|
|
|
|
}
|
|
|
|
if node.HasPublicAddress() {
|
|
|
|
reg.TaggedAddresses = map[string]string{
|
|
|
|
"lan": node.LocalAddress(),
|
|
|
|
"lan_ipv4": node.LocalAddress(),
|
|
|
|
"wan": node.PublicAddress(),
|
|
|
|
"wan_ipv4": node.PublicAddress(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if cluster.Enterprise {
|
|
|
|
reg.Partition = pid.Partition
|
|
|
|
reg.Service.Namespace = pid.Namespace
|
|
|
|
reg.Service.Partition = pid.Partition
|
|
|
|
reg.Checks[0].Namespace = pid.Namespace
|
|
|
|
reg.Checks[0].Partition = pid.Partition
|
|
|
|
}
|
|
|
|
|
2024-05-21 19:52:19 +00:00
|
|
|
for _, us := range wrk.Upstreams {
|
2023-07-17 22:15:22 +00:00
|
|
|
pu := api.Upstream{
|
2024-05-21 19:52:19 +00:00
|
|
|
DestinationName: us.ID.Name,
|
|
|
|
DestinationPeer: us.Peer,
|
|
|
|
LocalBindAddress: us.LocalAddress,
|
|
|
|
LocalBindPort: us.LocalPort,
|
2023-07-17 22:15:22 +00:00
|
|
|
}
|
|
|
|
if cluster.Enterprise {
|
2024-05-21 19:52:19 +00:00
|
|
|
pu.DestinationNamespace = us.ID.Namespace
|
|
|
|
if us.Peer == "" {
|
|
|
|
pu.DestinationPartition = us.ID.Partition
|
2023-07-17 22:15:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
reg.Service.Proxy.Upstreams = append(reg.Service.Proxy.Upstreams, pu)
|
|
|
|
}
|
|
|
|
|
|
|
|
return pid, reg
|
|
|
|
}
|