-
Notifications
You must be signed in to change notification settings - Fork 184
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Expands OnboardCosumer and creates odf-info
Signed-off-by: raaizik <[email protected]>
- Loading branch information
Showing
8 changed files
with
491 additions
and
218 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
package storagecluster | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
operatorsv1alpha1 "github.com/operator-framework/api/pkg/operators/v1alpha1" | ||
ocsv1 "github.com/red-hat-storage/ocs-operator/api/v4/v1" | ||
corev1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/types" | ||
"k8s.io/klog/v2" | ||
"reflect" | ||
ctrl "sigs.k8s.io/controller-runtime" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
"sigs.k8s.io/controller-runtime/pkg/reconcile" | ||
"strings" | ||
) | ||
|
||
type ConnectedClient struct { | ||
Metadata types.NamespacedName `json:"metadata"` | ||
ClusterID string `json:"clusterId"` | ||
} | ||
type InfoStorageCluster struct { | ||
Metadata types.NamespacedName `json:"metadata"` | ||
StorageProviderEndpoint string `json:"storageProviderEndpoint"` | ||
CephClusterFSID string `json:"cephClusterFSID"` | ||
} | ||
|
||
type OdfInfoData struct { | ||
OdfVersion string `json:"odfVersion"` | ||
OdfDeploymentType string `json:"odfDeploymentType"` | ||
Clients []ConnectedClient `json:"clients"` | ||
StorageCluster InfoStorageCluster `json:"storageCluster"` | ||
} | ||
|
||
const ( | ||
RDROdfInfoKeyName = "config.yaml" | ||
RDROdfDeploymentTypeExternal = "external" | ||
RDROdfDeploymentTypeInternal = "internal" | ||
RookCephMonSecretName = "rook-ceph-mon" | ||
FsidKey = "fsid" | ||
OdfOperatorNamePrefix = "odf-operator" | ||
RDROdfInfoConfigMapName = "odf-info" | ||
RDROdfInfoMapKind = "ConfigMap" | ||
) | ||
|
||
type odfInfoConfig struct{} | ||
|
||
// ensureCreated ensures that a ConfigMap resource exists with its Spec in | ||
// the desired state. | ||
func (obj *odfInfoConfig) ensureCreated(r *StorageClusterReconciler, sc *ocsv1.StorageCluster) (reconcile.Result, error) { | ||
odfInfo, configErr := getOdfInfoConfig(r, sc) | ||
if configErr != nil { | ||
return reconcile.Result{}, fmt.Errorf("failed to get ODF info config data: %w", configErr) | ||
} | ||
odfInfoConfigOverrideData := map[string]string{ | ||
RDROdfInfoKeyName: odfInfo, | ||
} | ||
odfInfoConfigOverrideCM := &corev1.ConfigMap{ | ||
TypeMeta: metav1.TypeMeta{ | ||
Kind: RDROdfInfoMapKind, | ||
APIVersion: sc.APIVersion, | ||
}, | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: RDROdfInfoConfigMapName + "-" + sc.Name, | ||
Namespace: sc.Namespace, | ||
}, | ||
Data: odfInfoConfigOverrideData, | ||
} | ||
|
||
_, err := ctrl.CreateOrUpdate(context.Background(), r.Client, odfInfoConfigOverrideCM, func() error { | ||
if !reflect.DeepEqual(odfInfoConfigOverrideCM.Data, odfInfoConfigOverrideData) { | ||
r.Log.Info("updating ODF info config override configmap", RDROdfInfoMapKind, klog.KRef(sc.Namespace, RDROdfInfoConfigMapName)) | ||
odfInfoConfigOverrideCM.Data = odfInfoConfigOverrideData | ||
} | ||
return ctrl.SetControllerReference(sc, odfInfoConfigOverrideCM, r.Scheme) | ||
}) | ||
if err != nil { | ||
r.Log.Error(err, "failed to create or update ODF info config override", RDROdfInfoMapKind, klog.KRef(sc.Namespace, RDROdfInfoConfigMapName)) | ||
return reconcile.Result{}, fmt.Errorf("failed to create or update ODF info config override: %w", err) | ||
} | ||
return reconcile.Result{}, nil | ||
} | ||
|
||
// ensureDeleted is dummy func for the odfInfoConfig | ||
func (obj *odfInfoConfig) ensureDeleted(_ *StorageClusterReconciler, _ *ocsv1.StorageCluster) (reconcile.Result, error) { | ||
return reconcile.Result{}, nil | ||
} | ||
|
||
func getOdfInfoConfig(r *StorageClusterReconciler, sc *ocsv1.StorageCluster) (string, error) { | ||
var odfVersion, cephFSId string | ||
var err error | ||
if odfVersion, err = getOdfVersion(r, sc); err != nil { | ||
return odfVersion, err | ||
} | ||
if cephFSId, err = getCephFsid(r, sc); err != nil { | ||
return cephFSId, err | ||
} | ||
var odfDeploymentType string | ||
if sc.Spec.ExternalStorage.Enable { | ||
odfDeploymentType = RDROdfDeploymentTypeExternal | ||
} else { | ||
odfDeploymentType = RDROdfDeploymentTypeInternal | ||
} | ||
var data = OdfInfoData{ | ||
OdfVersion: odfVersion, | ||
OdfDeploymentType: odfDeploymentType, | ||
// Clients array is populated with the onboarding request's fields via server.go | ||
Clients: []ConnectedClient{}, | ||
StorageCluster: InfoStorageCluster{ | ||
Metadata: types.NamespacedName{Name: sc.Name, Namespace: sc.Namespace}, | ||
StorageProviderEndpoint: sc.Status.StorageProviderEndpoint, | ||
CephClusterFSID: cephFSId, | ||
}, | ||
} | ||
yamlData, err := json.Marshal(data) | ||
if err != nil { | ||
return "", err | ||
} | ||
return string(yamlData), nil | ||
|
||
} | ||
|
||
func getOdfVersion(r *StorageClusterReconciler, storageCluster *ocsv1.StorageCluster) (string, error) { | ||
var csvs operatorsv1alpha1.ClusterServiceVersionList | ||
err := r.Client.List(context.TODO(), &csvs, &client.ListOptions{Namespace: storageCluster.Namespace}) | ||
if err != nil { | ||
return "", err | ||
} | ||
for _, csv := range csvs.Items { | ||
if strings.HasPrefix(csv.Name, OdfOperatorNamePrefix) { | ||
return csv.Spec.Version.String(), nil | ||
} | ||
} | ||
|
||
return "", fmt.Errorf("failed to find csv with prefix %q", OdfOperatorNamePrefix) | ||
} | ||
|
||
func getCephFsid(r *StorageClusterReconciler, storageCluster *ocsv1.StorageCluster) (string, error) { | ||
var rookCephMonSecret corev1.Secret | ||
err := r.Client.Get(context.TODO(), types.NamespacedName{Name: RookCephMonSecretName, Namespace: storageCluster.Namespace}, &rookCephMonSecret) | ||
if err != nil { | ||
return "", err | ||
} | ||
if val, ok := rookCephMonSecret.Data[FsidKey]; ok { | ||
return string(val), nil | ||
} | ||
|
||
return "", fmt.Errorf("failed to fetch ceph fsid from %q secret", RookCephMonSecretName) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.