Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add retry loop for client.get of replicaset as that sometimes fails #1072

Merged
merged 6 commits into from
Sep 5, 2022
Merged
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 20 additions & 6 deletions pkg/instrumentation/sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,19 @@ import (
"fmt"
"sort"
"strings"
"time"
"unsafe"

"github.com/go-logr/logr"
"go.opentelemetry.io/otel/attribute"
semconv "go.opentelemetry.io/otel/semconv/v1.7.0"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/util/retry"
"sigs.k8s.io/controller-runtime/pkg/client"

"github.com/open-telemetry/opentelemetry-operator/apis/v1alpha1"
Expand Down Expand Up @@ -291,12 +295,22 @@ func (i *sdkInjector) addParentResourceLabels(ctx context.Context, uid bool, ns
}
// parent of ReplicaSet is e.g. Deployment which we are interested to know
rs := appsv1.ReplicaSet{}
// ignore the error. The object might not exist, the error is not important, getting labels is just the best effort
//nolint:errcheck
i.client.Get(ctx, types.NamespacedName{
Namespace: ns.Name,
Name: owner.Name,
}, &rs)
nsn := types.NamespacedName{Namespace: ns.Name, Name: owner.Name}
backOff := wait.Backoff{Duration: 10 * time.Millisecond, Factor: 1.5, Jitter: 0.1, Steps: 20, Cap: 2 * time.Second}

checkError := func(err error) bool {
return apierrors.IsNotFound(err)
}

getReplicaSet := func() error {
return i.client.Get(ctx, nsn, &rs)
}

// use a retry loop to get the Deployment. A single call to client.get fails occasionally
err := retry.OnError(backOff, checkError, getReplicaSet)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we use the same retry approach for other objects as well?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could. Do you have specific places you'd want to do this? So far this is the only place I've seen failures

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh right this is the only place that uses k8s client in this function. I don't know any other place.

if err != nil {
i.logger.Error(err, "failed to get replicaset", "replicaset", nsn.Name, "namespace", nsn.Namespace)
}
i.addParentResourceLabels(ctx, uid, ns, rs.ObjectMeta, resources)
case "deployment":
resources[semconv.K8SDeploymentNameKey] = owner.Name
Expand Down