operator-framework / api

Contains the API definitions used by OLM and Marketplace
Apache License 2.0
29 stars 73 forks source link

Converting an operator group object to an unstructured object causes a panic #269

Open MaxMcAdam opened 2 years ago

MaxMcAdam commented 2 years ago

This works with the v1alpha1 types that I have tried (ClusterServiceVersion, Subscription, CatalogSource) but not OperatorGroups. I've included a minimal example to trigger the panic but it also panics when the required fields in the OperatorGroup object are filled out. For context, I am trying to use the v1 scheme to recognize an operator group yaml but create the object with a dynamic client which is why I want to convert from OperatorGroup to unstructured.

package main

import (
        "fmt"
        olmv1 "github.com/operator-framework/api/pkg/operators/v1"
        olmv1scheme "github.com/operator-framework/api/pkg/operators/v1"
        "k8s.io/apimachinery/pkg/runtime"
        "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
        )

func main() {
        var opGroup runtime.Object
        opGroup = &olmv1.OperatorGroup{}

        sch := runtime.NewScheme()
        if err := olmv1scheme.AddToScheme(sch); err != nil {
                fmt.Printf("Error adding scheme: %v", err)
        }

        unstr := unstructured.Unstructured{}
        err := sch.Convert(opGroup, &unstr, nil)
        if err != nil {
                fmt.Printf("Error: %v",err)
        } else {
                fmt.Printf("Converted operator group to unstructured successfully")
        }
}
panic: value method k8s.io/apimachinery/pkg/apis/meta/v1.Time.ToUnstructured called using nil *Time pointer

goroutine 1 [running]:
k8s.io/apimachinery/pkg/apis/meta/v1.(*Time).ToUnstructured(0x100000001?)
    <autogenerated>:1 +0x47
sigs.k8s.io/structured-merge-diff/v4/value.TypeReflectCacheEntry.ToUnstructured({0x1, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, {0x0, 0x0, 0x0}}, ...)
    /root/go/pkg/mod/sigs.k8s.io/structured-merge-diff/v4@v4.2.1/value/reflectcache.go:188 +0x6fd
k8s.io/apimachinery/pkg/runtime.toUnstructured({0xb06b80?, 0xc00019d020?, 0x2?}, {0xa60120?, 0xc0001f63f0?, 0x4?})
    /root/go/pkg/mod/k8s.io/apimachinery@v0.24.0/pkg/runtime/converter.go:655 +0x125
k8s.io/apimachinery/pkg/runtime.structToUnstructured({0xab25e0?, 0xc00019d000?, 0x45bb25?}, {0xa60120?, 0xc0001f63a0?, 0x28a60?})
    /root/go/pkg/mod/k8s.io/apimachinery@v0.24.0/pkg/runtime/converter.go:843 +0x7dd
k8s.io/apimachinery/pkg/runtime.toUnstructured({0xab25e0?, 0xc00019d000?, 0x3?}, {0xa60120?, 0xc0001f63a0?, 0x98?})
    /root/go/pkg/mod/k8s.io/apimachinery@v0.24.0/pkg/runtime/converter.go:692 +0x85c
k8s.io/apimachinery/pkg/runtime.structToUnstructured({0xab2520?, 0xc00019cea0?, 0xc0001d2ff0?}, {0xa6c500?, 0xc00012c5f8?, 0x415945?})
    /root/go/pkg/mod/k8s.io/apimachinery@v0.24.0/pkg/runtime/converter.go:843 +0x7dd
k8s.io/apimachinery/pkg/runtime.toUnstructured({0xab2520?, 0xc00019cea0?, 0xc00019cea0?}, {0xa6c500?, 0xc00012c5f8?, 0xc000151db8?})
    /root/go/pkg/mod/k8s.io/apimachinery@v0.24.0/pkg/runtime/converter.go:692 +0x85c
k8s.io/apimachinery/pkg/runtime.(*unstructuredConverter).ToUnstructured(0x1023a50, {0xaf9a20?, 0xc00019cea0})
    /root/go/pkg/mod/k8s.io/apimachinery@v0.24.0/pkg/runtime/converter.go:586 +0x3ba
k8s.io/apimachinery/pkg/runtime.(*Scheme).Convert(0xc0001a39d0, {0xaf9a20, 0xc00019cea0}, {0xb03720, 0xc00012c5f0}, {0x0?, 0x0})
    /root/go/pkg/mod/k8s.io/apimachinery@v0.24.0/pkg/runtime/scheme.go:381 +0x2af
main.main()`
JustinKuli commented 10 months ago

I had this same problem. It looks like the nil Time field is at .Status.LastUpdated, so adding this line before doing the conversion resolved the panic for me:

// import metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

opGroup.Status.LastUpdated = &metav1.Time{}