Skip to content

Commit

Permalink
[cleanup] move constants to feature-specific files (#1444)
Browse files Browse the repository at this point in the history
* [cleanup] move constants to feature-specific files

* update from review
  • Loading branch information
celenechang authored Oct 8, 2024
1 parent 1d9b69b commit 172caff
Show file tree
Hide file tree
Showing 34 changed files with 288 additions and 245 deletions.
134 changes: 34 additions & 100 deletions api/datadoghq/common/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,12 @@ const (

// Datadog volume names and mount paths
const (
ConfdVolumeName = "confd"
ConfdVolumePath = "/conf.d"
ConfigVolumeName = "config"
ConfigVolumePath = "/etc/datadog-agent"
KubeStateMetricCoreVolumeName = "ksm-core-config"
OrchestratorExplorerVolumeName = "orchestrator-explorer-config"
ChecksdVolumeName = "checksd"
ChecksdVolumePath = "/checks.d"
ConfdVolumeName = "confd"
ConfdVolumePath = "/conf.d"
ConfigVolumeName = "config"
ConfigVolumePath = "/etc/datadog-agent"
ChecksdVolumeName = "checksd"
ChecksdVolumePath = "/checks.d"

HostRootVolumeName = "hostroot"
HostRootHostPath = "/"
Expand All @@ -66,52 +64,13 @@ const (
SystemProbeOSReleaseDirVolumePath = "/etc/os-release"
SystemProbeOSReleaseDirMountPath = "/host/etc/os-release"

ContainerdDirVolumeName = "host-containerd-dir"
ContainerdDirVolumePath = "/var/lib/containerd"
ContainerdDirMountPath = "/host/var/lib/containerd"

ApkDirVolumeName = "host-apk-dir"
ApkDirVolumePath = "/var/lib/apk"
ApkDirMountPath = "/host/var/lib/apk"

DpkgDirVolumeName = "host-dpkg-dir"
DpkgDirVolumePath = "/var/lib/dpkg"
DpkgDirMountPath = "/host/var/lib/dpkg"

RpmDirVolumeName = "host-rpm-dir"
RpmDirVolumePath = "/var/lib/rpm"
RpmDirMountPath = "/host/var/lib/rpm"

RedhatReleaseVolumeName = "etc-redhat-release"
RedhatReleaseVolumePath = "/etc/redhat-release"
RedhatReleaseMountPath = "/host/etc/redhat-release"

FedoraReleaseVolumeName = "etc-fedora-release"
FedoraReleaseVolumePath = "/etc/fedora-release"
FedoraReleaseMountPath = "/host/etc/fedora-release"

LsbReleaseVolumeName = "etc-lsb-release"
LsbReleaseVolumePath = "/etc/lsb-release"
LsbReleaseMountPath = "/host/etc/lsb-release"

SystemReleaseVolumeName = "etc-system-release"
SystemReleaseVolumePath = "/etc/system-release"
SystemReleaseMountPath = "/host/etc/system-release"

SystemProbeSocketVolumeName = "sysprobe-socket-dir"
SystemProbeSocketVolumePath = "/var/run/sysprobe"

DebugfsVolumeName = "debugfs"
// same path on host and container
DebugfsPath = "/sys/kernel/debug"

TracefsVolumeName = "tracefs"
TracefsPath = "/sys/kernel/tracing"

SecurityfsVolumeName = "securityfs"
SecurityfsVolumePath = "/sys/kernel/security"
SecurityfsMountPath = "/host/sys/kernel/security"

ModulesVolumeName = "modules"
// same path on host and container
ModulesVolumePath = "/lib/modules"
Expand All @@ -124,56 +83,38 @@ const (
SystemProbeConfigVolumePath = "/etc/datadog-agent/system-probe.yaml"
OtelCustomConfigVolumePath = "/etc/datadog-agent/otel-config.yaml"

LogDatadogVolumeName = "logdatadog"
LogDatadogVolumePath = "/var/log/datadog"
TmpVolumeName = "tmp"
TmpVolumePath = "/tmp"
CertificatesVolumeName = "certificates"
CertificatesVolumePath = "/etc/datadog-agent/certificates"
AuthVolumeName = "datadog-agent-auth"
AuthVolumePath = "/etc/datadog-agent/auth"
InstallInfoVolumeName = "installinfo"
InstallInfoVolumeSubPath = "install_info"
InstallInfoVolumePath = "/etc/datadog-agent/install_info"
InstallInfoVolumeReadOnly = true
PointerVolumeName = "pointerdir"
PointerVolumePath = "/opt/datadog-agent/run"
LogTempStoragePath = "/var/lib/datadog-agent/logs"
PodLogVolumeName = "logpodpath"
PodLogVolumePath = "/var/log/pods"
ContainerLogVolumeName = "logcontainerpath"
ContainerLogVolumePath = "/var/lib/docker/containers"
SymlinkContainerVolumeName = "symlinkcontainerpath"
SymlinkContainerVolumePath = "/var/log/containers"
LogDatadogVolumeName = "logdatadog"
LogDatadogVolumePath = "/var/log/datadog"
DefaultLogTempStoragePath = "/var/lib/datadog-agent/logs"
TmpVolumeName = "tmp"
TmpVolumePath = "/tmp"
CertificatesVolumeName = "certificates"
CertificatesVolumePath = "/etc/datadog-agent/certificates"
AuthVolumeName = "datadog-agent-auth"
AuthVolumePath = "/etc/datadog-agent/auth"
InstallInfoVolumeName = "installinfo"
InstallInfoVolumeSubPath = "install_info"
InstallInfoVolumePath = "/etc/datadog-agent/install_info"
InstallInfoVolumeReadOnly = true

DogstatsdHostPortName = "dogstatsdport"
DogstatsdHostPortHostPort = 8125
DogstatsdSocketVolumeName = "dsdsocket"
DogstatsdAPMSocketHostPath = "/var/run/datadog"
DogstatsdSocketLocalPath = "/var/run/datadog"
DogstatsdSocketName = "dsd.socket"
// SecurityAgentComplianceCustomConfigDirVolumeName = "customcompliancebenchmarks" // Not used
SecurityAgentComplianceConfigDirVolumeName = "compliancedir"
SecurityAgentComplianceConfigDirVolumePath = "/etc/datadog-agent/compliance.d"
SecurityAgentRuntimeCustomPoliciesVolumeName = "customruntimepolicies"
SecurityAgentRuntimeCustomPoliciesVolumePath = "/etc/datadog-agent-runtime-policies"
SecurityAgentRuntimePoliciesDirVolumeName = "runtimepoliciesdir"
SecurityAgentRuntimePoliciesDirVolumePath = "/etc/datadog-agent/runtime-security.d"
HostCriSocketPathPrefix = "/host"
CriSocketVolumeName = "runtimesocketdir"
RuntimeDirVolumePath = "/var/run"
KubeletAgentCAPath = "/var/run/host-kubelet-ca.crt"
KubeletCAVolumeName = "kubelet-ca"
APMHostPortName = "traceport"
// APMHostPortHostPort = 8126 // Not used
APMSocketVolumeName = "apmsocket"
APMSocketVolumeLocalPath = "/var/run/datadog"
APMSocketName = "apm.socket"
AdmissionControllerPortName = "admissioncontrollerport"
AdmissionControllerSocketCommunicationMode = "socket"
ExternalMetricsPortName = "metricsapi"
ExternalMetricsAPIServiceName = "v1beta1.external.metrics.k8s.io"
OTLPGRPCPortName = "otlpgrpcport"
OTLPHTTPPortName = "otlphttpport"

HostCriSocketPathPrefix = "/host"
CriSocketVolumeName = "runtimesocketdir"
RuntimeDirVolumePath = "/var/run"

KubeletAgentCAPath = "/var/run/host-kubelet-ca.crt"
KubeletCAVolumeName = "kubelet-ca"

APMSocketName = "apm.socket"

ExternalMetricsAPIServiceName = "v1beta1.external.metrics.k8s.io"

SeccompSecurityVolumeName = "datadog-agent-security"
SeccompSecurityVolumePath = "/etc/config"
SeccompRootVolumeName = "seccomp-root"
Expand All @@ -185,15 +126,8 @@ const (

AppArmorAnnotationKey = "container.apparmor.security.beta.kubernetes.io"

AgentCustomConfigVolumeName = "custom-datadog-yaml"
AgentCustomConfigVolumeSubPath = "datadog.yaml"

ClusterAgentCustomConfigVolumeName = "custom-cluster-agent-yaml"
ClusterAgentCustomConfigVolumePath = "/etc/datadog-agent/datadog-cluster.yaml"
ClusterAgentCustomConfigVolumeSubPath = "datadog-cluster.yaml"

HelmCheckConfigVolumeName = "helm-check-config"
KubernetesAPIServerCheckConfigVolumeName = "kubernetes-apiserver-check-config"
AgentCustomConfigVolumeName = "custom-datadog-yaml"
ClusterAgentCustomConfigVolumeName = "custom-cluster-agent-yaml"

FIPSProxyCustomConfigVolumeName = "fips-proxy-cfg"
FIPSProxyCustomConfigFileName = "datadog-fips-proxy.cfg"
Expand Down
3 changes: 1 addition & 2 deletions api/datadoghq/v2alpha1/datadogagent_default.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ const (
defaultLogContainerLogsPath string = "/var/lib/docker/containers"
defaultLogPodLogsPath string = "/var/log/pods"
defaultLogContainerSymlinksPath string = "/var/log/containers"
defaultLogTempStoragePath string = "/var/lib/datadog-agent/logs"

defaultLiveProcessCollectionEnabled bool = false
defaultLiveContainerCollectionEnabled bool = true
Expand Down Expand Up @@ -205,7 +204,7 @@ func defaultFeaturesConfig(ddaSpec *DatadogAgentSpec) {

apiutils.DefaultStringIfUnset(&ddaSpec.Features.LogCollection.ContainerSymlinksPath, defaultLogContainerSymlinksPath)

apiutils.DefaultStringIfUnset(&ddaSpec.Features.LogCollection.TempStoragePath, defaultLogTempStoragePath)
apiutils.DefaultStringIfUnset(&ddaSpec.Features.LogCollection.TempStoragePath, apicommon.DefaultLogTempStoragePath)
}

// LiveContainerCollection Feature
Expand Down
3 changes: 2 additions & 1 deletion api/datadoghq/v2alpha1/datadogagent_default_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package v2alpha1
import (
"testing"

apicommon "github.com/DataDog/datadog-operator/api/datadoghq/common"
apiutils "github.com/DataDog/datadog-operator/api/utils"

"github.com/google/go-cmp/cmp"
Expand Down Expand Up @@ -619,7 +620,7 @@ func Test_defaultFeatures(t *testing.T) {
ContainerLogsPath: apiutils.NewStringPointer(defaultLogContainerLogsPath),
PodLogsPath: apiutils.NewStringPointer(defaultLogPodLogsPath),
ContainerSymlinksPath: apiutils.NewStringPointer(defaultLogContainerSymlinksPath),
TempStoragePath: apiutils.NewStringPointer(defaultLogTempStoragePath),
TempStoragePath: apiutils.NewStringPointer(apicommon.DefaultLogTempStoragePath),
},
LiveProcessCollection: &LiveProcessCollectionFeatureConfig{
Enabled: apiutils.NewBoolPointer(defaultLiveProcessCollectionEnabled),
Expand Down
2 changes: 2 additions & 0 deletions internal/controller/datadogagent/common/volumes.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import (
apicommon "github.com/DataDog/datadog-operator/api/datadoghq/common"
)

// This file contains definitions of volumes used in the agent specs

// GetVolumeForConfig return the volume that contains the agent config
func GetVolumeForConfig() corev1.Volume {
return corev1.Volume{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// 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 admissioncontroller

const (
admissionControllerPortName = "admissioncontrollerport"
admissionControllerSocketCommunicationMode = "socket"
)
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func (f *admissionControllerFeature) Configure(dda *v2alpha1.DatadogAgent) (reqC
dsd := dda.Spec.Features.Dogstatsd
if (apm != nil && apm.UnixDomainSocketConfig != nil && apiutils.BoolValue(apm.Enabled) && apiutils.BoolValue(apm.UnixDomainSocketConfig.Enabled)) ||
(dsd != nil && dsd.UnixDomainSocketConfig != nil && apiutils.BoolValue(dsd.UnixDomainSocketConfig.Enabled)) {
f.agentCommunicationMode = apicommon.AdmissionControllerSocketCommunicationMode
f.agentCommunicationMode = admissionControllerSocketCommunicationMode
}
// otherwise don't set to fall back to default agent setting `hostip`
}
Expand Down Expand Up @@ -210,7 +210,7 @@ func (f *admissionControllerFeature) ManageDependencies(managers feature.Resourc
}
port := []corev1.ServicePort{
{
Name: apicommon.AdmissionControllerPortName,
Name: admissionControllerPortName,
Protocol: corev1.ProtocolTCP,
TargetPort: intstr.FromInt(v2alpha1.DefaultAdmissionControllerTargetPort),
Port: v2alpha1.DefaultAdmissionControllerServicePort,
Expand Down
4 changes: 4 additions & 0 deletions internal/controller/datadogagent/feature/apm/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ import (

const (
apmRBACPrefix = "apm"

apmHostPortName = "traceport"
apmSocketVolumeName = "apmsocket"
apmSocketVolumeLocalPath = "/var/run/datadog"
)

// getRBACResourceName return the RBAC resources name
Expand Down
6 changes: 3 additions & 3 deletions internal/controller/datadogagent/feature/apm/feature.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ func (f *apmFeature) ManageDependencies(managers feature.ResourceManagers, compo
}
if f.hostPortEnabled {
apmPort.Port = f.hostPortHostPort
apmPort.Name = apicommon.APMHostPortName
apmPort.Name = apmHostPortName
if f.useHostNetwork {
apmPort.TargetPort = intstr.FromInt(int(f.hostPortHostPort))
}
Expand Down Expand Up @@ -394,9 +394,9 @@ func (f *apmFeature) manageNodeAgent(agentContainerName apicommon.AgentContainer
sockName := filepath.Base(f.udsHostFilepath)
managers.EnvVar().AddEnvVarToContainer(agentContainerName, &corev1.EnvVar{
Name: apicommon.DDAPMReceiverSocket,
Value: filepath.Join(apicommon.APMSocketVolumeLocalPath, sockName),
Value: filepath.Join(apmSocketVolumeLocalPath, sockName),
})
socketVol, socketVolMount := volume.GetVolumes(apicommon.APMSocketVolumeName, udsHostFolder, apicommon.APMSocketVolumeLocalPath, false)
socketVol, socketVolMount := volume.GetVolumes(apmSocketVolumeName, udsHostFolder, apmSocketVolumeLocalPath, false)
volType := corev1.HostPathDirectoryOrCreate // We need to create the directory on the host if it does not exist.
socketVol.VolumeSource.HostPath.Type = &volType
managers.VolumeMount().AddVolumeMountToContainerWithMergeFunc(&socketVolMount, agentContainerName, merger.OverrideCurrentVolumeMountMergeFunction)
Expand Down
14 changes: 7 additions & 7 deletions internal/controller/datadogagent/feature/apm/feature_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (

const (
apmSocketHostPath = apicommon.DogstatsdAPMSocketHostPath + "/" + apicommon.APMSocketName
apmSocketLocalPath = apicommon.APMSocketVolumeLocalPath + "/" + apicommon.APMSocketName
apmSocketLocalPath = apmSocketVolumeLocalPath + "/" + apicommon.APMSocketName
)

func TestShouldEnableAPM(t *testing.T) {
Expand Down Expand Up @@ -471,8 +471,8 @@ func testAgentUDSOnly(agentContainerName apicommon.AgentContainerName) *test.Com
agentVolumeMounts := mgr.VolumeMountMgr.VolumeMountsByC[agentContainerName]
expectedVolumeMounts := []*corev1.VolumeMount{
{
Name: apicommon.APMSocketVolumeName,
MountPath: apicommon.APMSocketVolumeLocalPath,
Name: apmSocketVolumeName,
MountPath: apmSocketVolumeLocalPath,
ReadOnly: false,
},
}
Expand All @@ -486,7 +486,7 @@ func testAgentUDSOnly(agentContainerName apicommon.AgentContainerName) *test.Com
volType := corev1.HostPathDirectoryOrCreate
expectedVolumes := []*corev1.Volume{
{
Name: apicommon.APMSocketVolumeName,
Name: apmSocketVolumeName,
VolumeSource: corev1.VolumeSource{
HostPath: &corev1.HostPathVolumeSource{
Path: apicommon.DogstatsdAPMSocketHostPath,
Expand Down Expand Up @@ -717,8 +717,8 @@ func testAgentHostPortUDS(agentContainerName apicommon.AgentContainerName, hostP
agentVolumeMounts := mgr.VolumeMountMgr.VolumeMountsByC[agentContainerName]
expectedVolumeMounts := []*corev1.VolumeMount{
{
Name: apicommon.APMSocketVolumeName,
MountPath: apicommon.APMSocketVolumeLocalPath,
Name: apmSocketVolumeName,
MountPath: apmSocketVolumeLocalPath,
ReadOnly: false,
},
}
Expand All @@ -732,7 +732,7 @@ func testAgentHostPortUDS(agentContainerName apicommon.AgentContainerName, hostP
volType := corev1.HostPathDirectoryOrCreate
expectedVolumes := []*corev1.Volume{
{
Name: apicommon.APMSocketVolumeName,
Name: apmSocketVolumeName,
VolumeSource: corev1.VolumeSource{
HostPath: &corev1.HostPathVolumeSource{
Path: apicommon.DogstatsdAPMSocketHostPath,
Expand Down
3 changes: 3 additions & 0 deletions internal/controller/datadogagent/feature/cspm/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ const (
cspmRBACPrefix = "cspm"
cspmConfigVolumeName = "complianceconfigdir"
cspmConfFileName = "compliance.yaml"

securityAgentComplianceConfigDirVolumeName = "compliancedir"
securityAgentComplianceConfigDirVolumePath = "/etc/datadog-agent/compliance.d"
)

func getPSPName(owner metav1.Object) string {
Expand Down
8 changes: 4 additions & 4 deletions internal/controller/datadogagent/feature/cspm/feature.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ func (f *cspmFeature) ManageClusterAgent(managers feature.PodTemplateManagers) e
for _, item := range f.customConfig.ConfigMap.Items {
volMount = corev1.VolumeMount{
Name: cspmConfigVolumeName,
MountPath: apicommon.SecurityAgentComplianceConfigDirVolumePath + "/" + item.Path,
MountPath: securityAgentComplianceConfigDirVolumePath + "/" + item.Path,
SubPath: item.Path,
ReadOnly: true,
}
Expand All @@ -203,7 +203,7 @@ func (f *cspmFeature) ManageClusterAgent(managers feature.PodTemplateManagers) e
// Need to use subpaths so that existing configurations are not overwritten
volMount = volume.GetVolumeMountWithSubPath(
cspmConfigVolumeName,
apicommon.SecurityAgentComplianceConfigDirVolumePath+"/"+cspmConfFileName,
securityAgentComplianceConfigDirVolumePath+"/"+cspmConfFileName,
cspmConfFileName,
)
managers.VolumeMount().AddVolumeMountToContainer(&volMount, apicommon.ClusterAgentContainerName)
Expand Down Expand Up @@ -291,13 +291,13 @@ func (f *cspmFeature) ManageNodeAgent(managers feature.PodTemplateManagers, prov
}

// Add empty volume to Security Agent
benchmarksVol, benchmarksVolMount := volume.GetVolumesEmptyDir(apicommon.SecurityAgentComplianceConfigDirVolumeName, apicommon.SecurityAgentComplianceConfigDirVolumePath, true)
benchmarksVol, benchmarksVolMount := volume.GetVolumesEmptyDir(securityAgentComplianceConfigDirVolumeName, securityAgentComplianceConfigDirVolumePath, true)
managers.Volume().AddVolume(&benchmarksVol)
managers.VolumeMount().AddVolumeMountToContainer(&benchmarksVolMount, apicommon.SecurityAgentContainerName)

// Add compliance.d volume mount to init-volume container at different path
benchmarkVolMountInitVol := corev1.VolumeMount{
Name: apicommon.SecurityAgentComplianceConfigDirVolumeName,
Name: securityAgentComplianceConfigDirVolumeName,
MountPath: "/opt/datadog-agent/compliance.d",
ReadOnly: false,
}
Expand Down
4 changes: 2 additions & 2 deletions internal/controller/datadogagent/feature/cspm/feature_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ func cspmAgentNodeWantFunc() *test.ComponentTest {
// check volume mounts
wantVolumeMounts := []corev1.VolumeMount{
{
Name: apicommon.SecurityAgentComplianceConfigDirVolumeName,
Name: securityAgentComplianceConfigDirVolumeName,
MountPath: "/etc/datadog-agent/compliance.d",
ReadOnly: true,
},
Expand Down Expand Up @@ -221,7 +221,7 @@ func cspmAgentNodeWantFunc() *test.ComponentTest {
},
},
{
Name: apicommon.SecurityAgentComplianceConfigDirVolumeName,
Name: securityAgentComplianceConfigDirVolumeName,
VolumeSource: corev1.VolumeSource{
EmptyDir: &corev1.EmptyDirVolumeSource{},
},
Expand Down
Loading

0 comments on commit 172caff

Please sign in to comment.