From 0ed49a177e27e0a073e9ada5161281c9f9de46b7 Mon Sep 17 00:00:00 2001 From: Logan McNaughton <848146+loganmc10@users.noreply.github.com> Date: Wed, 28 Jun 2023 09:00:07 -0700 Subject: [PATCH] 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 --- config/rbac/role.yaml | 10 ++++++++++ internal/catalog/reconcile.go | 31 ++++++++++++++++++++++++++++--- 2 files changed, 38 insertions(+), 3 deletions(-) diff --git a/config/rbac/role.yaml b/config/rbac/role.yaml index 7721923..898f37c 100644 --- a/config/rbac/role.yaml +++ b/config/rbac/role.yaml @@ -15,6 +15,16 @@ rules: - list - update - watch +- apiGroups: + - "" + resources: + - namespaces + verbs: + - create + - get + - list + - patch + - watch - apiGroups: - "" resources: diff --git a/internal/catalog/reconcile.go b/internal/catalog/reconcile.go index efcb00c..78ca6e0 100644 --- a/internal/catalog/reconcile.go +++ b/internal/catalog/reconcile.go @@ -6,6 +6,7 @@ import ( rhsysenggithubiov1beta1 "github.com/RHsyseng/cluster-relocation-operator/api/v1beta1" "github.com/go-logr/logr" operatorhubv1alpha1 "github.com/operator-framework/api/pkg/operators/v1alpha1" + corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime" "sigs.k8s.io/controller-runtime/pkg/client" @@ -13,16 +14,40 @@ import ( ) //+kubebuilder:rbac:groups=operators.coreos.com,resources=catalogsources,verbs=create;update;get;list;delete;watch +//+kubebuilder:rbac:groups="",resources=namespaces,verbs=create;patch;get;list;watch -const marketplaceNamespace = "openshift-marketplace" +const marketplaceNamespaceName = "openshift-marketplace" func Reconcile(ctx context.Context, c client.Client, scheme *runtime.Scheme, relocation *rhsysenggithubiov1beta1.ClusterRelocation, logger logr.Logger) error { if err := Cleanup(ctx, c, relocation, logger); err != nil { return err } + if relocation.Spec.CatalogSources != nil { + marketplaceNamespace := &corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: marketplaceNamespaceName}} + op, err := controllerutil.CreateOrPatch(ctx, c, marketplaceNamespace, func() error { + if marketplaceNamespace.Annotations == nil { + marketplaceNamespace.Annotations = map[string]string{"workload.openshift.io/allowed": "management"} + } else { + marketplaceNamespace.Annotations["workload.openshift.io/allowed"] = "management" + } + if marketplaceNamespace.Labels == nil { + marketplaceNamespace.Labels = map[string]string{"openshift.io/cluster-monitoring": "true"} + } else { + marketplaceNamespace.Labels["openshift.io/cluster-monitoring"] = "true" + } + return nil + }) + if err != nil { + return err + } + if op != controllerutil.OperationResultNone { + logger.Info("Created Marketplace namespace", "Namespace", marketplaceNamespaceName, "OperationResult", op) + } + } + for _, v := range relocation.Spec.CatalogSources { - catalogSource := &operatorhubv1alpha1.CatalogSource{ObjectMeta: metav1.ObjectMeta{Name: v.Name, Namespace: marketplaceNamespace}} + catalogSource := &operatorhubv1alpha1.CatalogSource{ObjectMeta: metav1.ObjectMeta{Name: v.Name, Namespace: marketplaceNamespaceName}} op, err := controllerutil.CreateOrUpdate(ctx, c, catalogSource, func() error { catalogSource.Spec.Image = v.Image catalogSource.Spec.SourceType = operatorhubv1alpha1.SourceTypeGrpc @@ -42,7 +67,7 @@ func Reconcile(ctx context.Context, c client.Client, scheme *runtime.Scheme, rel func Cleanup(ctx context.Context, c client.Client, relocation *rhsysenggithubiov1beta1.ClusterRelocation, logger logr.Logger) error { // if they remove something from relocation.Spec.CatalogSources, we need to clean it up catalogSources := &operatorhubv1alpha1.CatalogSourceList{} - if err := c.List(ctx, catalogSources, client.InNamespace(marketplaceNamespace)); err != nil { + if err := c.List(ctx, catalogSources, client.InNamespace(marketplaceNamespaceName)); err != nil { return err } for _, v := range catalogSources.Items { // loop through all existing CatalogSources