@@ -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
1921func 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
4267func 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