gpu-operator icon indicating copy to clipboard operation
gpu-operator copied to clipboard

[Feature]: Allow custom ServiceAccount for DCGM Exporter DaemonSet

Open arushigarg1031 opened this issue 3 months ago • 3 comments

Problem

GPU Operator hardcodes the DCGM Exporter ServiceAccount to nvidia-dcgm-exporter (assets + DaemonSet). DCGMExporterSpec has no serviceAccount / serviceAccountName field, and TransformDCGMExporter never overrides it. Helm/ClusterPolicy cannot change it; patches are reverted on reconcile.

Request

Add ClusterPolicy/Helm support to configure the DCGM Exporter SA, with default behavior unchanged:

dcgmExporter:
  serviceAccount:
    create: false                 # true = operator creates SA (current behavior)
    name: my-metrics-identity     # default: nvidia-dcgm-exporter
  • Default unset → keep creating/using nvidia-dcgm-exporter (backward compatible).
  • create: false → use an existing SA in the operator namespace; bind Role/ClusterRoleBindings to that SA.
  • Document that SAs are namespaced (cross-namespace SA is out of scope).

Why

Enterprise platforms bind identity/PKI/IRSA/Workload Identity to a specific SA name. Without this, users must run a standalone exporter, vendor-patch the operator, or use fragile admission mutators.

Pros

  • Keep using the operator-managed DCGM Exporter (no parallel DaemonSet).
  • Fit org identity/IAM/PKI SA naming requirements.
  • Align with existing per-component knobs (dcgmExporter.annotations, env, serviceMonitor).
  • Backward compatible; reduces forks/mutators/reconcile fights.

References

  • https://github.com/NVIDIA/gpu-operator/blob/v26.3.3/assets/state-dcgm-exporter/0100_service_account.yaml
  • https://github.com/NVIDIA/gpu-operator/blob/v26.3.3/assets/state-dcgm-exporter/0800_daemonset.yaml

arushigarg1031 avatar Aug 11 '26 10:08 arushigarg1031

I'd like to take this one. Before opening a PR I want to confirm the shape with maintainers, since there is no per-component ServiceAccount override anywhere in ClusterPolicy today and I'd rather match your preference than invent a convention.

Where the name is hardcoded today (main)

Location Today
assets/state-dcgm-exporter/0100_service_account.yaml creates nvidia-dcgm-exporter
0300_rolebinding.yaml, 0310_clusterrolebinding.yaml subject ServiceAccount/nvidia-dcgm-exporter (namespace filled by RoleBinding() / ClusterRoleBinding())
0700_scc.openshift.yaml users: ["FILLED BY THE OPERATOR"] → SecurityContextConstraints() fills system:serviceaccount:<ns>:<scc name> — i.e. it derives the SA name from the SCC name
0800_daemonset.yaml serviceAccountName: nvidia-dcgm-exporter; TransformDCGMExporter never touches it
GPUCluster/DRA path: manifests/state-dcgm-exporter/* hardcoded nvidia-dcgm-exporter-dra, rendered by internal/state/dcgm_exporter.go

Proposed API (as in the issue)

// DCGMExporterSpec
// Optional: ServiceAccount used by the DCGM Exporter DaemonSet
ServiceAccount *DCGMExporterServiceAccountConfig `json:"serviceAccount,omitempty"`

type DCGMExporterServiceAccountConfig struct {
    // Name of the ServiceAccount. Defaults to "nvidia-dcgm-exporter".
    Name string `json:"name,omitempty"`
    // Create controls whether the operator creates the ServiceAccount. Defaults to true.
    // Set to false to bind to a pre-existing ServiceAccount in the operator namespace.
    Create *bool `json:"create,omitempty"`
}
serviceAccount Behaviour
unset (default) identical to today
{name: X} operator creates SA X and binds Role/ClusterRole/SCC to it (useful when only the name matters)
{name: X, create: false} operator does not create an SA; RBAC + SCC + DaemonSet reference the pre-existing X in the operator namespace
{create: false} with no name rejected by CRD validation (name required when create is false)

Alternative if you'd rather keep the API minimal: a single serviceAccountName string, where a non-default value implies "bring your own SA". I lean towards the {name, create} form because IRSA / Workload Identity users typically need annotations on the SA and therefore must own it, while some users only want a specific name. Happy to go either way.

Controller changes (ClusterPolicy path)

  • A small resolver, e.g. dcgmExporterServiceAccountName(spec), used by all consumers so the name is computed in one place.
  • ServiceAccount(): for state-dcgm-exporter, apply the name; when create: false, skip creation and return Ready. It would never delete a user-provided SA. For an operator-created default SA that becomes unused after switching to create: false, I'd propose deleting it only when it carries the ClusterPolicy controller reference — or leaving it in place if you prefer zero deletion side effects; please advise.
  • RoleBinding() / ClusterRoleBinding(): rewrite Subjects[].Name for state-dcgm-exporter, same style as the existing isRBACEnabled(obj.Name, …) name-based hook. Role/ClusterRole themselves are unchanged (still needed for pod-metadata enrichment).
  • SecurityContextConstraints(): fill users from the resolved SA name instead of assuming SA name == SCC name.
  • TransformDCGMExporter: set spec.template.spec.serviceAccountName. automountServiceAccountToken handling stays as is.
  • Helm: dcgmExporter.serviceAccount: {name: "", create: true} in values.yaml, passthrough in templates/clusterpolicy.yaml; make generate manifests sync-crds validate-generated-assets.

GPUCluster / DRA path

GPUCluster embeds the same DCGMExporterSpec, so the field will appear there too. For parity I'd template the SA name in manifests/state-dcgm-exporter/ ({{ .ServiceAccountName }}, default nvidia-dcgm-exporter-dra) and honour create in internal/state/dcgm_exporter.go. Would you like that in the same PR, or as a follow-up so the ClusterPolicy change stays small?

Tests

  • transforms_test.go: DaemonSet gets the configured SA name; default unchanged.
  • object_controls_test.go: ServiceAccount() skips creation with create: false, RoleBinding() / ClusterRoleBinding() subjects and SCC users follow the configured name, defaults identical to current fixtures.
  • Helm: rendered ClusterPolicy carries the block only when set.

Docs (cloud-native-docs) I'd do as a follow-up once the API is settled.

If this looks right I'll open the PR referencing this issue.

100-JM avatar Sep 09 '26 01:09 100-JM

{name, create} is the right API — better than a lone serviceAccountName. It matches NFD in this chart and dcgmExporter.config, and it covers both “operator creates this name” and BYO. Default unset should stay today’s behavior. Keep this DCGM-only for the first PR.

Please treat BYO as unmanaged: no ownerRef, never delete that SA (including when the exporter is disabled — ServiceAccount() deletes on disable today). If create: false and the SA is missing, return NotReady. Don’t adopt an SA that already exists. Delete a previous SA only if it has a ClusterPolicy ownerRef.

SCC: keep the SCC name and openshift.io/scc; only users[] should use the resolved SA. Rewrite only the exporter SA subject on RoleBinding/ClusterRoleBinding.

Because DCGMExporterSpec is shared, honor this on GPUCluster in the same PR (nvidia-dcgm-exporter-dra as the default). Helm should emit the block only when set.

SA annotations can wait — IRSA/WI should use create: false and own the SA. Happy to take the PR on that basis.

arushigarg1031 avatar Sep 09 '26 04:09 arushigarg1031

Opened #2870 on the basis agreed above.

Everything from your reply is implemented: the {name, create} shape, default unset unchanged, BYO treated as unmanaged (no owner reference, never deleted — including when the exporter is disabled), NotReady when a create: false ServiceAccount is missing, no adoption of a pre-existing one, deletion only when a ClusterPolicy owner reference is present, the SCC name and openshift.io/scc left alone with only users following the ServiceAccount, only the exporter subject rewritten on the RBAC bindings, GPUCluster honoured in the same PR with nvidia-dcgm-exporter-dra as its default, and the Helm block emitted only when set. ServiceAccount annotations are left out.

make unit-test, lint, fmt, license-check, validate-generated-assets and validate-modules are all clean locally; the OpenShift SCC path is covered by unit tests only since I have no OpenShift cluster to verify it on.

100-JM avatar Sep 09 '26 05:09 100-JM