@@ -21,6 +21,7 @@ import (
21
21
"encoding/json"
22
22
"fmt"
23
23
"math"
24
+ "math/rand"
24
25
"runtime/debug"
25
26
"strings"
26
27
"time"
@@ -43,8 +44,9 @@ import (
43
44
"k8s.io/client-go/restmapper"
44
45
)
45
46
46
- var appwrapperJobName = "appwrapper.mcad.ibm.com"
47
- var resourceName = "resourceName"
47
+ var appwrapperJobLabelName = "workload.codeflare.dev/appwrapper"
48
+ var appwrapperJobLabelNamespace = "workload.codeflare.dev/appwrapper-namespace"
49
+ var resourceName = "workload.codeflare.dev/resourceName"
48
50
var appWrapperKind = arbv1 .SchemeGroupVersion .WithKind ("AppWrapper" )
49
51
50
52
type GenericResources struct {
@@ -72,6 +74,18 @@ func join(strs ...string) string {
72
74
return result
73
75
}
74
76
77
+
78
+ func GetRandomString (n int ) string {
79
+ var letters = []rune ("abcdefghijklmnopqrstuvwxyz0123456789" )
80
+
81
+ rand .Seed (time .Now ().UnixNano ())
82
+ b := make ([]rune , n )
83
+ for i := range b {
84
+ b [i ] = letters [rand .Intn (len (letters ))]
85
+ }
86
+ return string (b )
87
+ }
88
+
75
89
func (gr * GenericResources ) Cleanup (aw * arbv1.AppWrapper , awr * arbv1.AppWrapperGenericResource ) (genericResourceName string , groupversionkind * schema.GroupVersionKind , erro error ) {
76
90
var err error
77
91
err = nil
@@ -166,7 +180,7 @@ func (gr *GenericResources) Cleanup(aw *arbv1.AppWrapper, awr *arbv1.AppWrapperG
166
180
}
167
181
168
182
// Get the resource to see if it exists in the AppWrapper namespace
169
- labelSelector := fmt .Sprintf ("%s=%s, %s=%s" , appwrapperJobName , aw .Name , resourceName , unstruct .GetName ())
183
+ labelSelector := fmt .Sprintf ("%s=%s, %s=%s, %s=%s " , appwrapperJobLabelName , aw .Name , appwrapperJobLabelNamespace , aw . Namespace , resourceName , unstruct .GetName ())
170
184
inEtcd , err := dclient .Resource (rsrc ).Namespace (aw .Namespace ).List (context .Background (), metav1.ListOptions {LabelSelector : labelSelector })
171
185
if err != nil {
172
186
return name , gvk , err
@@ -175,8 +189,9 @@ func (gr *GenericResources) Cleanup(aw *arbv1.AppWrapper, awr *arbv1.AppWrapperG
175
189
// Check to see if object already exists in etcd, if not, create the object.
176
190
if inEtcd != nil || len (inEtcd .Items ) > 0 {
177
191
newName := name
178
- if len (newName ) > 63 {
179
- newName = newName [:63 ]
192
+ if len (newName ) > 60 {
193
+ newName = newName [:60 ]
194
+ newName += GetRandomString (3 )
180
195
}
181
196
182
197
err = deleteObject (namespaced , namespace , newName , rsrc , dclient )
@@ -187,7 +202,7 @@ func (gr *GenericResources) Cleanup(aw *arbv1.AppWrapper, awr *arbv1.AppWrapperG
187
202
return name , gvk , err
188
203
}
189
204
} else {
190
- klog .Warningf ("[Cleanup] %s/%s not found using label selector: %s.\n " , name , namespace , labelSelector )
205
+ klog .Warningf ("[Cleanup] %s/%s not found using label selector: %s.\n " , namespace , name , labelSelector )
191
206
}
192
207
193
208
return name , gvk , err
@@ -297,18 +312,19 @@ func (gr *GenericResources) SyncQueueJob(aw *arbv1.AppWrapper, awr *arbv1.AppWra
297
312
} else {
298
313
labels = unstruct .GetLabels ()
299
314
}
300
- labels [appwrapperJobName ] = aw .Name
315
+ labels [appwrapperJobLabelName ] = aw .Name
316
+ labels [appwrapperJobLabelNamespace ] = aw .Namespace
301
317
labels [resourceName ] = unstruct .GetName ()
302
318
unstruct .SetLabels (labels )
303
319
304
320
// Add labels to pod template if one exists.
305
321
podTemplateFound := addLabelsToPodTemplateField (& unstruct , labels )
306
322
if ! podTemplateFound {
307
- klog .V (4 ).Infof ("[SyncQueueJob] No pod template spec exists for resource: %s to add labels." , name )
323
+ klog .V (4 ).Infof ("[SyncQueueJob] No pod template spec exists for resource: %s/%s to add labels." , namespace , name )
308
324
}
309
325
310
- // Get the resource to see if it exists
311
- labelSelector := fmt .Sprintf ("%s=%s, %s=%s" , appwrapperJobName , aw .Name , resourceName , unstruct .GetName ())
326
+ // Get the resource to see if it exists
327
+ labelSelector := fmt .Sprintf ("%s=%s, %s=%s, %s=%s " , appwrapperJobLabelName , aw .Name , appwrapperJobLabelNamespace , aw . Namespace , resourceName , unstruct .GetName ())
312
328
inEtcd , err := dclient .Resource (rsrc ).List (context .Background (), metav1.ListOptions {LabelSelector : labelSelector })
313
329
if err != nil {
314
330
return []* v1.Pod {}, err
@@ -317,8 +333,9 @@ func (gr *GenericResources) SyncQueueJob(aw *arbv1.AppWrapper, awr *arbv1.AppWra
317
333
// Check to see if object already exists in etcd, if not, create the object.
318
334
if inEtcd == nil || len (inEtcd .Items ) < 1 {
319
335
newName := name
320
- if len (newName ) > 63 {
321
- newName = newName [:63 ]
336
+ if len (newName ) > 60 {
337
+ newName = newName [:60 ]
338
+ newName += GetRandomString (3 )
322
339
}
323
340
unstruct .SetName (newName )
324
341
//Asumption object is always namespaced
@@ -329,7 +346,7 @@ func (gr *GenericResources) SyncQueueJob(aw *arbv1.AppWrapper, awr *arbv1.AppWra
329
346
if errors .IsAlreadyExists (err ) {
330
347
klog .V (4 ).Infof ("%v\n " , err .Error ())
331
348
} else {
332
- klog .Errorf ("Error creating the object `%v `, the error is `%v`" , newName , errors .ReasonForError (err ))
349
+ klog .Errorf ("Error creating the object `%s/%s `, the error is `%v`" , namespace , newName , errors .ReasonForError (err ))
333
350
return []* v1.Pod {}, err
334
351
}
335
352
}
@@ -499,7 +516,7 @@ func deleteObject(namespaced bool, namespace string, name string, rsrc schema.Gr
499
516
}
500
517
501
518
if err != nil && ! errors .IsNotFound (err ) {
502
- klog .Errorf ("[deleteObject] Error deleting the object `%v`, the error is `%v`." , name , errors .ReasonForError (err ))
519
+ klog .Errorf ("[deleteObject] Error deleting the object `%v`, in namespace %v, the error is `%v`." , name , namespace , errors .ReasonForError (err ))
503
520
return err
504
521
} else {
505
522
klog .V (4 ).Infof ("[deleteObject] Resource `%v` deleted.\n " , name )
@@ -531,7 +548,7 @@ func GetListOfPodResourcesFromOneGenericItem(awr *arbv1.AppWrapperGenericResourc
531
548
klog .V (8 ).Infof ("[GetListOfPodResourcesFromOneGenericItem] Requested total allocation resource from 1 pod `%v`.\n " , podTotalresource )
532
549
}
533
550
534
- // Addd individual pods to results
551
+ // Add individual pods to results
535
552
var replicaCount int = int (replicas )
536
553
for i := 0 ; i < replicaCount ; i ++ {
537
554
podResourcesList = append (podResourcesList , podTotalresource )
@@ -623,7 +640,7 @@ func getContainerResources(container v1.Container, replicas float64) *clustersta
623
640
}
624
641
625
642
// returns status of an item present in etcd
626
- func (gr * GenericResources ) IsItemCompleted (awgr * arbv1.AppWrapperGenericResource , namespace string , appwrapperName string , genericItemName string ) (completed bool ) {
643
+ func (gr * GenericResources ) IsItemCompleted (awgr * arbv1.AppWrapperGenericResource , appwrapperNamespace string , appwrapperName string , genericItemName string ) (completed bool ) {
627
644
dd := gr .clients .Discovery ()
628
645
apigroups , err := restmapper .GetAPIGroupResources (dd )
629
646
if err != nil {
@@ -654,8 +671,8 @@ func (gr *GenericResources) IsItemCompleted(awgr *arbv1.AppWrapperGenericResourc
654
671
return false
655
672
}
656
673
657
- labelSelector := fmt .Sprintf ("%s=%s" , appwrapperJobName , appwrapperName )
658
- inEtcd , err := dclient .Resource (rsrc ).Namespace (namespace ).List (context .Background (), metav1.ListOptions {LabelSelector : labelSelector })
674
+ labelSelector := fmt .Sprintf ("%s=%s, %s=%s " , appwrapperJobLabelName , appwrapperName , appwrapperJobLabelNamespace , appwrapperNamespace )
675
+ inEtcd , err := dclient .Resource (rsrc ).Namespace (appwrapperNamespace ).List (context .Background (), metav1.ListOptions {LabelSelector : labelSelector })
659
676
if err != nil {
660
677
klog .Errorf ("[IsItemCompleted] Error listing object: %v" , err )
661
678
return false
@@ -675,7 +692,7 @@ func (gr *GenericResources) IsItemCompleted(awgr *arbv1.AppWrapperGenericResourc
675
692
}
676
693
}
677
694
if ! validAwOwnerRef {
678
- klog .Warningf ("[IsItemCompleted] Item owner name %v does match appwrappper name %v in namespace %v" , unstructuredObjectName , appwrapperName , namespace )
695
+ klog .Warningf ("[IsItemCompleted] Item owner name %v does match appwrappper name %v in namespace %v" , unstructuredObjectName , appwrapperName , appwrapperNamespace )
679
696
continue
680
697
}
681
698
0 commit comments