Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 2 additions & 23 deletions cmd/pro/provider/create/workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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)
}
4 changes: 1 addition & 3 deletions cmd/pro/provider/up.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
23 changes: 7 additions & 16 deletions cmd/pro/provider/update/workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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)
}
4 changes: 1 addition & 3 deletions pkg/client/clientimplementation/daemonclient/up.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
43 changes: 7 additions & 36 deletions pkg/daemon/platform/local_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
51 changes: 51 additions & 0 deletions pkg/platform/instance_ops.go
Original file line number Diff line number Diff line change
@@ -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
}
}
84 changes: 84 additions & 0 deletions pkg/platform/instance_ops_test.go
Original file line number Diff line number Diff line change
@@ -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
}
Loading