Skip to content

Commit 37fe61b

Browse files
authored
Merge pull request #779 from vincepri/use-scheme-convert
🏃 Use scheme.Convert to convert unstructrured objects
2 parents cc3327f + 5c49cc4 commit 37fe61b

File tree

3 files changed

+25
-14
lines changed

3 files changed

+25
-14
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/envtest_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ var _ = Describe("Test", func() {
5959

6060
// Cleanup CRDs
6161
AfterEach(func(done Done) {
62-
for _, crd := range runtimeListToUnstructured(crds) {
62+
for _, crd := range runtimeCRDListToUnstructured(crds) {
6363
// Delete only if CRD exists.
6464
crdObjectKey := client.ObjectKey{
6565
Name: crd.GetName(),

pkg/envtest/helper.go

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,22 @@ 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+
// init is required to correctly initialize the crdScheme package variable.
17+
func init() {
18+
_ = apiextensionsv1.AddToScheme(crdScheme)
19+
_ = apiextensionsv1beta1.AddToScheme(crdScheme)
20+
}
21+
1022
// mergePaths merges two string slices containing paths.
1123
// This function makes no guarantees about order of the merged slice.
1224
func mergePaths(s1, s2 []string) []string {
@@ -30,10 +42,10 @@ func mergePaths(s1, s2 []string) []string {
3042
// This function makes no guarantees about order of the merged slice.
3143
func mergeCRDs(s1, s2 []runtime.Object) []runtime.Object {
3244
m := make(map[string]*unstructured.Unstructured)
33-
for _, obj := range runtimeListToUnstructured(s1) {
45+
for _, obj := range runtimeCRDListToUnstructured(s1) {
3446
m[obj.GetName()] = obj
3547
}
36-
for _, obj := range runtimeListToUnstructured(s2) {
48+
for _, obj := range runtimeCRDListToUnstructured(s2) {
3749
m[obj.GetName()] = obj
3850
}
3951
merged := make([]runtime.Object, len(m))
@@ -57,21 +69,20 @@ func existsUnstructured(s1, s2 []*unstructured.Unstructured) bool {
5769
return false
5870
}
5971

60-
func runtimeListToUnstructured(l []runtime.Object) []*unstructured.Unstructured {
72+
func runtimeCRDListToUnstructured(l []runtime.Object) []*unstructured.Unstructured {
6173
res := []*unstructured.Unstructured{}
6274
for _, obj := range l {
63-
m, err := runtime.DefaultUnstructuredConverter.ToUnstructured(obj.DeepCopyObject())
64-
if err != nil {
75+
u := &unstructured.Unstructured{}
76+
if err := crdScheme.Convert(obj, u, nil); err != nil {
77+
log.Error(err, "error converting to unstructured object", "object-kind", obj.GetObjectKind())
6578
continue
6679
}
67-
res = append(res, &unstructured.Unstructured{
68-
Object: m,
69-
})
80+
res = append(res, u)
7081
}
7182
return res
7283
}
7384

74-
func unstructuredListToRuntime(l []*unstructured.Unstructured) []runtime.Object {
85+
func unstructuredCRDListToRuntime(l []*unstructured.Unstructured) []runtime.Object {
7586
res := []runtime.Object{}
7687
for _, obj := range l {
7788
res = append(res, obj.DeepCopy())

0 commit comments

Comments
 (0)