Skip to content

Commit b8b77a1

Browse files
Merge pull request #820 from njhale/sub-ip-ref
Subscription Status InstallPlan References
2 parents c718ec8 + 9a65711 commit b8b77a1

File tree

16 files changed

+563
-123
lines changed

16 files changed

+563
-123
lines changed

pkg/api/apis/operators/reference.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package operators
2+
3+
import (
4+
corev1 "k8s.io/api/core/v1"
5+
"k8s.io/apimachinery/pkg/runtime"
6+
"k8s.io/client-go/tools/reference"
7+
)
8+
9+
// GetReference returns an ObjectReference for a given object whose concrete value is an OLM type.
10+
func GetReference(obj runtime.Object) (*corev1.ObjectReference, error) {
11+
return reference.GetReference(scheme, obj)
12+
}
Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
package operators
2+
3+
import (
4+
"fmt"
5+
"reflect"
6+
"testing"
7+
8+
"github.com/stretchr/testify/require"
9+
corev1 "k8s.io/api/core/v1"
10+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
11+
"k8s.io/apimachinery/pkg/runtime"
12+
"k8s.io/apimachinery/pkg/types"
13+
14+
v1 "github.com/operator-framework/operator-lifecycle-manager/pkg/api/apis/operators/v1"
15+
"github.com/operator-framework/operator-lifecycle-manager/pkg/api/apis/operators/v1alpha1"
16+
)
17+
18+
func TestGetReference(t *testing.T) {
19+
type args struct {
20+
obj runtime.Object
21+
}
22+
type want struct {
23+
ref *corev1.ObjectReference
24+
err error
25+
}
26+
tests := []struct {
27+
name string
28+
args args
29+
want want
30+
}{
31+
{
32+
name: "Nil/Error",
33+
args: args{obj: nil},
34+
want: want{
35+
ref: nil,
36+
err: fmt.Errorf("can't reference a nil object"),
37+
},
38+
},
39+
{
40+
name: "v1/Pod/NotRegistered/Error",
41+
args: args{&corev1.Pod{}},
42+
want: want{
43+
ref: nil,
44+
err: runtime.NewNotRegisteredErrForType(scheme.Name(), reflect.TypeOf(corev1.Pod{})),
45+
},
46+
},
47+
{
48+
name: "v1alpha1/ClusterServiceVersion",
49+
args: args{
50+
&v1alpha1.ClusterServiceVersion{
51+
ObjectMeta: metav1.ObjectMeta{
52+
Namespace: "ns",
53+
Name: "csv",
54+
UID: types.UID("uid"),
55+
SelfLink: buildSelfLink(v1alpha1.SchemeGroupVersion.String(), "clusterserviceversions", "ns", "csv"),
56+
},
57+
},
58+
},
59+
want: want{
60+
ref: &corev1.ObjectReference{
61+
Namespace: "ns",
62+
Name: "csv",
63+
UID: types.UID("uid"),
64+
Kind: v1alpha1.ClusterServiceVersionKind,
65+
APIVersion: v1alpha1.SchemeGroupVersion.String(),
66+
},
67+
err: nil,
68+
},
69+
},
70+
{
71+
name: "v1alpha1/InstallPlan",
72+
args: args{
73+
&v1alpha1.InstallPlan{
74+
ObjectMeta: metav1.ObjectMeta{
75+
Namespace: "ns",
76+
Name: "ip",
77+
UID: types.UID("uid"),
78+
SelfLink: buildSelfLink(v1alpha1.SchemeGroupVersion.String(), "installplans", "ns", "ip"),
79+
},
80+
},
81+
},
82+
want: want{
83+
ref: &corev1.ObjectReference{
84+
Namespace: "ns",
85+
Name: "ip",
86+
UID: types.UID("uid"),
87+
Kind: v1alpha1.InstallPlanKind,
88+
APIVersion: v1alpha1.SchemeGroupVersion.String(),
89+
},
90+
err: nil,
91+
},
92+
},
93+
{
94+
name: "v1alpha1/Subscription",
95+
args: args{
96+
&v1alpha1.Subscription{
97+
ObjectMeta: metav1.ObjectMeta{
98+
Namespace: "ns",
99+
Name: "sub",
100+
UID: types.UID("uid"),
101+
SelfLink: buildSelfLink(v1alpha1.SchemeGroupVersion.String(), "subscriptions", "ns", "sub"),
102+
},
103+
},
104+
},
105+
want: want{
106+
ref: &corev1.ObjectReference{
107+
Namespace: "ns",
108+
Name: "sub",
109+
UID: types.UID("uid"),
110+
Kind: v1alpha1.SubscriptionKind,
111+
APIVersion: v1alpha1.SchemeGroupVersion.String(),
112+
},
113+
err: nil,
114+
},
115+
},
116+
{
117+
name: "v1alpha1/CatalogSource",
118+
args: args{
119+
&v1alpha1.CatalogSource{
120+
ObjectMeta: metav1.ObjectMeta{
121+
Namespace: "ns",
122+
Name: "catsrc",
123+
UID: types.UID("uid"),
124+
SelfLink: buildSelfLink(v1alpha1.SchemeGroupVersion.String(), "catalogsources", "ns", "catsrc"),
125+
},
126+
},
127+
},
128+
want: want{
129+
ref: &corev1.ObjectReference{
130+
Namespace: "ns",
131+
Name: "catsrc",
132+
UID: types.UID("uid"),
133+
Kind: v1alpha1.CatalogSourceKind,
134+
APIVersion: v1alpha1.SchemeGroupVersion.String(),
135+
},
136+
err: nil,
137+
},
138+
},
139+
{
140+
name: "v1/OperatorGroup",
141+
args: args{
142+
&v1.OperatorGroup{
143+
ObjectMeta: metav1.ObjectMeta{
144+
Namespace: "ns",
145+
Name: "og",
146+
UID: types.UID("uid"),
147+
SelfLink: buildSelfLink(v1.SchemeGroupVersion.String(), "operatorgroups", "ns", "og"),
148+
},
149+
},
150+
},
151+
want: want{
152+
ref: &corev1.ObjectReference{
153+
Namespace: "ns",
154+
Name: "og",
155+
UID: types.UID("uid"),
156+
Kind: v1.OperatorGroupKind,
157+
APIVersion: v1.SchemeGroupVersion.String(),
158+
},
159+
err: nil,
160+
},
161+
},
162+
}
163+
164+
for _, tt := range tests {
165+
t.Run(tt.name, func(t *testing.T) {
166+
ref, err := GetReference(tt.args.obj)
167+
require.Equal(t, tt.want.err, err)
168+
require.Equal(t, tt.want.ref, ref)
169+
})
170+
}
171+
}
172+
173+
// buildSelfLink returns a selfLink.
174+
func buildSelfLink(groupVersion, plural, namespace, name string) string {
175+
if namespace == metav1.NamespaceAll {
176+
return fmt.Sprintf("/apis/%s/%s/%s", groupVersion, plural, name)
177+
}
178+
return fmt.Sprintf("/apis/%s/namespaces/%s/%s/%s", groupVersion, namespace, plural, name)
179+
}

pkg/api/apis/operators/register.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,27 @@
11
package operators
22

3+
import (
4+
"k8s.io/apimachinery/pkg/runtime"
5+
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
6+
7+
v1 "github.com/operator-framework/operator-lifecycle-manager/pkg/api/apis/operators/v1"
8+
"github.com/operator-framework/operator-lifecycle-manager/pkg/api/apis/operators/v1alpha1"
9+
)
10+
11+
// GroupName is the API group name.
312
const GroupName = "operators.coreos.com"
13+
14+
var (
15+
scheme = runtime.NewScheme()
16+
localSchemeBuilder = runtime.SchemeBuilder{
17+
v1alpha1.AddToScheme,
18+
v1.AddToScheme,
19+
}
20+
21+
// AddToScheme adds all types in the operators.coreos.com group to the given scheme.
22+
AddToScheme = localSchemeBuilder.AddToScheme
23+
)
24+
25+
func init() {
26+
utilruntime.Must(AddToScheme(scheme))
27+
}

pkg/api/apis/operators/v1/operatorgroup_types.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ const (
1313
OperatorGroupNamespaceAnnotationKey = "olm.operatorNamespace"
1414
OperatorGroupTargetsAnnotationKey = "olm.targetNamespaces"
1515
OperatorGroupProvidedAPIsAnnotationKey = "olm.providedAPIs"
16+
17+
OperatorGroupKind = "OperatorGroup"
1618
)
1719

1820
type OperatorGroupSpec struct {

pkg/api/apis/operators/v1/register.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ import (
66
"k8s.io/apimachinery/pkg/runtime"
77
"k8s.io/apimachinery/pkg/runtime/schema"
88
k8sscheme "k8s.io/client-go/kubernetes/scheme"
9-
10-
"github.com/operator-framework/operator-lifecycle-manager/pkg/api/apis/operators"
119
)
1210

1311
const (
@@ -16,7 +14,7 @@ const (
1614
)
1715

1816
// SchemeGroupVersion is group version used to register these objects
19-
var SchemeGroupVersion = schema.GroupVersion{Group: operators.GroupName, Version: GroupVersion}
17+
var SchemeGroupVersion = schema.GroupVersion{Group: GroupName, Version: GroupVersion}
2018

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

pkg/api/apis/operators/v1alpha1/catalogsource_types.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,10 @@ import (
55

66
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
77
"k8s.io/apimachinery/pkg/types"
8-
9-
"github.com/operator-framework/operator-lifecycle-manager/pkg/api/apis/operators"
108
)
119

1210
const (
13-
CatalogSourceCRDAPIVersion = operators.GroupName + "/" + GroupVersion
11+
CatalogSourceCRDAPIVersion = GroupName + "/" + GroupVersion
1412
CatalogSourceKind = "CatalogSource"
1513
)
1614

pkg/api/apis/operators/v1alpha1/clusterserviceversion_types.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,10 @@ import (
99

1010
"github.com/coreos/go-semver/semver"
1111
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
12-
13-
"github.com/operator-framework/operator-lifecycle-manager/pkg/api/apis/operators"
1412
)
1513

1614
const (
17-
ClusterServiceVersionAPIVersion = operators.GroupName + "/" + GroupVersion
15+
ClusterServiceVersionAPIVersion = GroupName + "/" + GroupVersion
1816
ClusterServiceVersionKind = "ClusterServiceVersion"
1917
OperatorGroupNamespaceAnnotationKey = "olm.operatorNamespace"
2018
)

pkg/api/apis/operators/v1alpha1/installplan_types.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,11 @@ import (
66

77
corev1 "k8s.io/api/core/v1"
88
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
9-
10-
"github.com/operator-framework/operator-lifecycle-manager/pkg/api/apis/operators"
119
)
1210

1311
const (
1412
InstallPlanKind = "InstallPlan"
15-
InstallPlanAPIVersion = operators.GroupName + "/" + GroupVersion
13+
InstallPlanAPIVersion = GroupName + "/" + GroupVersion
1614
)
1715

1816
// Approval is the user approval policy for an InstallPlan.

pkg/api/apis/operators/v1alpha1/register.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package v1alpha1
22

33
import (
4-
"github.com/operator-framework/operator-lifecycle-manager/pkg/api/apis/operators"
54
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
65
"k8s.io/apimachinery/pkg/runtime"
76
"k8s.io/apimachinery/pkg/runtime/schema"
@@ -13,7 +12,7 @@ const (
1312
)
1413

1514
// SchemeGroupVersion is group version used to register these objects
16-
var SchemeGroupVersion = schema.GroupVersion{Group: operators.GroupName, Version: GroupVersion}
15+
var SchemeGroupVersion = schema.GroupVersion{Group: GroupName, Version: GroupVersion}
1716

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

0 commit comments

Comments
 (0)