From 9782319c31e496d998bdf9d505f32a4d8e6e937e Mon Sep 17 00:00:00 2001 From: Andrey Smirnov Date: Tue, 16 Jan 2024 20:40:38 +0400 Subject: [PATCH] fix: support KubePrism settings in Kubernetes Discovery Fixes #8143 Signed-off-by: Andrey Smirnov --- internal/pkg/discovery/registry/kubernetes.go | 18 ++++++ .../pkg/discovery/registry/kubernetes_test.go | 63 +++++++++++++++++++ pkg/machinery/constants/constants.go | 3 + 3 files changed, 84 insertions(+) diff --git a/internal/pkg/discovery/registry/kubernetes.go b/internal/pkg/discovery/registry/kubernetes.go index 6ac1775bdc..8a6e07cca2 100644 --- a/internal/pkg/discovery/registry/kubernetes.go +++ b/internal/pkg/discovery/registry/kubernetes.go @@ -9,6 +9,7 @@ import ( "encoding/json" "fmt" "net/netip" + "strconv" "strings" "time" @@ -53,9 +54,16 @@ func AnnotationsFromAffiliate(affiliate *cluster.Affiliate) map[string]string { kubeSpanAddress = affiliate.TypedSpec().KubeSpan.Address.String() } + var apiServerPort string + + if affiliate.TypedSpec().ControlPlane != nil { + apiServerPort = strconv.Itoa(affiliate.TypedSpec().ControlPlane.APIServerPort) + } + return map[string]string{ constants.ClusterNodeIDAnnotation: affiliate.Metadata().ID(), constants.NetworkSelfIPsAnnotation: ipsToString(affiliate.TypedSpec().Addresses), + constants.NetworkAPIServerPortAnnotation: apiServerPort, constants.KubeSpanIPAnnotation: kubeSpanAddress, constants.KubeSpanPublicKeyAnnotation: affiliate.TypedSpec().KubeSpan.PublicKey, constants.KubeSpanAssignedPrefixesAnnotation: ipPrefixesToString(affiliate.TypedSpec().KubeSpan.AdditionalAddresses), @@ -66,6 +74,8 @@ func AnnotationsFromAffiliate(affiliate *cluster.Affiliate) map[string]string { // AffiliateFromNode converts Kubernetes Node resource to Affiliate. // // If the Node resource doesn't have cluster discovery annotations, nil is returned. +// +//nolint:gocyclo func AffiliateFromNode(node *v1.Node) *cluster.AffiliateSpec { nodeID, ok := node.Annotations[constants.ClusterNodeIDAnnotation] if !ok { @@ -120,6 +130,14 @@ func AffiliateFromNode(node *v1.Node) *cluster.AffiliateSpec { affiliate.KubeSpan.Endpoints = parseIPPorts(endpoints) } + if apiServerPort, ok := node.Annotations[constants.NetworkAPIServerPortAnnotation]; ok { + if port, err := strconv.Atoi(apiServerPort); err == nil { + affiliate.ControlPlane = &cluster.ControlPlane{ + APIServerPort: port, + } + } + } + return affiliate } diff --git a/internal/pkg/discovery/registry/kubernetes_test.go b/internal/pkg/discovery/registry/kubernetes_test.go index 0c53e1c5b0..3f37cd3def 100644 --- a/internal/pkg/discovery/registry/kubernetes_test.go +++ b/internal/pkg/discovery/registry/kubernetes_test.go @@ -28,6 +28,7 @@ func TestAnnotationsFromAffiliate(t *testing.T) { name: "zero", expected: map[string]string{ "cluster.talos.dev/node-id": "", + "networking.talos.dev/api-server-port": "", "networking.talos.dev/assigned-prefixes": "", "networking.talos.dev/kubespan-endpoints": "", "networking.talos.dev/kubespan-ip": "", @@ -52,6 +53,7 @@ func TestAnnotationsFromAffiliate(t *testing.T) { }, expected: map[string]string{ "cluster.talos.dev/node-id": "29QQTc97U5ZyFTIX33Dp9NqtwxqQI8QI13scCLzffrZ", + "networking.talos.dev/api-server-port": "", "networking.talos.dev/assigned-prefixes": "10.244.3.1/24", "networking.talos.dev/kubespan-endpoints": "10.0.0.2:51820,192.168.3.4:51820", "networking.talos.dev/kubespan-ip": "fd50:8d60:4238:6302:f857:23ff:fe21:d1e0", @@ -59,6 +61,28 @@ func TestAnnotationsFromAffiliate(t *testing.T) { "networking.talos.dev/self-ips": "10.0.0.2,192.168.3.4", }, }, + { + name: "controlplane", + affiliate: cluster.AffiliateSpec{ + NodeID: "29QQTc97U5ZyFTIX33Dp9NqtwxqQI8QI13scCLzffrZ", + Hostname: "foo.com", + Nodename: "bar", + MachineType: machine.TypeControlPlane, + Addresses: []netip.Addr{netip.MustParseAddr("10.0.0.2"), netip.MustParseAddr("192.168.3.4")}, + ControlPlane: &cluster.ControlPlane{ + APIServerPort: 443, + }, + }, + expected: map[string]string{ + "cluster.talos.dev/node-id": "29QQTc97U5ZyFTIX33Dp9NqtwxqQI8QI13scCLzffrZ", + "networking.talos.dev/api-server-port": "443", + "networking.talos.dev/assigned-prefixes": "", + "networking.talos.dev/kubespan-endpoints": "", + "networking.talos.dev/kubespan-ip": "", + "networking.talos.dev/kubespan-public-key": "", + "networking.talos.dev/self-ips": "10.0.0.2,192.168.3.4", + }, + }, } { tt := tt @@ -133,6 +157,45 @@ func TestAffiliateFromNode(t *testing.T) { }, }, }, + { + name: "controlplane", + node: v1.Node{ + ObjectMeta: metav1.ObjectMeta{ + Name: "bar", + Annotations: map[string]string{ + "cluster.talos.dev/node-id": "29QQTc97U5ZyFTIX33Dp9NqtwxqQI8QI13scCLzffrZ", + "networking.talos.dev/api-server-port": "6443", + "networking.talos.dev/self-ips": "10.0.0.2,192.168.3.4", + }, + Labels: map[string]string{ + constants.LabelNodeRoleControlPlane: "", + }, + }, + Spec: v1.NodeSpec{}, + Status: v1.NodeStatus{ + Addresses: []v1.NodeAddress{ + { + Type: v1.NodeHostName, + Address: "foo.com", + }, + }, + NodeInfo: v1.NodeSystemInfo{ + OSImage: "Talos (v1.0.0)", + }, + }, + }, + expected: &cluster.AffiliateSpec{ + NodeID: "29QQTc97U5ZyFTIX33Dp9NqtwxqQI8QI13scCLzffrZ", + Hostname: "foo.com", + Nodename: "bar", + MachineType: machine.TypeControlPlane, + Addresses: []netip.Addr{netip.MustParseAddr("10.0.0.2"), netip.MustParseAddr("192.168.3.4")}, + OperatingSystem: "Talos (v1.0.0)", + ControlPlane: &cluster.ControlPlane{ + APIServerPort: 6443, + }, + }, + }, } { tt := tt diff --git a/pkg/machinery/constants/constants.go b/pkg/machinery/constants/constants.go index 76d6b48a74..6f0c3aac2d 100644 --- a/pkg/machinery/constants/constants.go +++ b/pkg/machinery/constants/constants.go @@ -799,6 +799,9 @@ const ( // NetworkSelfIPsAnnotation is the node annotation used to list the (comma-separated) IP addresses of the host, as discovered by Talos tooling. NetworkSelfIPsAnnotation = "networking.talos.dev/self-ips" + // NetworkAPIServerPortAnnotation is the node annotation used to report the control plane API server port. + NetworkAPIServerPortAnnotation = "networking.talos.dev/api-server-port" + // ClusterNodeIDAnnotation is the node annotation used to represent node ID. ClusterNodeIDAnnotation = "cluster.talos.dev/node-id"