From 5d0cece8ad82998cd8bae3714a9f4a2d819db762 Mon Sep 17 00:00:00 2001 From: Samuel K Date: Sat, 17 Oct 2026 12:15:52 -0600 Subject: [PATCH] refactor(platform): share workspace instance create and update --- cmd/pro/provider/create/workspace.go | 25 +----- cmd/pro/provider/up.go | 4 +- cmd/pro/provider/update/workspace.go | 23 ++--- .../clientimplementation/daemonclient/up.go | 4 +- pkg/daemon/platform/local_server.go | 43 ++-------- pkg/platform/instance_ops.go | 51 +++++++++++ pkg/platform/instance_ops_test.go | 84 +++++++++++++++++++ 7 files changed, 153 insertions(+), 81 deletions(-) create mode 100644 pkg/platform/instance_ops.go create mode 100644 pkg/platform/instance_ops_test.go diff --git a/cmd/pro/provider/create/workspace.go b/cmd/pro/provider/create/workspace.go index 8a1ed090ef..3c15c76aa1 100644 --- a/cmd/pro/provider/create/workspace.go +++ b/cmd/pro/provider/create/workspace.go @@ -17,7 +17,6 @@ import ( "github.com/devsy-org/devsy/pkg/provider" "github.com/devsy-org/devsy/pkg/terminal" "github.com/spf13/cobra" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) // WorkspaceCmd holds the cmd flags. @@ -74,7 +73,7 @@ func createFromInstanceEnv( return fmt.Errorf("unmarshal workspace instance %s: %w", instanceEnv, err) } - updatedInstance, err := createInstance(ctx, baseClient, instance) + updatedInstance, err := platform.CreateInstance(ctx, baseClient, instance) if err != nil { return err } @@ -152,7 +151,7 @@ func createFromEnv(ctx context.Context, baseClient client.Client) error { return err } - _, err = createInstance(ctx, baseClient, instance) + _, err = platform.CreateInstance(ctx, baseClient, instance) if err != nil { return err } @@ -183,23 +182,3 @@ func saveImportedWorkspaceConfig( return nil } - -func createInstance( - ctx context.Context, - client client.Client, - instance *managementv1.DevsyWorkspaceInstance, -) (*managementv1.DevsyWorkspaceInstance, error) { - managementClient, err := client.Management() - if err != nil { - return nil, err - } - - updatedInstance, err := managementClient.Loft().ManagementV1(). - DevsyWorkspaceInstances(instance.GetNamespace()). - Create(ctx, instance, metav1.CreateOptions{}) - if err != nil { - return nil, fmt.Errorf("create workspace instance: %w", err) - } - - return platform.WaitForInstance(ctx, client, updatedInstance) -} diff --git a/cmd/pro/provider/up.go b/cmd/pro/provider/up.go index eb61311ef6..d3ebb47a3e 100644 --- a/cmd/pro/provider/up.go +++ b/cmd/pro/provider/up.go @@ -85,9 +85,7 @@ func (cmd *UpCmd) Run(ctx context.Context) error { if instance.Spec.TemplateRef != nil && templateUpdateRequired(instance) { log.Info("Template update required") oldInstance := instance.DeepCopy() - instance.Spec.TemplateRef.SyncOnce = true - - instance, err = platform.UpdateInstance( + instance, err = platform.UpdateInstanceSyncTemplate( ctx, baseClient, oldInstance, diff --git a/cmd/pro/provider/update/workspace.go b/cmd/pro/provider/update/workspace.go index 8d7e0495bf..da6c68ebd1 100644 --- a/cmd/pro/provider/update/workspace.go +++ b/cmd/pro/provider/update/workspace.go @@ -87,7 +87,12 @@ func updateFromInstanceEnv( ) } - updatedInstance, err := updateInstance(ctx, baseClient, oldInstance, newInstance) + updatedInstance, err := platform.UpdateInstanceSyncTemplate( + ctx, + baseClient, + oldInstance, + newInstance, + ) if err != nil { return err } @@ -153,24 +158,10 @@ func updateFromEnv(ctx context.Context, baseClient client.Client) error { return err } - _, err = updateInstance(ctx, baseClient, oldInstance, newInstance) + _, err = platform.UpdateInstanceSyncTemplate(ctx, baseClient, oldInstance, newInstance) if err != nil { return err } return nil } - -func updateInstance( - ctx context.Context, - client client.Client, - oldInstance *managementv1.DevsyWorkspaceInstance, - newInstance *managementv1.DevsyWorkspaceInstance, -) (*managementv1.DevsyWorkspaceInstance, error) { - // This ensures the template is kept up to date with configuration changes - if newInstance.Spec.TemplateRef != nil { - newInstance.Spec.TemplateRef.SyncOnce = true - } - - return platform.UpdateInstance(ctx, client, oldInstance, newInstance) -} diff --git a/pkg/client/clientimplementation/daemonclient/up.go b/pkg/client/clientimplementation/daemonclient/up.go index 7201db1d6a..b0eb188940 100644 --- a/pkg/client/clientimplementation/daemonclient/up.go +++ b/pkg/client/clientimplementation/daemonclient/up.go @@ -105,9 +105,7 @@ func syncTemplateIfRequired( log.Debug("Template update required") oldInstance := instance.DeepCopy() - instance.Spec.TemplateRef.SyncOnce = true - - updated, err := platform.UpdateInstance(ctx, baseClient, oldInstance, instance) + updated, err := platform.UpdateInstanceSyncTemplate(ctx, baseClient, oldInstance, instance) if err != nil { return nil, fmt.Errorf("update instance: %w", err) } diff --git a/pkg/daemon/platform/local_server.go b/pkg/daemon/platform/local_server.go index aa0cd6812f..d4a55e40d9 100644 --- a/pkg/daemon/platform/local_server.go +++ b/pkg/daemon/platform/local_server.go @@ -674,7 +674,7 @@ func (l *localServer) createWorkspace( } instance.TypeMeta = metav1.TypeMeta{} - updatedInstance, err := createInstance(r.Context(), l.pc, instance) + updatedInstance, err := platform.CreateInstance(r.Context(), l.pc, instance) if err != nil { http.Error(w, fmt.Errorf("create workspace: %w", err).Error(), http.StatusBadRequest) return @@ -703,7 +703,12 @@ func (l *localServer) updateWorkspace( return } - updatedInstance, err := updateInstance(r.Context(), l.pc, oldInstance, newInstance) + updatedInstance, err := platform.UpdateInstanceSyncTemplate( + r.Context(), + l.pc, + oldInstance, + newInstance, + ) if err != nil { http.Error(w, fmt.Errorf("update workspace: %w", err).Error(), http.StatusBadRequest) return @@ -780,37 +785,3 @@ func tryJSON(w http.ResponseWriter, obj any) { w.Header().Set("Content-Type", "application/json") _, _ = w.Write(out) } - -func createInstance( - ctx context.Context, - client platformclient.Client, - instance *managementv1.DevsyWorkspaceInstance, -) (*managementv1.DevsyWorkspaceInstance, error) { - managementClient, err := client.Management() - if err != nil { - return nil, err - } - - updatedInstance, err := managementClient.Loft().ManagementV1(). - DevsyWorkspaceInstances(instance.GetNamespace()). - Create(ctx, instance, metav1.CreateOptions{}) - if err != nil { - return nil, fmt.Errorf("create workspace instance: %w", err) - } - - return platform.WaitForInstance(ctx, client, updatedInstance) -} - -func updateInstance( - ctx context.Context, - client platformclient.Client, - oldInstance *managementv1.DevsyWorkspaceInstance, - newInstance *managementv1.DevsyWorkspaceInstance, -) (*managementv1.DevsyWorkspaceInstance, error) { - // This ensures the template is kept up to date with configuration changes - if newInstance.Spec.TemplateRef != nil { - newInstance.Spec.TemplateRef.SyncOnce = true - } - - return platform.UpdateInstance(ctx, client, oldInstance, newInstance) -} diff --git a/pkg/platform/instance_ops.go b/pkg/platform/instance_ops.go new file mode 100644 index 0000000000..d864162c43 --- /dev/null +++ b/pkg/platform/instance_ops.go @@ -0,0 +1,51 @@ +package platform + +import ( + "context" + "fmt" + + managementv1 "github.com/devsy-org/api/pkg/apis/management/v1" + "github.com/devsy-org/devsy/pkg/platform/client" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" +) + +// CreateInstance creates a workspace instance in its namespace and waits until it is ready. +func CreateInstance( + ctx context.Context, + client client.Client, + instance *managementv1.DevsyWorkspaceInstance, +) (*managementv1.DevsyWorkspaceInstance, error) { + managementClient, err := client.Management() + if err != nil { + return nil, err + } + + updatedInstance, err := managementClient.Loft().ManagementV1(). + DevsyWorkspaceInstances(instance.GetNamespace()). + Create(ctx, instance, metav1.CreateOptions{}) + if err != nil { + return nil, fmt.Errorf("create workspace instance: %w", err) + } + + return WaitForInstance(ctx, client, updatedInstance) +} + +// UpdateInstanceSyncTemplate is UpdateInstance with the template sync flag set, +// so the workspace template is kept up to date with configuration changes. +func UpdateInstanceSyncTemplate( + ctx context.Context, + client client.Client, + oldInstance *managementv1.DevsyWorkspaceInstance, + newInstance *managementv1.DevsyWorkspaceInstance, +) (*managementv1.DevsyWorkspaceInstance, error) { + setTemplateSyncOnce(newInstance) + + return UpdateInstance(ctx, client, oldInstance, newInstance) +} + +// setTemplateSyncOnce requests a one-time template sync. Instances without a template are left unchanged. +func setTemplateSyncOnce(instance *managementv1.DevsyWorkspaceInstance) { + if instance.Spec.TemplateRef != nil { + instance.Spec.TemplateRef.SyncOnce = true + } +} diff --git a/pkg/platform/instance_ops_test.go b/pkg/platform/instance_ops_test.go new file mode 100644 index 0000000000..aee5b8b83f --- /dev/null +++ b/pkg/platform/instance_ops_test.go @@ -0,0 +1,84 @@ +package platform + +import ( + "context" + "errors" + "testing" + + managementv1 "github.com/devsy-org/api/pkg/apis/management/v1" + storagev1 "github.com/devsy-org/api/pkg/apis/storage/v1" + loftclient "github.com/devsy-org/api/pkg/clientset/versioned" + loftfake "github.com/devsy-org/api/pkg/clientset/versioned/fake" + "github.com/devsy-org/devsy/pkg/platform/client" + "github.com/devsy-org/devsy/pkg/platform/kube" + "github.com/stretchr/testify/suite" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/runtime" + k8stesting "k8s.io/client-go/testing" +) + +type instanceOpsSuite struct { + suite.Suite +} + +func TestInstanceOpsSuite(t *testing.T) { + suite.Run(t, new(instanceOpsSuite)) +} + +func (s *instanceOpsSuite) TestSetTemplateSyncOnceSetsFlagWhenTemplateRefPresent() { + instance := &managementv1.DevsyWorkspaceInstance{} + instance.Spec.TemplateRef = &storagev1.TemplateRef{} + + setTemplateSyncOnce(instance) + + s.True(instance.Spec.TemplateRef.SyncOnce) +} + +func (s *instanceOpsSuite) TestSetTemplateSyncOnceLeavesInstanceWithoutTemplateRefUnchanged() { + instance := &managementv1.DevsyWorkspaceInstance{} + + setTemplateSyncOnce(instance) + + s.Nil(instance.Spec.TemplateRef) +} + +func (s *instanceOpsSuite) TestCreateInstanceWrapsCreateError() { + createErr := errors.New("boom") + loft := loftfake.NewSimpleClientset() + loft.PrependReactor( + "create", + "devsyworkspaceinstances", + func(k8stesting.Action) (bool, runtime.Object, error) { + return true, nil, createErr + }, + ) + pc := &instanceOpsPlatformClient{management: &instanceOpsKubeClient{loft: loft}} + instance := &managementv1.DevsyWorkspaceInstance{ + ObjectMeta: metav1.ObjectMeta{Name: "ws", Namespace: "p-default"}, + } + + _, err := CreateInstance(context.Background(), pc, instance) + + s.ErrorIs(err, createErr) + s.EqualError(err, "create workspace instance: boom") +} + +// instanceOpsKubeClient provides only Loft(); other kube.Interface methods are unused by these tests. +type instanceOpsKubeClient struct { + kube.Interface + loft loftclient.Interface +} + +func (c *instanceOpsKubeClient) Loft() loftclient.Interface { + return c.loft +} + +// instanceOpsPlatformClient provides only Management(); other client.Client methods are unused by these tests. +type instanceOpsPlatformClient struct { + client.Client + management kube.Interface +} + +func (c *instanceOpsPlatformClient) Management() (kube.Interface, error) { + return c.management, nil +}