|
| 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 helpers |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "time" |
| 22 | + |
| 23 | + nvidiav1 "github.com/NVIDIA/gpu-operator/api/nvidia/v1" |
| 24 | + gpuclientset "github.com/NVIDIA/gpu-operator/api/versioned" |
| 25 | + "github.com/NVIDIA/gpu-operator/internal/conditions" |
| 26 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 27 | + "k8s.io/apimachinery/pkg/util/wait" |
| 28 | + "k8s.io/client-go/util/retry" |
| 29 | + "k8s.io/utils/ptr" |
| 30 | +) |
| 31 | + |
| 32 | +type ClusterPolicyClient struct { |
| 33 | + client gpuclientset.Interface |
| 34 | +} |
| 35 | + |
| 36 | +func NewClusterPolicyClient(client gpuclientset.Interface) *ClusterPolicyClient { |
| 37 | + return &ClusterPolicyClient{ |
| 38 | + client: client, |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +func (h *ClusterPolicyClient) Get(ctx context.Context, name string) (*nvidiav1.ClusterPolicy, error) { |
| 43 | + return h.client.NvidiaV1().ClusterPolicies().Get(ctx, name, metav1.GetOptions{}) |
| 44 | +} |
| 45 | + |
| 46 | +func (h *ClusterPolicyClient) Update(ctx context.Context, cp *nvidiav1.ClusterPolicy) (*nvidiav1.ClusterPolicy, error) { |
| 47 | + return h.client.NvidiaV1().ClusterPolicies().Update(ctx, cp, metav1.UpdateOptions{}) |
| 48 | +} |
| 49 | + |
| 50 | +// modify applies a mutation function to a ClusterPolicy and persists the changes. |
| 51 | +// It uses RetryOnConflict to handle concurrent modifications by the operator controller. |
| 52 | +func (h *ClusterPolicyClient) modify(ctx context.Context, name string, mutate func(*nvidiav1.ClusterPolicy)) error { |
| 53 | + return retry.RetryOnConflict(retry.DefaultBackoff, func() error { |
| 54 | + clusterPolicy, err := h.Get(ctx, name) |
| 55 | + if err != nil { |
| 56 | + return err |
| 57 | + } |
| 58 | + |
| 59 | + mutate(clusterPolicy) |
| 60 | + |
| 61 | + _, err = h.Update(ctx, clusterPolicy) |
| 62 | + return err |
| 63 | + }) |
| 64 | +} |
| 65 | + |
| 66 | +func (h *ClusterPolicyClient) UpdateDriverVersion(ctx context.Context, name, version string) error { |
| 67 | + return h.modify(ctx, name, func(clusterPolicy *nvidiav1.ClusterPolicy) { |
| 68 | + clusterPolicy.Spec.Driver.Version = version |
| 69 | + }) |
| 70 | +} |
| 71 | + |
| 72 | +func (h *ClusterPolicyClient) EnableDCGM(ctx context.Context, name string) error { |
| 73 | + return h.modify(ctx, name, func(clusterPolicy *nvidiav1.ClusterPolicy) { |
| 74 | + clusterPolicy.Spec.DCGM.Enabled = ptr.To(true) |
| 75 | + }) |
| 76 | +} |
| 77 | + |
| 78 | +func (h *ClusterPolicyClient) DisableDCGM(ctx context.Context, name string) error { |
| 79 | + return h.modify(ctx, name, func(clusterPolicy *nvidiav1.ClusterPolicy) { |
| 80 | + clusterPolicy.Spec.DCGM.Enabled = ptr.To(false) |
| 81 | + }) |
| 82 | +} |
| 83 | + |
| 84 | +func (h *ClusterPolicyClient) EnableDCGMExporter(ctx context.Context, name string) error { |
| 85 | + return h.modify(ctx, name, func(clusterPolicy *nvidiav1.ClusterPolicy) { |
| 86 | + clusterPolicy.Spec.DCGMExporter.Enabled = ptr.To(true) |
| 87 | + }) |
| 88 | +} |
| 89 | + |
| 90 | +func (h *ClusterPolicyClient) DisableDCGMExporter(ctx context.Context, name string) error { |
| 91 | + return h.modify(ctx, name, func(clusterPolicy *nvidiav1.ClusterPolicy) { |
| 92 | + clusterPolicy.Spec.DCGMExporter.Enabled = ptr.To(false) |
| 93 | + }) |
| 94 | +} |
| 95 | + |
| 96 | +func (h *ClusterPolicyClient) EnableGFD(ctx context.Context, name string) error { |
| 97 | + return h.modify(ctx, name, func(clusterPolicy *nvidiav1.ClusterPolicy) { |
| 98 | + clusterPolicy.Spec.GPUFeatureDiscovery.Enabled = ptr.To(true) |
| 99 | + }) |
| 100 | +} |
| 101 | + |
| 102 | +func (h *ClusterPolicyClient) DisableGFD(ctx context.Context, name string) error { |
| 103 | + return h.modify(ctx, name, func(clusterPolicy *nvidiav1.ClusterPolicy) { |
| 104 | + clusterPolicy.Spec.GPUFeatureDiscovery.Enabled = ptr.To(false) |
| 105 | + }) |
| 106 | +} |
| 107 | + |
| 108 | +func (h *ClusterPolicyClient) SetMIGStrategy(ctx context.Context, name, strategy string) error { |
| 109 | + return h.modify(ctx, name, func(clusterPolicy *nvidiav1.ClusterPolicy) { |
| 110 | + clusterPolicy.Spec.MIG.Strategy = nvidiav1.MIGStrategy(strategy) |
| 111 | + }) |
| 112 | +} |
| 113 | + |
| 114 | +func (h *ClusterPolicyClient) WaitForReady(ctx context.Context, name string, timeout time.Duration) error { |
| 115 | + return wait.PollUntilContextTimeout(ctx, defaultPollingInterval, timeout, true, func(ctx context.Context) (bool, error) { |
| 116 | + clusterPolicy, err := h.Get(ctx, name) |
| 117 | + if err != nil { |
| 118 | + return false, err |
| 119 | + } |
| 120 | + |
| 121 | + for _, condition := range clusterPolicy.Status.Conditions { |
| 122 | + if condition.Type == conditions.Ready && condition.Status == metav1.ConditionTrue { |
| 123 | + return true, nil |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + return false, nil |
| 128 | + }) |
| 129 | +} |
| 130 | + |
0 commit comments