@@ -51,7 +51,7 @@ const (
51
51
namespace = "namespace"
52
52
metadata = "metadata"
53
53
kind = "kind"
54
- version = "version "
54
+ group = "group "
55
55
spec = "spec"
56
56
status = "status"
57
57
state = "state"
@@ -240,15 +240,25 @@ func (r *ReconcileComposable) GetServerPreferredResources() ([]*metav1.APIResour
240
240
}
241
241
242
242
243
- func NameMatchesResource (name string , apiResource metav1.APIResource , group string ) bool {
243
+ func NameMatchesResource (name string , objGroup string , resource metav1.APIResource , resGroup string ) bool {
244
244
lowerCaseName := strings .ToLower (name )
245
- if lowerCaseName == apiResource .Name ||
246
- lowerCaseName == apiResource .SingularName ||
247
- lowerCaseName == strings .ToLower (apiResource .Kind ) ||
248
- lowerCaseName == fmt .Sprintf ("%s.%s" , apiResource .Name , group ) {
245
+ if len (objGroup ) > 0 {
246
+ if objGroup == resGroup &&
247
+ ( lowerCaseName == resource .Name ||
248
+ lowerCaseName == resource .SingularName ||
249
+ lowerCaseName == strings .ToLower (resource .Kind ) ) {
250
+ return true
251
+ } else {
252
+ return false
253
+ }
254
+ }
255
+ if lowerCaseName == resource .Name ||
256
+ lowerCaseName == resource .SingularName ||
257
+ lowerCaseName == strings .ToLower (resource .Kind ) ||
258
+ lowerCaseName == fmt .Sprintf ("%s.%s" , resource .Name , resGroup ) {
249
259
return true
250
260
}
251
- for _ , shortName := range apiResource .ShortNames {
261
+ for _ , shortName := range resource .ShortNames {
252
262
if lowerCaseName == strings .ToLower (shortName ) {
253
263
return true
254
264
}
@@ -272,17 +282,21 @@ func GroupQualifiedName(apiResource metav1.APIResource) string {
272
282
return fmt .Sprintf ("%s.%s" , apiResource .Name , apiResource .Group )
273
283
}
274
284
275
- func (r * ReconcileComposable ) LookupAPIResource (kind , apiVersion string , cache * composableCache ) (* metav1.APIResource , error ) {
276
-
285
+ func (r * ReconcileComposable ) LookupAPIResource (objKind , objGroup string , cache * composableCache ) (* metav1.APIResource , error ) {
277
286
var resources []* metav1.APIResourceList
278
- if len (apiVersion ) > 0 {
279
- // TODO comment it out
280
- res , err := r .discoveryClient .ServerResourcesForGroupVersion (apiVersion )
281
- if err != nil {
282
- return nil , err
283
- }
284
- resources = []* metav1.APIResourceList {res }
285
- } else {
287
+ //if len(apiVersion) > 0 {
288
+ //if cache.resourceMap == nil {
289
+ // res, err := r.discoveryClient.ServerResourcesForGroupVersion(apiVersion)
290
+ // if err != nil {
291
+ // fmt.Printf(" apiVersion error %s\n", err)
292
+ // return nil, err
293
+ // }
294
+ // cache.resourceMap = make(map[string]*metav1.APIResourceList)
295
+ // cache.resourceMap[apiVersion] = res
296
+ //}
297
+ //resources = []*metav1.APIResourceList{cache.resourceMap[apiVersion]}
298
+ //fmt.Printf(" apiVersion2 %s %v \n", apiVersion, resources)
299
+ //} else {
286
300
if cache .resources == nil {
287
301
klog .V (6 ).Infoln ("Resources is nil" )
288
302
resourceList , err := r .GetServerPreferredResources ()
@@ -292,10 +306,12 @@ func (r *ReconcileComposable) LookupAPIResource(kind, apiVersion string, cache *
292
306
cache .resources = resourceList
293
307
}
294
308
resources = cache .resources
295
- }
309
+ // }
296
310
297
311
var targetResource * metav1.APIResource
298
312
var matchedResources []string
313
+ coreGroupObject := false
314
+ Loop:
299
315
for _ , resourceList := range resources {
300
316
// The list holds the GroupVersion for its list of APIResources
301
317
gv , err := schema .ParseGroupVersion (resourceList .GroupVersion )
@@ -305,7 +321,16 @@ func (r *ReconcileComposable) LookupAPIResource(kind, apiVersion string, cache *
305
321
306
322
for _ , resource := range resourceList .APIResources {
307
323
group := gv .Group
308
- if NameMatchesResource (kind , resource , group ) {
324
+ if NameMatchesResource (objKind , objGroup , resource , group ) {
325
+ if len (group ) == 0 && len (objGroup ) == 0 {
326
+ // K8s core group object
327
+ coreGroupObject = true
328
+ targetResource = resource .DeepCopy ()
329
+ targetResource .Group = group
330
+ targetResource .Version = gv .Version
331
+ coreGroupObject = true
332
+ break Loop
333
+ }
309
334
if targetResource == nil {
310
335
targetResource = resource .DeepCopy ()
311
336
targetResource .Group = group
@@ -315,7 +340,7 @@ func (r *ReconcileComposable) LookupAPIResource(kind, apiVersion string, cache *
315
340
}
316
341
}
317
342
}
318
- if len (matchedResources ) > 1 {
343
+ if ! coreGroupObject && len (matchedResources ) > 1 {
319
344
return nil , fmt .Errorf ("Multiple resources are matched by %q: %s. A group-qualified plural name must be provided." , kind , strings .Join (matchedResources , ", " ))
320
345
}
321
346
@@ -327,14 +352,13 @@ func (r *ReconcileComposable) LookupAPIResource(kind, apiVersion string, cache *
327
352
}
328
353
329
354
func (r * ReconcileComposable ) resolveValue (value interface {}, composableNamespace string , cache * composableCache ) (interface {}, error ) {
330
- klog .V (5 ).Infof (" resolve value type %T %v\n " , value , value )
331
355
if val , ok := value .(map [string ]interface {}); ok {
332
- if kind , ok := val [kind ].(string ); ok {
333
- vers := ""
334
- if vers , ok = val [version ].(string ); ! ok {
335
- vers = ""
356
+ if objKind , ok := val [kind ].(string ); ok {
357
+ objGroup := ""
358
+ if objGroup , ok = val [group ].(string ); ! ok {
359
+ objGroup = ""
336
360
}
337
- res , err := r .LookupAPIResource (kind , vers , cache )
361
+ res , err := r .LookupAPIResource (objKind , objGroup , cache )
338
362
if err != nil {
339
363
// If an input object API resource is not installed, we return error even if a default value is set.
340
364
return nil , err
@@ -372,7 +396,6 @@ func (r *ReconcileComposable) resolveValue(value interface{}, composableNamespac
372
396
unstrObj = unstructured.Unstructured {}
373
397
//unstrObj.SetAPIVersion(res.Version)
374
398
unstrObj .SetGroupVersionKind (groupVersionKind )
375
- klog .V (5 ).Infof ("Get Object %s %s r type = %T\n " , objNamespacedname , groupVersionKind , r .Client )
376
399
err = r .Get (context .TODO (), objNamespacedname , & unstrObj )
377
400
if err != nil {
378
401
klog .V (5 ).Infof ("Get object returned %s" , err .Error ())
0 commit comments