Skip to content

Commit

Permalink
add option in DatadogAgent CRD to disable nonResourceRules (#1074)
Browse files Browse the repository at this point in the history
  • Loading branch information
celenechang committed Feb 9, 2024
1 parent cea20ff commit 0463654
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 18 deletions.
5 changes: 5 additions & 0 deletions apis/datadoghq/v2alpha1/datadogagent_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -781,6 +781,11 @@ type GlobalConfig struct {
// +optional
CriSocketPath *string `json:"criSocketPath,omitempty"`

// Set DisableNonResourceRules to exclude NonResourceURLs from default ClusterRoles.
// Required 'true' for Google Cloud Marketplace.
// +optional
DisableNonResourceRules *bool `json:"disableNonResourceRules,omitempty"`

// ContainerStrategy determines whether agents run in a single or multiple containers.
// Default: 'optimized'
// +optional
Expand Down
5 changes: 5 additions & 0 deletions apis/datadoghq/v2alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions config/crd/bases/v1/datadoghq.com_datadogagents.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8468,6 +8468,9 @@ spec:
criSocketPath:
description: Path to the container runtime socket (if different from Docker).
type: string
disableNonResourceRules:
description: Set DisableNonResourceRules to exclude NonResourceURLs from default ClusterRoles. Required 'true' for Google Cloud Marketplace.
type: boolean
dockerSocketPath:
description: Path to the docker runtime socket.
type: string
Expand Down
3 changes: 3 additions & 0 deletions config/crd/bases/v1beta1/datadoghq.com_datadogagents.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16018,6 +16018,9 @@ spec:
criSocketPath:
description: Path to the container runtime socket (if different from Docker).
type: string
disableNonResourceRules:
description: Set DisableNonResourceRules to exclude NonResourceURLs from default ClusterRoles. Required 'true' for Google Cloud Marketplace.
type: boolean
dockerSocketPath:
description: Path to the docker runtime socket.
type: string
Expand Down
2 changes: 1 addition & 1 deletion controllers/datadogagent/clusteragent.go
Original file line number Diff line number Diff line change
Expand Up @@ -952,7 +952,7 @@ func buildClusterRole(dda *datadoghqv1alpha1.DatadogAgent, needClusterLevelRBAC
},
}

rbacRules := agent.GetDefaultAgentClusterRolePolicyRules()
rbacRules := agent.GetDefaultAgentClusterRolePolicyRules(false)

// If the secret backend uses the provided `/readsecret_multiple_providers.sh` script, then we need to add secrets GET permissions
if *dda.Spec.Credentials.UseSecretBackend &&
Expand Down
11 changes: 8 additions & 3 deletions controllers/datadogagent/component/agent/rbac.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,18 @@ func GetAgentRoleName(dda metav1.Object) string {
}

// GetDefaultAgentClusterRolePolicyRules returns the default policy rules for the Agent cluster role
func GetDefaultAgentClusterRolePolicyRules() []rbacv1.PolicyRule {
return []rbacv1.PolicyRule{
getMetricsEndpointPolicyRule(),
func GetDefaultAgentClusterRolePolicyRules(excludeNonResourceRules bool) []rbacv1.PolicyRule {
policyRule := []rbacv1.PolicyRule{
getKubeletPolicyRule(),
getEndpointsPolicyRule(),
getLeaderElectionPolicyRule(),
}

if !excludeNonResourceRules {
policyRule = append(policyRule, getMetricsEndpointPolicyRule())
}

return policyRule
}

func getMetricsEndpointPolicyRule() rbacv1.PolicyRule {
Expand Down
17 changes: 11 additions & 6 deletions controllers/datadogagent/component/clusterchecksrunner/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,8 @@ func NewDefaultClusterChecksRunnerPodTemplateSpec(dda metav1.Object) *corev1.Pod
}

// GetDefaultClusterChecksRunnerClusterRolePolicyRules returns the default Cluster Role Policy Rules for the Cluster Checks Runner
func GetDefaultClusterChecksRunnerClusterRolePolicyRules(dda metav1.Object) []rbacv1.PolicyRule {
return []rbacv1.PolicyRule{
{
NonResourceURLs: []string{rbac.MetricsURL},
Verbs: []string{rbac.GetVerb},
},
func GetDefaultClusterChecksRunnerClusterRolePolicyRules(dda metav1.Object, excludeNonResourceRules bool) []rbacv1.PolicyRule {
policyRule := []rbacv1.PolicyRule{
{
APIGroups: []string{rbac.CoreAPIGroup},
Resources: []string{
Expand Down Expand Up @@ -183,6 +179,15 @@ func GetDefaultClusterChecksRunnerClusterRolePolicyRules(dda metav1.Object) []rb
},
},
}

if !excludeNonResourceRules {
policyRule = append(policyRule, rbacv1.PolicyRule{
NonResourceURLs: []string{rbac.MetricsURL},
Verbs: []string{rbac.GetVerb},
})
}

return policyRule
}

// GetDefaultServiceAccountName return the default Cluster-Agent ServiceAccountName
Expand Down
21 changes: 13 additions & 8 deletions controllers/datadogagent/feature/enabledefault/feature.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,13 @@ func buildDefaultFeature(options *feature.Options) feature.Feature {
type defaultFeature struct {
owner metav1.Object

credentialsInfo credentialsInfo
dcaTokenInfo dcaTokenInfo
clusterAgent clusterAgentConfig
agent agentConfig
clusterChecksRunner clusterChecksRunnerConfig
logger logr.Logger
credentialsInfo credentialsInfo
dcaTokenInfo dcaTokenInfo
clusterAgent clusterAgentConfig
agent agentConfig
clusterChecksRunner clusterChecksRunnerConfig
logger logr.Logger
disableNonResourceRules bool

customConfigAnnotationKey string
customConfigAnnotationValue string
Expand Down Expand Up @@ -119,6 +120,10 @@ func (f *defaultFeature) Configure(dda *v2alpha1.DatadogAgent) feature.RequiredC
f.clusterChecksRunner.serviceAccountName = v2alpha1.GetClusterChecksRunnerServiceAccount(dda)

if dda.Spec.Global != nil {
if dda.Spec.Global.DisableNonResourceRules != nil && *dda.Spec.Global.DisableNonResourceRules {
f.disableNonResourceRules = true
}

if dda.Spec.Global.Credentials != nil {
creds := dda.Spec.Global.Credentials

Expand Down Expand Up @@ -309,7 +314,7 @@ func (f *defaultFeature) agentDependencies(managers feature.ResourceManagers, re
}

// ClusterRole creation
if err := managers.RBACManager().AddClusterPolicyRules(f.owner.GetNamespace(), agent.GetAgentRoleName(f.owner), f.agent.serviceAccountName, agent.GetDefaultAgentClusterRolePolicyRules()); err != nil {
if err := managers.RBACManager().AddClusterPolicyRules(f.owner.GetNamespace(), agent.GetAgentRoleName(f.owner), f.agent.serviceAccountName, agent.GetDefaultAgentClusterRolePolicyRules(f.disableNonResourceRules)); err != nil {
errs = append(errs, err)
}

Expand Down Expand Up @@ -366,7 +371,7 @@ func (f *defaultFeature) clusterChecksRunnerDependencies(managers feature.Resour
}

// ClusterRole creation
if err := managers.RBACManager().AddClusterPolicyRulesByComponent(f.owner.GetNamespace(), componentccr.GetCCRRbacResourcesName(f.owner), f.clusterChecksRunner.serviceAccountName, componentccr.GetDefaultClusterChecksRunnerClusterRolePolicyRules(f.owner), string(v2alpha1.ClusterChecksRunnerComponentName)); err != nil {
if err := managers.RBACManager().AddClusterPolicyRulesByComponent(f.owner.GetNamespace(), componentccr.GetCCRRbacResourcesName(f.owner), f.clusterChecksRunner.serviceAccountName, componentccr.GetDefaultClusterChecksRunnerClusterRolePolicyRules(f.owner, f.disableNonResourceRules), string(v2alpha1.ClusterChecksRunnerComponentName)); err != nil {
errs = append(errs, err)
}
}
Expand Down
1 change: 1 addition & 0 deletions docs/configuration.v2alpha1.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ spec:
| global.credentials.appSecret.keyName | KeyName is the key of the secret to use. |
| global.credentials.appSecret.secretName | SecretName is the name of the secret. |
| global.criSocketPath | Path to the container runtime socket (if different from Docker). |
| global.disableNonResourceRules | Set DisableNonResourceRules to exclude NonResourceURLs from default ClusterRoles. Required 'true' for Google Cloud Marketplace. |
| global.dockerSocketPath | Path to the docker runtime socket. |
| global.endpoint.credentials.apiKey | APIKey configures your Datadog API key. See also: https://app.datadoghq.com/account/settings#agent/kubernetes |
| global.endpoint.credentials.apiSecret.keyName | KeyName is the key of the secret to use. |
Expand Down

0 comments on commit 0463654

Please sign in to comment.