[bug] Update DefaultObjectDescriber to handle interface params (#118190)

* Update DefaultObjectDescriber to handle interface params

Signed-off-by: Vihang Mehta <vihang@pixielabs.ai>

* Minor test cleanup for more descriptive errors

Signed-off-by: Vihang Mehta <vihang@pixielabs.ai>

---------

Signed-off-by: Vihang Mehta <vihang@pixielabs.ai>
This commit is contained in:
Vihang Mehta 2023-06-21 13:17:41 -07:00 committed by GitHub
parent 55fb1805a1
commit 56cb4c9391
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 7 deletions

View File

@ -5030,8 +5030,12 @@ func (fn typeFunc) Matches(types []reflect.Type) bool {
// Describe invokes the nested function with the exact number of arguments.
func (fn typeFunc) Describe(exact interface{}, extra ...interface{}) (string, error) {
values := []reflect.Value{reflect.ValueOf(exact)}
for _, obj := range extra {
values = append(values, reflect.ValueOf(obj))
for i, obj := range extra {
if obj != nil {
values = append(values, reflect.ValueOf(obj))
} else {
values = append(values, reflect.New(fn.Extra[i]).Elem())
}
}
out := fn.Fn.Call(values)
s := out[0].Interface().(string)

View File

@ -1254,7 +1254,7 @@ func TestDefaultDescribers(t *testing.T) {
t.Fatalf("unexpected error: %v", err)
}
if !strings.Contains(out, "foo") {
t.Errorf("unexpected output: %s", out)
t.Errorf("missing Pod `foo` in output: %s", out)
}
out, err = DefaultObjectDescriber.DescribeObject(&corev1.Service{ObjectMeta: metav1.ObjectMeta{Name: "foo"}})
@ -1262,18 +1262,18 @@ func TestDefaultDescribers(t *testing.T) {
t.Fatalf("unexpected error: %v", err)
}
if !strings.Contains(out, "foo") {
t.Errorf("unexpected output: %s", out)
t.Errorf("missing Service `foo` in output: %s", out)
}
out, err = DefaultObjectDescriber.DescribeObject(&corev1.ReplicationController{
ObjectMeta: metav1.ObjectMeta{Name: "foo"},
Spec: corev1.ReplicationControllerSpec{Replicas: utilpointer.Int32Ptr(1)},
Spec: corev1.ReplicationControllerSpec{Replicas: utilpointer.Int32(1)},
})
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if !strings.Contains(out, "foo") {
t.Errorf("unexpected output: %s", out)
t.Errorf("missing Replication Controller `foo` in output: %s", out)
}
out, err = DefaultObjectDescriber.DescribeObject(&corev1.Node{ObjectMeta: metav1.ObjectMeta{Name: "foo"}})
@ -1281,7 +1281,18 @@ func TestDefaultDescribers(t *testing.T) {
t.Fatalf("unexpected error: %v", err)
}
if !strings.Contains(out, "foo") {
t.Errorf("unexpected output: %s", out)
t.Errorf("missing Node `foo` output: %s", out)
}
out, err = DefaultObjectDescriber.DescribeObject(&appsv1.StatefulSet{
ObjectMeta: metav1.ObjectMeta{Name: "foo"},
Spec: appsv1.StatefulSetSpec{Replicas: utilpointer.Int32(1)},
})
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if !strings.Contains(out, "foo") {
t.Errorf("missing StatefulSet `foo` in output: %s", out)
}
}