|
| 1 | +/* |
| 2 | + * Copyright 2025 The Kubernetes Authors. |
| 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 computedomaindraplugin |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + "os" |
| 22 | + "path/filepath" |
| 23 | + |
| 24 | + "github.com/run-ai/fake-gpu-operator/pkg/compute-domain/consts" |
| 25 | + |
| 26 | + cdiapi "tags.cncf.io/container-device-interface/pkg/cdi" |
| 27 | + cdiparser "tags.cncf.io/container-device-interface/pkg/parser" |
| 28 | + cdispec "tags.cncf.io/container-device-interface/specs-go" |
| 29 | +) |
| 30 | + |
| 31 | +const ( |
| 32 | + computeDomainCDIVendor = "k8s." + consts.ComputeDomainDriverName |
| 33 | + computeDomainCDIClass = "computedomain" |
| 34 | + computeDomainCDIKind = computeDomainCDIVendor + "/" + computeDomainCDIClass |
| 35 | + |
| 36 | + computeDomainCDICommonDeviceName = "common" |
| 37 | +) |
| 38 | + |
| 39 | +type ComputeDomainCDIHandler struct { |
| 40 | + cache *cdiapi.Cache |
| 41 | + nvcdiDevice *computeDomainNvcdiDevice |
| 42 | + deviceRoot string |
| 43 | + claimDevName string |
| 44 | +} |
| 45 | + |
| 46 | +func NewComputeDomainCDIHandler(config *Config) (*ComputeDomainCDIHandler, error) { |
| 47 | + cache, err := cdiapi.NewCache( |
| 48 | + cdiapi.WithSpecDirs(config.flags.cdiRoot), |
| 49 | + ) |
| 50 | + if err != nil { |
| 51 | + return nil, fmt.Errorf("unable to create a new CDI cache: %w", err) |
| 52 | + } |
| 53 | + deviceRoot := filepath.Join(config.DriverPluginPath(), "nvcdi") |
| 54 | + nvcdiDevice, err := newComputeDomainNvcdiDevice(deviceRoot) |
| 55 | + if err != nil { |
| 56 | + return nil, fmt.Errorf("unable to initialize nvcdi device helper: %w", err) |
| 57 | + } |
| 58 | + |
| 59 | + handler := &ComputeDomainCDIHandler{ |
| 60 | + cache: cache, |
| 61 | + nvcdiDevice: nvcdiDevice, |
| 62 | + deviceRoot: deviceRoot, |
| 63 | + claimDevName: "channel", |
| 64 | + } |
| 65 | + |
| 66 | + return handler, nil |
| 67 | +} |
| 68 | + |
| 69 | +func (cdi *ComputeDomainCDIHandler) CreateCommonSpecFile() error { |
| 70 | + spec := &cdispec.Spec{ |
| 71 | + Kind: computeDomainCDIKind, |
| 72 | + Devices: []cdispec.Device{ |
| 73 | + { |
| 74 | + Name: computeDomainCDICommonDeviceName, |
| 75 | + ContainerEdits: cdispec.ContainerEdits{ |
| 76 | + Env: []string{ |
| 77 | + fmt.Sprintf("KUBERNETES_NODE_NAME=%s", os.Getenv("NODE_NAME")), |
| 78 | + fmt.Sprintf("DRA_RESOURCE_DRIVER_NAME=%s", consts.ComputeDomainDriverName), |
| 79 | + }, |
| 80 | + }, |
| 81 | + }, |
| 82 | + }, |
| 83 | + } |
| 84 | + |
| 85 | + minVersion, err := cdispec.MinimumRequiredVersion(spec) |
| 86 | + if err != nil { |
| 87 | + return fmt.Errorf("failed to get minimum required CDI spec version: %v", err) |
| 88 | + } |
| 89 | + spec.Version = minVersion |
| 90 | + |
| 91 | + specName, err := cdiapi.GenerateNameForTransientSpec(spec, computeDomainCDICommonDeviceName) |
| 92 | + if err != nil { |
| 93 | + return fmt.Errorf("failed to generate Spec name: %w", err) |
| 94 | + } |
| 95 | + |
| 96 | + return cdi.cache.WriteSpec(spec, specName) |
| 97 | +} |
| 98 | + |
| 99 | +func (cdi *ComputeDomainCDIHandler) CreateClaimSpecFile(claimUID string, devices ComputeDomainPreparedDevices) error { |
| 100 | + specName := cdiapi.GenerateTransientSpecName(computeDomainCDIVendor, computeDomainCDIClass, claimUID) |
| 101 | + |
| 102 | + spec := &cdispec.Spec{ |
| 103 | + Kind: computeDomainCDIKind, |
| 104 | + Devices: []cdispec.Device{}, |
| 105 | + } |
| 106 | + |
| 107 | + for _, device := range devices { |
| 108 | + claimEdits := cdiapi.ContainerEdits{ |
| 109 | + ContainerEdits: &cdispec.ContainerEdits{ |
| 110 | + Env: []string{ |
| 111 | + fmt.Sprintf("COMPUTE_DOMAIN_DEVICE_%s_RESOURCE_CLAIM=%s", device.DeviceName, claimUID), |
| 112 | + }, |
| 113 | + }, |
| 114 | + } |
| 115 | + if device.ContainerEdits != nil { |
| 116 | + claimEdits.Append(device.ContainerEdits) |
| 117 | + } |
| 118 | + |
| 119 | + cdiDevice := cdispec.Device{ |
| 120 | + Name: fmt.Sprintf("%s-%s", claimUID, device.DeviceName), |
| 121 | + ContainerEdits: *claimEdits.ContainerEdits, |
| 122 | + } |
| 123 | + |
| 124 | + spec.Devices = append(spec.Devices, cdiDevice) |
| 125 | + } |
| 126 | + |
| 127 | + minVersion, err := cdiapi.MinimumRequiredVersion(spec) |
| 128 | + if err != nil { |
| 129 | + return fmt.Errorf("failed to get minimum required CDI spec version: %v", err) |
| 130 | + } |
| 131 | + spec.Version = minVersion |
| 132 | + |
| 133 | + return cdi.cache.WriteSpec(spec, specName) |
| 134 | +} |
| 135 | + |
| 136 | +func (cdi *ComputeDomainCDIHandler) DeleteClaimSpecFile(claimUID string) error { |
| 137 | + specName := cdiapi.GenerateTransientSpecName(computeDomainCDIVendor, computeDomainCDIClass, claimUID) |
| 138 | + return cdi.cache.RemoveSpec(specName) |
| 139 | +} |
| 140 | + |
| 141 | +func (cdi *ComputeDomainCDIHandler) GetClaimDevices(claimUID string, devices []string) []string { |
| 142 | + cdiDevices := make([]string, 0, len(devices)) |
| 143 | + for _, device := range devices { |
| 144 | + cdiDevice := cdiparser.QualifiedName(computeDomainCDIVendor, computeDomainCDIClass, fmt.Sprintf("%s-%s", claimUID, device)) |
| 145 | + cdiDevices = append(cdiDevices, cdiDevice) |
| 146 | + } |
| 147 | + return cdiDevices |
| 148 | +} |
| 149 | + |
| 150 | +func (cdi *ComputeDomainCDIHandler) CreateDomainCDIDevice(domainInfo *DomainInfo) (*cdiapi.ContainerEdits, error) { |
| 151 | + if domainInfo == nil { |
| 152 | + return nil, fmt.Errorf("domain info is required to create CDI device") |
| 153 | + } |
| 154 | + return cdi.nvcdiDevice.ContainerEdits(domainInfo) |
| 155 | +} |
0 commit comments