2024-01-29 16:40:10 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: BUSL-1.1
|
|
|
|
|
|
|
|
//go:build !consulent
|
|
|
|
|
|
|
|
package dns
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/hashicorp/consul/agent/discovery"
|
|
|
|
)
|
|
|
|
|
2024-01-29 22:33:45 +00:00
|
|
|
// canonicalNameForResult returns the canonical name for a discovery result.
|
2024-02-03 03:23:52 +00:00
|
|
|
func canonicalNameForResult(resultType discovery.ResultType, target, domain string,
|
|
|
|
tenancy discovery.ResultTenancy, portName string) string {
|
|
|
|
switch resultType {
|
2024-01-29 16:40:10 +00:00
|
|
|
case discovery.ResultTypeService:
|
2024-02-03 03:23:52 +00:00
|
|
|
return fmt.Sprintf("%s.%s.%s.%s", target, "service", tenancy.Datacenter, domain)
|
2024-01-29 16:40:10 +00:00
|
|
|
case discovery.ResultTypeNode:
|
2024-02-03 03:23:52 +00:00
|
|
|
if tenancy.PeerName != "" {
|
2024-01-29 16:40:10 +00:00
|
|
|
// We must return a more-specific DNS name for peering so
|
|
|
|
// that there is no ambiguity with lookups.
|
|
|
|
return fmt.Sprintf("%s.node.%s.peer.%s",
|
2024-02-03 03:23:52 +00:00
|
|
|
target,
|
|
|
|
tenancy.PeerName,
|
2024-01-29 16:40:10 +00:00
|
|
|
domain)
|
|
|
|
}
|
|
|
|
// Return a simpler format for non-peering nodes.
|
2024-02-03 03:23:52 +00:00
|
|
|
return fmt.Sprintf("%s.node.%s.%s", target, tenancy.Datacenter, domain)
|
2024-01-29 16:40:10 +00:00
|
|
|
case discovery.ResultTypeWorkload:
|
2024-02-03 03:23:52 +00:00
|
|
|
if portName != "" {
|
|
|
|
return fmt.Sprintf("%s.port.%s.workload.%s", portName, target, domain)
|
2024-02-02 23:29:38 +00:00
|
|
|
}
|
2024-02-03 03:23:52 +00:00
|
|
|
return fmt.Sprintf("%s.workload.%s", target, domain)
|
2024-01-29 16:40:10 +00:00
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|