77 "context"
88 "fmt"
99 "strconv"
10- "strings"
1110
1211 v1 "k8s.io/api/core/v1"
1312 "k8s.io/apimachinery/pkg/api/errors"
@@ -18,11 +17,11 @@ import (
1817)
1918
2019const (
21- GPUSharingConfigMap = "runai-sh- gpu"
22- DesiredConfigMapPrefixKey = "runai/ shared-gpu-configmap "
23- maxVolumeNameLength = 63
24- configMapNameNumRandomChars = 7
25- configMapNameExtraChars = configMapNameNumRandomChars + 6
20+ gpuSharingConfigMapAnnotation = "runai/shared- gpu-configmap "
21+ gpuSharingConfigMap = "shared-gpu"
22+ maxVolumeNameLength = 63
23+ configMapNameNumRandomChars = 7
24+ configMapNameExtraChars = configMapNameNumRandomChars + 6
2625)
2726
2827func UpsertJobConfigMap (ctx context.Context ,
@@ -107,7 +106,7 @@ func patchConfigMap(
107106}
108107
109108func SetGpuCapabilitiesConfigMapName (pod * v1.Pod , containerIndex int , containerType ContainerType ) string {
110- namePrefix , found := pod .Annotations [DesiredConfigMapPrefixKey ]
109+ namePrefix , found := pod .Annotations [gpuSharingConfigMapAnnotation ]
111110 if ! found {
112111 namePrefix = generateConfigMapNamePrefix (pod , containerIndex )
113112 setConfigMapNameAnnotation (pod , namePrefix )
@@ -128,7 +127,7 @@ func generateConfigMapNamePrefix(pod *v1.Pod, containerIndex int) string {
128127 }
129128 // volume name is the `${configMapName}-vol` and should be up to 63 bytes long,
130129 // 4 for "-vol" , 7 random chars, and 2 hyphens - 13 in total
131- maxBaseNameLength := (maxVolumeNameLength - configMapNameExtraChars ) - len (GPUSharingConfigMap )
130+ maxBaseNameLength := (maxVolumeNameLength - configMapNameExtraChars ) - len (gpuSharingConfigMap )
132131 // also remove from the max length for "-{containerIndex}" or "-i{initContainerIndex}" in the name
133132 maxBaseNameLength = maxBaseNameLength - len (strconv .Itoa (containerIndex )) - 1
134133 // also allow for appending "-evar" in case of envFrom config map
@@ -137,7 +136,7 @@ func generateConfigMapNamePrefix(pod *v1.Pod, containerIndex int) string {
137136 baseName = baseName [:maxBaseNameLength ]
138137 }
139138 return fmt .Sprintf ("%v-%v-%v" , baseName ,
140- utilrand .String (configMapNameNumRandomChars ), GPUSharingConfigMap )
139+ utilrand .String (configMapNameNumRandomChars ), gpuSharingConfigMap )
141140}
142141
143142func ExtractCapabilitiesConfigMapName (pod * v1.Pod , containerIndex int , containerType ContainerType ) (string , error ) {
@@ -146,7 +145,7 @@ func ExtractCapabilitiesConfigMapName(pod *v1.Pod, containerIndex int, container
146145 containerIndexStr = "i" + containerIndexStr
147146 }
148147
149- namePrefix , found := pod .Annotations [DesiredConfigMapPrefixKey ]
148+ namePrefix , found := pod .Annotations [gpuSharingConfigMapAnnotation ]
150149 if ! found {
151150 return "" , fmt .Errorf ("no desired configmap name found in pod %s/%s annotations" , pod .Namespace , pod .Name )
152151 }
@@ -162,59 +161,11 @@ func ExtractDirectEnvVarsConfigMapName(pod *v1.Pod, containerIndex int, containe
162161 return fmt .Sprintf ("%s-evar" , configNameBase ), nil
163162}
164163
165- func HandleBCPod (ctx context.Context , kubeClient client.Client , pod * v1.Pod ) (string , error ) {
166- logger := log .FromContext (ctx )
167- desiredConfigMapName , err := GetDesiredConfigMapNameBC (pod )
168- if err != nil {
169- return "" , err
170- }
171- logger .Info ("Desired configmap name for backwards compatibility pod" ,
172- "namespace" , pod .Namespace , "name" , pod .Name , "configMapName" , desiredConfigMapName )
173- err = UpdateBCPod (ctx , kubeClient , pod , desiredConfigMapName )
174- return desiredConfigMapName , err
175- }
176-
177- func GetDesiredConfigMapNameBC (pod * v1.Pod ) (string , error ) {
178- cmName := ""
179- for _ , volume := range pod .Spec .Volumes {
180- if volume .ConfigMap == nil {
181- continue
182- }
183-
184- possibleCmName := volume .ConfigMap .LocalObjectReference .Name
185- if strings .HasSuffix (possibleCmName , GPUSharingConfigMap ) {
186- if cmName != "" {
187- return "" , fmt .Errorf ("multiple desired gpu sharing configmap volumes detected for backwards " +
188- "compatibility pod %s/%s" , pod .Namespace , pod .Name )
189- }
190- cmName = possibleCmName
191- }
192- }
193-
194- if cmName == "" {
195- return "" , fmt .Errorf ("no desired gpu sharing configmap volume detected for backwards compatibility pod " +
196- "%s/%s" , pod .Namespace , pod .Name )
197- }
198-
199- return cmName , nil
200- }
201-
202- func UpdateBCPod (ctx context.Context , kubeClient client.Client , pod * v1.Pod , desiredConfigMapName string ) error {
203- updatedPod := pod .DeepCopy ()
204- setConfigMapNameAnnotation (updatedPod , desiredConfigMapName )
205- err := kubeClient .Patch (ctx , updatedPod , client .MergeFrom (pod ))
206- if err != nil {
207- return fmt .Errorf ("failed to update pod %v/%v with desired configmap name %v, error: %v" ,
208- pod .Namespace , pod .Name , desiredConfigMapName , err )
209- }
210- return nil
211- }
212-
213164func setConfigMapNameAnnotation (pod * v1.Pod , name string ) {
214165 if pod .Annotations == nil {
215166 pod .Annotations = map [string ]string {}
216167 }
217- pod .Annotations [DesiredConfigMapPrefixKey ] = name
168+ pod .Annotations [gpuSharingConfigMapAnnotation ] = name
218169}
219170
220171// ownerReferencesDifferent compares two OwnerReferences and returns true if they are not the same
0 commit comments