Skip to content

Commit 4c3cbe5

Browse files
committed
Use a namespaces datagatherer to get the cluster ID
Remove obsolete clusteruid package Signed-off-by: Richard Wall <[email protected]>
1 parent 8b9a233 commit 4c3cbe5

File tree

7 files changed

+38
-107
lines changed

7 files changed

+38
-107
lines changed

pkg/agent/run.go

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

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

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-
10481
group, gctx := errgroup.WithContext(ctx)
10582
defer func() {
10683
cancel()

pkg/clusteruid/clusteruid.go

Lines changed: 0 additions & 45 deletions
This file was deleted.

pkg/clusteruid/clusteruid_test.go

Lines changed: 0 additions & 39 deletions
This file was deleted.

pkg/internal/cyberark/dataupload/snapshot.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,34 @@ func extractServerVersionFromReading(reading *api.DataReading) (string, error) {
6767
return data.ServerVersion.GitVersion, nil
6868
}
6969

70+
// extractClusterUIDFromReading converts the opaque data from a DynamicData
71+
// reading to Unstructured Namespace resources, and finds the UID of the
72+
// `kube-system` namespace.
73+
// This UID can be used as a unique identifier for the Kubernetes cluster.
74+
// - https://venafi.slack.com/archives/C04SQR5DAD7/p1747825325264979
75+
// - https://github.com/kubernetes/kubernetes/issues/77487#issuecomment-489786023
76+
func extractClusterUIDFromReading(reading *api.DataReading) (string, error) {
77+
resources, err := extractResourceListFromReading(reading)
78+
if err != nil {
79+
return "", err
80+
}
81+
for _, resource := range resources {
82+
if resource.GetName() == "kube-system" {
83+
return string(resource.GetUID()), nil
84+
}
85+
}
86+
return "", fmt.Errorf("kube-system namespace UID not found in data reading: %v", reading)
87+
}
88+
7089
// convertDataReadingsToCyberarkSnapshot converts DataReadings to the Cyberark
7190
// Snapshot format.
91+
// The ClusterUID is the UID of the kube-system namespace, which is assumed to
92+
// be unique to the cluster and assumed to never change.
7293
func convertDataReadingsToCyberarkSnapshot(
7394
readings []*api.DataReading,
7495
) (*snapshot, error) {
7596
k8sVersion := ""
97+
clusterID := ""
7698
resourceData := resourceData{}
7799
for _, reading := range readings {
78100
if reading.DataGatherer == "ark/discovery" {
@@ -82,6 +104,15 @@ func convertDataReadingsToCyberarkSnapshot(
82104
return nil, fmt.Errorf("while extracting server version from data-reading: %s", err)
83105
}
84106
}
107+
108+
if reading.DataGatherer == "ark/namespaces" {
109+
var err error
110+
clusterID, err = extractClusterUIDFromReading(reading)
111+
if err != nil {
112+
return nil, fmt.Errorf("while extracting cluster UID from data-reading: %s", err)
113+
}
114+
}
115+
85116
if key, found := gathererNameToResourceDataKeyMap[reading.DataGatherer]; found {
86117
resources, err := extractResourceListFromReading(reading)
87118
if err != nil {
@@ -94,6 +125,7 @@ func convertDataReadingsToCyberarkSnapshot(
94125
return &snapshot{
95126
AgentVersion: version.PreflightVersion,
96127
K8SVersion: k8sVersion,
128+
ClusterID: clusterID,
97129
Secrets: resourceData["secrets"],
98130
ServiceAccounts: resourceData["serviceaccounts"],
99131
Roles: resourceData["roles"],

pkg/internal/cyberark/dataupload/testdata/example-1/agent.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ data-gatherers:
44
# gather k8s apiserver version information
55
- kind: k8s-discovery
66
name: ark/discovery
7+
- kind: k8s-dynamic
8+
name: ark/namespaces
9+
config:
10+
resource-type:
11+
version: v1
12+
resource: namespaces
713
- kind: k8s-dynamic
814
name: ark/serviceaccounts
915
config:
Binary file not shown.
Binary file not shown.

0 commit comments

Comments
 (0)