Documentation
¶
Overview ¶
Package kubernetes - Kubernetes API client
Package kubernetes - Common constants for Kubernetes middleware ¶
Package kubernetes provides a Kubernetes DNS middleware for SDNS. It answers cluster-domain queries (services, pods, SRV, PTR) from a sharded in-memory registry populated by Kubernetes informers. ResolveQuery is a single sharded map lookup plus a slice-header copy — zero allocations per query.
Package kubernetes - DNS types
Index ¶
- Constants
- func FormatPodIP(ip net.IP) string
- func FormatReverseIP(ip net.IP) string
- func ParsePodIP(podPart string) net.IP
- func ParseReverseIP(labels []string) (net.IP, bool)
- type Client
- type Endpoint
- type Kubernetes
- type ObjectRef
- type Pod
- type Port
- type Registry
- func (r *Registry) AddPod(pod *Pod)
- func (r *Registry) AddService(svc *Service)
- func (r *Registry) ApplyEndpointSlice(svcName, namespace, sliceName string, eps []Endpoint) bool
- func (r *Registry) DeletePod(name, namespace string)
- func (r *Registry) DeleteService(name, namespace string)
- func (r *Registry) GetEndpoints(service, namespace string) []Endpoint
- func (r *Registry) GetPodByIP(ip string) *Pod
- func (r *Registry) GetPodByName(name, namespace string) *Pod
- func (r *Registry) GetService(name, namespace string) *Service
- func (r *Registry) GetServiceByIP(ip []byte) *Service
- func (r *Registry) MaterialiseHeadless(svcName, namespace string)
- func (r *Registry) RemoveEndpointSlice(svcName, namespace, sliceName string) bool
- func (r *Registry) ResolveQuery(qname string, qtype uint16) (answer, extra []dns.RR, ok bool)
- func (r *Registry) SetClusterDomain(domain string)
- func (r *Registry) SetEndpoints(service, namespace string, endpoints []Endpoint)
- func (r *Registry) SetTTLs(service, pod, srv, ptr uint32)
- func (r *Registry) Stats() map[string]int64
- type Response
- type Service
Constants ¶
const ( // Cache sizes and limits CacheMaxEntries = 10000 // Maximum number of entries in zero-alloc cache CacheIndexSize = 16384 // Must be power of 2 for fast modulo CacheLockStripes = 256 // Number of lock stripes for sharding CacheMaxWireSize = 4096 // Maximum wire format DNS message size (EDNS0 support) CacheLinearProbeSize = 16 // Maximum linear probe attempts for collision handling // Cache cleanup and expiry CacheCleanupInterval = 10 * time.Second CacheDefaultTTL = 30 // Default TTL in seconds )
Cache configuration constants
const ( RegistryServiceShards = 256 // Number of shards for services RegistryPodShards = 256 // Number of shards for pods )
Sharding constants for registry
const ( PredictorBufferSize = 1024 // Size of circular buffer for recent queries PredictorMaxPredictions = 10 // Maximum predictions in pool PredictorMaxResults = 5 // Maximum predictions to return PredictorThresholdDiv = 10 // Threshold divisor (>10% probability) PredictorTrainInterval = 30 * time.Second )
Predictor constants
const ( IPv4AddressSize = 4 // Size of IPv4 address in bytes IPv6AddressSize = 16 // Size of IPv6 address in bytes )
Network constants
const ( DNSTypeA = 1 // A record type DNSTypeAAAA = 28 // AAAA record type )
DNS query type constants (for ML predictor)
const ( FNVOffsetBasis = 14695981039346656037 // FNV-1a offset basis FNVPrime = 1099511628211 // FNV-1a prime HashMultiplier = 31 // Simple hash multiplier )
Hash constants
const ( SRVPriority = 0 // Default SRV priority SRVWeight = 100 // Default SRV weight for single entry SRVWeight1 = 1 // Alternative SRV weight )
SRV record constants
const ( IPv4LastOctetIndex = 3 // Index of last octet in IPv4 address IPv6LastByteIndex = 15 // Index of last byte in IPv6 address )
IP byte positions
const ( WireMessageIDOffset = 0 // Offset of message ID in DNS wire format WireMessageIDSize = 2 // Size of message ID in bytes )
Wire format constants
const ( BenchmarkServiceStart = 1 // Starting index for benchmark services NetworkOctet10 = 10 // First octet for test IPs (10.x.x.x) NetworkOctet96 = 96 // Second octet for test IPs (10.96.x.x) NetworkOctet244 = 244 // Third octet for test pod IPs (10.244.x.x) IPv6TestPrefix = 0xfe80 // IPv6 test prefix (fe80::) )
Benchmark and test constants
const ( PortHTTPS = 443 // HTTPS port PortDNS = 53 // DNS port )
Port numbers for test services
const ( DefaultServiceTTL = uint32(30) DefaultPodTTL = uint32(30) DefaultSRVTTL = uint32(30) DefaultPTRTTL = uint32(30) )
DNS TTL values (configurable)
const (
ClientStopTimeout = 5 * time.Second // Timeout for client stop operation
)
Client timeout constants
const (
DemoServiceCount = 10 // Number of demo services to create
)
Service population constants (for demo/test data)
const (
PercentageMultiplier = 100
)
Registry statistics percentage calculation
const (
StatsLogInterval = 30 * time.Second // Interval for logging statistics
)
Performance monitoring constants
Variables ¶
This section is empty.
Functions ¶
func FormatPodIP ¶
FormatPodIP formats an IP for pod DNS name IPv4: 10.244.1.1 -> 10-244-1-1 IPv6: 2001:db8::1 -> 2001-db8--1
func FormatReverseIP ¶
FormatReverseIP formats an IP for reverse DNS IPv4: 10.96.0.1 -> 1.0.96.10.in-addr.arpa IPv6: 2001:db8::1 -> 1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.8.b.d.0.1.0.0.2.ip6.arpa
func ParsePodIP ¶
ParsePodIP parses both IPv4 and IPv6 pod query formats IPv4: 10-244-1-1.namespace.pod.cluster.local IPv6: 2001-db8--1.namespace.pod.cluster.local or
2001-0db8-0000-0000-0000-0000-0000-0001.namespace.pod.cluster.local
Types ¶
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client connects to the Kubernetes API.
func (*Client) Rebuilds ¶ added in v1.6.6
Rebuilds returns the total number of per-service rebuilds.
type Endpoint ¶
type Endpoint struct {
Addresses []string // Dual-stack: [IPv4, IPv6] addresses
Hostname string // Optional hostname
Ready bool // Is endpoint ready
TargetRef *ObjectRef // Reference to pod
}
Endpoint represents a service endpoint
type Kubernetes ¶
type Kubernetes struct {
// contains filtered or unexported fields
}
Kubernetes is the DNS middleware that answers cluster-domain queries.
func (*Kubernetes) ServeDNS ¶
func (k *Kubernetes) ServeDNS(ctx context.Context, ch *middleware.Chain)
ServeDNS handles DNS queries. The default stub has no registry and passes a wire-born request through with one check; a configured cluster needs the decoded question and materializes.
func (*Kubernetes) Stats ¶
func (k *Kubernetes) Stats() map[string]any
Stats returns runtime statistics.
type Pod ¶
type Pod struct {
Name string
Namespace string
IPs []string // Dual-stack: [IPv4, IPv6] addresses
Hostname string // Pod hostname
Subdomain string // For StatefulSet DNS
}
Pod represents a Kubernetes pod
type Registry ¶
type Registry struct {
// contains filtered or unexported fields
}
Registry is a 256-way sharded store of services, pods, and endpoints with pre-built DNS answers per FQDN.
func NewRegistry ¶
func NewRegistry() *Registry
NewRegistry creates an empty registry with all shards initialised.
func (*Registry) AddService ¶
func (*Registry) ApplyEndpointSlice ¶ added in v1.6.6
ApplyEndpointSlice records sliceName's contribution to a headless service. The answer cache is NOT refreshed here — call MaterialiseHeadless once the burst of Apply/Remove calls has settled. Returns true when state actually changed.
When a real slice arrives for a service whose state holds a synthetic-slice contribution, the synthetic is retracted first; otherwise the two would double-count.
func (*Registry) DeleteService ¶
DeleteService removes a service and every piece of state derived from it. The endpoint shard is wiped unconditionally so that endpoints-before-Service ordering can't leave stale endpoints for a later AddService to pick up.
func (*Registry) GetEndpoints ¶
func (*Registry) GetPodByIP ¶
func (*Registry) GetPodByName ¶
func (*Registry) GetService ¶
func (*Registry) GetServiceByIP ¶
GetServiceByIP returns the service whose ClusterIPs contain ip (IPv4 or IPv6 byte slice).
func (*Registry) MaterialiseHeadless ¶ added in v1.6.6
MaterialiseHeadless rebuilds the answer cache from the headless state. Worker callers debounce so a flurry of Apply/Remove calls amortises to one materialise per debounce window.
func (*Registry) RemoveEndpointSlice ¶ added in v1.6.6
RemoveEndpointSlice retracts sliceName's contribution. Returns true when state changed.
func (*Registry) ResolveQuery ¶ added in v1.6.6
ResolveQuery resolves a DNS query against the registry. Returns ok=false for unknown names; ok=true with a nil/empty answer means authoritative NOERROR/NODATA. SRV queries return A/AAAA glue in extra.
func (*Registry) SetClusterDomain ¶ added in v1.6.6
SetClusterDomain configures the cluster suffix used for suffix matching and PTR/SRV target construction. The input is lowercased and stripped of any trailing dot.
func (*Registry) SetEndpoints ¶
SetEndpoints replaces a service's endpoint set. Headless services route through the per-slice incremental state.
type Service ¶
type Service struct {
Name string
Namespace string
Type string // ClusterIP, NodePort, LoadBalancer, ExternalName
ClusterIPs [][]byte // Dual-stack: [IPv4, IPv6] addresses
IPFamilies []string // ["IPv4", "IPv6"] or ["IPv6", "IPv4"]
ExternalName string // For ExternalName type
Headless bool // True if ClusterIP is None
Ports []Port
}
Service represents a Kubernetes service