Skip to content

Commit 9c25343

Browse files
committed
🏃 Use scheme.Convert to convert unstructrured objects
Signed-off-by: Vince Prignano <[email protected]>
1 parent c14d8e6 commit 9c25343

File tree

2 files changed

+22
-13
lines changed

2 files changed

+22
-13
lines changed

pkg/envtest/crd.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ func defaultCRDOptions(o *CRDInstallOptions) {
114114
func WaitForCRDs(config *rest.Config, crds []runtime.Object, options CRDInstallOptions) error {
115115
// Add each CRD to a map of GroupVersion to Resource
116116
waitingFor := map[schema.GroupVersion]*sets.String{}
117-
for _, crd := range runtimeListToUnstructured(crds) {
117+
for _, crd := range runtimeCRDListToUnstructured(crds) {
118118
gvs := []schema.GroupVersion{}
119119
crdGroup, _, err := unstructured.NestedString(crd.Object, "spec", "group")
120120
if err != nil {
@@ -230,7 +230,7 @@ func UninstallCRDs(config *rest.Config, options CRDInstallOptions) error {
230230
}
231231

232232
// Uninstall each CRD
233-
for _, crd := range runtimeListToUnstructured(options.CRDs) {
233+
for _, crd := range runtimeCRDListToUnstructured(options.CRDs) {
234234
log.V(1).Info("uninstalling CRD", "crd", crd.GetName())
235235
if err := cs.Delete(context.TODO(), crd); err != nil {
236236
// If CRD is not found, we can consider success
@@ -251,7 +251,7 @@ func CreateCRDs(config *rest.Config, crds []runtime.Object) error {
251251
}
252252

253253
// Create each CRD
254-
for _, crd := range runtimeListToUnstructured(crds) {
254+
for _, crd := range runtimeCRDListToUnstructured(crds) {
255255
log.V(1).Info("installing CRD", "crd", crd.GetName())
256256
existingCrd := crd.DeepCopy()
257257
err := cs.Get(context.TODO(), client.ObjectKey{Name: crd.GetName()}, existingCrd)
@@ -314,7 +314,7 @@ func renderCRDs(options *CRDInstallOptions) ([]runtime.Object, error) {
314314
crds = append(crds, crdList...)
315315
}
316316

317-
return unstructuredListToRuntime(crds), nil
317+
return unstructuredCRDListToRuntime(crds), nil
318318
}
319319

320320
// readCRDs reads the CRDs from files and Unmarshals them into structs

pkg/envtest/helper.go

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,21 @@ package envtest
33
import (
44
"reflect"
55

6+
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
7+
apiextensionsv1beta1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
68
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
79
"k8s.io/apimachinery/pkg/runtime"
810
)
911

12+
var (
13+
crdScheme = runtime.NewScheme()
14+
)
15+
16+
func init() {
17+
_ = apiextensionsv1.AddToScheme(crdScheme)
18+
_ = apiextensionsv1beta1.AddToScheme(crdScheme)
19+
}
20+
1021
// mergePaths merges two string slices containing paths.
1122
// This function makes no guarantees about order of the merged slice.
1223
func mergePaths(s1, s2 []string) []string {
@@ -30,10 +41,10 @@ func mergePaths(s1, s2 []string) []string {
3041
// This function makes no guarantees about order of the merged slice.
3142
func mergeCRDs(s1, s2 []runtime.Object) []runtime.Object {
3243
m := make(map[string]*unstructured.Unstructured)
33-
for _, obj := range runtimeListToUnstructured(s1) {
44+
for _, obj := range runtimeCRDListToUnstructured(s1) {
3445
m[obj.GetName()] = obj
3546
}
36-
for _, obj := range runtimeListToUnstructured(s2) {
47+
for _, obj := range runtimeCRDListToUnstructured(s2) {
3748
m[obj.GetName()] = obj
3849
}
3950
merged := make([]runtime.Object, len(m))
@@ -57,21 +68,19 @@ func existsUnstructured(s1, s2 []*unstructured.Unstructured) bool {
5768
return false
5869
}
5970

60-
func runtimeListToUnstructured(l []runtime.Object) []*unstructured.Unstructured {
71+
func runtimeCRDListToUnstructured(l []runtime.Object) []*unstructured.Unstructured {
6172
res := []*unstructured.Unstructured{}
6273
for _, obj := range l {
63-
m, err := runtime.DefaultUnstructuredConverter.ToUnstructured(obj.DeepCopyObject())
64-
if err != nil {
74+
u := &unstructured.Unstructured{}
75+
if err := crdScheme.Convert(obj, u, nil); err != nil {
6576
continue
6677
}
67-
res = append(res, &unstructured.Unstructured{
68-
Object: m,
69-
})
78+
res = append(res, u)
7079
}
7180
return res
7281
}
7382

74-
func unstructuredListToRuntime(l []*unstructured.Unstructured) []runtime.Object {
83+
func unstructuredCRDListToRuntime(l []*unstructured.Unstructured) []runtime.Object {
7584
res := []runtime.Object{}
7685
for _, obj := range l {
7786
res = append(res, obj.DeepCopy())

0 commit comments

Comments
 (0)