diff --git a/pkg/controller/operators/catalog/subscriptions.go b/pkg/controller/operators/catalog/subscriptions.go index 9d9389ee48c..6549758513f 100644 --- a/pkg/controller/operators/catalog/subscriptions.go +++ b/pkg/controller/operators/catalog/subscriptions.go @@ -7,6 +7,7 @@ import ( ipv1alpha1 "github.com/operator-framework/operator-lifecycle-manager/pkg/api/apis/installplan/v1alpha1" "github.com/operator-framework/operator-lifecycle-manager/pkg/api/apis/subscription/v1alpha1" + "github.com/operator-framework/operator-lifecycle-manager/pkg/lib/ownerutil" log "github.com/sirupsen/logrus" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) @@ -87,15 +88,7 @@ func (o *Operator) syncSubscription(sub *v1alpha1.Subscription) (*v1alpha1.Subsc Approval: sub.GetInstallPlanApproval(), }, } - owner := []metav1.OwnerReference{ - { - APIVersion: v1alpha1.SchemeGroupVersion.String(), - Kind: v1alpha1.SubscriptionKind, - Name: sub.GetName(), - UID: sub.GetUID(), - }, - } - ip.SetOwnerReferences(owner) + ownerutil.AddNonBlockingOwner(ip, sub) ip.SetGenerateName(fmt.Sprintf("install-%s-", sub.Status.CurrentCSV)) ip.SetNamespace(sub.GetNamespace()) res, err := o.client.InstallplanV1alpha1().InstallPlans(sub.GetNamespace()).Create(ip) diff --git a/pkg/lib/ownerutil/util.go b/pkg/lib/ownerutil/util.go index 55c1deee743..0aaa9075d01 100644 --- a/pkg/lib/ownerutil/util.go +++ b/pkg/lib/ownerutil/util.go @@ -2,7 +2,11 @@ package ownerutil import ( "github.com/operator-framework/operator-lifecycle-manager/pkg/api/apis" - "github.com/operator-framework/operator-lifecycle-manager/pkg/api/apis/clusterserviceversion/v1alpha1" + csv1alpha1 "github.com/operator-framework/operator-lifecycle-manager/pkg/api/apis/catalogsource/v1alpha1" + csvv1alpha1 "github.com/operator-framework/operator-lifecycle-manager/pkg/api/apis/clusterserviceversion/v1alpha1" + ipv1alpha1 "github.com/operator-framework/operator-lifecycle-manager/pkg/api/apis/installplan/v1alpha1" + subv1alpha1 "github.com/operator-framework/operator-lifecycle-manager/pkg/api/apis/subscription/v1alpha1" + "github.com/prometheus/common/log" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime/schema" ) @@ -22,20 +26,10 @@ func IsOwnedBy(object metav1.Object, owner Owner) bool { return false } +// AddNonBlockingOwner adds a nonblocking owner to the ownerref list. func AddNonBlockingOwner(object metav1.Object, owner Owner) { - // TODO: Remove as soon as possible - // This is a hack, for some reason CSVs that we get out of the informer are missing - // TypeMeta, which means we can't get the APIVersion or Kind generically here. - // The underlying issue should be found and fixes as soon as possible - // This needs to be removed before a new APIVersion is cut - if _, ok := owner.(*v1alpha1.ClusterServiceVersion); ok { - owner.SetGroupVersionKind(schema.GroupVersionKind{ - Group: apis.GroupName, - Version: v1alpha1.GroupVersion, - Kind: v1alpha1.ClusterServiceVersionKind, - }) - } - + // Most of the time we won't have TypeMeta on the object, so we infer it for types we know about + inferGroupVersionKind(owner) blockOwnerDeletion := false isController := false @@ -55,3 +49,43 @@ func AddNonBlockingOwner(object metav1.Object, owner Owner) { }) object.SetOwnerReferences(ownerRefs) } + +// inferGroupVersionKind adds TypeMeta to an owner so that it can be written to an ownerref. +// TypeMeta is generally only known at serialization time, so we often won't know what GVK an owner has. +// For the types we know about, we can add the GVK of the apis that we're using the interact with the object. +func inferGroupVersionKind(owner Owner) { + if !owner.GroupVersionKind().Empty() { + // owner already has TypeMeta, no inference needed + return + } + + switch v := owner.(type) { + case *csvv1alpha1.ClusterServiceVersion: + owner.SetGroupVersionKind(schema.GroupVersionKind{ + Group: apis.GroupName, + Version: csvv1alpha1.GroupVersion, + Kind: csvv1alpha1.ClusterServiceVersionKind, + }) + case *ipv1alpha1.InstallPlan: + owner.SetGroupVersionKind(schema.GroupVersionKind{ + Group: apis.GroupName, + Version: ipv1alpha1.GroupVersion, + Kind: ipv1alpha1.InstallPlanKind, + }) + case *subv1alpha1.Subscription: + owner.SetGroupVersionKind(schema.GroupVersionKind{ + Group: apis.GroupName, + Version: subv1alpha1.GroupVersion, + Kind: subv1alpha1.SubscriptionKind, + }) + case *csv1alpha1.CatalogSource: + owner.SetGroupVersionKind(schema.GroupVersionKind{ + Group: apis.GroupName, + Version: csv1alpha1.GroupVersion, + Kind: csv1alpha1.CatalogSourceKind, + }) + default: + log.Warnf("could not infer GVK for object: %#v, %#v", v, owner) + } + return +}