Skip to content

Commit 9d0bb26

Browse files
committed
fix: telemetry UUID
1 parent 92a7724 commit 9d0bb26

File tree

7 files changed

+130
-3
lines changed

7 files changed

+130
-3
lines changed

pkg/consts/clusterid.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,21 @@ package consts
1717
import "k8s.io/apimachinery/pkg/labels"
1818

1919
const (
20-
// ClusterIDLabelName is the name of the label key to use with Cluster ID.
20+
// ClusterIDLabelName is the name of the label key to use with cluster-id.
2121
ClusterIDLabelName = "clusterID"
2222
// ClusterIDConfigMapKey is the key of the configmap where the cluster-id is stored.
2323
ClusterIDConfigMapKey = "CLUSTER_ID"
2424
// ClusterIDConfigMapNameLabelValue value of the name key of the configmap used to get it by label.
2525
ClusterIDConfigMapNameLabelValue = "clusterid-configmap"
26+
27+
// ClusterIDTelemetryLabelName is the name of the label key to use with telemetry cluster-id.
28+
ClusterIDTelemetryLabelName = "clusterIDTelemetry"
29+
// ClusterIDTelemetryConfigMapKey is the key of the configmap where the telemetry cluster-id is stored.
30+
ClusterIDTelemetryConfigMapKey = "CLUSTER_ID_TELEMETRY"
31+
// ClusterIDTelemetryConfigMapNameLabelValue value of the name key of the configmap used to get it by label.
32+
ClusterIDTelemetryConfigMapNameLabelValue = "clusterid-telemetry-configmap"
33+
// ClusterIDTelemetryConfigMapName is the name of the configmap where the telemetry cluster-id is stored.
34+
ClusterIDTelemetryConfigMapName = "telemetry-identity"
2635
)
2736

2837
// ClusterIDConfigMapSelector returns the selector for the configmap where the cluster-id is stored.

pkg/telemetry/builder.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,11 @@ package telemetry
1616

1717
import (
1818
"context"
19+
"fmt"
1920
"time"
2021

2122
corev1 "k8s.io/api/core/v1"
23+
apierrors "k8s.io/apimachinery/pkg/api/errors"
2224
"k8s.io/apimachinery/pkg/labels"
2325
"k8s.io/apimachinery/pkg/util/runtime"
2426
"k8s.io/klog/v2"
@@ -34,9 +36,16 @@ import (
3436

3537
// ForgeTelemetryItem returns a Telemetry item with the current status of the cluster.
3638
func (c *Builder) ForgeTelemetryItem(ctx context.Context) (*Telemetry, error) {
37-
clusterID, err := utils.GetClusterIDWithControllerClient(ctx, c.Client, c.Namespace)
39+
clusterID, err := utils.GetClusterIDTelemetryWithControllerClient(ctx, c.Client, c.Namespace)
3840
if err != nil {
39-
return nil, err
41+
switch{
42+
case apierrors.IsNotFound(err):
43+
klog.V(4).InfoS("ClusterID not found, creating a new one")
44+
if err := createClusterIDTelemetryConfigMap(ctx, c.Client, c.Namespace); err != nil {
45+
return nil, fmt.Errorf("failed to create configmap: %w", err)
46+
}
47+
default:
48+
}
4049
}
4150

4251
return &Telemetry{

pkg/telemetry/identity.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Copyright 2019-2025 The Liqo Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package telemetry
16+
17+
import (
18+
"github.com/google/uuid"
19+
)
20+
21+
func generateClusterIDTelemetry() string {
22+
return uuid.New().String()
23+
}

pkg/telemetry/k8s.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// Copyright 2019-2025 The Liqo Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package telemetry
16+
17+
import (
18+
"context"
19+
"fmt"
20+
21+
corev1 "k8s.io/api/core/v1"
22+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
23+
"sigs.k8s.io/controller-runtime/pkg/client"
24+
25+
"github.com/liqotech/liqo/pkg/consts"
26+
)
27+
28+
func createClusterIDTelemetryConfigMap(ctx context.Context, cl client.Client, namespace string) error {
29+
cm := &corev1.ConfigMap{
30+
ObjectMeta: metav1.ObjectMeta{
31+
Name: consts.ClusterIDTelemetryConfigMapName,
32+
Namespace: namespace,
33+
Labels: map[string]string{
34+
consts.K8sAppNameKey: consts.ClusterIDTelemetryConfigMapNameLabelValue,
35+
},
36+
},
37+
Data: map[string]string{
38+
consts.ClusterIDTelemetryConfigMapKey: generateClusterIDTelemetry(),
39+
},
40+
}
41+
42+
if err := cl.Create(ctx, cm); err != nil {
43+
return fmt.Errorf("failed to create configmap: %w", err)
44+
}
45+
return nil
46+
}

pkg/utils/cluster_info.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,24 @@ func GetClusterIDWithControllerClient(ctx context.Context, cl client.Client, nam
7474
return clusterID, nil
7575
}
7676

77+
// GetClusterIDTelemetryWithControllerClient returns telemetry cluster identity using a client.Client client.
78+
func GetClusterIDTelemetryWithControllerClient(ctx context.Context, cl client.Client, namespace string) (string, error) {
79+
selector, err := metav1.LabelSelectorAsSelector(&liqolabels.ClusterIDTelemetryConfigMapLabelSelector)
80+
if err != nil {
81+
return "", err
82+
}
83+
cm, err := liqogetters.GetConfigMapByLabel(ctx, cl, namespace, selector)
84+
if err != nil {
85+
return "", err
86+
}
87+
clusterID, err := liqogetters.RetrieveClusterIDTelemetryFromConfigMap(cm)
88+
if err != nil {
89+
return "", err
90+
}
91+
92+
return clusterID, nil
93+
}
94+
7795
// GetClusterID returns the local clusterID.
7896
func GetClusterID(ctx context.Context, cl kubernetes.Interface, namespace string) (liqov1beta1.ClusterID, error) {
7997
clusterID, err := GetClusterIDWithNativeClient(ctx, cl, namespace)

pkg/utils/getters/dataGetters.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,17 @@ func RetrieveClusterIDFromConfigMap(cm *corev1.ConfigMap) (liqov1beta1.ClusterID
6262
return liqov1beta1.ClusterID(id), nil
6363
}
6464

65+
// RetrieveClusterIDTelemetryFromConfigMap retrieves telemetry ClusterID from a given configmap.
66+
func RetrieveClusterIDTelemetryFromConfigMap(cm *corev1.ConfigMap) (string, error) {
67+
id, found := cm.Data[liqoconsts.ClusterIDTelemetryConfigMapKey]
68+
if !found {
69+
return "", fmt.Errorf("unable to get cluster ID telemetry: field {%s} not found in configmap {%s/%s}",
70+
liqoconsts.ClusterIDTelemetryConfigMapKey, cm.Namespace, cm.Name)
71+
}
72+
73+
return id, nil
74+
}
75+
6576
// RetrieveEndpointFromService retrieves an ip address and port from a given service object
6677
// based on the service and port name.
6778
func RetrieveEndpointFromService(svc *corev1.Service, svcType corev1.ServiceType, portName string) (endpointIP, endpointPort string, err error) {

pkg/utils/labels/labelSelectors.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,17 @@ var (
3939
},
4040
}
4141

42+
// ClusterIDTelemetryConfigMapLabelSelector selector used to get the cluster id telemetry configmap.
43+
ClusterIDTelemetryConfigMapLabelSelector = metav1.LabelSelector{
44+
MatchExpressions: []metav1.LabelSelectorRequirement{
45+
{
46+
Key: liqoconst.K8sAppNameKey,
47+
Operator: metav1.LabelSelectorOpIn,
48+
Values: []string{liqoconst.ClusterIDTelemetryConfigMapNameLabelValue},
49+
},
50+
},
51+
}
52+
4253
// ProxyServiceLabelSelector selector used to get the gateway service.
4354
ProxyServiceLabelSelector = metav1.LabelSelector{
4455
MatchExpressions: []metav1.LabelSelectorRequirement{

0 commit comments

Comments
 (0)