Skip to content

Commit

Permalink
feat(ownerutil): update ownerutil to infer GVK for OLM types
Browse files Browse the repository at this point in the history
  • Loading branch information
ecordell committed May 29, 2018
1 parent d155769 commit 95c03e0
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 23 deletions.
11 changes: 2 additions & 9 deletions pkg/controller/operators/catalog/subscriptions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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)
Expand Down
62 changes: 48 additions & 14 deletions pkg/lib/ownerutil/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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

Expand All @@ -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
}

0 comments on commit 95c03e0

Please sign in to comment.