Skip to content

Commit

Permalink
generated changes
Browse files Browse the repository at this point in the history
Signed-off-by: Kaustav Majumder <[email protected]>
  • Loading branch information
Kaustav Majumder committed Sep 17, 2024
1 parent 920efc2 commit 3377cfa
Show file tree
Hide file tree
Showing 10 changed files with 170 additions and 149 deletions.
1 change: 1 addition & 0 deletions config/rbac/role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ rules:
- apiGroups:
- noobaa.io
resources:
- noobaaaccounts
- noobaas
verbs:
- create
Expand Down
53 changes: 52 additions & 1 deletion controllers/storageconsumer/consumer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ package controllers
import (
"testing"

noobaaApis "github.com/noobaa/noobaa-operator/v5/pkg/apis"
"github.com/noobaa/noobaa-operator/v5/pkg/apis/noobaa/v1alpha1"
routev1 "github.com/openshift/api/route/v1"
v1 "github.com/red-hat-storage/ocs-operator/api/v4/v1"
ocsv1alpha1 "github.com/red-hat-storage/ocs-operator/api/v4/v1alpha1"
"github.com/red-hat-storage/ocs-operator/v4/controllers/util"
Expand Down Expand Up @@ -46,7 +49,15 @@ func createFakeScheme(t *testing.T) *runtime.Scheme {

err = rookCephv1.AddToScheme(scheme)
if err != nil {
assert.Fail(t, "failed to add rookCephv1scheme")
assert.Fail(t, "failed to add rookCephv1 scheme")
}
err = routev1.AddToScheme(scheme)
if err != nil {
assert.Fail(t, "failed to add routev1 scheme")
}
err = noobaaApis.AddToScheme(scheme)
if err != nil {
assert.Fail(t, "failed to add nbapis scheme")
}

return scheme
Expand Down Expand Up @@ -90,7 +101,23 @@ func TestCephName(t *testing.T) {
Name: "cephfs",
Phase: "Ready",
},
{
Kind: "NooBaaAccount",
Name: "consumer-acc",
Phase: "Ready",
},
},
Client: ocsv1alpha1.ClientStatus{
ClusterID: "consumer",
},
},
}
r.noobaaAccount = &v1alpha1.NooBaaAccount{
ObjectMeta: metav1.ObjectMeta{
Name: "consumer-acc",
},
Status: v1alpha1.NooBaaAccountStatus{
Phase: v1alpha1.NooBaaAccountPhaseReady,
},
}
_, err := r.reconcilePhases()
Expand All @@ -102,6 +129,11 @@ func TestCephName(t *testing.T) {
Name: "healthchecker",
Phase: "Ready",
},
{
Kind: "NooBaaAccount",
Name: "consumer-acc",
Phase: "Ready",
},
}
assert.Equal(t, r.storageConsumer.Status.CephResources, want)

Expand Down Expand Up @@ -138,9 +170,23 @@ func TestCephName(t *testing.T) {
Name: "healthchecker",
Phase: "Error",
},
{
Kind: "NooBaaAccount",
Name: "consumer-acc",
Phase: "Error",
},
},
},
}
r.noobaaAccount = &v1alpha1.NooBaaAccount{
ObjectMeta: metav1.ObjectMeta{
Name: "consumer-acc",
},
Status: v1alpha1.NooBaaAccountStatus{
Phase: v1alpha1.NooBaaAccountPhaseRejected,
},
}

_, err = r.reconcilePhases()
assert.NoError(t, err)

Expand All @@ -150,6 +196,11 @@ func TestCephName(t *testing.T) {
Name: "healthchecker",
Phase: "Error",
},
{
Kind: "NooBaaAccount",
Name: "consumer-acc",
Phase: "Rejected",
},
}
assert.Equal(t, r.storageConsumer.Status.CephResources, want)
}
32 changes: 32 additions & 0 deletions controllers/storageconsumer/storageconsumer_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@ import (
"crypto/md5"
"encoding/hex"
"encoding/json"
"fmt"

"github.com/go-logr/logr"
"github.com/red-hat-storage/ocs-operator/api/v4/v1alpha1"
"github.com/red-hat-storage/ocs-operator/v4/controllers/util"
rookCephv1 "github.com/rook/rook/pkg/apis/ceph.rook.io/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand All @@ -36,6 +38,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/predicate"

nbv1 "github.com/noobaa/noobaa-operator/v5/pkg/apis/noobaa/v1alpha1"
ocsv1alpha1 "github.com/red-hat-storage/ocs-operator/api/v4/v1alpha1"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
)
Expand All @@ -59,12 +62,14 @@ type StorageConsumerReconciler struct {
storageConsumer *ocsv1alpha1.StorageConsumer
cephClientHealthChecker *rookCephv1.CephClient
namespace string
noobaaAccount *nbv1.NooBaaAccount
}

//+kubebuilder:rbac:groups=ocs.openshift.io,resources=storageconsumers,verbs=get;list;watch;create;update;patch;delete
//+kubebuilder:rbac:groups=ceph.rook.io,resources=cephclients,verbs=get;list;watch;create;update;delete
//+kubebuilder:rbac:groups=ocs.openshift.io,resources=storageconsumers/status,verbs=get;update;patch
// +kubebuilder:rbac:groups=ocs.openshift.io,resources=storagerequests,verbs=get;list;
// +kubebuilder:rbac:groups=noobaa.io,resources=noobaaaccounts,verbs=get;list;watch;create;update;delete

// Reconcile reads that state of the cluster for a StorageConsumer object and makes changes based on the state read
// and what is in the StorageConsumer.Spec
Expand Down Expand Up @@ -127,6 +132,10 @@ func (r *StorageConsumerReconciler) initReconciler(request reconcile.Request) {
r.cephClientHealthChecker = &rookCephv1.CephClient{}
r.cephClientHealthChecker.Name = GenerateHashForCephClient(r.storageConsumer.Name, "global")
r.cephClientHealthChecker.Namespace = r.namespace

r.noobaaAccount = &nbv1.NooBaaAccount{}
r.noobaaAccount.Name = "noobaa-remote-" + r.storageConsumer.Name
r.noobaaAccount.Namespace = r.storageConsumer.Namespace
}

func (r *StorageConsumerReconciler) reconcilePhases() (reconcile.Result, error) {
Expand All @@ -145,6 +154,10 @@ func (r *StorageConsumerReconciler) reconcilePhases() (reconcile.Result, error)
return reconcile.Result{}, err
}

if err := r.reconcileNoobaaAccount(); err != nil {
return reconcile.Result{}, err
}

cephResourcesReady := true
for _, cephResource := range r.storageConsumer.Status.CephResources {
if cephResource.Phase != "Ready" {
Expand Down Expand Up @@ -205,6 +218,25 @@ func (r *StorageConsumerReconciler) reconcileCephClientHealthChecker() error {
return nil
}

func (r *StorageConsumerReconciler) reconcileNoobaaAccount() error {
_, err := ctrl.CreateOrUpdate(r.ctx, r.Client, r.noobaaAccount, func() error {
if err := r.own(r.noobaaAccount); err != nil {
return err
}
// the following annotation will enable noobaa-operator to create a auth_token secret based on this account
util.AddAnnotation(r.noobaaAccount, "remote-operator", "true")
return nil
})
if err != nil {
return fmt.Errorf("failed to create noobaa account for storageConsumer %v: %v", r.storageConsumer.Name, err)
}

phase := string(r.noobaaAccount.Status.Phase)
r.setCephResourceStatus(r.noobaaAccount.Name, "NooBaaAccount", phase, nil)

return nil
}

func (r *StorageConsumerReconciler) setCephResourceStatus(name string, kind string, phase string, cephClients map[string]string) {
cephResourceSpec := ocsv1alpha1.CephResourcesSpec{
Name: name,
Expand Down
1 change: 1 addition & 0 deletions deploy/csv-templates/ocs-operator.csv.yaml.in
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,7 @@ spec:
- apiGroups:
- noobaa.io
resources:
- noobaaaccounts
- noobaas
verbs:
- create
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,7 @@ spec:
- apiGroups:
- noobaa.io
resources:
- noobaaaccounts
- noobaas
verbs:
- create
Expand Down
7 changes: 7 additions & 0 deletions deploy/ocs-operator/manifests/provider-role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,10 @@ rules:
verbs:
- get
- list
- apiGroups:
- route.openshift.io
resources:
- routes
verbs:
- get
- list
9 changes: 0 additions & 9 deletions rbac/provider-role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,6 @@ rules:
verbs:
- get
- list
- apiGroups:
- noobaa.io
resources:
- noobaaaccounts
verbs:
- get
- list
- create
- delete
- apiGroups:
- route.openshift.io
resources:
Expand Down
47 changes: 0 additions & 47 deletions services/provider/server/consumer.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,9 @@ import (
"context"
"errors"
"fmt"
"strings"
"sync"

nbv1 "github.com/noobaa/noobaa-operator/v5/pkg/apis/noobaa/v1alpha1"
ocsv1alpha1 "github.com/red-hat-storage/ocs-operator/api/v4/v1alpha1"
"github.com/red-hat-storage/ocs-operator/v4/controllers/util"
ifaces "github.com/red-hat-storage/ocs-operator/v4/services/provider/interfaces"
kerrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -229,47 +226,3 @@ func (c *ocsConsumerManager) UpdateConsumerStatus(ctx context.Context, id string
klog.Infof("successfully updated Status for StorageConsumer %v", consumerObj.Name)
return nil
}

func (c *ocsConsumerManager) CreateNoobaaAccount(ctx context.Context, id string) error {

consumerObj, err := c.Get(ctx, id)
if err != nil {
return err
}
consumerClusterID := strings.TrimPrefix(consumerObj.Name, "storageconsumer-")
if consumerClusterID != "" && len(consumerClusterID) == 0 {
return fmt.Errorf("failed to get clusterID from consumerResource Name: %s %v", consumerObj.Name, err)
}

noobaaAccountName := fmt.Sprintf("noobaa-remote-%s", consumerClusterID)
nbAccountObj := &nbv1.NooBaaAccount{}
nbAccountObj.Name = noobaaAccountName
nbAccountObj.Namespace = consumerObj.Namespace
// the following annotation will enable noobaa-operator to create a auth_token secret based on this account
util.AddAnnotation(nbAccountObj, "remote-operator", "true")

err = c.client.Create(ctx, nbAccountObj)
if err != nil {
return fmt.Errorf("failed to create noobaa account for storageConsumer %v: %v", consumerObj.Name, err)
}
return nil
}

func (c *ocsConsumerManager) DeleteNoobaaAccount(ctx context.Context, id string) error {
consumerObj, err := c.Get(ctx, id)
if err != nil {
return err
}
clusterID := strings.TrimPrefix(consumerObj.Name, "storageconsumer-")
if clusterID != "" && len(clusterID) == 0 {
return fmt.Errorf("failed to get clusterID from consumerResource Name: %s %v", consumerObj.Name, err)
}
noobaaAccountName := fmt.Sprintf("noobaa-remote-%s", clusterID)
nbAccountObj := &nbv1.NooBaaAccount{}
nbAccountObj.Name = noobaaAccountName
nbAccountObj.Namespace = consumerObj.Namespace
if err := c.client.Delete(ctx, nbAccountObj); err != nil {
return fmt.Errorf("failed to delete Noobaa account %q. %v", nbAccountObj.Name, err)
}
return nil
}
Loading

0 comments on commit 3377cfa

Please sign in to comment.