Skip to content

Commit a852055

Browse files
committed
fix(tracing): Resync informer cache in root owner retries
Companion to #14682 The previous retry logic relied on direct metadata API calls in cases where the informer cache hadn't updated when injecting a pod. This updates the retry logic to force a refresh of the informer cache instead of going to the metadata API. Signed-off-by: Scott Fleener <scott@buoyant.io>
1 parent 63c5069 commit a852055

File tree

1 file changed

+12
-21
lines changed

1 file changed

+12
-21
lines changed

controller/k8s/metadata_api.go

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -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.
310302
log.Warnf("parent ref has no kind: %v", parentRef)
311303
return tm, om
312304
}
313-
indexedResource, ok := indexedParents[parentRef.Kind]
305+
resource, ok := indexedParents[parentRef.Kind]
314306
if ok {
315307
getFromInformer := 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)
317309
if err != nil {
318310
return nil, nil, err
319311
}
@@ -328,20 +320,19 @@ func (api *MetadataAPI) GetRootOwnerKindAndName(ctx context.Context, tm *metav1.
328320
if err == nil {
329321
return parentTm, parentOm
330322
}
331-
log.Warnf("failed to retrieve %s from informer %s/%s: %s", parentRef.Kind, om.Namespace, parentRef.Name, err)
332323
if 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()
337331
if 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)
345336
return &parentType, &metav1.ObjectMeta{Name: parentRef.Name, Namespace: om.Namespace}
346337
}
347338

0 commit comments

Comments
 (0)