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

retry on webhook failures #3906

Merged
merged 1 commit into from
Aug 25, 2020
Merged
Changes from all 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
35 changes: 34 additions & 1 deletion test/lib/duck/resource_getters.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,40 @@ limitations under the License.
package duck

import (
"fmt"
"strings"

"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/util/retry"
"knative.dev/pkg/apis"
"knative.dev/pkg/apis/duck"

"knative.dev/eventing/test/lib/resources"
)

// This is a workaround for https://github.com/knative/pkg/issues/1509
// Because tests currently fail immediately on any creation failure, this
// is problematic. On the reconcilers it's not an issue because they recover,
// but tests need this retry.
//
// https://github.com/knative/eventing/issues/3681
func isWebhookError(err error) bool {
return strings.Contains(err.Error(), "eventing-webhook.knative-eventing")
}

func RetryWebhookErrors(updater func(int) error) error {
attempts := 0
return retry.OnError(retry.DefaultRetry, isWebhookError, func() error {
err := updater(attempts)
attempts++
return err
})
}

// GetGenericObject returns a generic object representing a Kubernetes resource.
// Callers can cast this returned object to other objects that implement the corresponding duck-type.
func GetGenericObject(
Expand All @@ -38,7 +62,16 @@ func GetGenericObject(
) (runtime.Object, error) {
// get the resource's namespace and gvr
gvr, _ := meta.UnsafeGuessKindToResource(obj.GroupVersionKind())
u, err := dynamicClient.Resource(gvr).Namespace(obj.Namespace).Get(obj.Name, metav1.GetOptions{})
var u *unstructured.Unstructured
err := RetryWebhookErrors(func(attempts int) (err error) {
var e error
u, e = dynamicClient.Resource(gvr).Namespace(obj.Namespace).Get(obj.Name, metav1.GetOptions{})
if e != nil {
// TODO: Plumb some sort of logging here
fmt.Printf("Failed to get %s/%s: %v", obj.Namespace, obj.Name, e)
}
return e
})

if err != nil {
return nil, err
Expand Down