Skip to content

Commit

Permalink
🏃 Use scheme.Convert to convert unstructrured objects
Browse files Browse the repository at this point in the history
Signed-off-by: Vince Prignano <vincepri@vmware.com>
  • Loading branch information
vincepri committed Jan 23, 2020
1 parent c14d8e6 commit a2070f7
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 14 deletions.
8 changes: 4 additions & 4 deletions pkg/envtest/crd.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func defaultCRDOptions(o *CRDInstallOptions) {
func WaitForCRDs(config *rest.Config, crds []runtime.Object, options CRDInstallOptions) error {
// Add each CRD to a map of GroupVersion to Resource
waitingFor := map[schema.GroupVersion]*sets.String{}
for _, crd := range runtimeListToUnstructured(crds) {
for _, crd := range runtimeCRDListToUnstructured(crds) {
gvs := []schema.GroupVersion{}
crdGroup, _, err := unstructured.NestedString(crd.Object, "spec", "group")
if err != nil {
Expand Down Expand Up @@ -230,7 +230,7 @@ func UninstallCRDs(config *rest.Config, options CRDInstallOptions) error {
}

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

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

return unstructuredListToRuntime(crds), nil
return unstructuredCRDListToRuntime(crds), nil
}

// readCRDs reads the CRDs from files and Unmarshals them into structs
Expand Down
2 changes: 1 addition & 1 deletion pkg/envtest/envtest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ var _ = Describe("Test", func() {

// Cleanup CRDs
AfterEach(func(done Done) {
for _, crd := range runtimeListToUnstructured(crds) {
for _, crd := range runtimeCRDListToUnstructured(crds) {
// Delete only if CRD exists.
crdObjectKey := client.ObjectKey{
Name: crd.GetName(),
Expand Down
27 changes: 18 additions & 9 deletions pkg/envtest/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,21 @@ package envtest
import (
"reflect"

apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
apiextensionsv1beta1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
)

var (
crdScheme = runtime.NewScheme()
)

func init() {
_ = apiextensionsv1.AddToScheme(crdScheme)
_ = apiextensionsv1beta1.AddToScheme(crdScheme)
}

// mergePaths merges two string slices containing paths.
// This function makes no guarantees about order of the merged slice.
func mergePaths(s1, s2 []string) []string {
Expand All @@ -30,10 +41,10 @@ func mergePaths(s1, s2 []string) []string {
// This function makes no guarantees about order of the merged slice.
func mergeCRDs(s1, s2 []runtime.Object) []runtime.Object {
m := make(map[string]*unstructured.Unstructured)
for _, obj := range runtimeListToUnstructured(s1) {
for _, obj := range runtimeCRDListToUnstructured(s1) {
m[obj.GetName()] = obj
}
for _, obj := range runtimeListToUnstructured(s2) {
for _, obj := range runtimeCRDListToUnstructured(s2) {
m[obj.GetName()] = obj
}
merged := make([]runtime.Object, len(m))
Expand All @@ -57,21 +68,19 @@ func existsUnstructured(s1, s2 []*unstructured.Unstructured) bool {
return false
}

func runtimeListToUnstructured(l []runtime.Object) []*unstructured.Unstructured {
func runtimeCRDListToUnstructured(l []runtime.Object) []*unstructured.Unstructured {
res := []*unstructured.Unstructured{}
for _, obj := range l {
m, err := runtime.DefaultUnstructuredConverter.ToUnstructured(obj.DeepCopyObject())
if err != nil {
u := &unstructured.Unstructured{}
if err := crdScheme.Convert(obj, u, nil); err != nil {
continue
}
res = append(res, &unstructured.Unstructured{
Object: m,
})
res = append(res, u)
}
return res
}

func unstructuredListToRuntime(l []*unstructured.Unstructured) []runtime.Object {
func unstructuredCRDListToRuntime(l []*unstructured.Unstructured) []runtime.Object {
res := []runtime.Object{}
for _, obj := range l {
res = append(res, obj.DeepCopy())
Expand Down

0 comments on commit a2070f7

Please sign in to comment.