mirror of
https://github.com/psviderski/uncloud.git
synced 2026-10-06 21:29:02 +00:00
feat(runtime-templates): add support for runtime templates in bind mounts (#412)
This commit is contained in:
1 parent
88dda435e7
commit
5c33e4be86
17 files changed
+571
-10
No files matched your search
@@ -538,6 +538,15 @@ func (s *Server) CreateServiceContainer(
|
||||
containerName = fmt.Sprintf("%s-%s", spec.Name, suffix)
|
||||
}
|
||||
|
||||
desiredSpec := spec
|
||||
renderedSpec, renderErr := desiredSpec.RenderRuntimeTemplates(api.RuntimeTemplateContext{
|
||||
Container: api.RuntimeTemplateContainerContext{Name: containerName},
|
||||
})
|
||||
if renderErr != nil {
|
||||
return nil, status.Errorf(codes.InvalidArgument, "render runtime templates: %v", renderErr)
|
||||
}
|
||||
spec = renderedSpec
|
||||
|
||||
envVars := maps.Clone(spec.Container.Env)
|
||||
if envVars == nil {
|
||||
envVars = make(api.EnvVars)
|
||||
@@ -745,7 +754,7 @@ func (s *Server) CreateServiceContainer(
|
||||
_ = s.client.ContainerRemove(ctx, resp.ID, container.RemoveOptions{RemoveVolumes: true})
|
||||
}
|
||||
|
||||
specBytes, err := json.Marshal(spec)
|
||||
specBytes, err := json.Marshal(desiredSpec)
|
||||
if err != nil {
|
||||
removeContainer()
|
||||
return nil, status.Errorf(codes.Internal, "marshal service spec: %v", err)
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
package docker
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/psviderski/uncloud/api/pb"
|
||||
"github.com/psviderski/uncloud/pkg/api"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/status"
|
||||
_ "modernc.org/sqlite"
|
||||
)
|
||||
|
||||
func TestCreateServiceContainer_RejectsRuntimeTemplateError(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
desiredSpec := api.ServiceSpec{
|
||||
Name: "web",
|
||||
Container: api.ContainerSpec{
|
||||
Image: "busybox:latest",
|
||||
VolumeMounts: []api.VolumeMount{
|
||||
{VolumeName: "data", ContainerPath: "/data"},
|
||||
},
|
||||
},
|
||||
Volumes: []api.VolumeSpec{
|
||||
{
|
||||
Name: "data",
|
||||
Type: api.VolumeTypeBind,
|
||||
BindOptions: &api.BindOptions{
|
||||
// Early validation takes the valid branch. Rendering with the real container name takes the invalid one.
|
||||
HostPath: `/host/{{if eq .Container.Name "web-a1b2"}}{{.Container.ID}}{{else}}valid{{end}}`,
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
specJSON, err := json.Marshal(desiredSpec)
|
||||
require.NoError(t, err)
|
||||
|
||||
server := &Server{}
|
||||
_, err = server.CreateServiceContainer(context.Background(), &pb.CreateServiceContainerRequest{
|
||||
ServiceId: strings.Repeat("a", 32),
|
||||
ServiceSpec: specJSON,
|
||||
ContainerName: "web-a1b2",
|
||||
})
|
||||
|
||||
require.Error(t, err)
|
||||
assert.Equal(t, codes.InvalidArgument, status.Code(err))
|
||||
assert.ErrorContains(t, err, "render runtime template in bind volume 'data' host path")
|
||||
}
|
||||
Reference in new issue
Block a user