Skip to content

[Telemetry] UUID #3039

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion cmd/telemetry/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ func init() {
}

// cluster-role
// +kubebuilder:rbac:groups=core,resources=configmaps;nodes;pods,verbs=get;list;watch
// +kubebuilder:rbac:groups=core,resources=nodes;pods,verbs=get;list;watch
// +kubebuilder:rbac:groups=core,resources=configmaps,verbs=get;list;watch;create;update
// +kubebuilder:rbac:groups=core.liqo.io,resources=foreignclusters,verbs=get;list;watch
// +kubebuilder:rbac:groups=offloading.liqo.io,resources=namespaceoffloadings,verbs=get;list;watch
// +kubebuilder:rbac:groups=offloading.liqo.io,resources=virtualnodes,verbs=get;list;watch
Expand Down
9 changes: 9 additions & 0 deletions deployments/liqo/files/liqo-telemetry-ClusterRole.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,15 @@ rules:
- ""
resources:
- configmaps
verbs:
- create
- get
- list
- update
- watch
- apiGroups:
- ""
resources:
- nodes
- pods
verbs:
Expand Down
11 changes: 10 additions & 1 deletion pkg/consts/clusterid.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,21 @@ package consts
import "k8s.io/apimachinery/pkg/labels"

const (
// ClusterIDLabelName is the name of the label key to use with Cluster ID.
// ClusterIDLabelName is the name of the label key to use with cluster-id.
ClusterIDLabelName = "clusterID"
// ClusterIDConfigMapKey is the key of the configmap where the cluster-id is stored.
ClusterIDConfigMapKey = "CLUSTER_ID"
// ClusterIDConfigMapNameLabelValue value of the name key of the configmap used to get it by label.
ClusterIDConfigMapNameLabelValue = "clusterid-configmap"

// ClusterIDTelemetryLabelName is the name of the label key to use with telemetry cluster-id.
ClusterIDTelemetryLabelName = "clusterIDTelemetry"
// ClusterIDTelemetryConfigMapKey is the key of the configmap where the telemetry cluster-id is stored.
ClusterIDTelemetryConfigMapKey = "CLUSTER_ID_TELEMETRY"
// ClusterIDTelemetryConfigMapNameLabelValue value of the name key of the configmap used to get it by label.
ClusterIDTelemetryConfigMapNameLabelValue = "clusterid-telemetry-configmap"
// ClusterIDTelemetryConfigMapName is the name of the configmap where the telemetry cluster-id is stored.
ClusterIDTelemetryConfigMapName = "telemetry-identity"
)

// ClusterIDConfigMapSelector returns the selector for the configmap where the cluster-id is stored.
Expand Down
14 changes: 12 additions & 2 deletions pkg/telemetry/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ package telemetry

import (
"context"
"fmt"
"time"

corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/klog/v2"
Expand All @@ -34,9 +36,17 @@ import (

// ForgeTelemetryItem returns a Telemetry item with the current status of the cluster.
func (c *Builder) ForgeTelemetryItem(ctx context.Context) (*Telemetry, error) {
clusterID, err := utils.GetClusterIDWithControllerClient(ctx, c.Client, c.Namespace)
clusterID, err := utils.GetClusterIDTelemetryWithControllerClient(ctx, c.Client, c.Namespace)
if err != nil {
return nil, err
switch {
case apierrors.IsNotFound(err):
klog.V(4).InfoS("ClusterID not found, creating a new one")
if err := createClusterIDTelemetryConfigMap(ctx, c.Client, c.Namespace); err != nil {
return nil, fmt.Errorf("failed to create configmap: %w", err)
}
default:
return nil, fmt.Errorf("failed to get clusterID: %w", err)
}
}

return &Telemetry{
Expand Down
23 changes: 23 additions & 0 deletions pkg/telemetry/identity.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright 2019-2025 The Liqo Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package telemetry

import (
"github.com/google/uuid"
)

func generateClusterIDTelemetry() string {
return uuid.New().String()
}
46 changes: 46 additions & 0 deletions pkg/telemetry/k8s.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright 2019-2025 The Liqo Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package telemetry

import (
"context"
"fmt"

corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client"

"github.com/liqotech/liqo/pkg/consts"
)

func createClusterIDTelemetryConfigMap(ctx context.Context, cl client.Client, namespace string) error {
cm := &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: consts.ClusterIDTelemetryConfigMapName,
Namespace: namespace,
Labels: map[string]string{
consts.K8sAppNameKey: consts.ClusterIDTelemetryConfigMapNameLabelValue,
},
},
Data: map[string]string{
consts.ClusterIDTelemetryConfigMapKey: generateClusterIDTelemetry(),
},
}

if err := cl.Create(ctx, cm); err != nil {
return fmt.Errorf("failed to create configmap: %w", err)
}
return nil
}
18 changes: 18 additions & 0 deletions pkg/utils/cluster_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,24 @@ func GetClusterIDWithControllerClient(ctx context.Context, cl client.Client, nam
return clusterID, nil
}

// GetClusterIDTelemetryWithControllerClient returns telemetry cluster identity using a client.Client client.
func GetClusterIDTelemetryWithControllerClient(ctx context.Context, cl client.Client, namespace string) (string, error) {
selector, err := metav1.LabelSelectorAsSelector(&liqolabels.ClusterIDTelemetryConfigMapLabelSelector)
if err != nil {
return "", err
}
cm, err := liqogetters.GetConfigMapByLabel(ctx, cl, namespace, selector)
if err != nil {
return "", err
}
clusterID, err := liqogetters.RetrieveClusterIDTelemetryFromConfigMap(cm)
if err != nil {
return "", err
}

return clusterID, nil
}

// GetClusterID returns the local clusterID.
func GetClusterID(ctx context.Context, cl kubernetes.Interface, namespace string) (liqov1beta1.ClusterID, error) {
clusterID, err := GetClusterIDWithNativeClient(ctx, cl, namespace)
Expand Down
11 changes: 11 additions & 0 deletions pkg/utils/getters/dataGetters.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,17 @@ func RetrieveClusterIDFromConfigMap(cm *corev1.ConfigMap) (liqov1beta1.ClusterID
return liqov1beta1.ClusterID(id), nil
}

// RetrieveClusterIDTelemetryFromConfigMap retrieves telemetry ClusterID from a given configmap.
func RetrieveClusterIDTelemetryFromConfigMap(cm *corev1.ConfigMap) (string, error) {
id, found := cm.Data[liqoconsts.ClusterIDTelemetryConfigMapKey]
if !found {
return "", fmt.Errorf("unable to get cluster ID telemetry: field {%s} not found in configmap {%s/%s}",
liqoconsts.ClusterIDTelemetryConfigMapKey, cm.Namespace, cm.Name)
}

return id, nil
}

// RetrieveEndpointFromService retrieves an ip address and port from a given service object
// based on the service and port name.
func RetrieveEndpointFromService(svc *corev1.Service, svcType corev1.ServiceType, portName string) (endpointIP, endpointPort string, err error) {
Expand Down
11 changes: 11 additions & 0 deletions pkg/utils/labels/labelSelectors.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,17 @@ var (
},
}

// ClusterIDTelemetryConfigMapLabelSelector selector used to get the cluster id telemetry configmap.
ClusterIDTelemetryConfigMapLabelSelector = metav1.LabelSelector{
MatchExpressions: []metav1.LabelSelectorRequirement{
{
Key: liqoconst.K8sAppNameKey,
Operator: metav1.LabelSelectorOpIn,
Values: []string{liqoconst.ClusterIDTelemetryConfigMapNameLabelValue},
},
},
}

// ProxyServiceLabelSelector selector used to get the gateway service.
ProxyServiceLabelSelector = metav1.LabelSelector{
MatchExpressions: []metav1.LabelSelectorRequirement{
Expand Down