Skip to content
Closed
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
159 changes: 159 additions & 0 deletions core/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ type Container struct {
// Secrets to expose to the container.
Secrets []ContainerSecret

// Volatile env vars exposed to future execs but not persisted in image config.
VolatileEnv []string

// Sockets to expose to the container.
Sockets []ContainerSocket

Expand Down Expand Up @@ -177,12 +180,25 @@ type ContainerWithSystemEnvVariableLazy struct {
Name string
}

type ContainerWithVolatileVariableLazy struct {
LazyState
Parent dagql.ObjectResult[*Container]
Name string
Value string
}

type ContainerWithoutEnvVariableLazy struct {
LazyState
Parent dagql.ObjectResult[*Container]
Name string
}

type ContainerWithoutVolatileVariableLazy struct {
LazyState
Parent dagql.ObjectResult[*Container]
Name string
}

type ContainerWithLabelLazy struct {
LazyState
Parent dagql.ObjectResult[*Container]
Expand Down Expand Up @@ -510,6 +526,7 @@ type persistedContainerPayload struct {
Services []persistedServiceBinding `json:"services,omitempty"`
DefaultTerminalCmd DefaultTerminalCmdOpts `json:"defaultTerminalCmd"`
SystemEnvNames []string `json:"systemEnvNames,omitempty"`
VolatileEnv []string `json:"volatileEnv,omitempty"`
DefaultArgs bool `json:"defaultArgs,omitempty"`
LazyJSON json.RawMessage `json:"lazyJSON,omitempty"`
}
Expand Down Expand Up @@ -575,11 +592,22 @@ type persistedContainerWithSystemEnvVariableLazy struct {
Name string `json:"name"`
}

type persistedContainerWithVolatileVariableLazy struct {
ParentResultID uint64 `json:"parentResultID"`
Name string `json:"name"`
Value string `json:"value"`
}

type persistedContainerWithoutEnvVariableLazy struct {
ParentResultID uint64 `json:"parentResultID"`
Name string `json:"name"`
}

type persistedContainerWithoutVolatileVariableLazy struct {
ParentResultID uint64 `json:"parentResultID"`
Name string `json:"name"`
}

type persistedContainerWithLabelLazy struct {
ParentResultID uint64 `json:"parentResultID"`
Name string `json:"name"`
Expand Down Expand Up @@ -986,6 +1014,7 @@ func materializeContainerStateFromParent(ctx context.Context, dst *Container, pa
dst.Platform = parent.Self().Platform
dst.Annotations = slices.Clone(parent.Self().Annotations)
dst.Secrets = slices.Clone(parent.Self().Secrets)
dst.VolatileEnv = slices.Clone(parent.Self().VolatileEnv)
dst.Sockets = slices.Clone(parent.Self().Sockets)
dst.ImageRef = parent.Self().ImageRef
dst.Ports = slices.Clone(parent.Self().Ports)
Expand Down Expand Up @@ -1438,6 +1467,7 @@ func (container *Container) EncodePersistedObject(ctx context.Context, cache dag
Services: services,
DefaultTerminalCmd: container.DefaultTerminalCmd,
SystemEnvNames: slices.Clone(container.SystemEnvNames),
VolatileEnv: slices.Clone(container.VolatileEnv),
DefaultArgs: container.DefaultArgs,
}
if container.Lazy != nil {
Expand Down Expand Up @@ -1676,6 +1706,7 @@ func (*Container) DecodePersistedObject(ctx context.Context, dag *dagql.Server,
Services: services,
DefaultTerminalCmd: persisted.DefaultTerminalCmd,
SystemEnvNames: slices.Clone(persisted.SystemEnvNames),
VolatileEnv: slices.Clone(persisted.VolatileEnv),
DefaultArgs: persisted.DefaultArgs,
}
if persisted.Form != persistedContainerFormLazy {
Expand Down Expand Up @@ -1889,13 +1920,21 @@ func expandContainerInput(container *Container, input string, expand bool) (stri
for _, secret := range container.Secrets {
secretEnvs = append(secretEnvs, secret.EnvName)
}
volatileEnvs := []string{}
WalkEnv(container.VolatileEnv, func(name, _, _ string) {
volatileEnvs = append(volatileEnvs, name)
})

var secretEnvFoundError error
expanded := os.Expand(input, func(k string) string {
if slices.Contains(secretEnvs, k) {
secretEnvFoundError = fmt.Errorf("expand cannot be used with secret env variable %q", k)
return ""
}
if slices.Contains(volatileEnvs, k) {
secretEnvFoundError = fmt.Errorf("expand cannot be used with volatile env variable %q", k)
return ""
}

v, _ := LookupEnv(container.Config.Env, k)
return v
Expand Down Expand Up @@ -2364,6 +2403,38 @@ func (lazy *ContainerWithSystemEnvVariableLazy) EncodePersisted(ctx context.Cont
})
}

func (lazy *ContainerWithVolatileVariableLazy) Evaluate(ctx context.Context, container *Container) error {
return lazy.LazyState.Evaluate(ctx, "Container.withVolatileVariable", func(ctx context.Context) error {
if err := materializeContainerStateFromParent(ctx, container, lazy.Parent); err != nil {
return err
}
container.WithVolatileVariable(lazy.Name, lazy.Value)
container.Lazy = nil
return nil
})
}

func (lazy *ContainerWithVolatileVariableLazy) AttachDependencies(ctx context.Context, attach func(dagql.AnyResult) (dagql.AnyResult, error)) ([]dagql.AnyResult, error) {
parent, err := attachContainerResult(attach, lazy.Parent, "attach container withVolatileVariable parent")
if err != nil {
return nil, err
}
lazy.Parent = parent
return []dagql.AnyResult{parent}, nil
}

func (lazy *ContainerWithVolatileVariableLazy) EncodePersisted(ctx context.Context, cache dagql.PersistedObjectCache) (json.RawMessage, error) {
parentID, err := encodePersistedObjectRef(cache, lazy.Parent, "container withVolatileVariable parent")
if err != nil {
return nil, err
}
return json.Marshal(persistedContainerWithVolatileVariableLazy{
ParentResultID: parentID,
Name: lazy.Name,
Value: lazy.Value,
})
}

func (lazy *ContainerWithoutEnvVariableLazy) Evaluate(ctx context.Context, container *Container) error {
return lazy.LazyState.Evaluate(ctx, "Container.withoutEnvVariable", func(ctx context.Context) error {
if err := materializeContainerStateFromParent(ctx, container, lazy.Parent); err != nil {
Expand All @@ -2387,6 +2458,37 @@ func (lazy *ContainerWithoutEnvVariableLazy) Evaluate(ctx context.Context, conta
})
}

func (lazy *ContainerWithoutVolatileVariableLazy) Evaluate(ctx context.Context, container *Container) error {
return lazy.LazyState.Evaluate(ctx, "Container.withoutVolatileVariable", func(ctx context.Context) error {
if err := materializeContainerStateFromParent(ctx, container, lazy.Parent); err != nil {
return err
}
container.WithoutVolatileVariable(lazy.Name)
container.Lazy = nil
return nil
})
}

func (lazy *ContainerWithoutVolatileVariableLazy) AttachDependencies(ctx context.Context, attach func(dagql.AnyResult) (dagql.AnyResult, error)) ([]dagql.AnyResult, error) {
parent, err := attachContainerResult(attach, lazy.Parent, "attach container withoutVolatileVariable parent")
if err != nil {
return nil, err
}
lazy.Parent = parent
return []dagql.AnyResult{parent}, nil
}

func (lazy *ContainerWithoutVolatileVariableLazy) EncodePersisted(ctx context.Context, cache dagql.PersistedObjectCache) (json.RawMessage, error) {
parentID, err := encodePersistedObjectRef(cache, lazy.Parent, "container withoutVolatileVariable parent")
if err != nil {
return nil, err
}
return json.Marshal(persistedContainerWithoutVolatileVariableLazy{
ParentResultID: parentID,
Name: lazy.Name,
})
}

func (lazy *ContainerWithoutEnvVariableLazy) AttachDependencies(ctx context.Context, attach func(dagql.AnyResult) (dagql.AnyResult, error)) ([]dagql.AnyResult, error) {
parent, err := attachContainerResult(attach, lazy.Parent, "attach container withoutEnvVariable parent")
if err != nil {
Expand Down Expand Up @@ -4193,6 +4295,22 @@ func decodePersistedContainerLazy(
Name: persisted.Name,
}
return nil
case "withVolatileVariable":
var persisted persistedContainerWithVolatileVariableLazy
if err := json.Unmarshal(payload, &persisted); err != nil {
return fmt.Errorf("decode persisted container withVolatileVariable lazy payload: %w", err)
}
parent, err := loadPersistedObjectResultByResultID[*Container](ctx, dag, persisted.ParentResultID, "container withVolatileVariable parent")
if err != nil {
return err
}
container.Lazy = &ContainerWithVolatileVariableLazy{
LazyState: NewLazyState(),
Parent: parent,
Name: persisted.Name,
Value: persisted.Value,
}
return nil
case "withoutEnvVariable":
var persisted persistedContainerWithoutEnvVariableLazy
if err := json.Unmarshal(payload, &persisted); err != nil {
Expand All @@ -4208,6 +4326,21 @@ func decodePersistedContainerLazy(
Name: persisted.Name,
}
return nil
case "withoutVolatileVariable":
var persisted persistedContainerWithoutVolatileVariableLazy
if err := json.Unmarshal(payload, &persisted); err != nil {
return fmt.Errorf("decode persisted container withoutVolatileVariable lazy payload: %w", err)
}
parent, err := loadPersistedObjectResultByResultID[*Container](ctx, dag, persisted.ParentResultID, "container withoutVolatileVariable parent")
if err != nil {
return err
}
container.Lazy = &ContainerWithoutVolatileVariableLazy{
LazyState: NewLazyState(),
Parent: parent,
Name: persisted.Name,
}
return nil
case "withLabel":
var persisted persistedContainerWithLabelLazy
if err := json.Unmarshal(payload, &persisted); err != nil {
Expand Down Expand Up @@ -5870,6 +6003,32 @@ func (container *Container) WithSecretVariable(
return container, nil
}

// mutates container caller must have handled cloning or creating a new child.
func (container *Container) WithVolatileVariable(name string, value string) *Container {
container.VolatileEnv = AddEnv(container.VolatileEnv, name, value)

// set image ref to empty string
container.ImageRef = ""

return container
}

// mutates container caller must have handled cloning or creating a new child.
func (container *Container) WithoutVolatileVariable(name string) *Container {
newEnv := []string{}
WalkEnv(container.VolatileEnv, func(k, _, env string) {
if !shell.EqualEnvKeys(k, name) {
newEnv = append(newEnv, env)
}
})
container.VolatileEnv = newEnv

// set image ref to empty string
container.ImageRef = ""

return container
}

// mutates container caller must have handled cloning or creating a new child.
func (container *Container) WithoutSecretVariable(ctx context.Context, name string) (*Container, error) {
for i, secret := range container.Secrets {
Expand Down
83 changes: 75 additions & 8 deletions core/container_exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,24 @@ type ContainerExecLazy struct {
}

type persistedContainerExecLazy struct {
ParentResultID uint64 `json:"parentResultID"`
ModuleContextResultID uint64 `json:"moduleContextResultID,omitempty"`
Opts ContainerExecOpts `json:"opts"`
ExecMD *engineutil.ExecutionMetadata `json:"execMD,omitempty"`
FunctionCall *FunctionCall `json:"functionCall,omitempty"`
ExtractModuleError bool `json:"extractModuleError,omitempty"`
ParentResultID uint64 `json:"parentResultID"`
ModuleContextResultID uint64 `json:"moduleContextResultID,omitempty"`
Opts ContainerExecOpts `json:"opts"`
ExecMD *engineutil.ExecutionMetadata `json:"execMD,omitempty"`
FunctionCall *FunctionCall `json:"functionCall,omitempty"`
ExtractModuleError bool `json:"extractModuleError,omitempty"`
VolatileCacheHitParentResultID uint64 `json:"volatileCacheHitParentResultID,omitempty"`
VolatileCacheHitVolatileEnv []string `json:"volatileCacheHitVolatileEnv,omitempty"`
}

// ContainerVolatileExecCacheHitLazy materializes from a broad volatile exec
// cache hit, then restores the request-local volatile environment for
// descendants of the returned container.
type ContainerVolatileExecCacheHitLazy struct {
LazyState

Parent dagql.ObjectResult[*Container]
VolatileEnv []string
}

func (lazy *ContainerExecLazy) Evaluate(ctx context.Context, ctr *Container) error {
Expand Down Expand Up @@ -169,6 +181,46 @@ func (lazy *ContainerExecLazy) EncodePersisted(ctx context.Context, cache dagql.
})
}

func (lazy *ContainerVolatileExecCacheHitLazy) Evaluate(ctx context.Context, container *Container) error {
if lazy == nil {
return nil
}
return lazy.LazyState.Evaluate(ctx, "Container.withExec.cacheHit", func(ctx context.Context) error {
if err := materializeContainerStateFromParent(ctx, container, lazy.Parent); err != nil {
return err
}
container.VolatileEnv = slices.Clone(lazy.VolatileEnv)
container.Lazy = nil
return nil
})
}

func (lazy *ContainerVolatileExecCacheHitLazy) AttachDependencies(ctx context.Context, attach func(dagql.AnyResult) (dagql.AnyResult, error)) ([]dagql.AnyResult, error) {
if lazy == nil {
return nil, nil
}
parent, err := attachContainerResult(attach, lazy.Parent, "attach container volatile exec cache hit parent")
if err != nil {
return nil, err
}
lazy.Parent = parent
return []dagql.AnyResult{parent}, nil
}

func (lazy *ContainerVolatileExecCacheHitLazy) EncodePersisted(ctx context.Context, cache dagql.PersistedObjectCache) (json.RawMessage, error) {
if lazy == nil {
return nil, fmt.Errorf("encode persisted container volatile exec cache hit lazy: nil lazy")
}
parentID, err := encodePersistedObjectRef(cache, lazy.Parent, "container volatile exec cache hit parent")
if err != nil {
return nil, err
}
return json.Marshal(persistedContainerExecLazy{
VolatileCacheHitParentResultID: parentID,
VolatileCacheHitVolatileEnv: slices.Clone(lazy.VolatileEnv),
})
}

func (container *Container) execMeta(
ctx context.Context,
opts ContainerExecOpts,
Expand Down Expand Up @@ -262,7 +314,7 @@ func (container *Container) execMeta(
return &execMD, nil
}

func (container *Container) metaSpec(ctx context.Context, opts ContainerExecOpts) (*executor.Meta, error) {
func (container *Container) metaSpec(ctx context.Context, opts ContainerExecOpts, includeVolatileEnv bool) (*executor.Meta, error) {
query, err := CurrentQuery(ctx)
if err != nil {
return nil, fmt.Errorf("get current query: %w", err)
Expand Down Expand Up @@ -297,6 +349,9 @@ func (container *Container) metaSpec(ctx context.Context, opts ContainerExecOpts
}

metaSpec.Env = addDefaultEnvvar(metaSpec.Env, "PATH", utilsystem.DefaultPathEnv(platform.OS))
if includeVolatileEnv {
metaSpec.Env = mergeEnv(metaSpec.Env, container.VolatileEnv)
}

if opts.Expect != ReturnSuccess {
metaSpec.ValidExitCodes = opts.Expect.ReturnCodes()
Expand Down Expand Up @@ -1224,7 +1279,7 @@ func (state *ContainerExecState) Evaluate(ctx context.Context, container *Contai

cache := query.SnapshotManager()

metaSpec, err := container.metaSpec(ctx, opts)
metaSpec, err := container.metaSpec(ctx, opts, true)
if err != nil {
return err
}
Expand Down Expand Up @@ -2143,6 +2198,18 @@ func decodePersistedContainerExecLazy(
if err := json.Unmarshal(payload, &persisted); err != nil {
return fmt.Errorf("decode persisted container withExec lazy payload: %w", err)
}
if persisted.VolatileCacheHitParentResultID != 0 {
parent, err := loadPersistedObjectResultByResultID[*Container](ctx, dag, persisted.VolatileCacheHitParentResultID, "container volatile exec cache hit parent")
if err != nil {
return err
}
container.Lazy = &ContainerVolatileExecCacheHitLazy{
LazyState: NewLazyState(),
Parent: parent,
VolatileEnv: slices.Clone(persisted.VolatileCacheHitVolatileEnv),
}
return nil
}
parent, err := loadPersistedObjectResultByResultID[*Container](ctx, dag, persisted.ParentResultID, "container exec parent")
if err != nil {
return err
Expand Down
Loading