Skip to content

Commit

Permalink
[APM Onboarding] Use creation timestamp and UID of DatadogAgent as en…
Browse files Browse the repository at this point in the history
…v vars for APM Telemetry KPIs (#1157) (#1164)

* Use UUID and timestamp generated during DatadogAgent installation

* Fix unit tests

* Use util functions

(cherry picked from commit 77d0df8)

Co-authored-by: Liliya Belaus <59583867+liliyadd@users.noreply.github.com>
  • Loading branch information
fanny-jiang and liliyadd committed May 2, 2024
1 parent 70db137 commit ac65f38
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 25 deletions.
20 changes: 14 additions & 6 deletions apis/datadoghq/v1alpha1/test/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ package test

import (
"fmt"
"strconv"
"time"

appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"

apicommon "github.com/DataDog/datadog-operator/apis/datadoghq/common"
commonv1 "github.com/DataDog/datadog-operator/apis/datadoghq/common/v1"
Expand All @@ -20,6 +22,7 @@ import (
"github.com/DataDog/datadog-operator/controllers/datadogagent/component"
"github.com/DataDog/datadog-operator/pkg/controller/utils/comparison"
"github.com/DataDog/datadog-operator/pkg/defaulting"
"github.com/google/uuid"

edsdatadoghqv1alpha1 "github.com/DataDog/extendeddaemonset/api/v1alpha1"
apiregistrationv1 "k8s.io/kube-aggregator/pkg/apis/apiregistration/v1"
Expand All @@ -30,6 +33,9 @@ var (
apiVersion = fmt.Sprintf("%s/%s", datadoghqv1alpha1.GroupVersion.Group, datadoghqv1alpha1.GroupVersion.Version)
pullPolicy = corev1.PullIfNotPresent
defaultImage = defaulting.GetLatestAgentImage()
// AgentInstallTime records the Agent install time
AgentInstallTime = metav1.NewTime(time.Now())
AgentInstallId = types.UID(uuid.New().String())
)

// NewDatadogAgentOptions set of option for the DatadogAgent creation
Expand Down Expand Up @@ -108,10 +114,12 @@ func NewDefaultedDatadogAgent(ns, name string, options *NewDatadogAgentOptions)
APIVersion: apiVersion,
},
ObjectMeta: metav1.ObjectMeta{
Namespace: ns,
Name: name,
Labels: map[string]string{},
Finalizers: []string{"finalizer.agent.datadoghq.com"},
Namespace: ns,
Name: name,
Labels: map[string]string{},
Finalizers: []string{"finalizer.agent.datadoghq.com"},
CreationTimestamp: AgentInstallTime,
UID: AgentInstallId,
},
}
ad.Spec = datadoghqv1alpha1.DatadogAgentSpec{
Expand Down Expand Up @@ -310,11 +318,11 @@ func NewDefaultedDatadogAgent(ns, name string, options *NewDatadogAgentOptions)
ad.Spec.Agent.Apm.Env = []corev1.EnvVar{
{
Name: apicommon.DDAPMInstrumentationInstallId,
Value: component.AgentInstallId,
Value: string(ad.GetUID()),
},
{
Name: apicommon.DDAPMInstrumentationInstallTime,
Value: component.AgentInstallTime,
Value: strconv.FormatInt(ad.GetCreationTimestamp().Unix(), 10),
},
{
Name: apicommon.DDAPMInstrumentationInstallType,
Expand Down
4 changes: 2 additions & 2 deletions controllers/datadogagent/agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -948,15 +948,15 @@ func defaultAPMContainerEnvVars() []corev1.EnvVar {
},
{
Name: "DD_INSTRUMENTATION_INSTALL_TIME",
Value: component.AgentInstallTime,
Value: strconv.FormatInt(test.AgentInstallTime.Unix(), 10),
},
{
Name: "DD_INSTRUMENTATION_INSTALL_TYPE",
Value: component.DefaultAgentInstallType,
},
{
Name: "DD_INSTRUMENTATION_INSTALL_ID",
Value: component.AgentInstallId,
Value: string(test.AgentInstallId),
},
}
}
Expand Down
4 changes: 2 additions & 2 deletions controllers/datadogagent/clusteragent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,15 +208,15 @@ func clusterAgentDefaultEnvVars() []corev1.EnvVar {
},
{
Name: "DD_INSTRUMENTATION_INSTALL_TIME",
Value: component.AgentInstallTime,
Value: strconv.FormatInt(test.AgentInstallTime.Time.Unix(), 10),
},
{
Name: "DD_INSTRUMENTATION_INSTALL_TYPE",
Value: component.DefaultAgentInstallType,
},
{
Name: "DD_INSTRUMENTATION_INSTALL_ID",
Value: component.AgentInstallId,
Value: string(test.AgentInstallId),
},
}
}
Expand Down
5 changes: 3 additions & 2 deletions controllers/datadogagent/component/agent/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/DataDog/datadog-operator/controllers/datadogagent/component"
componentdca "github.com/DataDog/datadog-operator/controllers/datadogagent/component/clusteragent"
"github.com/DataDog/datadog-operator/controllers/datadogagent/feature"
"github.com/DataDog/datadog-operator/pkg/controller/utils"
"github.com/DataDog/datadog-operator/pkg/defaulting"

appsv1 "k8s.io/api/apps/v1"
Expand Down Expand Up @@ -311,11 +312,11 @@ func envVarsForTraceAgent(dda metav1.Object) []corev1.EnvVar {
envs := []corev1.EnvVar{
{
Name: apicommon.DDAPMInstrumentationInstallId,
Value: component.AgentInstallId,
Value: utils.GetDatadogAgentResourceUID(dda),
},
{
Name: apicommon.DDAPMInstrumentationInstallTime,
Value: component.AgentInstallTime,
Value: utils.GetDatadogAgentResourceCreationTime(dda),
},
{
Name: apicommon.DDAPMInstrumentationInstallType,
Expand Down
5 changes: 2 additions & 3 deletions controllers/datadogagent/component/clusteragent/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import (
// NewDefaultClusterAgentDeployment return a new default cluster-agent deployment
func NewDefaultClusterAgentDeployment(dda metav1.Object) *appsv1.Deployment {
deployment := component.NewDeployment(dda, apicommon.DefaultClusterAgentResourceSuffix, GetClusterAgentName(dda), GetClusterAgentVersion(dda), nil)

podTemplate := NewDefaultClusterAgentPodTemplateSpec(dda)
for key, val := range deployment.GetLabels() {
podTemplate.Labels[key] = val
Expand Down Expand Up @@ -147,11 +146,11 @@ func defaultEnvVars(dda metav1.Object) []corev1.EnvVar {
},
{
Name: apicommon.DDAPMInstrumentationInstallId,
Value: component.AgentInstallId,
Value: utils.GetDatadogAgentResourceUID(dda),
},
{
Name: apicommon.DDAPMInstrumentationInstallTime,
Value: component.AgentInstallTime,
Value: utils.GetDatadogAgentResourceCreationTime(dda),
},
{
Name: apicommon.DDAPMInstrumentationInstallType,
Expand Down
10 changes: 0 additions & 10 deletions controllers/datadogagent/component/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,13 @@ import (
"fmt"
"strconv"
"strings"
"time"

corev1 "k8s.io/api/core/v1"
netv1 "k8s.io/api/networking/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
"k8s.io/apimachinery/pkg/version"

"github.com/google/uuid"

apicommon "github.com/DataDog/datadog-operator/apis/datadoghq/common"
commonv1 "github.com/DataDog/datadog-operator/apis/datadoghq/common/v1"
"github.com/DataDog/datadog-operator/apis/datadoghq/v2alpha1"
Expand All @@ -34,13 +31,6 @@ const (
DefaultAgentInstallType = "k8s_manual"
)

var (
// AgentInstallTime records the Agent install time
AgentInstallTime = strconv.FormatInt(time.Now().Unix(), 10)

AgentInstallId = uuid.NewString()
)

// GetVolumeForConfig return the volume that contains the agent config
func GetVolumeForConfig() corev1.Volume {
return corev1.Volume{
Expand Down
11 changes: 11 additions & 0 deletions pkg/controller/utils/shared_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package utils

import (
"fmt"
"strconv"

"sigs.k8s.io/controller-runtime/pkg/reconcile"

Expand All @@ -32,3 +33,13 @@ func GetDatadogAgentResourceNamespace(dda metav1.Object) string {
func GetDatadogTokenResourceName(dda metav1.Object) string {
return fmt.Sprintf("%stoken", dda.GetName())
}

// GetDatadogAgentResourceNamespace returns the UID of the Datadog Agent Resource
func GetDatadogAgentResourceUID(dda metav1.Object) string {
return string(dda.GetUID())
}

// GetDatadogAgentResourceCreationTime returns the creation timestamp of the Datadog Agent Resource
func GetDatadogAgentResourceCreationTime(dda metav1.Object) string {
return strconv.FormatInt(dda.GetCreationTimestamp().Unix(), 10)
}

0 comments on commit ac65f38

Please sign in to comment.