retry on webhook failures
@@ -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(
@@ -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
