Skip to content

Commit

Permalink
[cleanup] reorganize constant files (#1399)
Browse files Browse the repository at this point in the history
  • Loading branch information
celenechang authored and mftoure committed Oct 3, 2024
1 parent d30e73c commit 54e1c11
Show file tree
Hide file tree
Showing 14 changed files with 174 additions and 158 deletions.
3 changes: 2 additions & 1 deletion api/datadoghq/common/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import (
appsv1 "k8s.io/api/apps/v1"
)

// Datadog const value
// This file tracks constants related to setting up the Datadog Agents

const (
// AgentDeploymentNameLabelKey label key use to link a Resource to a DatadogAgent
AgentDeploymentNameLabelKey = "agent.datadoghq.com/name"
Expand Down
12 changes: 7 additions & 5 deletions api/datadoghq/v2alpha1/const.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2016-present Datadog, Inc.

package v2alpha1

// This file tracks constants closely related to the CRD, such as ConditionTypes

const (

// ClusterAgentReconcileConditionType ReconcileConditionType for Cluster Agent component
Expand All @@ -12,9 +19,4 @@ const (
OverrideReconcileConflictConditionType = "OverrideReconcileConflict"
// DatadogAgentReconcileErrorConditionType ReconcileConditionType for DatadogAgent reconcile error
DatadogAgentReconcileErrorConditionType = "DatadogAgentReconcileError"

// ExtraConfdConfigMapName is the name of the ConfigMap storing Custom Confd data
ExtraConfdConfigMapName = "%s-extra-confd"
// ExtraChecksdConfigMapName is the name of the ConfigMap storing Custom Checksd data
ExtraChecksdConfigMapName = "%s-extra-checksd"
)
2 changes: 2 additions & 0 deletions internal/controller/datadogagent/common/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

package common

// This file tracks constants used in features, component default code

// Resource names
const (
DatadogTokenOldResourceName = "datadogtoken" // Kept for backward compatibility with agent <7.37.0
Expand Down
25 changes: 0 additions & 25 deletions internal/controller/datadogagent/const.go

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ func (r *Reconciler) deleteV2DaemonSet(logger logr.Logger, dda *datadoghqv2alpha
return err
}
logger.Info("Delete DaemonSet", "daemonSet.Namespace", ds.Namespace, "daemonSet.Name", ds.Name)
event := buildEventInfo(ds.Name, ds.Namespace, daemonSetKind, datadog.DeletionEvent)
event := buildEventInfo(ds.Name, ds.Namespace, kubernetes.DaemonSetKind, datadog.DeletionEvent)
r.recordEvent(dda, event)
removeStaleStatus(newStatus, ds.Name)

Expand All @@ -233,7 +233,7 @@ func (r *Reconciler) deleteV2ExtendedDaemonSet(logger logr.Logger, dda *datadogh
return err
}
logger.Info("Delete DaemonSet", "extendedDaemonSet.Namespace", eds.Namespace, "extendedDaemonSet.Name", eds.Name)
event := buildEventInfo(eds.Name, eds.Namespace, extendedDaemonSetKind, datadog.DeletionEvent)
event := buildEventInfo(eds.Name, eds.Namespace, kubernetes.ExtendedDaemonSetKind, datadog.DeletionEvent)
r.recordEvent(dda, event)
removeStaleStatus(newStatus, eds.Name)

Expand Down
5 changes: 3 additions & 2 deletions internal/controller/datadogagent/controller_reconcile_ccr.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@ import (
"github.com/DataDog/datadog-operator/internal/controller/datadogagent/feature"
"github.com/DataDog/datadog-operator/internal/controller/datadogagent/override"
"github.com/DataDog/datadog-operator/pkg/controller/utils/datadog"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"github.com/DataDog/datadog-operator/pkg/kubernetes"

"github.com/go-logr/logr"
appsv1 "k8s.io/api/apps/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
)
Expand Down Expand Up @@ -102,7 +103,7 @@ func (r *Reconciler) cleanupV2ClusterChecksRunner(logger logr.Logger, dda *datad
}
} else {
logger.Info("Deleting Cluster Checks Runner Deployment", "deployment.Namespace", ClusterChecksRunnerDeployment.Namespace, "deployment.Name", ClusterChecksRunnerDeployment.Name)
event := buildEventInfo(ClusterChecksRunnerDeployment.Name, ClusterChecksRunnerDeployment.Namespace, deploymentKind, datadog.DeletionEvent)
event := buildEventInfo(ClusterChecksRunnerDeployment.Name, ClusterChecksRunnerDeployment.Namespace, kubernetes.DeploymentKind, datadog.DeletionEvent)
r.recordEvent(dda, event)
if err := r.client.Delete(context.TODO(), ClusterChecksRunnerDeployment); err != nil {
return reconcile.Result{}, err
Expand Down
3 changes: 2 additions & 1 deletion internal/controller/datadogagent/controller_reconcile_dca.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/DataDog/datadog-operator/internal/controller/datadogagent/feature"
"github.com/DataDog/datadog-operator/internal/controller/datadogagent/override"
"github.com/DataDog/datadog-operator/pkg/controller/utils/datadog"
"github.com/DataDog/datadog-operator/pkg/kubernetes"

"github.com/go-logr/logr"
appsv1 "k8s.io/api/apps/v1"
Expand Down Expand Up @@ -108,7 +109,7 @@ func (r *Reconciler) cleanupV2ClusterAgent(logger logr.Logger, dda *datadoghqv2a
return reconcile.Result{}, err
}
logger.Info("Deleting Cluster Agent Deployment", "deployment.Namespace", clusterAgentDeployment.Namespace, "deployment.Name", clusterAgentDeployment.Name)
event := buildEventInfo(clusterAgentDeployment.Name, clusterAgentDeployment.Namespace, clusterRoleBindingKind, datadog.DeletionEvent)
event := buildEventInfo(clusterAgentDeployment.Name, clusterAgentDeployment.Namespace, kubernetes.ClusterRoleBindingKind, datadog.DeletionEvent)
r.recordEvent(dda, event)
if err := r.client.Delete(context.TODO(), clusterAgentDeployment); err != nil {
return reconcile.Result{}, err
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func (r *Reconciler) createOrUpdateDeployment(parentLogger logr.Logger, dda *dat
updateStatusFunc(nil, newStatus, now, metav1.ConditionFalse, updateSucceeded, "Unable to update Deployment")
return reconcile.Result{}, err
}
event := buildEventInfo(updateDeployment.Name, updateDeployment.Namespace, deploymentKind, datadog.UpdateEvent)
event := buildEventInfo(updateDeployment.Name, updateDeployment.Namespace, kubernetes.DeploymentKind, datadog.UpdateEvent)
r.recordEvent(dda, event)
updateStatusFunc(updateDeployment, newStatus, now, metav1.ConditionTrue, updateSucceeded, "Deployment updated")
} else {
Expand All @@ -114,7 +114,7 @@ func (r *Reconciler) createOrUpdateDeployment(parentLogger logr.Logger, dda *dat
updateStatusFunc(nil, newStatus, now, metav1.ConditionFalse, createSucceeded, "Unable to create Deployment")
return reconcile.Result{}, err
}
event := buildEventInfo(deployment.Name, deployment.Namespace, deploymentKind, datadog.CreationEvent)
event := buildEventInfo(deployment.Name, deployment.Namespace, kubernetes.DeploymentKind, datadog.CreationEvent)
r.recordEvent(dda, event)
updateStatusFunc(deployment, newStatus, now, metav1.ConditionTrue, createSucceeded, "Deployment created")
}
Expand Down Expand Up @@ -245,7 +245,7 @@ func (r *Reconciler) createOrUpdateDaemonset(parentLogger logr.Logger, dda *data
updateStatusFunc(updateDaemonset, newStatus, now, metav1.ConditionFalse, updateSucceeded, "Unable to update Daemonset")
return reconcile.Result{}, err
}
event := buildEventInfo(updateDaemonset.Name, updateDaemonset.Namespace, daemonSetKind, datadog.UpdateEvent)
event := buildEventInfo(updateDaemonset.Name, updateDaemonset.Namespace, kubernetes.DaemonSetKind, datadog.UpdateEvent)
r.recordEvent(dda, event)
updateStatusFunc(updateDaemonset, newStatus, now, metav1.ConditionTrue, updateSucceeded, "Daemonset updated")
} else {
Expand All @@ -262,7 +262,7 @@ func (r *Reconciler) createOrUpdateDaemonset(parentLogger logr.Logger, dda *data
updateStatusFunc(nil, newStatus, now, metav1.ConditionFalse, createSucceeded, "Unable to create Daemonset")
return reconcile.Result{}, err
}
event := buildEventInfo(daemonset.Name, daemonset.Namespace, daemonSetKind, datadog.CreationEvent)
event := buildEventInfo(daemonset.Name, daemonset.Namespace, kubernetes.DaemonSetKind, datadog.CreationEvent)
r.recordEvent(dda, event)
updateStatusFunc(daemonset, newStatus, now, metav1.ConditionTrue, createSucceeded, "Daemonset created")
}
Expand Down Expand Up @@ -342,7 +342,7 @@ func (r *Reconciler) createOrUpdateExtendedDaemonset(parentLogger logr.Logger, d
updateStatusFunc(updateEDS, newStatus, now, metav1.ConditionFalse, updateSucceeded, "Unable to update ExtendedDaemonSet")
return reconcile.Result{}, err
}
event := buildEventInfo(updateEDS.Name, updateEDS.Namespace, extendedDaemonSetKind, datadog.UpdateEvent)
event := buildEventInfo(updateEDS.Name, updateEDS.Namespace, kubernetes.ExtendedDaemonSetKind, datadog.UpdateEvent)
r.recordEvent(dda, event)
updateStatusFunc(updateEDS, newStatus, now, metav1.ConditionTrue, updateSucceeded, "ExtendedDaemonSet updated")
} else {
Expand All @@ -353,7 +353,7 @@ func (r *Reconciler) createOrUpdateExtendedDaemonset(parentLogger logr.Logger, d
updateStatusFunc(nil, newStatus, now, metav1.ConditionFalse, createSucceeded, "Unable to create ExtendedDaemonSet")
return reconcile.Result{}, err
}
event := buildEventInfo(eds.Name, eds.Namespace, extendedDaemonSetKind, datadog.CreationEvent)
event := buildEventInfo(eds.Name, eds.Namespace, kubernetes.ExtendedDaemonSetKind, datadog.CreationEvent)
r.recordEvent(dda, event)
updateStatusFunc(eds, newStatus, now, metav1.ConditionTrue, createSucceeded, "ExtendedDaemonSet created")
}
Expand Down
10 changes: 6 additions & 4 deletions internal/controller/datadogagent/finalizer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
agenttestutils "github.com/DataDog/datadog-operator/internal/controller/datadogagent/testutils"
"github.com/DataDog/datadog-operator/internal/controller/testutils"
"github.com/DataDog/datadog-operator/pkg/agentprofile"
"github.com/DataDog/datadog-operator/pkg/kubernetes/rbac"

"github.com/stretchr/testify/assert"
corev1 "k8s.io/api/core/v1"
rbacv1 "k8s.io/api/rbac/v1"
Expand Down Expand Up @@ -47,7 +49,7 @@ func Test_handleFinalizer(t *testing.T) {
existingClusterRoles := []*rbacv1.ClusterRole{
{
TypeMeta: metav1.TypeMeta{
Kind: clusterRoleKind,
Kind: rbac.ClusterRoleKind,
APIVersion: rbacv1.SchemeGroupVersion.String(),
},
ObjectMeta: metav1.ObjectMeta{
Expand All @@ -59,7 +61,7 @@ func Test_handleFinalizer(t *testing.T) {
},
{
TypeMeta: metav1.TypeMeta{
Kind: clusterRoleKind,
Kind: rbac.ClusterRoleKind,
APIVersion: rbacv1.SchemeGroupVersion.String(),
},
ObjectMeta: metav1.ObjectMeta{
Expand All @@ -76,7 +78,7 @@ func Test_handleFinalizer(t *testing.T) {
existingClusterRoleBindings := []*rbacv1.ClusterRoleBinding{
{
TypeMeta: metav1.TypeMeta{
Kind: clusterRoleBindingKind,
Kind: rbac.ClusterRoleBindingKind,
APIVersion: rbacv1.SchemeGroupVersion.String(),
},
ObjectMeta: metav1.ObjectMeta{
Expand All @@ -88,7 +90,7 @@ func Test_handleFinalizer(t *testing.T) {
},
{
TypeMeta: metav1.TypeMeta{
Kind: clusterRoleBindingKind,
Kind: rbac.ClusterRoleBindingKind,
APIVersion: rbacv1.SchemeGroupVersion.String(),
},
ObjectMeta: metav1.ObjectMeta{
Expand Down
15 changes: 15 additions & 0 deletions internal/controller/datadogagent/override/const.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2016-present Datadog, Inc.

package override

// This file tracks constants used in overrides

const (
// extraConfdConfigMapName is the name of the ConfigMap storing Custom Confd data
extraConfdConfigMapName = "%s-extra-confd"
// extraChecksdConfigMapName is the name of the ConfigMap storing Custom Checksd data
extraChecksdConfigMapName = "%s-extra-checksd"
)
4 changes: 2 additions & 2 deletions internal/controller/datadogagent/override/dependencies.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ func Dependencies(logger logr.Logger, manager feature.ResourceManagers, dda *v2a
errs = append(errs, overrideCustomConfigs(logger, manager, override.CustomConfigurations, dda.Name, namespace)...)

// Handle custom check configurations
confdCMName := fmt.Sprintf(v2alpha1.ExtraConfdConfigMapName, strings.ToLower((string(component))))
confdCMName := fmt.Sprintf(extraConfdConfigMapName, strings.ToLower((string(component))))
errs = append(errs, overrideExtraConfigs(logger, manager, override.ExtraConfd, namespace, confdCMName, true)...)

// Handle custom check files
checksdCMName := fmt.Sprintf(v2alpha1.ExtraChecksdConfigMapName, strings.ToLower((string(component))))
checksdCMName := fmt.Sprintf(extraChecksdConfigMapName, strings.ToLower((string(component))))
errs = append(errs, overrideExtraConfigs(logger, manager, override.ExtraChecksd, namespace, checksdCMName, false)...)
}

Expand Down
4 changes: 2 additions & 2 deletions internal/controller/datadogagent/override/podtemplatespec.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func PodTemplateSpec(logger logr.Logger, manager feature.PodTemplateManagers, ov
// defined in the init container; just overwrite the Volume to mount the ConfigMap instead of an EmptyDir.
// If both ConfigMap and ConfigData exist, ConfigMap has higher priority.
if override.ExtraConfd != nil {
cmName := fmt.Sprintf(v2alpha1.ExtraConfdConfigMapName, strings.ToLower((string(componentName))))
cmName := fmt.Sprintf(extraConfdConfigMapName, strings.ToLower((string(componentName))))
vol := volume.GetVolumeFromMultiCustomConfig(override.ExtraConfd, apicommon.ConfdVolumeName, cmName)
manager.Volume().AddVolume(&vol)

Expand All @@ -86,7 +86,7 @@ func PodTemplateSpec(logger logr.Logger, manager feature.PodTemplateManagers, ov

// If both ConfigMap and ConfigData exist, ConfigMap has higher priority.
if override.ExtraChecksd != nil {
cmName := fmt.Sprintf(v2alpha1.ExtraChecksdConfigMapName, strings.ToLower((string(componentName))))
cmName := fmt.Sprintf(extraChecksdConfigMapName, strings.ToLower((string(componentName))))
vol := volume.GetVolumeFromMultiCustomConfig(override.ExtraChecksd, apicommon.ChecksdVolumeName, cmName)
manager.Volume().AddVolume(&vol)

Expand Down
Loading

0 comments on commit 54e1c11

Please sign in to comment.