Skip to content

Commit

Permalink
Create the openshift-marketplace namespace if it does not exist (#46)
Browse files Browse the repository at this point in the history
* Create the openshift-marketplace namespace if it does not exist

* only create namespace if catalogSources are listed

* fix crash

---------

Co-authored-by: Daniel Chavero <[email protected]>
  • Loading branch information
loganmc10 and danielchg authored Jun 28, 2023
1 parent 62cfc79 commit 0ed49a1
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
10 changes: 10 additions & 0 deletions config/rbac/role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@ rules:
- list
- update
- watch
- apiGroups:
- ""
resources:
- namespaces
verbs:
- create
- get
- list
- patch
- watch
- apiGroups:
- ""
resources:
Expand Down
31 changes: 28 additions & 3 deletions internal/catalog/reconcile.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,48 @@ 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"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
)

//+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
Expand All @@ -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
Expand Down

0 comments on commit 0ed49a1

Please sign in to comment.