|
| 1 | +/** |
| 2 | +# Copyright (c) NVIDIA CORPORATION. All rights reserved. |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | +**/ |
| 16 | + |
| 17 | +package state |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "path/filepath" |
| 22 | + "strings" |
| 23 | + |
| 24 | + "k8s.io/apimachinery/pkg/runtime" |
| 25 | + "k8s.io/apimachinery/pkg/runtime/schema" |
| 26 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 27 | + "sigs.k8s.io/controller-runtime/pkg/log" |
| 28 | + |
| 29 | + nvidiav1alpha1 "github.com/NVIDIA/gpu-operator/api/nvidia/v1alpha1" |
| 30 | + "github.com/NVIDIA/gpu-operator/internal/consts" |
| 31 | +) |
| 32 | + |
| 33 | +const ( |
| 34 | + // dcgmExporterImageEnvName is the fallback env var for the dcgm-exporter image when the |
| 35 | + // CR does not specify repository/image/version. |
| 36 | + dcgmExporterImageEnvName = "DCGM_EXPORTER_IMAGE" |
| 37 | + |
| 38 | + // dcgmRemoteHostEngine points dcgm-exporter at the standalone nvidia-dcgm hostengine |
| 39 | + // Service (manifests/state-dcgm/0600_service.yaml). |
| 40 | + dcgmRemoteHostEngine = "nvidia-dcgm:5555" |
| 41 | + |
| 42 | + dcgmExporterDefaultCollectors = "/etc/dcgm-exporter/dcp-metrics-included.csv" |
| 43 | + dcgmExporterCustomCollectors = "/etc/dcgm-exporter/dcgm-metrics.csv" |
| 44 | + dcgmExporterDefaultKubeletRootDir = "/var/lib/kubelet" |
| 45 | + dcgmExporterDefaultJobMappingDir = "/var/lib/dcgm-exporter/job-mapping" |
| 46 | +) |
| 47 | + |
| 48 | +func NewStateDCGMExporter( |
| 49 | + k8sClient client.Client, |
| 50 | + namespace string, |
| 51 | + scheme *runtime.Scheme, |
| 52 | + manifestDir string) (State, error) { |
| 53 | + |
| 54 | + skel, err := newStateSkel(k8sClient, namespace, scheme, manifestDir, |
| 55 | + "state-dcgm-exporter", "NVIDIA DCGM Exporter deployed in the cluster") |
| 56 | + if err != nil { |
| 57 | + return nil, err |
| 58 | + } |
| 59 | + return &configurableState{ |
| 60 | + stateSkel: skel, |
| 61 | + isEnabled: func(cr *nvidiav1alpha1.GPUCluster) bool { |
| 62 | + return cr.Spec.DCGMExporter != nil && cr.Spec.DCGMExporter.IsEnabled() |
| 63 | + }, |
| 64 | + imageOverride: func(cr *nvidiav1alpha1.GPUCluster) (string, string, string) { |
| 65 | + spec := cr.Spec.DCGMExporter |
| 66 | + return spec.Repository, spec.Image, spec.Version |
| 67 | + }, |
| 68 | + imageEnvName: dcgmExporterImageEnvName, |
| 69 | + buildRenderData: buildDCGMExporterRenderData, |
| 70 | + }, nil |
| 71 | +} |
| 72 | + |
| 73 | +func buildDCGMExporterRenderData(ctx context.Context, s *configurableState, cr *nvidiav1alpha1.GPUCluster, imagePath, apiVersion string) (interface{}, error) { |
| 74 | + spec := cr.Spec.DCGMExporter |
| 75 | + |
| 76 | + // When standalone DCGM is enabled the exporter targets it; otherwise it runs embedded. |
| 77 | + remoteHostEngine := "" |
| 78 | + if dcgmEnabled(cr) { |
| 79 | + remoteHostEngine = dcgmRemoteHostEngine |
| 80 | + } |
| 81 | + |
| 82 | + collectors := dcgmExporterDefaultCollectors |
| 83 | + metricsConfigName := "" |
| 84 | + if spec.MetricsConfig != nil && spec.MetricsConfig.Name != "" { |
| 85 | + metricsConfigName = spec.MetricsConfig.Name |
| 86 | + collectors = dcgmExporterCustomCollectors |
| 87 | + } |
| 88 | + |
| 89 | + hpcJobMappingDir := "" |
| 90 | + if spec.IsHPCJobMappingEnabled() { |
| 91 | + hpcJobMappingDir = spec.GetHPCJobMappingDirectory() |
| 92 | + if hpcJobMappingDir == "" { |
| 93 | + hpcJobMappingDir = dcgmExporterDefaultJobMappingDir |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + kubeletRootDir := cr.Spec.HostPaths.KubeletRootDir |
| 98 | + if kubeletRootDir == "" { |
| 99 | + kubeletRootDir = dcgmExporterDefaultKubeletRootDir |
| 100 | + } |
| 101 | + |
| 102 | + // Skip the ServiceMonitor when its CRD is absent so a default install without the |
| 103 | + // Prometheus Operator does not fail (matches the ClusterPolicy path). |
| 104 | + serviceMonitorEnabled := spec.ServiceMonitor != nil && |
| 105 | + spec.ServiceMonitor.Enabled != nil && *spec.ServiceMonitor.Enabled |
| 106 | + if serviceMonitorEnabled && !serviceMonitorCRDServed(s.client) { |
| 107 | + log.FromContext(ctx).V(consts.LogLevelInfo).Info( |
| 108 | + "ServiceMonitor CRD not served; skipping dcgm-exporter ServiceMonitor creation") |
| 109 | + serviceMonitorEnabled = false |
| 110 | + } |
| 111 | + |
| 112 | + serviceType := "ClusterIP" |
| 113 | + serviceInternalTrafficPolicy := "" |
| 114 | + if spec.ServiceSpec != nil { |
| 115 | + if spec.ServiceSpec.Type != "" { |
| 116 | + serviceType = string(spec.ServiceSpec.Type) |
| 117 | + } |
| 118 | + if spec.ServiceSpec.InternalTrafficPolicy != nil { |
| 119 | + serviceInternalTrafficPolicy = string(*spec.ServiceSpec.InternalTrafficPolicy) |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + daemonsets := cr.Spec.Daemonsets |
| 124 | + return &dcgmExporterRenderData{ |
| 125 | + DCGMExporter: &dcgmExporterSpec{Spec: spec, ImagePath: imagePath}, |
| 126 | + Daemonsets: &daemonsets, |
| 127 | + Namespace: s.namespace, |
| 128 | + ResourceClaimAPIVersion: apiVersion, |
| 129 | + RemoteHostEngine: remoteHostEngine, |
| 130 | + Collectors: collectors, |
| 131 | + HPCJobMappingDir: hpcJobMappingDir, |
| 132 | + PodLabelAllowlistRegex: strings.Join(spec.PodLabelAllowlistRegex, ","), |
| 133 | + PodMetadataEnabled: spec.IsKubernetesPodMetadataEnabled(), |
| 134 | + EnablePodLabels: spec.IsPodLabelsEnabled(), |
| 135 | + EnablePodUID: spec.IsPodUIDEnabled(), |
| 136 | + HostPID: spec.IsHostPIDEnabled(), |
| 137 | + HostNetwork: spec.IsHostNetworkEnabled(), |
| 138 | + MetricsConfigName: metricsConfigName, |
| 139 | + ServiceMonitorEnabled: serviceMonitorEnabled, |
| 140 | + PodResourcesDir: filepath.Join(kubeletRootDir, "pod-resources"), |
| 141 | + ServiceType: serviceType, |
| 142 | + ServiceInternalTrafficPolicy: serviceInternalTrafficPolicy, |
| 143 | + }, nil |
| 144 | +} |
| 145 | + |
| 146 | +// serviceMonitorCRDServed reports whether the cluster serves the monitoring.coreos.com |
| 147 | +// ServiceMonitor kind (i.e. the Prometheus Operator CRDs are installed). |
| 148 | +func serviceMonitorCRDServed(k8sClient client.Client) bool { |
| 149 | + _, err := k8sClient.RESTMapper().RESTMapping( |
| 150 | + schema.GroupKind{Group: "monitoring.coreos.com", Kind: "ServiceMonitor"}, "v1") |
| 151 | + return err == nil |
| 152 | +} |
0 commit comments