Skip to content

Commit

Permalink
[bug] Update DefaultObjectDescriber to handle interface params (kuber…
Browse files Browse the repository at this point in the history
…netes#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>
  • Loading branch information
vihangm committed Jun 21, 2023
1 parent 55fb180 commit 56cb4c9
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
8 changes: 6 additions & 2 deletions staging/src/k8s.io/kubectl/pkg/describe/describe.go
Expand Up @@ -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)
Expand Down
21 changes: 16 additions & 5 deletions staging/src/k8s.io/kubectl/pkg/describe/describe_test.go
Expand Up @@ -1254,34 +1254,45 @@ 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"}})
if err != nil {
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"}})
if err != nil {
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)
}
}

Expand Down

0 comments on commit 56cb4c9

Please sign in to comment.