You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airavata.apache.org by ya...@apache.org on 2026/10/03 21:30:41 UTC

(airavata-custos) branch slurm-default-account updated (dbbce78ee -> 67b32d2fe)

This is an automated email from the ASF dual-hosted git repository.

yasithdev pushed a change to branch slurm-default-account
in repository https://gitbox.apache.org/repos/asf/airavata-custos.git


 discard dbbce78ee delete a slurm user whose last associations are revoked
     add 483918e5a Manage POSIX groups and user:group mapping for compute allocations (#581)
     add db64e2a72 create vast home directories when a cluster account is provisioned (#590)
     add 4798c316b add a local VAST VMS stand-in for testing the VAST connector (#592)
     add bfb584dd6 create vast project directories when an allocation's group is provisioned (#584)
     new 67b32d2fe revoke slurm associations one at a time, moving the default first

This update added new revisions after undoing existing revisions.
That is to say, some revisions that were in the old version of the
branch are not in the new version.  This situation occurs
when a user --force pushes a change and generates a repository
containing something like this:

 * -- * -- B -- O -- O -- O   (dbbce78ee)
            \
             N -- N -- N   refs/heads/slurm-default-account (67b32d2fe)

You should already have received notification emails for all of the O
revisions, and so the following emails describe only the N revisions
from the common base, B.

Any revisions marked "omit" are not gone; other references still
refer to them.  Any revisions marked "discard" are gone forever.

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 CONFIG.md                                          |  23 +++
 api/core.openapi.yaml                              |   3 +
 config/custos.yaml                                 |  37 +++++
 connectors/COmanage/Identity-Provisioner/README.md |  20 ++-
 .../internal/client/client_test.go                 |   4 +-
 .../internal/client/group_members.go               |  24 +++
 .../internal/client/unix_cluster.go                |   2 +-
 .../internal/operations/ensure_allocation_group.go | 111 +++++++++++++
 .../operations/ensure_allocation_group_test.go     |  60 +++++++
 .../internal/operations/ensure_posix_account.go    |   7 +-
 .../internal/operations/lookup.go                  |  14 +-
 .../internal/operations/mocks_test.go              |  21 ++-
 .../internal/operations/orchestrator.go            |   3 +
 .../internal/operations/orchestrator_test.go       |   7 +
 .../internal/operations/remove_allocation_group.go |  26 +--
 .../internal/operations/remove_posix_account.go    |  30 ++--
 .../internal/subscribers/allocation_group.go       |  66 ++++++++
 .../internal/subscribers/cluster_user.go           |  13 +-
 .../internal/subscribers/deprovision.go            |  61 +++++--
 .../internal/subscribers/deprovision_test.go       | 122 ++++++++------
 .../internal/subscribers/members_test.go           |   9 --
 .../internal/subscribers/subscriber.go             |   1 -
 .../SLURM/Rest-Client/pkg/client/associations.go   |   6 -
 connectors/VAST/Storage-Provisioner/README.md      |  46 ++++++
 .../Storage-Provisioner/internal/client/client.go  | 113 +++++++++++++
 .../internal/client/client_test.go                 |  58 +++++++
 .../internal/subscribers/subscriber.go             | 178 +++++++++++++++++++++
 .../internal/subscribers/subscriber_test.go        |  94 +++++++++++
 .../VAST/Storage-Provisioner/pkg/vast/loader.go    |  84 ++++++++++
 dev-ops/local-vast/README.md                       |  52 ++++++
 dev-ops/local-vast/local-vast.service              |  13 ++
 dev-ops/local-vast/main.go                         | 105 ++++++++++++
 internal/connectors/loader.go                      |   2 +
 internal/db/migrations/000004_allocations.up.sql   |   2 +
 .../store/compute_allocation_membership_store.go   |  21 ++-
 internal/store/compute_allocation_store.go         |   6 +-
 pkg/models/allocation.go                           |   2 +
 pkg/service/compute_allocation.go                  |  27 +++-
 pkg/service/compute_cluster_user.go                |   8 +-
 .../compute_cluster_user_integration_test.go       |  90 +++++++++++
 web/src/generated/core/types.gen.ts                |   4 +
 web/src/generated/core/zod.gen.ts                  |   1 +
 42 files changed, 1448 insertions(+), 128 deletions(-)
 create mode 100644 connectors/COmanage/Identity-Provisioner/internal/operations/ensure_allocation_group.go
 create mode 100644 connectors/COmanage/Identity-Provisioner/internal/operations/ensure_allocation_group_test.go
 copy internal/server/testidentity_test.go => connectors/COmanage/Identity-Provisioner/internal/operations/remove_allocation_group.go (59%)
 create mode 100644 connectors/COmanage/Identity-Provisioner/internal/subscribers/allocation_group.go
 create mode 100644 connectors/VAST/Storage-Provisioner/README.md
 create mode 100644 connectors/VAST/Storage-Provisioner/internal/client/client.go
 create mode 100644 connectors/VAST/Storage-Provisioner/internal/client/client_test.go
 create mode 100644 connectors/VAST/Storage-Provisioner/internal/subscribers/subscriber.go
 create mode 100644 connectors/VAST/Storage-Provisioner/internal/subscribers/subscriber_test.go
 create mode 100644 connectors/VAST/Storage-Provisioner/pkg/vast/loader.go
 create mode 100644 dev-ops/local-vast/README.md
 create mode 100644 dev-ops/local-vast/local-vast.service
 create mode 100644 dev-ops/local-vast/main.go


(airavata-custos) 01/01: revoke slurm associations one at a time, moving the default first

Posted by ya...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

yasithdev pushed a commit to branch slurm-default-account
in repository https://gitbox.apache.org/repos/asf/airavata-custos.git

commit 67b32d2fed8360fdd105479424cdf344f03f72bf
Author: Yasith Jayawardana <ya...@icloud.com>
AuthorDate: Sat Oct 3 16:43:15 2026 -0400

    revoke slurm associations one at a time, moving the default first
---
 .../internal/subscribers/deprovision.go            |  68 ++++++++++++-
 .../internal/subscribers/deprovision_test.go       | 110 +++++++++++++++------
 .../internal/subscribers/members_test.go           |  11 ++-
 .../internal/subscribers/reconciler.go             |  39 +++++---
 .../SLURM/Rest-Client/pkg/client/associations.go   |  25 +++++
 .../Rest-Client/pkg/client/associations_test.go    |  14 +++
 6 files changed, 219 insertions(+), 48 deletions(-)

diff --git a/connectors/SLURM/Association-Mapper/internal/subscribers/deprovision.go b/connectors/SLURM/Association-Mapper/internal/subscribers/deprovision.go
index 23ddaf124..94fd7d06b 100644
--- a/connectors/SLURM/Association-Mapper/internal/subscribers/deprovision.go
+++ b/connectors/SLURM/Association-Mapper/internal/subscribers/deprovision.go
@@ -22,6 +22,7 @@ import (
 	"errors"
 	"fmt"
 	"log/slog"
+	"slices"
 
 	"github.com/apache/airavata-custos/connectors/SLURM/Rest-Client/pkg/client"
 	"github.com/apache/airavata-custos/pkg/models"
@@ -34,7 +35,7 @@ import (
 //
 // A member holds one association per partition on the allocation. The filter
 // names the cluster, account, and user but leaves the partition empty, which
-// matches every partition, so one call removes the member's whole set.
+// matches every partition, so revoke removes the member's whole set.
 func (a *AssociationSubscriber) removeAssociationsForMembership(ctx context.Context, membership models.ComputeAllocationMembership) error {
 	allocation, err := a.coreService.GetComputeAllocation(ctx, membership.ComputeAllocationID)
 	if err != nil {
@@ -54,7 +55,7 @@ func (a *AssociationSubscriber) removeAssociationsForMembership(ctx context.Cont
 		Account: allocation.Name,
 		User:    csu.LocalUsername,
 	}
-	if err := a.slurmClient.DeleteAssociation(filter); err != nil {
+	if err := a.revoke(filter); err != nil {
 		return fmt.Errorf("delete associations for %s on %s: %w", csu.LocalUsername, allocation.Name, err)
 	}
 	slog.Info("Removed associations for membership",
@@ -73,7 +74,7 @@ func (a *AssociationSubscriber) removeAssociationsForAllocation(ctx context.Cont
 		Cluster: cluster.Name,
 		Account: allocation.Name,
 	}
-	if err := a.slurmClient.DeleteAssociation(filter); err != nil {
+	if err := a.revoke(filter); err != nil {
 		return fmt.Errorf("delete associations for allocation %s: %w", allocation.Name, err)
 	}
 	slog.Info("Removed associations for allocation",
@@ -81,6 +82,67 @@ func (a *AssociationSubscriber) removeAssociationsForAllocation(ctx context.Cont
 	return nil
 }
 
+// revoke removes the member associations f matches, one call each, since
+// slurmrestd removes at most one per call. Slurm refuses to remove a user's
+// default association while they hold another on the cluster, so the default
+// first moves to another account they keep, or, when they keep only other
+// partitions of the same account, those are removed too and added back after.
+func (a *AssociationSubscriber) revoke(f client.AssocFilter) error {
+	matched, err := a.slurmClient.ListAssociations(f)
+	if err != nil {
+		return err
+	}
+	byUser := map[string][]client.Association{}
+	for _, m := range matched {
+		if m.User != "" {
+			byUser[m.User] = append(byUser[m.User], m)
+		}
+	}
+	for user, drop := range byUser {
+		held, err := a.slurmClient.ListAssociations(client.AssocFilter{Cluster: f.Cluster, User: user})
+		if err != nil {
+			return err
+		}
+		kept := slices.DeleteFunc(held, func(h client.Association) bool {
+			return slices.ContainsFunc(drop, func(d client.Association) bool { return d.Account == h.Account && d.Partition == h.Partition })
+		})
+		var restore []client.Association
+		if i := slices.IndexFunc(drop, isDefault); i >= 0 && len(kept) > 0 {
+			if j := slices.IndexFunc(kept, func(k client.Association) bool { return k.Account != drop[i].Account }); j >= 0 {
+				next, yes := kept[j], true
+				next.ID, next.IsDefault = 0, &yes
+				if err := a.slurmClient.UpsertAssociation(next); err != nil {
+					return fmt.Errorf("move default of %s to %s: %w", user, next.Account, err)
+				}
+			} else {
+				restore, drop = kept, append(kept, drop...)
+			}
+		}
+		slices.SortStableFunc(drop, func(x, y client.Association) int { return btoi(isDefault(x)) - btoi(isDefault(y)) })
+		for _, d := range drop {
+			if err := a.slurmClient.DeleteAssociation(client.AssocFilter{Cluster: d.Cluster, Account: d.Account, User: d.User, Partition: d.Partition}); err != nil {
+				return err
+			}
+		}
+		for _, r := range restore {
+			r.ID, r.IsDefault = 0, nil
+			if err := a.slurmClient.UpsertAssociation(r); err != nil {
+				return fmt.Errorf("restore %s on %s/%s: %w", user, r.Account, r.Partition, err)
+			}
+		}
+	}
+	return nil
+}
+
+func isDefault(a client.Association) bool { return a.IsDefault != nil && *a.IsDefault }
+
+func btoi(b bool) int {
+	if b {
+		return 1
+	}
+	return 0
+}
+
 // activeAllocation reports whether an allocation should still grant access.
 func activeAllocation(allocation models.ComputeAllocation) bool {
 	return allocation.Status == models.ACTIVE
diff --git a/connectors/SLURM/Association-Mapper/internal/subscribers/deprovision_test.go b/connectors/SLURM/Association-Mapper/internal/subscribers/deprovision_test.go
index 6c6a2c54f..edb437b03 100644
--- a/connectors/SLURM/Association-Mapper/internal/subscribers/deprovision_test.go
+++ b/connectors/SLURM/Association-Mapper/internal/subscribers/deprovision_test.go
@@ -19,6 +19,7 @@ package subscribers
 
 import (
 	"context"
+	"slices"
 	"testing"
 	"time"
 
@@ -32,22 +33,37 @@ func deactivatedMembership() models.ComputeAllocationMembership {
 	return m
 }
 
+// held is the allocation's own association and its member's two partitions.
+func held() *fakeSlurmClient {
+	return &fakeSlurmClient{existing: []client.Association{
+		{Account: "test-alloc", Cluster: "testcluster"},
+		{Account: "test-alloc", Cluster: "testcluster", User: "testuser", Partition: "compute"},
+		{Account: "test-alloc", Cluster: "testcluster", User: "testuser", Partition: "gpu"},
+	}}
+}
+
+// removedPartitions checks every delete is one of testuser's test-alloc associations.
+func removedPartitions(t *testing.T, got []client.AssocFilter) []string {
+	t.Helper()
+	var partitions []string
+	for _, f := range got {
+		if f.User != "testuser" || f.Account != "test-alloc" || f.Cluster != "testcluster" {
+			t.Errorf("unexpected delete filter: %+v", f)
+		}
+		partitions = append(partitions, f.Partition)
+	}
+	return partitions
+}
+
 func TestMembershipDeactivationRemovesAssociations(t *testing.T) {
 	core := coreMock(mockOpts{provisionedAt: ago(time.Minute)})
-	slurm := &fakeSlurmClient{}
+	slurm := held()
 	NewAssociationSubscriber(slurm, nil, core, 0, 0).
 		SubscribeToComputeAllocationMembershipUpdate(context.Background(), deactivatedMembership())
 
-	got := slurm.allDeletes()
-	if len(got) != 1 {
-		t.Fatalf("expected 1 delete, got %d", len(got))
-	}
-	// Deleting by user+account covers every partition in one call.
-	if got[0].User != "testuser" || got[0].Account != "test-alloc" || got[0].Cluster != "testcluster" {
-		t.Errorf("unexpected delete filter: %+v", got[0])
-	}
-	if got[0].Partition != "" {
-		t.Errorf("filter should not pin a partition, got %q", got[0].Partition)
+	// slurmrestd removes one association per call, so each partition goes on its own.
+	if got := removedPartitions(t, slurm.allDeletes()); !slices.Equal(got, []string{"compute", "gpu"}) {
+		t.Fatalf("expected both partitions removed, got %v", got)
 	}
 	if n := len(slurm.all()); n != 0 {
 		t.Errorf("a deactivation must not write associations, got %d", n)
@@ -71,32 +87,27 @@ func TestMembershipReactivationRestoresAssociations(t *testing.T) {
 
 func TestMembershipDeletionRemovesAssociations(t *testing.T) {
 	core := coreMock(mockOpts{provisionedAt: ago(time.Minute)})
-	slurm := &fakeSlurmClient{}
+	slurm := held()
 	NewAssociationSubscriber(slurm, nil, core, 0, 0).
 		SubscribeToComputeAllocationMembershipDeletion(context.Background(), testMembership())
 
-	got := slurm.allDeletes()
-	if len(got) != 1 || got[0].User != "testuser" || got[0].Account != "test-alloc" {
-		t.Fatalf("expected the member's associations to be removed, got %+v", got)
+	if got := removedPartitions(t, slurm.allDeletes()); len(got) != 2 {
+		t.Fatalf("expected the member's associations to be removed, got %v", got)
 	}
 }
 
 func TestAllocationDeactivationRemovesAllAssociations(t *testing.T) {
 	core := coreMock(mockOpts{})
-	slurm := &fakeSlurmClient{}
+	slurm := held()
 	alloc := models.ComputeAllocation{
 		ID: "alloc-1", Name: "test-alloc", ComputeClusterID: "cluster-1", Status: models.INACTIVE,
 	}
 	NewAssociationSubscriber(slurm, nil, core, 0, 0).
 		SubscribeToComputeAllocationUpdate(context.Background(), alloc)
 
-	got := slurm.allDeletes()
-	if len(got) != 1 {
-		t.Fatalf("expected 1 delete, got %d", len(got))
-	}
-	// No user pinned: every member of the allocation loses access.
-	if got[0].Account != "test-alloc" || got[0].User != "" {
-		t.Errorf("expected an account-wide delete, got %+v", got[0])
+	// Every member loses access; the allocation's own association stays.
+	if got := removedPartitions(t, slurm.allDeletes()); len(got) != 2 {
+		t.Fatalf("expected every member association removed, got %v", got)
 	}
 }
 
@@ -142,16 +153,15 @@ func TestAllocationReactivationSkipsUnprovisionedMembers(t *testing.T) {
 
 func TestAllocationDeletionRemovesAllAssociations(t *testing.T) {
 	core := coreMock(mockOpts{})
-	slurm := &fakeSlurmClient{}
+	slurm := held()
 	alloc := models.ComputeAllocation{
 		ID: "alloc-1", Name: "test-alloc", ComputeClusterID: "cluster-1", Status: models.ACTIVE,
 	}
 	NewAssociationSubscriber(slurm, nil, core, 0, 0).
 		SubscribeToComputeAllocationDeletion(context.Background(), alloc)
 
-	got := slurm.allDeletes()
-	if len(got) != 1 || got[0].Account != "test-alloc" || got[0].User != "" {
-		t.Fatalf("expected an account-wide delete on allocation deletion, got %+v", got)
+	if got := removedPartitions(t, slurm.allDeletes()); len(got) != 2 {
+		t.Fatalf("expected every member association removed on allocation deletion, got %v", got)
 	}
 }
 
@@ -167,6 +177,7 @@ func TestReconcilerRemovesStaleAssociation(t *testing.T) {
 		{Account: "test-alloc", Cluster: "testcluster", User: "testuser", Partition: "compute"},
 		// left behind by a membership that is gone
 		{Account: "test-alloc", Cluster: "testcluster", User: "ghost", Partition: "compute"},
+		{Account: "other-account", Cluster: "testcluster", User: "ghost", Partition: "compute"},
 	}}
 	NewAssociationSubscriber(slurm, nil, core, 0, 0).reconcile(context.Background())
 
@@ -174,11 +185,54 @@ func TestReconcilerRemovesStaleAssociation(t *testing.T) {
 	if len(got) != 1 {
 		t.Fatalf("expected the stale association to be removed, got %d deletes", len(got))
 	}
-	if got[0].User != "ghost" || got[0].Partition != "compute" {
+	if got[0].User != "ghost" || got[0].Account != "test-alloc" {
 		t.Errorf("removed the wrong association: %+v", got[0])
 	}
 }
 
+func TestRevokeMovesTheDefaultFirst(t *testing.T) {
+	yes := true
+	assoc := func(account, partition string, def bool) client.Association {
+		a := client.Association{Cluster: "c", Account: account, User: "u", Partition: partition}
+		if def {
+			a.IsDefault = &yes
+		}
+		return a
+	}
+	tests := []struct {
+		name           string
+		held           []client.Association
+		revoke         client.AssocFilter
+		upsert, delete []string
+	}{
+		{name: "default moves to another account", held: []client.Association{assoc("a", "cpu", true), assoc("b", "cpu", false)},
+			revoke: client.AssocFilter{Account: "a"}, upsert: []string{"b/cpu default"}, delete: []string{"a/cpu"}},
+		{name: "default goes last", held: []client.Association{assoc("a", "cpu", true), assoc("a", "gpu", false)},
+			revoke: client.AssocFilter{Account: "a"}, delete: []string{"a/gpu", "a/cpu"}},
+		{name: "same-account partitions are restored", held: []client.Association{assoc("a", "cpu", true), assoc("a", "gpu", false)},
+			revoke: client.AssocFilter{Account: "a", Partition: "cpu"}, delete: []string{"a/gpu", "a/cpu"}, upsert: []string{"a/gpu"}},
+	}
+	for _, tt := range tests {
+		t.Run(tt.name, func(t *testing.T) {
+			slurm := &fakeSlurmClient{existing: tt.held}
+			tt.revoke.Cluster, tt.revoke.User = "c", "u"
+			if err := NewAssociationSubscriber(slurm, nil, nil, 0, 0).revoke(tt.revoke); err != nil {
+				t.Fatal(err)
+			}
+			var upserts, deletes []string
+			for _, a := range slurm.upserts {
+				upserts = append(upserts, a.Account+"/"+a.Partition+map[bool]string{true: " default"}[isDefault(a)])
+			}
+			for _, f := range slurm.allDeletes() {
+				deletes = append(deletes, f.Account+"/"+f.Partition)
+			}
+			if !slices.Equal(upserts, tt.upsert) || !slices.Equal(deletes, tt.delete) {
+				t.Fatalf("upserts %v, deletes %v; want %v, %v", upserts, deletes, tt.upsert, tt.delete)
+			}
+		})
+	}
+}
+
 // Guard: an empty desired set almost always means a failed lookup, so the
 // sweep must not read it as "revoke everyone".
 func TestReconcilerDoesNotPruneWhenNothingIsDesired(t *testing.T) {
diff --git a/connectors/SLURM/Association-Mapper/internal/subscribers/members_test.go b/connectors/SLURM/Association-Mapper/internal/subscribers/members_test.go
index d3be7f56d..9d21a1991 100644
--- a/connectors/SLURM/Association-Mapper/internal/subscribers/members_test.go
+++ b/connectors/SLURM/Association-Mapper/internal/subscribers/members_test.go
@@ -50,8 +50,15 @@ func (f *fakeSlurmClient) allDeletes() []client.AssocFilter {
 	return append([]client.AssocFilter(nil), f.deletes...)
 }
 
-func (f *fakeSlurmClient) ListAssociations(client.AssocFilter) ([]client.Association, error) {
-	return f.existing, nil
+func (f *fakeSlurmClient) ListAssociations(q client.AssocFilter) ([]client.Association, error) {
+	var out []client.Association
+	for _, a := range f.existing {
+		if (q.Account == "" || q.Account == a.Account) && (q.User == "" || q.User == a.User) &&
+			(q.Cluster == "" || q.Cluster == a.Cluster) && (q.Partition == "" || q.Partition == a.Partition) {
+			out = append(out, a)
+		}
+	}
+	return out, nil
 }
 
 func (f *fakeSlurmClient) CreateAccount(client.Account, string) error { return nil }
diff --git a/connectors/SLURM/Association-Mapper/internal/subscribers/reconciler.go b/connectors/SLURM/Association-Mapper/internal/subscribers/reconciler.go
index c761a4bac..a817ac4f8 100644
--- a/connectors/SLURM/Association-Mapper/internal/subscribers/reconciler.go
+++ b/connectors/SLURM/Association-Mapper/internal/subscribers/reconciler.go
@@ -179,7 +179,11 @@ func (a *AssociationSubscriber) pruneStaleAssociations(ctx context.Context, clus
 		return 0
 	}
 
-	removed := 0
+	// A member whose every partition on an account is stale is revoked in one call,
+	// since Slurm refuses partition-by-partition removal of a default account.
+	type member struct{ account, user string }
+	held := map[member]int{}
+	stale := map[member][]string{}
 	for key, assoc := range existing {
 		if assoc.User == "" {
 			continue
@@ -187,23 +191,28 @@ func (a *AssociationSubscriber) pruneStaleAssociations(ctx context.Context, clus
 		if _, ok := managed[assoc.Account]; !ok {
 			continue
 		}
-		if _, ok := desired[key]; ok {
-			continue
+		m := member{assoc.Account, assoc.User}
+		held[m]++
+		if _, ok := desired[key]; !ok {
+			stale[m] = append(stale[m], assoc.Partition)
 		}
-		filter := client.AssocFilter{
-			Cluster:   cluster.Name,
-			Account:   assoc.Account,
-			User:      assoc.User,
-			Partition: assoc.Partition,
+	}
+
+	removed := 0
+	for m, partitions := range stale {
+		if len(partitions) == held[m] {
+			partitions = []string{""}
 		}
-		if err := a.slurmClient.DeleteAssociation(filter); err != nil {
-			slog.Error("Association reconciler: failed to remove stale association",
-				"account", assoc.Account, "user", assoc.User, "partition", assoc.Partition, "error", err)
-			continue
+		for _, partition := range partitions {
+			filter := client.AssocFilter{Cluster: cluster.Name, Account: m.account, User: m.user, Partition: partition}
+			if err := a.revoke(filter); err != nil {
+				slog.Error("Association reconciler: failed to remove stale association",
+					"account", m.account, "user", m.user, "partition", partition, "error", err)
+				continue
+			}
+			slog.Info("Removed stale association", "account", m.account, "user", m.user, "partition", partition)
+			removed++
 		}
-		slog.Info("Removed stale association",
-			"account", assoc.Account, "user", assoc.User, "partition", assoc.Partition)
-		removed++
 	}
 	return removed
 }
diff --git a/connectors/SLURM/Rest-Client/pkg/client/associations.go b/connectors/SLURM/Rest-Client/pkg/client/associations.go
index b1eca11be..254eaf579 100644
--- a/connectors/SLURM/Rest-Client/pkg/client/associations.go
+++ b/connectors/SLURM/Rest-Client/pkg/client/associations.go
@@ -20,6 +20,7 @@ package client
 
 import (
 	"errors"
+	"fmt"
 	"log"
 	"log/slog"
 	"net/url"
@@ -76,6 +77,11 @@ func (c *Client) UpsertAssociation(a Association) error {
 		log.Printf("Failed to upsert association: %v", err)
 		return err
 	}
+	if a.User != "" {
+		if err := c.ensureUser(a.User, a.Account); err != nil {
+			return fmt.Errorf("ensure user %s: %w", a.User, err)
+		}
+	}
 
 	filter := AssocFilter{
 		Account:   a.Account,
@@ -137,6 +143,25 @@ func tresSliceEqualUnordered(a, b []TRES) bool {
 	return true
 }
 
+// ensureUser creates a missing user record defaulting to account, which writing
+// an association does not, and without which Slurm cannot later remove it.
+func (c *Client) ensureUser(name, account string) error {
+	var out struct {
+		Users []struct {
+			Name string `json:"name"`
+		} `json:"users"`
+	}
+	if _, err := c.do("GET", "/slurmdb/v0.0."+c.apiVersion+"/user/"+url.PathEscape(name), nil, &out); err != nil {
+		return err
+	}
+	if len(out.Users) > 0 {
+		return nil
+	}
+	body := map[string]any{"users": []map[string]any{{"name": name, "default": map[string]any{"account": account}}}}
+	_, err := c.do("POST", "/slurmdb/v0.0."+c.apiVersion+"/users", body, nil)
+	return err
+}
+
 func (c *Client) DeleteAssociation(f AssocFilter) error {
 	path := "/slurmdb/v0.0." + c.apiVersion + "/association"
 	if q := f.query(); q != "" {
diff --git a/connectors/SLURM/Rest-Client/pkg/client/associations_test.go b/connectors/SLURM/Rest-Client/pkg/client/associations_test.go
index bd9bd3b49..9bc57302d 100644
--- a/connectors/SLURM/Rest-Client/pkg/client/associations_test.go
+++ b/connectors/SLURM/Rest-Client/pkg/client/associations_test.go
@@ -49,6 +49,7 @@ func TestListAssociationsByAccount(t *testing.T) {
 }
 
 func TestCreateAssociation(t *testing.T) {
+	var createdUser string
 	srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
 
 		// Support to list associations so we can verify the association was created successfully
@@ -63,6 +64,16 @@ func TestCreateAssociation(t *testing.T) {
 			return
 		}
 
+		if r.Method == "GET" && r.URL.Path == "/slurmdb/v0.0.41/user/alice" {
+			_, _ = w.Write([]byte(`{"users":[]}`))
+			return
+		}
+		if r.Method == "POST" && r.URL.Path == "/slurmdb/v0.0.41/users" {
+			b, _ := io.ReadAll(r.Body)
+			createdUser = string(b)
+			_, _ = w.Write([]byte(`{}`))
+			return
+		}
 		if r.Method != "POST" || r.URL.Path != "/slurmdb/v0.0.41/associations" {
 			t.Fatalf("unexpected %s %s", r.Method, r.URL.Path)
 		}
@@ -83,6 +94,9 @@ func TestCreateAssociation(t *testing.T) {
 	if err != nil {
 		t.Fatal(err)
 	}
+	if createdUser != `{"users":[{"default":{"account":"eng"},"name":"alice"}]}` {
+		t.Errorf("a user with no record must be created defaulting to the account, got %s", createdUser)
+	}
 }
 
 func TestDeleteAssociation(t *testing.T) {