Skip to content

Commit

Permalink
Merge pull request #820 from njhale/sub-ip-ref
Browse files Browse the repository at this point in the history
Subscription Status InstallPlan References
  • Loading branch information
openshift-merge-robot authored Apr 24, 2019
2 parents c718ec8 + 9a65711 commit b8b77a1
Show file tree
Hide file tree
Showing 16 changed files with 563 additions and 123 deletions.
12 changes: 12 additions & 0 deletions pkg/api/apis/operators/reference.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package operators

import (
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/tools/reference"
)

// GetReference returns an ObjectReference for a given object whose concrete value is an OLM type.
func GetReference(obj runtime.Object) (*corev1.ObjectReference, error) {
return reference.GetReference(scheme, obj)
}
179 changes: 179 additions & 0 deletions pkg/api/apis/operators/reference_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
package operators

import (
"fmt"
"reflect"
"testing"

"github.com/stretchr/testify/require"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"

v1 "github.com/operator-framework/operator-lifecycle-manager/pkg/api/apis/operators/v1"
"github.com/operator-framework/operator-lifecycle-manager/pkg/api/apis/operators/v1alpha1"
)

func TestGetReference(t *testing.T) {
type args struct {
obj runtime.Object
}
type want struct {
ref *corev1.ObjectReference
err error
}
tests := []struct {
name string
args args
want want
}{
{
name: "Nil/Error",
args: args{obj: nil},
want: want{
ref: nil,
err: fmt.Errorf("can't reference a nil object"),
},
},
{
name: "v1/Pod/NotRegistered/Error",
args: args{&corev1.Pod{}},
want: want{
ref: nil,
err: runtime.NewNotRegisteredErrForType(scheme.Name(), reflect.TypeOf(corev1.Pod{})),
},
},
{
name: "v1alpha1/ClusterServiceVersion",
args: args{
&v1alpha1.ClusterServiceVersion{
ObjectMeta: metav1.ObjectMeta{
Namespace: "ns",
Name: "csv",
UID: types.UID("uid"),
SelfLink: buildSelfLink(v1alpha1.SchemeGroupVersion.String(), "clusterserviceversions", "ns", "csv"),
},
},
},
want: want{
ref: &corev1.ObjectReference{
Namespace: "ns",
Name: "csv",
UID: types.UID("uid"),
Kind: v1alpha1.ClusterServiceVersionKind,
APIVersion: v1alpha1.SchemeGroupVersion.String(),
},
err: nil,
},
},
{
name: "v1alpha1/InstallPlan",
args: args{
&v1alpha1.InstallPlan{
ObjectMeta: metav1.ObjectMeta{
Namespace: "ns",
Name: "ip",
UID: types.UID("uid"),
SelfLink: buildSelfLink(v1alpha1.SchemeGroupVersion.String(), "installplans", "ns", "ip"),
},
},
},
want: want{
ref: &corev1.ObjectReference{
Namespace: "ns",
Name: "ip",
UID: types.UID("uid"),
Kind: v1alpha1.InstallPlanKind,
APIVersion: v1alpha1.SchemeGroupVersion.String(),
},
err: nil,
},
},
{
name: "v1alpha1/Subscription",
args: args{
&v1alpha1.Subscription{
ObjectMeta: metav1.ObjectMeta{
Namespace: "ns",
Name: "sub",
UID: types.UID("uid"),
SelfLink: buildSelfLink(v1alpha1.SchemeGroupVersion.String(), "subscriptions", "ns", "sub"),
},
},
},
want: want{
ref: &corev1.ObjectReference{
Namespace: "ns",
Name: "sub",
UID: types.UID("uid"),
Kind: v1alpha1.SubscriptionKind,
APIVersion: v1alpha1.SchemeGroupVersion.String(),
},
err: nil,
},
},
{
name: "v1alpha1/CatalogSource",
args: args{
&v1alpha1.CatalogSource{
ObjectMeta: metav1.ObjectMeta{
Namespace: "ns",
Name: "catsrc",
UID: types.UID("uid"),
SelfLink: buildSelfLink(v1alpha1.SchemeGroupVersion.String(), "catalogsources", "ns", "catsrc"),
},
},
},
want: want{
ref: &corev1.ObjectReference{
Namespace: "ns",
Name: "catsrc",
UID: types.UID("uid"),
Kind: v1alpha1.CatalogSourceKind,
APIVersion: v1alpha1.SchemeGroupVersion.String(),
},
err: nil,
},
},
{
name: "v1/OperatorGroup",
args: args{
&v1.OperatorGroup{
ObjectMeta: metav1.ObjectMeta{
Namespace: "ns",
Name: "og",
UID: types.UID("uid"),
SelfLink: buildSelfLink(v1.SchemeGroupVersion.String(), "operatorgroups", "ns", "og"),
},
},
},
want: want{
ref: &corev1.ObjectReference{
Namespace: "ns",
Name: "og",
UID: types.UID("uid"),
Kind: v1.OperatorGroupKind,
APIVersion: v1.SchemeGroupVersion.String(),
},
err: nil,
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ref, err := GetReference(tt.args.obj)
require.Equal(t, tt.want.err, err)
require.Equal(t, tt.want.ref, ref)
})
}
}

// buildSelfLink returns a selfLink.
func buildSelfLink(groupVersion, plural, namespace, name string) string {
if namespace == metav1.NamespaceAll {
return fmt.Sprintf("/apis/%s/%s/%s", groupVersion, plural, name)
}
return fmt.Sprintf("/apis/%s/namespaces/%s/%s/%s", groupVersion, namespace, plural, name)
}
24 changes: 24 additions & 0 deletions pkg/api/apis/operators/register.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,27 @@
package operators

import (
"k8s.io/apimachinery/pkg/runtime"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"

v1 "github.com/operator-framework/operator-lifecycle-manager/pkg/api/apis/operators/v1"
"github.com/operator-framework/operator-lifecycle-manager/pkg/api/apis/operators/v1alpha1"
)

// GroupName is the API group name.
const GroupName = "operators.coreos.com"

var (
scheme = runtime.NewScheme()
localSchemeBuilder = runtime.SchemeBuilder{
v1alpha1.AddToScheme,
v1.AddToScheme,
}

// AddToScheme adds all types in the operators.coreos.com group to the given scheme.
AddToScheme = localSchemeBuilder.AddToScheme
)

func init() {
utilruntime.Must(AddToScheme(scheme))
}
2 changes: 2 additions & 0 deletions pkg/api/apis/operators/v1/operatorgroup_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ const (
OperatorGroupNamespaceAnnotationKey = "olm.operatorNamespace"
OperatorGroupTargetsAnnotationKey = "olm.targetNamespaces"
OperatorGroupProvidedAPIsAnnotationKey = "olm.providedAPIs"

OperatorGroupKind = "OperatorGroup"
)

type OperatorGroupSpec struct {
Expand Down
4 changes: 1 addition & 3 deletions pkg/api/apis/operators/v1/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import (
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
k8sscheme "k8s.io/client-go/kubernetes/scheme"

"github.com/operator-framework/operator-lifecycle-manager/pkg/api/apis/operators"
)

const (
Expand All @@ -16,7 +14,7 @@ const (
)

// SchemeGroupVersion is group version used to register these objects
var SchemeGroupVersion = schema.GroupVersion{Group: operators.GroupName, Version: GroupVersion}
var SchemeGroupVersion = schema.GroupVersion{Group: GroupName, Version: GroupVersion}

// Kind takes an unqualified kind and returns back a Group qualified GroupKind
func Kind(kind string) schema.GroupKind {
Expand Down
4 changes: 1 addition & 3 deletions pkg/api/apis/operators/v1alpha1/catalogsource_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,10 @@ import (

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

"github.com/operator-framework/operator-lifecycle-manager/pkg/api/apis/operators"
)

const (
CatalogSourceCRDAPIVersion = operators.GroupName + "/" + GroupVersion
CatalogSourceCRDAPIVersion = GroupName + "/" + GroupVersion
CatalogSourceKind = "CatalogSource"
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,10 @@ import (

"github.com/coreos/go-semver/semver"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/operator-framework/operator-lifecycle-manager/pkg/api/apis/operators"
)

const (
ClusterServiceVersionAPIVersion = operators.GroupName + "/" + GroupVersion
ClusterServiceVersionAPIVersion = GroupName + "/" + GroupVersion
ClusterServiceVersionKind = "ClusterServiceVersion"
OperatorGroupNamespaceAnnotationKey = "olm.operatorNamespace"
)
Expand Down
4 changes: 1 addition & 3 deletions pkg/api/apis/operators/v1alpha1/installplan_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,11 @@ import (

corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/operator-framework/operator-lifecycle-manager/pkg/api/apis/operators"
)

const (
InstallPlanKind = "InstallPlan"
InstallPlanAPIVersion = operators.GroupName + "/" + GroupVersion
InstallPlanAPIVersion = GroupName + "/" + GroupVersion
)

// Approval is the user approval policy for an InstallPlan.
Expand Down
3 changes: 1 addition & 2 deletions pkg/api/apis/operators/v1alpha1/register.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package v1alpha1

import (
"github.com/operator-framework/operator-lifecycle-manager/pkg/api/apis/operators"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
Expand All @@ -13,7 +12,7 @@ const (
)

// SchemeGroupVersion is group version used to register these objects
var SchemeGroupVersion = schema.GroupVersion{Group: operators.GroupName, Version: GroupVersion}
var SchemeGroupVersion = schema.GroupVersion{Group: GroupName, Version: GroupVersion}

// Kind takes an unqualified kind and returns back a Group qualified GroupKind
func Kind(kind string) schema.GroupKind {
Expand Down
Loading

0 comments on commit b8b77a1

Please sign in to comment.