Skip to content

Commit 6ee183e

Browse files
committed
fix(sdk/go): apply functional options to CreateFromTemplate
Extend the WithLabels/WithAnnotations pattern to the CreateFromTemplate and CreateSandboxFromTemplate methods added by upstream. Updates all call sites including template tests. Signed-off-by: Roland Huß <rhuss@redhat.com>
1 parent e9e75bd commit 6ee183e

8 files changed

Lines changed: 19 additions & 24 deletions

File tree

sdk/go/openshell/v1/client.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ type Config = types.Config
2020
type ClientInterface interface {
2121
Sandboxes() SandboxInterface
2222
SandboxTemplates() SandboxTemplateInterface
23-
CreateSandboxFromTemplate(ctx context.Context, workspace, name, templateName string, spec *SandboxSpec, labels map[string]string, opts ...CreateOptions) (*Sandbox, error)
23+
CreateSandboxFromTemplate(ctx context.Context, workspace, name, templateName string, spec *SandboxSpec, opts ...CreateOption) (*Sandbox, error)
2424
Providers() ProviderInterface
2525
Services() ServiceInterface
2626
Exec() ExecInterface
@@ -125,8 +125,8 @@ func (c *Client) SandboxTemplates() SandboxTemplateInterface { return c.template
125125

126126
// CreateSandboxFromTemplate creates a sandbox from a named workload template
127127
// without changing the legacy Sandboxes() interface.
128-
func (c *Client) CreateSandboxFromTemplate(ctx context.Context, workspace, name, templateName string, spec *SandboxSpec, labels map[string]string, opts ...CreateOptions) (*Sandbox, error) {
129-
return c.templateCreate.CreateFromTemplate(ctx, workspace, name, templateName, spec, labels, opts...)
128+
func (c *Client) CreateSandboxFromTemplate(ctx context.Context, workspace, name, templateName string, spec *SandboxSpec, opts ...CreateOption) (*Sandbox, error) {
129+
return c.templateCreate.CreateFromTemplate(ctx, workspace, name, templateName, spec, opts...)
130130
}
131131

132132
// Providers returns the provider sub-client.

sdk/go/openshell/v1/fake/fake.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,8 @@ func (fc *Client) SandboxTemplates() v1.SandboxTemplateInterface { return fc.tem
120120

121121
// CreateSandboxFromTemplate creates a sandbox from a named workload template
122122
// without changing the legacy Sandboxes() interface.
123-
func (fc *Client) CreateSandboxFromTemplate(ctx context.Context, workspace, name, templateName string, spec *types.SandboxSpec, labels map[string]string, opts ...types.CreateOptions) (*types.Sandbox, error) {
124-
return fc.templateCreate.CreateFromTemplate(ctx, workspace, name, templateName, spec, labels, opts...)
123+
func (fc *Client) CreateSandboxFromTemplate(ctx context.Context, workspace, name, templateName string, spec *types.SandboxSpec, opts ...types.CreateOption) (*types.Sandbox, error) {
124+
return fc.templateCreate.CreateFromTemplate(ctx, workspace, name, templateName, spec, opts...)
125125
}
126126

127127
// Providers returns the provider sub-client.

sdk/go/openshell/v1/fake/sandbox.go

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ func (c *fakeSandboxClient) Create(_ context.Context, workspace, name string, sp
324324
}
325325

326326
// CreateFromTemplate creates a new sandbox from a named template with Provisioning phase.
327-
func (c *fakeSandboxClient) CreateFromTemplate(_ context.Context, workspace, name, templateName string, spec *types.SandboxSpec, labels map[string]string, opts ...types.CreateOptions) (*types.Sandbox, error) {
327+
func (c *fakeSandboxClient) CreateFromTemplate(_ context.Context, workspace, name, templateName string, spec *types.SandboxSpec, opts ...types.CreateOption) (*types.Sandbox, error) {
328328
if c.closedFunc() {
329329
return nil, &types.StatusError{Code: types.ErrorUnavailable, Message: "client is closed"}
330330
}
@@ -342,10 +342,7 @@ func (c *fakeSandboxClient) CreateFromTemplate(_ context.Context, workspace, nam
342342
spec = &types.SandboxSpec{}
343343
}
344344

345-
var annotations map[string]string
346-
if len(opts) > 0 {
347-
annotations = copyStringMap(opts[0].Annotations)
348-
}
345+
cfg := types.ApplyCreateOptions(opts)
349346

350347
resolvedSpec := sandboxSpecFromWorkloadTemplate(template)
351348
resolvedSpec.Providers = copyStringSlice(spec.Providers)
@@ -357,8 +354,8 @@ func (c *fakeSandboxClient) CreateFromTemplate(_ context.Context, workspace, nam
357354
Name: name,
358355
Workspace: workspace,
359356
CreatedAt: time.Now(),
360-
Labels: copyStringMap(labels),
361-
Annotations: annotations,
357+
Labels: copyStringMap(cfg.Labels()),
358+
Annotations: copyStringMap(cfg.Annotations()),
362359
ResourceVersion: 1,
363360
Spec: resolvedSpec,
364361
CreatedFromWorkloadTemplate: &types.SandboxWorkloadTemplateProvenance{

sdk/go/openshell/v1/fake/sandbox_template_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ func TestSandboxTemplate_CreateSandboxFromTemplateResolvesWorkloadAndGovernance(
245245
Providers: []string{"github"},
246246
Policy: policy,
247247
},
248-
map[string]string{"team": "runtime"},
248+
types.WithLabels(map[string]string{"team": "runtime"}),
249249
)
250250

251251
require.NoError(t, err)
@@ -358,7 +358,6 @@ func TestSandboxTemplate_CreateSandboxFromTemplatePreservesDefaultGPURequest(t *
358358
"job-default-gpu",
359359
"default-gpu",
360360
nil,
361-
nil,
362361
)
363362

364363
require.NoError(t, err)

sdk/go/openshell/v1/fake/sandbox_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -932,7 +932,7 @@ func TestFakeSandboxCreateFromTemplatePreservesCommandAndTTY(t *testing.T) {
932932
TTY: true,
933933
}
934934

935-
created, err := sc.CreateFromTemplate(ctx, "default", "job-1", "gpu-kata", spec, map[string]string{"team": "runtime"})
935+
created, err := sc.CreateFromTemplate(ctx, "default", "job-1", "gpu-kata", spec, types.WithLabels(map[string]string{"team": "runtime"}))
936936

937937
require.NoError(t, err)
938938
assert.Equal(t, []string{"/opt/worker", "--serve"}, created.Spec.Command)

sdk/go/openshell/v1/sandbox.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,5 +71,5 @@ type SandboxInterface interface {
7171
// SandboxTemplateCreateInterface defines additive sandbox creation from named
7272
// workload templates without widening SandboxInterface.
7373
type SandboxTemplateCreateInterface interface {
74-
CreateFromTemplate(ctx context.Context, workspace, name, templateName string, spec *SandboxSpec, labels map[string]string, opts ...CreateOptions) (*Sandbox, error)
74+
CreateFromTemplate(ctx context.Context, workspace, name, templateName string, spec *SandboxSpec, opts ...CreateOption) (*Sandbox, error)
7575
}

sdk/go/openshell/v1/sandbox_client.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ func (s *sandboxClient) Create(ctx context.Context, workspace, name string, spec
4848
return converter.SandboxFromProto(resp.GetSandbox()), nil
4949
}
5050

51-
func (s *sandboxClient) CreateFromTemplate(ctx context.Context, workspace, name, templateName string, spec *SandboxSpec, labels map[string]string, opts ...CreateOptions) (*Sandbox, error) {
51+
func (s *sandboxClient) CreateFromTemplate(ctx context.Context, workspace, name, templateName string, spec *SandboxSpec, opts ...CreateOption) (*Sandbox, error) {
5252
if templateName == "" {
5353
return nil, &StatusError{Code: ErrorInvalidArgument, Message: "template name is required"}
5454
}
@@ -59,16 +59,15 @@ func (s *sandboxClient) CreateFromTemplate(ctx context.Context, workspace, name,
5959
if err != nil {
6060
return nil, &StatusError{Code: ErrorInvalidArgument, Message: err.Error()}
6161
}
62+
cfg := types.ApplyCreateOptions(opts)
6263
req := &pb.CreateSandboxRequest{
6364
Name: name,
6465
Spec: protoSpec,
65-
Labels: labels,
66+
Labels: converter.CopyStringMap(cfg.Labels()),
67+
Annotations: converter.CopyStringMap(cfg.Annotations()),
6668
Workspace: workspace,
6769
WorkloadTemplateName: templateName,
6870
}
69-
if len(opts) > 0 {
70-
req.Annotations = converter.CopyStringMap(opts[0].Annotations)
71-
}
7271
resp, err := s.client.CreateSandbox(ctx, req)
7372
if err != nil {
7473
return nil, converter.FromGRPCError(err)

sdk/go/openshell/v1/sandbox_client_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ func TestSandboxCreate_DefaultGPURequest(t *testing.T) {
274274

275275
result, err := client.Create(context.Background(), "default", "gpu-sandbox", &SandboxSpec{
276276
GPU: true,
277-
}, nil)
277+
})
278278

279279
require.NoError(t, err)
280280
require.NotNil(t, result)
@@ -312,7 +312,7 @@ func TestSandboxCreateFromTemplateRejectsGPUOverrideBeforeRPC(t *testing.T) {
312312

313313
_, err := client.CreateFromTemplate(context.Background(), "default", "bad", "gpu-kata", &SandboxSpec{
314314
GPU: true,
315-
}, nil)
315+
})
316316

317317
require.Error(t, err)
318318
assert.True(t, IsInvalidArgument(err))
@@ -330,7 +330,7 @@ func TestSandboxCreateFromTemplateSendsCommandAndTTY(t *testing.T) {
330330
Providers: []string{"github"},
331331
Command: []string{"/opt/worker", "--serve"},
332332
TTY: true,
333-
}, map[string]string{"team": "runtime"})
333+
}, WithLabels(map[string]string{"team": "runtime"}))
334334

335335
require.NoError(t, err)
336336
require.NotNil(t, result)

0 commit comments

Comments
 (0)