This repository has been archived by the owner on Aug 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Handle pointer items in ObjectList with ChildReconciler
Canonically, Items in an ObjectList are a slice of structs. Some types, like Istio, use a slice of pointers intead. We now detect and handle both. Signed-off-by: Scott Andrews <[email protected]>
- Loading branch information
Showing
5 changed files
with
186 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/* | ||
Copyright 2024 VMware, Inc. | ||
SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package resources | ||
|
||
import ( | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
) | ||
|
||
// +kubebuilder:object:root=true | ||
|
||
type TestResourcePointerList struct { | ||
metav1.TypeMeta `json:",inline"` | ||
metav1.ListMeta `json:"metadata"` | ||
|
||
Items []*TestResource `json:"items"` | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
Copyright 2024 VMware, Inc. | ||
SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package reconcilers | ||
|
||
import ( | ||
"fmt" | ||
"reflect" | ||
|
||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
) | ||
|
||
// extractItems returns a typed slice of objects from an object list | ||
func extractItems[T client.Object](list client.ObjectList) []T { | ||
items := []T{} | ||
listValue := reflect.ValueOf(list).Elem() | ||
itemsValue := listValue.FieldByName("Items") | ||
for i := 0; i < itemsValue.Len(); i++ { | ||
itemValue := itemsValue.Index(i) | ||
var item T | ||
switch itemValue.Kind() { | ||
case reflect.Pointer: | ||
item = itemValue.Interface().(T) | ||
case reflect.Interface: | ||
item = itemValue.Interface().(T) | ||
case reflect.Struct: | ||
item = itemValue.Addr().Interface().(T) | ||
default: | ||
panic(fmt.Errorf("unknown type %s, expected Pointer or Struct", itemValue.Kind().String())) | ||
} | ||
items = append(items, item) | ||
} | ||
return items | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
/* | ||
Copyright 2024 VMware, Inc. | ||
SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package reconcilers | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
"github.com/vmware-labs/reconciler-runtime/internal/resources" | ||
corev1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
) | ||
|
||
func TestExtractItems(t *testing.T) { | ||
tests := map[string]struct { | ||
list client.ObjectList | ||
expected []*resources.TestResource | ||
}{ | ||
"empty": { | ||
list: &resources.TestResourceList{ | ||
Items: []resources.TestResource{}, | ||
}, | ||
expected: []*resources.TestResource{}, | ||
}, | ||
"struct items": { | ||
list: &resources.TestResourceList{ | ||
Items: []resources.TestResource{ | ||
{ | ||
ObjectMeta: corev1.ObjectMeta{ | ||
Name: "obj1", | ||
}, | ||
}, | ||
{ | ||
ObjectMeta: corev1.ObjectMeta{ | ||
Name: "obj2", | ||
}, | ||
}, | ||
}, | ||
}, | ||
expected: []*resources.TestResource{ | ||
{ | ||
ObjectMeta: corev1.ObjectMeta{ | ||
Name: "obj1", | ||
}, | ||
}, | ||
{ | ||
ObjectMeta: corev1.ObjectMeta{ | ||
Name: "obj2", | ||
}, | ||
}, | ||
}, | ||
}, | ||
"struct-pointer items": { | ||
list: &resources.TestResourcePointerList{ | ||
Items: []*resources.TestResource{ | ||
{ | ||
ObjectMeta: corev1.ObjectMeta{ | ||
Name: "obj1", | ||
}, | ||
}, | ||
{ | ||
ObjectMeta: corev1.ObjectMeta{ | ||
Name: "obj2", | ||
}, | ||
}, | ||
}, | ||
}, | ||
expected: []*resources.TestResource{ | ||
{ | ||
ObjectMeta: corev1.ObjectMeta{ | ||
Name: "obj1", | ||
}, | ||
}, | ||
{ | ||
ObjectMeta: corev1.ObjectMeta{ | ||
Name: "obj2", | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
for name, tc := range tests { | ||
t.Run(name, func(t *testing.T) { | ||
actual := extractItems[*resources.TestResource](tc.list) | ||
expected := tc.expected | ||
if diff := cmp.Diff(expected, actual); diff != "" { | ||
t.Errorf("expected items to match actual items: %s", diff) | ||
} | ||
}) | ||
} | ||
} |