Skip to content

Commit

Permalink
Merge pull request #133 from leelavg/scl-clusterscope
Browse files Browse the repository at this point in the history
make storageclient api to clusterscope resource
  • Loading branch information
openshift-merge-bot[bot] authored Apr 17, 2024
2 parents e1db953 + c893ec3 commit 05917a6
Show file tree
Hide file tree
Showing 12 changed files with 228 additions and 364 deletions.
10 changes: 6 additions & 4 deletions api/v1alpha1/storageclaim_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ type StorageClaimStatus struct {
Phase storageClaimState `json:"phase,omitempty"`
}

// TODO: remove after storageclassclaim migration controller and storageclassclaim is deleted

type StorageClientNamespacedName struct {
Name string `json:"name"`
Namespace string `json:"namespace"`
Expand All @@ -52,10 +54,10 @@ type StorageClientNamespacedName struct {
// StorageClaimSpec defines the desired state of StorageClaim
type StorageClaimSpec struct {
//+kubebuilder:validation:Enum=block;sharedfile
Type string `json:"type"`
EncryptionMethod string `json:"encryptionMethod,omitempty"`
StorageProfile string `json:"storageProfile,omitempty"`
StorageClient *StorageClientNamespacedName `json:"storageClient"`
Type string `json:"type"`
EncryptionMethod string `json:"encryptionMethod,omitempty"`
StorageProfile string `json:"storageProfile,omitempty"`
StorageClient string `json:"storageClient"`
}

//+kubebuilder:object:root=true
Expand Down
1 change: 1 addition & 0 deletions api/v1alpha1/storageclient_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ type StorageClientStatus struct {

//+kubebuilder:object:root=true
//+kubebuilder:subresource:status
//+kubebuilder:resource:scope=Cluster
//+kubebuilder:printcolumn:name="Phase",type="string",JSONPath=".status.phase"
//+kubebuilder:printcolumn:name="consumer",type="string",JSONPath=".status.id"

Expand Down
7 changes: 1 addition & 6 deletions api/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 1 addition & 9 deletions bundle/manifests/ocs.openshift.io_storageclaims.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,7 @@ spec:
encryptionMethod:
type: string
storageClient:
properties:
name:
type: string
namespace:
type: string
required:
- name
- namespace
type: object
type: string
storageProfile:
type: string
type:
Expand Down
2 changes: 1 addition & 1 deletion bundle/manifests/ocs.openshift.io_storageclients.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ spec:
listKind: StorageClientList
plural: storageclients
singular: storageclient
scope: Namespaced
scope: Cluster
versions:
- additionalPrinterColumns:
- jsonPath: .status.phase
Expand Down
10 changes: 1 addition & 9 deletions config/crd/bases/ocs.openshift.io_storageclaims.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,7 @@ spec:
encryptionMethod:
type: string
storageClient:
properties:
name:
type: string
namespace:
type: string
required:
- name
- namespace
type: object
type: string
storageProfile:
type: string
type:
Expand Down
2 changes: 1 addition & 1 deletion config/crd/bases/ocs.openshift.io_storageclients.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ spec:
listKind: StorageClientList
plural: storageclients
singular: storageclient
scope: Namespaced
scope: Cluster
versions:
- additionalPrinterColumns:
- jsonPath: .status.phase
Expand Down
33 changes: 10 additions & 23 deletions controllers/storageclaim_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ func (r *StorageClaimReconciler) Reconcile(ctx context.Context, req ctrl.Request
r.storageClaimHash = getMD5Hash(r.storageClaim.Name)
r.storageClaim.Status.Phase = v1alpha1.StorageClaimInitializing

if r.storageClaim.Spec.StorageClient == nil {
if r.storageClaim.Spec.StorageClient == "" {
storageClientList := &v1alpha1.StorageClientList{}
if err := r.list(storageClientList); err != nil {
return reconcile.Result{}, err
Expand All @@ -170,8 +170,7 @@ func (r *StorageClaimReconciler) Reconcile(ctx context.Context, req ctrl.Request
} else {
// Fetch the StorageClient instance
r.storageClient = &v1alpha1.StorageClient{}
r.storageClient.Name = r.storageClaim.Spec.StorageClient.Name
r.storageClient.Namespace = r.storageClaim.Spec.StorageClient.Namespace
r.storageClient.Name = r.storageClaim.Spec.StorageClient
if err := r.get(r.storageClient); err != nil {
r.log.Error(err, "Failed to get StorageClient.")
return reconcile.Result{}, err
Expand Down Expand Up @@ -265,18 +264,8 @@ func (r *StorageClaimReconciler) reconcilePhases() (reconcile.Result, error) {
// Configuration phase.
r.storageClaim.Status.Phase = v1alpha1.StorageClaimConfiguring

updateStorageClaim := false
// Check if finalizers are present, if not, add them.
if !contains(r.storageClaim.GetFinalizers(), storageClaimFinalizer) {
r.log.Info("Finalizer not found for StorageClaim. Adding finalizer.", "StorageClaim", r.storageClaim.Name)
r.storageClaim.SetFinalizers(append(r.storageClaim.GetFinalizers(), storageClaimFinalizer))
updateStorageClaim = true
}
if utils.AddAnnotation(r.storageClaim, storageClientAnnotationKey, client.ObjectKeyFromObject(r.storageClient).String()) {
updateStorageClaim = true
}

if updateStorageClaim {
if controllerutil.AddFinalizer(r.storageClaim, storageClaimFinalizer) {
if err := r.update(r.storageClaim); err != nil {
return reconcile.Result{}, fmt.Errorf("failed to update StorageClaim %q: %v", r.storageClaim.Name, err)
}
Expand Down Expand Up @@ -347,7 +336,7 @@ func (r *StorageClaimReconciler) reconcilePhases() (reconcile.Result, error) {
case "Secret":
secret := &corev1.Secret{}
secret.Name = resource.Name
secret.Namespace = r.storageClient.Namespace
secret.Namespace = r.OperatorNamespace
_, err = controllerutil.CreateOrUpdate(r.ctx, r.Client, secret, func() error {
// cluster scoped resource owning namespace scoped resource which allows garbage collection
if err := r.own(secret); err != nil {
Expand Down Expand Up @@ -382,9 +371,9 @@ func (r *StorageClaimReconciler) reconcilePhases() (reconcile.Result, error) {
// same name.
csiClusterConfigEntry.ClusterID = r.storageClaimHash
var storageClass *storagev1.StorageClass
data["csi.storage.k8s.io/provisioner-secret-namespace"] = r.storageClient.Namespace
data["csi.storage.k8s.io/node-stage-secret-namespace"] = r.storageClient.Namespace
data["csi.storage.k8s.io/controller-expand-secret-namespace"] = r.storageClient.Namespace
data["csi.storage.k8s.io/provisioner-secret-namespace"] = r.OperatorNamespace
data["csi.storage.k8s.io/node-stage-secret-namespace"] = r.OperatorNamespace
data["csi.storage.k8s.io/controller-expand-secret-namespace"] = r.OperatorNamespace
data["clusterID"] = r.storageClaimHash

if resource.Name == "cephfs" {
Expand All @@ -403,7 +392,7 @@ func (r *StorageClaimReconciler) reconcilePhases() (reconcile.Result, error) {
}
case "VolumeSnapshotClass":
var volumeSnapshotClass *snapapi.VolumeSnapshotClass
data["csi.storage.k8s.io/snapshotter-secret-namespace"] = r.storageClient.Namespace
data["csi.storage.k8s.io/snapshotter-secret-namespace"] = r.OperatorNamespace
// generate a new clusterID for cephfs subvolumegroup, as
// storageclaim is clusterscoped resources using its
// hash as the clusterID
Expand Down Expand Up @@ -465,8 +454,7 @@ func (r *StorageClaimReconciler) reconcilePhases() (reconcile.Result, error) {
return reconcile.Result{}, fmt.Errorf("failed to revoke StorageClassClaim: %s", err)
}

if contains(r.storageClaim.GetFinalizers(), storageClaimFinalizer) {
r.storageClaim.Finalizers = remove(r.storageClaim.Finalizers, storageClaimFinalizer)
if controllerutil.RemoveFinalizer(r.storageClaim, storageClaimFinalizer) {
if err := r.update(r.storageClaim); err != nil {
return ctrl.Result{}, fmt.Errorf("failed to remove finalizer from storageClaim: %s", err)
}
Expand Down Expand Up @@ -595,8 +583,7 @@ func (r *StorageClaimReconciler) delete(obj client.Object) error {
}

func (r *StorageClaimReconciler) own(resource metav1.Object) error {
// Ensure StorageClaim ownership on a resource
return controllerutil.SetOwnerReference(r.storageClaim, resource, r.Scheme)
return controllerutil.SetControllerReference(r.storageClaim, resource, r.Scheme)
}

func (r *StorageClaimReconciler) createOrReplaceVolumeSnapshotClass(volumeSnapshotClass *snapapi.VolumeSnapshotClass) error {
Expand Down
4 changes: 3 additions & 1 deletion controllers/storageclassclaim_migration_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,9 @@ func (r *StorageClassClaimMigrationReconciler) Reconcile(ctx context.Context, re

storageClaim.Spec.EncryptionMethod = storageClassClaim.Spec.EncryptionMethod
storageClaim.Spec.StorageProfile = storageClassClaim.Spec.StorageProfile
storageClaim.Spec.StorageClient = storageClassClaim.Spec.StorageClient.DeepCopy()
if storageClassClaim.Spec.StorageClient != nil {
storageClaim.Spec.StorageClient = storageClassClaim.Spec.StorageClient.Name
}

r.log.Info(fmt.Sprintf("Migrating storageclassclaim %q", storageClassClaim.Name))
if err := r.create(storageClaim); err != nil && !kerrors.IsAlreadyExists(err) {
Expand Down
Loading

0 comments on commit 05917a6

Please sign in to comment.