Skip to content

Commit 0ed49a1

Browse files
loganmc10danielchg
andauthored
Create the openshift-marketplace namespace if it does not exist (#46)
* Create the openshift-marketplace namespace if it does not exist * only create namespace if catalogSources are listed * fix crash --------- Co-authored-by: Daniel Chavero <dchavero@redhat.com>
1 parent 62cfc79 commit 0ed49a1

File tree

2 files changed

+38
-3
lines changed

2 files changed

+38
-3
lines changed

config/rbac/role.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,16 @@ rules:
1515
- list
1616
- update
1717
- watch
18+
- apiGroups:
19+
- ""
20+
resources:
21+
- namespaces
22+
verbs:
23+
- create
24+
- get
25+
- list
26+
- patch
27+
- watch
1828
- apiGroups:
1929
- ""
2030
resources:

internal/catalog/reconcile.go

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,48 @@ import (
66
rhsysenggithubiov1beta1 "github.com/RHsyseng/cluster-relocation-operator/api/v1beta1"
77
"github.com/go-logr/logr"
88
operatorhubv1alpha1 "github.com/operator-framework/api/pkg/operators/v1alpha1"
9+
corev1 "k8s.io/api/core/v1"
910
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1011
"k8s.io/apimachinery/pkg/runtime"
1112
"sigs.k8s.io/controller-runtime/pkg/client"
1213
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
1314
)
1415

1516
//+kubebuilder:rbac:groups=operators.coreos.com,resources=catalogsources,verbs=create;update;get;list;delete;watch
17+
//+kubebuilder:rbac:groups="",resources=namespaces,verbs=create;patch;get;list;watch
1618

17-
const marketplaceNamespace = "openshift-marketplace"
19+
const marketplaceNamespaceName = "openshift-marketplace"
1820

1921
func Reconcile(ctx context.Context, c client.Client, scheme *runtime.Scheme, relocation *rhsysenggithubiov1beta1.ClusterRelocation, logger logr.Logger) error {
2022
if err := Cleanup(ctx, c, relocation, logger); err != nil {
2123
return err
2224
}
2325

26+
if relocation.Spec.CatalogSources != nil {
27+
marketplaceNamespace := &corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: marketplaceNamespaceName}}
28+
op, err := controllerutil.CreateOrPatch(ctx, c, marketplaceNamespace, func() error {
29+
if marketplaceNamespace.Annotations == nil {
30+
marketplaceNamespace.Annotations = map[string]string{"workload.openshift.io/allowed": "management"}
31+
} else {
32+
marketplaceNamespace.Annotations["workload.openshift.io/allowed"] = "management"
33+
}
34+
if marketplaceNamespace.Labels == nil {
35+
marketplaceNamespace.Labels = map[string]string{"openshift.io/cluster-monitoring": "true"}
36+
} else {
37+
marketplaceNamespace.Labels["openshift.io/cluster-monitoring"] = "true"
38+
}
39+
return nil
40+
})
41+
if err != nil {
42+
return err
43+
}
44+
if op != controllerutil.OperationResultNone {
45+
logger.Info("Created Marketplace namespace", "Namespace", marketplaceNamespaceName, "OperationResult", op)
46+
}
47+
}
48+
2449
for _, v := range relocation.Spec.CatalogSources {
25-
catalogSource := &operatorhubv1alpha1.CatalogSource{ObjectMeta: metav1.ObjectMeta{Name: v.Name, Namespace: marketplaceNamespace}}
50+
catalogSource := &operatorhubv1alpha1.CatalogSource{ObjectMeta: metav1.ObjectMeta{Name: v.Name, Namespace: marketplaceNamespaceName}}
2651
op, err := controllerutil.CreateOrUpdate(ctx, c, catalogSource, func() error {
2752
catalogSource.Spec.Image = v.Image
2853
catalogSource.Spec.SourceType = operatorhubv1alpha1.SourceTypeGrpc
@@ -42,7 +67,7 @@ func Reconcile(ctx context.Context, c client.Client, scheme *runtime.Scheme, rel
4267
func Cleanup(ctx context.Context, c client.Client, relocation *rhsysenggithubiov1beta1.ClusterRelocation, logger logr.Logger) error {
4368
// if they remove something from relocation.Spec.CatalogSources, we need to clean it up
4469
catalogSources := &operatorhubv1alpha1.CatalogSourceList{}
45-
if err := c.List(ctx, catalogSources, client.InNamespace(marketplaceNamespace)); err != nil {
70+
if err := c.List(ctx, catalogSources, client.InNamespace(marketplaceNamespaceName)); err != nil {
4671
return err
4772
}
4873
for _, v := range catalogSources.Items { // loop through all existing CatalogSources

0 commit comments

Comments
 (0)