From eeafd34807e8bae8f79877895e6367abc21866ff Mon Sep 17 00:00:00 2001 From: Aline Abler Date: Fri, 9 Feb 2024 15:25:12 +0100 Subject: [PATCH] Create user if they do not exist --- .../default_organization_controller.go | 16 ++++++++- .../default_organization_controller_test.go | 33 +++++++++++++++++-- 2 files changed, 46 insertions(+), 3 deletions(-) diff --git a/controllers/default_organization_controller.go b/controllers/default_organization_controller.go index cb4cb587..a879d182 100644 --- a/controllers/default_organization_controller.go +++ b/controllers/default_organization_controller.go @@ -4,6 +4,8 @@ import ( "context" "go.uber.org/multierr" + apierrors "k8s.io/apimachinery/pkg/api/errors" + v1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/types" "k8s.io/client-go/tools/record" @@ -68,7 +70,19 @@ func (r *DefaultOrganizationReconciler) Reconcile(ctx context.Context, req ctrl. func setUserDefaultOrganization(ctx context.Context, c client.Client, userName string, orgName string) error { user := controlv1.User{} if err := c.Get(ctx, types.NamespacedName{Name: userName}, &user); err != nil { - return err + if !apierrors.IsNotFound(err) { + return err + } + return c.Create(ctx, &controlv1.User{ + ObjectMeta: v1.ObjectMeta{ + Name: userName, + }, + Spec: controlv1.UserSpec{ + Preferences: controlv1.UserPreferences{ + DefaultOrganizationRef: orgName, + }, + }, + }) } if user.Spec.Preferences.DefaultOrganizationRef != "" { diff --git a/controllers/default_organization_controller_test.go b/controllers/default_organization_controller_test.go index 238215b9..e483a3bb 100644 --- a/controllers/default_organization_controller_test.go +++ b/controllers/default_organization_controller_test.go @@ -74,7 +74,7 @@ var u3 = controlv1.User{ }, } -func Test_DefaultOrganizationReconciler_Reconcile_Sucess(t *testing.T) { +func Test_DefaultOrganizationReconciler_Reconcile_Success(t *testing.T) { ctx := context.Background() c := prepareTest(t, &testMemberships1, &testMemberships2, &u1, &u2, &u3) fakeRecorder := record.NewFakeRecorder(3) @@ -103,7 +103,7 @@ func Test_DefaultOrganizationReconciler_Reconcile_Sucess(t *testing.T) { } -func Test_DefaultOrganizationReconciler_Reconcile_NoMembership_Sucess(t *testing.T) { +func Test_DefaultOrganizationReconciler_Reconcile_NoMembership_Success(t *testing.T) { ctx := context.Background() c := prepareTest(t, &testMemberships2, &u1, &u2, &u3) fakeRecorder := record.NewFakeRecorder(3) @@ -132,6 +132,35 @@ func Test_DefaultOrganizationReconciler_Reconcile_NoMembership_Sucess(t *testing } +func Test_DefaultOrganizationReconciler_Reconcile_UserNotExist_Success(t *testing.T) { + ctx := context.Background() + c := prepareTest(t, &testMemberships1, &u1) + fakeRecorder := record.NewFakeRecorder(3) + + _, err := (&DefaultOrganizationReconciler{ + Client: c, + Scheme: c.Scheme(), + Recorder: fakeRecorder, + }).Reconcile(ctx, ctrl.Request{ + NamespacedName: types.NamespacedName{ + Name: testMemberships1.Name, + Namespace: testMemberships1.Namespace, + }, + }) + require.NoError(t, err) + + user := controlv1.User{} + require.NoError(t, c.Get(context.TODO(), types.NamespacedName{Name: u1.ObjectMeta.Name}, &user)) + assert.Equal(t, testMemberships1.ObjectMeta.Namespace, user.Spec.Preferences.DefaultOrganizationRef) + + require.NoError(t, c.Get(context.TODO(), types.NamespacedName{Name: u2.ObjectMeta.Name}, &user)) + assert.Equal(t, testMemberships1.ObjectMeta.Namespace, user.Spec.Preferences.DefaultOrganizationRef) + + require.NoError(t, c.Get(context.TODO(), types.NamespacedName{Name: u3.ObjectMeta.Name}, &user)) + assert.Equal(t, testMemberships1.ObjectMeta.Namespace, user.Spec.Preferences.DefaultOrganizationRef) + +} + func Test_DefaultOrganizationReconciler_Reconcile_Error(t *testing.T) { failU4 := controlv1.User{ ObjectMeta: metav1.ObjectMeta{