Skip to content

Commit a6f90ec

Browse files
committed
feat: add cluster uid derived from kube-system ns
This seems like a simple enough way of uniquely identifying a cluster. Google searches show that this pattern is used elsewhere. It seems fair to assume that if the kube-system ns UID changes, it implies that the cluster changed enough to be considered different anyway! This is added to the context so it can be used elsewhere, on the assumption that it can be determined at startup and won't change later. We'll use this for "machinehub mode" soon. Signed-off-by: Ashley Davis <[email protected]>
1 parent 076f734 commit a6f90ec

File tree

3 files changed

+108
-0
lines changed

3 files changed

+108
-0
lines changed

pkg/agent/run.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import (
3232

3333
"github.com/jetstack/preflight/api"
3434
"github.com/jetstack/preflight/pkg/client"
35+
"github.com/jetstack/preflight/pkg/clusteruid"
3536
"github.com/jetstack/preflight/pkg/datagatherer"
3637
"github.com/jetstack/preflight/pkg/datagatherer/k8s"
3738
"github.com/jetstack/preflight/pkg/kubeconfig"
@@ -78,6 +79,28 @@ func Run(cmd *cobra.Command, args []string) (returnErr error) {
7879
return fmt.Errorf("While evaluating configuration: %v", err)
7980
}
8081

82+
// We need the cluster UID before we progress further so it can be sent along with other data readings
83+
84+
{
85+
restCfg, err := kubeconfig.LoadRESTConfig("")
86+
if err != nil {
87+
return err
88+
}
89+
90+
clientset, err := kubernetes.NewForConfig(restCfg)
91+
if err != nil {
92+
return err
93+
}
94+
95+
ctx, err = clusteruid.GetClusterUID(ctx, clientset)
96+
if err != nil {
97+
return fmt.Errorf("failed to get cluster UID: %v", err)
98+
}
99+
100+
clusterUID := clusteruid.ClusterUIDFromContext(ctx)
101+
log.V(logs.Debug).Info("Retrieved cluster UID", "clusterUID", clusterUID)
102+
}
103+
81104
group, gctx := errgroup.WithContext(ctx)
82105
defer func() {
83106
cancel()

pkg/clusteruid/clusteruid.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package clusteruid
2+
3+
import (
4+
"context"
5+
6+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
7+
"k8s.io/client-go/kubernetes"
8+
)
9+
10+
// clusterUIDKey is the context key for storing the cluster UID
11+
type clusterUIDKey struct{}
12+
13+
// GetClusterUID retrieves the UID of the kube-system namespace using the given Kubernetes clientset.
14+
// This UID can be used as a unique identifier for the Kubernetes cluster.
15+
// The UID is stored in the given context for later retrieval; use ClusterUIDFromContext to get it.
16+
func GetClusterUID(ctx context.Context, clientset kubernetes.Interface) (context.Context, error) {
17+
namespace, err := clientset.CoreV1().Namespaces().Get(ctx, "kube-system", metav1.GetOptions{})
18+
if err != nil {
19+
return ctx, err
20+
}
21+
22+
ctx = withClusterUID(ctx, string(namespace.ObjectMeta.UID))
23+
return ctx, nil
24+
}
25+
26+
// ClusterUIDFromContext retrieves the cluster UID from the context.
27+
// Panics if the value is not found or if the value is not a string.
28+
func ClusterUIDFromContext(ctx context.Context) string {
29+
value := ctx.Value(clusterUIDKey{})
30+
if value == nil {
31+
panic("cluster UID not found in context")
32+
}
33+
34+
uid, ok := value.(string)
35+
if !ok {
36+
panic("cluster UID in context is not a string")
37+
}
38+
39+
return uid
40+
}
41+
42+
// withClusterUID adds the given cluster UID to the context
43+
func withClusterUID(ctx context.Context, clusterUID string) context.Context {
44+
return context.WithValue(ctx, clusterUIDKey{}, clusterUID)
45+
}

pkg/clusteruid/clusteruid_test.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package clusteruid
2+
3+
import (
4+
"testing"
5+
6+
"k8s.io/client-go/kubernetes/fake"
7+
8+
corev1 "k8s.io/api/core/v1"
9+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
10+
"k8s.io/apimachinery/pkg/types"
11+
)
12+
13+
func TestGetClusterUID(t *testing.T) {
14+
client := fake.NewSimpleClientset()
15+
16+
mockUID := "12345678-1234-5678-1234-567812345678"
17+
18+
kubeSystemNS := &corev1.Namespace{
19+
ObjectMeta: metav1.ObjectMeta{
20+
Name: "kube-system",
21+
UID: types.UID(mockUID),
22+
},
23+
}
24+
25+
_, err := client.CoreV1().Namespaces().Create(t.Context(), kubeSystemNS, metav1.CreateOptions{})
26+
if err != nil {
27+
t.Fatalf("failed to create kube-system namespace with fake client: %v", err)
28+
}
29+
30+
ctx, err := GetClusterUID(t.Context(), client)
31+
if err != nil {
32+
t.Fatalf("expected no error, got %v", err)
33+
}
34+
35+
uid := ClusterUIDFromContext(ctx)
36+
37+
if uid != mockUID {
38+
t.Fatalf("expected to get uid=%v, but got uid=%v", mockUID, uid)
39+
}
40+
}

0 commit comments

Comments
 (0)