Skip to content

Commit

Permalink
api: Fetch Noobaa remote info and create the join secret
Browse files Browse the repository at this point in the history
Signed-off-by: Kaustav Majumder <[email protected]>
  • Loading branch information
Kaustav Majumder committed Jul 22, 2024
1 parent ca0c95c commit 21a21ab
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 1 deletion.
11 changes: 11 additions & 0 deletions config/rbac/role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,17 @@ rules:
- list
- update
- watch
- apiGroups:
- noobaa.io
resources:
- noobaas
verbs:
- create
- delete
- get
- list
- update
- watch
- apiGroups:
- ocs.openshift.io
resources:
Expand Down
75 changes: 74 additions & 1 deletion controllers/storageclient_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"github.com/red-hat-storage/ocs-client-operator/api/v1alpha1"
"github.com/red-hat-storage/ocs-client-operator/pkg/utils"

nbv1 "github.com/noobaa/noobaa-operator/v5/pkg/apis/noobaa/v1alpha1"
configv1 "github.com/openshift/api/config/v1"
opv1a1 "github.com/operator-framework/api/pkg/operators/v1alpha1"
providerClient "github.com/red-hat-storage/ocs-operator/v4/services/provider/client"
Expand Down Expand Up @@ -64,6 +65,8 @@ const (
ownerIndexName = "index:ownerUID"

csvPrefix = "ocs-client-operator"

joinSecretName = "noobaa-remote-join-secret"
)

// StorageClientReconciler reconciles a StorageClient object
Expand Down Expand Up @@ -98,6 +101,8 @@ func (r *StorageClientReconciler) SetupWithManager(mgr ctrl.Manager) error {
Owns(&v1alpha1.StorageClaim{}).
Owns(&batchv1.CronJob{}).
Owns(&quotav1.ClusterResourceQuota{}, builder.WithPredicates(predicate.GenerationChangedPredicate{})).
Owns(&nbv1.NooBaa{}, builder.WithPredicates(predicate.GenerationChangedPredicate{})).
Owns(&corev1.Secret{}).
Complete(r)
}

Expand All @@ -108,6 +113,8 @@ func (r *StorageClientReconciler) SetupWithManager(mgr ctrl.Manager) error {
//+kubebuilder:rbac:groups=config.openshift.io,resources=clusterversions,verbs=get;list;watch
//+kubebuilder:rbac:groups=batch,resources=cronjobs,verbs=get;list;create;update;watch;delete
//+kubebuilder:rbac:groups=operators.coreos.com,resources=clusterserviceversions,verbs=get;list;watch
//+kubebuilder:rbac:groups=noobaa.io,resources=noobaas,verbs=get;list;watch;create;update;delete
//+kubebuilder:rbac:groups="",resources=secrets,verbs=get;list;watch;create;update;delete

func (r *StorageClientReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
var err error
Expand Down Expand Up @@ -210,9 +217,25 @@ func (r *StorageClientReconciler) reconcilePhases() (ctrl.Result, error) {
if err := r.reconcileClusterResourceQuota(clusterResourceQuotaSpec); err != nil {
return reconcile.Result{}, err
}
case "Secret":
if eResource.Name == fmt.Sprintf("noobaa-remote-join-secret-%s", r.storageClient.Status.ConsumerID) {
data := map[string]string{}
if err := json.Unmarshal(eResource.Data, &data); err != nil {
return reconcile.Result{}, fmt.Errorf("failed to unmarshall noobaa join secret data: %v", err)
}

if err := r.reconcileNoobaaJoinSecret(data); err != nil {
return reconcile.Result{}, err
}
}
}
}
// reconcile remote noobaa only if it is not a local client
if _, exists := r.storageClient.GetAnnotations()["is-local-client"]; !exists {
if err := r.reconcileRemoteNoobaa(); err != nil {
return reconcile.Result{}, err
}
}

if r.storageClient.GetAnnotations()[storageClaimProcessedAnnotationKey] != "true" {
if err := r.reconcileBlockStorageClaim(); err != nil {
return reconcile.Result{}, err
Expand Down Expand Up @@ -549,6 +572,56 @@ func (r *StorageClientReconciler) reconcileSharedfileStorageClaim() error {
return nil
}

func (r *StorageClientReconciler) reconcileRemoteNoobaa() error {
// Create noobaa CR referencing the join secret
nb := &nbv1.NooBaa{}
nb.Name = "noobaa-remote"
nb.Namespace = r.OperatorNamespace
nb.Spec.JoinSecret = &corev1.SecretReference{
Name: joinSecretName,
Namespace: r.OperatorNamespace,
}
_, err := controllerutil.CreateOrUpdate(r.ctx, r.Client, nb, func() error {
utils.AddAnnotation(nb, "remote-client-noobaa", "true")
if err := r.own(nb); err != nil {
return err
}
return nil
})
if err != nil {
return fmt.Errorf("failed to create remote noobaa: %v", err)
}
return nil
}

func (r *StorageClientReconciler) reconcileNoobaaJoinSecret(noobaaData map[string]string) error {
noobaaAuthToken, ok := noobaaData["auth_token"]
if !ok {
return fmt.Errorf("noobaa auth token not found")
}
noobaaMgmtAddress, ok := noobaaData["mgmt_addr"]
if !ok {
return fmt.Errorf("noobaa mgmt address not found")
}
joinSecret := &corev1.Secret{}
joinSecret.Name = joinSecretName
joinSecret.Namespace = r.OperatorNamespace
joinSecret.Data = map[string][]byte{
"auth_token": []byte(noobaaAuthToken),
"mgmt_addr": []byte(noobaaMgmtAddress),
}
_, err := controllerutil.CreateOrUpdate(r.ctx, r.Client, joinSecret, func() error {
if err := r.own(joinSecret); err != nil {
return err
}
return nil
})
if err != nil {
return fmt.Errorf("failed to create or update noobaa-remote-join-secret %v", err)
}
return nil
}

func (r *StorageClientReconciler) get(obj client.Object, opts ...client.GetOption) error {
key := client.ObjectKeyFromObject(obj)
return r.Get(r.ctx, key, obj, opts...)
Expand Down
4 changes: 4 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
// Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.)
// to ensure that exec-entrypoint and run can make use of them.
snapapi "github.com/kubernetes-csi/external-snapshotter/client/v6/apis/volumesnapshot/v1"
nbapis "github.com/noobaa/noobaa-operator/v5/pkg/apis"
configv1 "github.com/openshift/api/config/v1"
consolev1 "github.com/openshift/api/console/v1"
quotav1 "github.com/openshift/api/quota/v1"
Expand All @@ -37,6 +38,7 @@ import (
monitoringv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1"
admrv1 "k8s.io/api/admissionregistration/v1"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
extv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/apimachinery/pkg/runtime"
Expand Down Expand Up @@ -71,6 +73,8 @@ func init() {
utilruntime.Must(opv1a1.AddToScheme(scheme))
utilruntime.Must(extv1.AddToScheme(scheme))
utilruntime.Must(quotav1.AddToScheme(scheme))
utilruntime.Must(nbapis.AddToScheme(scheme))
utilruntime.Must(corev1.AddToScheme(scheme))
//+kubebuilder:scaffold:scheme
}

Expand Down

0 comments on commit 21a21ab

Please sign in to comment.