Skip to content

Commit

Permalink
fix: support KubePrism settings in Kubernetes Discovery
Browse files Browse the repository at this point in the history
Fixes #8143

Signed-off-by: Andrey Smirnov <andrey.smirnov@siderolabs.com>
  • Loading branch information
smira committed Jan 16, 2024
1 parent 6c5a0c2 commit 9782319
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 0 deletions.
18 changes: 18 additions & 0 deletions internal/pkg/discovery/registry/kubernetes.go
Expand Up @@ -9,6 +9,7 @@ import (
"encoding/json"
"fmt"
"net/netip"
"strconv"
"strings"
"time"

Expand Down Expand Up @@ -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),
Expand All @@ -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 {
Expand Down Expand Up @@ -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
}

Expand Down
63 changes: 63 additions & 0 deletions internal/pkg/discovery/registry/kubernetes_test.go
Expand Up @@ -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": "",
Expand All @@ -52,13 +53,36 @@ 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",
"networking.talos.dev/kubespan-public-key": "PLPNBddmTgHJhtw0vxltq1ZBdPP9RNOEUd5JjJZzBRY=",
"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

Expand Down Expand Up @@ -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

Expand Down
3 changes: 3 additions & 0 deletions pkg/machinery/constants/constants.go
Expand Up @@ -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"

Expand Down

0 comments on commit 9782319

Please sign in to comment.