Skip to content

Commit

Permalink
move to filesystem_helpers.go
Browse files Browse the repository at this point in the history
  • Loading branch information
bdevcich committed Nov 27, 2024
1 parent 0cf76a2 commit 18233c0
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 87 deletions.
86 changes: 86 additions & 0 deletions internal/controller/filesystem_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,7 @@ func newLustreFileSystem(ctx context.Context, c client.Client, nnfNodeStorage *n
fs.CommandArgs.PreDeactivate = cmdLines.PreDeactivate
fs.TempDir = fmt.Sprintf("/mnt/temp/%s-%d", nnfNodeStorage.Name, index)

log.Info("BLAKE labels - before filesystem", "labels", nnfNodeStorage.Labels)
server, err := getServerForClientMountOrNnfNodeStorage(ctx, c, nnfNodeStorage.Labels)
if err != nil {
return nil, dwsv1alpha2.NewResourceError("could not retrieve corresponding NnfServer resource for this NnfNodeStorage").WithError(err).WithMajor()
Expand Down Expand Up @@ -553,3 +554,88 @@ func logicalVolumeName(ctx context.Context, c client.Client, nnfNodeStorage *nnf

return "lv", nil
}

// Retrieve the ClientMount's corresponding NnfServer resource. To do this, we first need to get the corresponding NnfStorage resource. That is done by
// looking at the owner of the ClientMount resource. It should be NnfStorage. Then, we inspect the NnfStorage resource's owner. In this case, there can
// be two different owners:
//
// 1. Workflow (non-persistent storage case)
// 2. PersistentStorageInstance (persistent storage case)
//
// Once we understand who owns the NnfStorage resource, we can then obtain the NnfServer resource through slightly different methods.
func getServerForClientMountOrNnfNodeStorage(ctx context.Context, c client.Client, labels map[string]string) (*dwsv1alpha2.Servers, error) {
storageKind := "NnfStorage"
persistentKind := "PersistentStorageInstance"
workflowKind := "Workflow"

// Get the owner and directive index from ClientMount's labels
ownerKind, ownerExists := labels[dwsv1alpha2.OwnerKindLabel]
ownerName, ownerNameExists := labels[dwsv1alpha2.OwnerNameLabel]
ownerNS, ownerNSExists := labels[dwsv1alpha2.OwnerNamespaceLabel]
_, idxExists := labels[nnfv1alpha4.DirectiveIndexLabel]

// log.FromContext(ctx).Info("BLAKE labels - after", "labels", labels)

// We should expect the owner to be NnfStorage and have the expected labels
if !ownerExists || !ownerNameExists || !ownerNSExists || !idxExists || ownerKind != storageKind {
return nil, dwsv1alpha2.NewResourceError(fmt.Sprintf("expected owner to be of kind NnfStorage and have the expected labels: %v", labels)).WithMajor()
}

// Retrieve the NnfStorage resource
storage := &nnfv1alpha4.NnfStorage{
ObjectMeta: metav1.ObjectMeta{
Name: ownerName,
Namespace: ownerNS,
},
}
if err := c.Get(ctx, client.ObjectKeyFromObject(storage), storage); err != nil {
return nil, dwsv1alpha2.NewResourceError("unable retrieve NnfStorage resource").WithError(err).WithMajor()
}

// Get the owner and directive index from NnfStorage's labels
ownerKind, ownerExists = storage.Labels[dwsv1alpha2.OwnerKindLabel]
ownerName, ownerNameExists = storage.Labels[dwsv1alpha2.OwnerNameLabel]
ownerNS, ownerNSExists = storage.Labels[dwsv1alpha2.OwnerNamespaceLabel]
idx, idxExists := storage.Labels[nnfv1alpha4.DirectiveIndexLabel]

// We should expect the owner of the NnfStorage to be Workflow or PersistentStorageInstance and
// have the expected labels
if !ownerExists || !ownerNameExists || !ownerNSExists || !idxExists || (ownerKind != workflowKind && ownerKind != persistentKind) {
return nil, dwsv1alpha2.NewResourceError("expected owner to be of kind Workflow or PersistentStorageInstance and have the expected labels").WithMajor()
}

// If the owner is a workflow, then we can use the workflow labels and directive index to get
// the Servers Resource.
var listOptions []client.ListOption
if ownerKind == workflowKind {
listOptions = []client.ListOption{
client.MatchingLabels(map[string]string{
dwsv1alpha2.WorkflowNameLabel: ownerName,
dwsv1alpha2.WorkflowNamespaceLabel: ownerNS,
nnfv1alpha4.DirectiveIndexLabel: idx,
}),
}
} else {
// Otherwise the owner is a PersistentStorageInstance and we'll need to use the owner
// labels. It also will not have a directive index.
listOptions = []client.ListOption{
client.MatchingLabels(map[string]string{
dwsv1alpha2.OwnerKindLabel: ownerKind,
dwsv1alpha2.OwnerNameLabel: ownerName,
dwsv1alpha2.OwnerNamespaceLabel: ownerNS,
}),
}
}

serversList := &dwsv1alpha2.ServersList{}
if err := c.List(ctx, serversList, listOptions...); err != nil {
return nil, dwsv1alpha2.NewResourceError("unable retrieve NnfServers resource").WithError(err).WithMajor()
}

// We should only have 1
if len(serversList.Items) != 1 {
return nil, dwsv1alpha2.NewResourceError(fmt.Sprintf("wrong number of NnfServers resources: expected 1, got %d", len(serversList.Items))).WithMajor()
}

return &serversList.Items[0], nil
}
88 changes: 1 addition & 87 deletions internal/controller/nnf_clientmount_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ package controller
import (
"context"
"encoding/json"
"fmt"
"os"
"path/filepath"
"runtime"
Expand Down Expand Up @@ -306,7 +305,7 @@ func (r *NnfClientMountReconciler) changeMount(ctx context.Context, clientMount
func (r *NnfClientMountReconciler) dumpServersToFile(ctx context.Context, clientMount *dwsv1alpha2.ClientMount, path string, uid, gid uint32) error {

// Get the NnfServers Resource
log.FromContext(ctx).Info("BLAKE labels - before", "labels", clientMount.Labels)
log.FromContext(ctx).Info("BLAKE labels - before clientmount", "labels", clientMount.Labels)
server, err := getServerForClientMountOrNnfNodeStorage(ctx, r.Client, clientMount.Labels)
if err != nil {
return dwsv1alpha2.NewResourceError("could not retrieve corresponding NnfServer resource for this ClientMount").WithError(err).WithMajor()
Expand Down Expand Up @@ -335,91 +334,6 @@ func (r *NnfClientMountReconciler) dumpServersToFile(ctx context.Context, client
return nil
}

// Retrieve the ClientMount's corresponding NnfServer resource. To do this, we first need to get the corresponding NnfStorage resource. That is done by
// looking at the owner of the ClientMount resource. It should be NnfStorage. Then, we inspect the NnfStorage resource's owner. In this case, there can
// be two different owners:
//
// 1. Workflow (non-persistent storage case)
// 2. PersistentStorageInstance (persistent storage case)
//
// Once we understand who owns the NnfStorage resource, we can then obtain the NnfServer resource through slightly different methods.
func getServerForClientMountOrNnfNodeStorage(ctx context.Context, c client.Client, labels map[string]string) (*dwsv1alpha2.Servers, error) {
storageKind := "NnfStorage"
persistentKind := "PersistentStorageInstance"
workflowKind := "Workflow"

// Get the owner and directive index from ClientMount's labels
ownerKind, ownerExists := labels[dwsv1alpha2.OwnerKindLabel]
ownerName, ownerNameExists := labels[dwsv1alpha2.OwnerNameLabel]
ownerNS, ownerNSExists := labels[dwsv1alpha2.OwnerNamespaceLabel]
_, idxExists := labels[nnfv1alpha4.DirectiveIndexLabel]

// log.FromContext(ctx).Info("BLAKE labels - after", "labels", labels)

// We should expect the owner to be NnfStorage and have the expected labels
if !ownerExists || !ownerNameExists || !ownerNSExists || !idxExists || ownerKind != storageKind {
return nil, dwsv1alpha2.NewResourceError(fmt.Sprintf("expected owner to be of kind NnfStorage and have the expected labels: %v", labels)).WithMajor()
}

// Retrieve the NnfStorage resource
storage := &nnfv1alpha4.NnfStorage{
ObjectMeta: metav1.ObjectMeta{
Name: ownerName,
Namespace: ownerNS,
},
}
if err := c.Get(ctx, client.ObjectKeyFromObject(storage), storage); err != nil {
return nil, dwsv1alpha2.NewResourceError("unable retrieve NnfStorage resource").WithError(err).WithMajor()
}

// Get the owner and directive index from NnfStorage's labels
ownerKind, ownerExists = storage.Labels[dwsv1alpha2.OwnerKindLabel]
ownerName, ownerNameExists = storage.Labels[dwsv1alpha2.OwnerNameLabel]
ownerNS, ownerNSExists = storage.Labels[dwsv1alpha2.OwnerNamespaceLabel]
idx, idxExists := storage.Labels[nnfv1alpha4.DirectiveIndexLabel]

// We should expect the owner of the NnfStorage to be Workflow or PersistentStorageInstance and
// have the expected labels
if !ownerExists || !ownerNameExists || !ownerNSExists || !idxExists || (ownerKind != workflowKind && ownerKind != persistentKind) {
return nil, dwsv1alpha2.NewResourceError("expected owner to be of kind Workflow or PersistentStorageInstance and have the expected labels").WithMajor()
}

// If the owner is a workflow, then we can use the workflow labels and directive index to get
// the Servers Resource.
var listOptions []client.ListOption
if ownerKind == workflowKind {
listOptions = []client.ListOption{
client.MatchingLabels(map[string]string{
dwsv1alpha2.WorkflowNameLabel: ownerName,
dwsv1alpha2.WorkflowNamespaceLabel: ownerNS,
nnfv1alpha4.DirectiveIndexLabel: idx,
}),
}
} else {
// Otherwise the owner is a PersistentStorageInstance and we'll need to use the owner
// labels. It also will not have a directive index.
listOptions = []client.ListOption{
client.MatchingLabels(map[string]string{
dwsv1alpha2.OwnerKindLabel: ownerKind,
dwsv1alpha2.OwnerNameLabel: ownerName,
dwsv1alpha2.OwnerNamespaceLabel: ownerNS,
}),
}
}

serversList := &dwsv1alpha2.ServersList{}
if err := c.List(ctx, serversList, listOptions...); err != nil {
return nil, dwsv1alpha2.NewResourceError("unable retrieve NnfServers resource").WithError(err).WithMajor()
}

// We should only have 1
if len(serversList.Items) != 1 {
return nil, dwsv1alpha2.NewResourceError(fmt.Sprintf("wrong number of NnfServers resources: expected 1, got %d", len(serversList.Items))).WithMajor()
}

return &serversList.Items[0], nil
}

// Go through the Server's allocation sets to determine the number of Lustre components and rabbit
// nodes. Returns a map with keys for each lustre component type and also the rabbits involved. The
// list of rabbits is kept unique.
Expand Down

0 comments on commit 18233c0

Please sign in to comment.