@@ -279,15 +279,7 @@ func (api *MetadataAPI) GetOwnerKindAndName(ctx context.Context, pod *corev1.Pod
279279// Currently, this is limited to Jobs (parented by CronJobs) and ReplicaSets (parented by
280280// Deployments, StatefulSets, argo Rollouts, etc.). This list may change in the future, but
281281// is sufficient for now.
282- var indexedParents = map [string ]indexedParent {
283- "Job" : {Job , batchv1 .SchemeGroupVersion .WithResource ("jobs" )},
284- "ReplicaSet" : {RS , appsv1 .SchemeGroupVersion .WithResource ("replicasets" )},
285- }
286-
287- type indexedParent struct {
288- resource APIResource
289- gvr schema.GroupVersionResource
290- }
282+ var indexedParents = map [string ]APIResource {"Job" : Job , "ReplicaSet" : RS }
291283
292284// GetRootOwnerKindAndName returns the resource's owner's type and metadata, using owner
293285// references from the Kubernetes API. Parent refs are recursively traversed to find the
@@ -310,10 +302,10 @@ func (api *MetadataAPI) GetRootOwnerKindAndName(ctx context.Context, tm *metav1.
310302log .Warnf ("parent ref has no kind: %v" , parentRef )
311303return tm , om
312304}
313- indexedResource , ok := indexedParents [parentRef .Kind ]
305+ resource , ok := indexedParents [parentRef .Kind ]
314306if ok {
315307getFromInformer := func () (* metav1.TypeMeta , * metav1.ObjectMeta , error ) {
316- parent , err := api .getByNamespace (indexedResource . resource , om .Namespace , parentRef .Name )
308+ parent , err := api .getByNamespace (resource , om .Namespace , parentRef .Name )
317309if err != nil {
318310return nil , nil , err
319311}
@@ -328,20 +320,19 @@ func (api *MetadataAPI) GetRootOwnerKindAndName(ctx context.Context, tm *metav1.
328320if err == nil {
329321return parentTm , parentOm
330322}
331- log .Warnf ("failed to retrieve %s from informer %s/%s: %s" , parentRef .Kind , om .Namespace , parentRef .Name , err )
332323if retry {
333- parentFromApi , err := api .client .
334- Resource (indexedResource .gvr ).
335- Namespace (om .Namespace ).
336- Get (ctx , parentRef .Name , metav1.GetOptions {})
324+ log .Warnf ("failed to retrieve %s from informer %s/%s, resyncing informer cache" , parentRef .Kind , om .Namespace , parentRef .Name )
325+ // The proxy injnector runs very soon after a resource is created,
326+ // which leaves a small possibility that the informer cache hasn't
327+ // picked up the new resource yet. If that is the case, we force an
328+ // informer resync to pick up the new resource.
329+ api .Sync (nil )
330+ parentTm , parentOm , err := getFromInformer ()
337331if err == nil {
338- if parentFromApi .ObjectMeta .Namespace == "" {
339- parentFromApi .ObjectMeta .Namespace = om .Namespace
340- }
341- return api .GetRootOwnerKindAndName (ctx , & parentFromApi .TypeMeta , & parentFromApi .ObjectMeta , retry )
332+ return parentTm , parentOm
342333}
343- log .Warnf ("failed to retrieve %s from direct API call %s/%s: %s" , parentRef .Kind , om .Namespace , parentRef .Name , err )
344334}
335+ log .Warnf ("failed to retrieve %s from informer %s/%s: %s" , parentRef .Kind , om .Namespace , parentRef .Name , err )
345336return & parentType , & metav1.ObjectMeta {Name : parentRef .Name , Namespace : om .Namespace }
346337}
347338
0 commit comments