@@ -53,6 +53,8 @@ func NewState(o Output) State {
5353// States are immutable, and all operations return a new state linked to the previous one.
5454// State is the core type of the LLB API and is used to build a graph of operations.
5555// The graph is then marshaled into a definition that can be executed by a backend (such as buildkitd).
56+ //
57+ // Operations performed on a State are executed lazily after the entire state graph is marshalled and sent to the backend.
5658type State struct {
5759 out Output
5860 prev * State
@@ -127,6 +129,7 @@ func (s State) SetMarshalDefaults(co ...ConstraintsOpt) State {
127129 return s
128130}
129131
132+ // Marshal marshals the state and all its parents into a [Definition].
130133func (s State ) Marshal (ctx context.Context , co ... ConstraintsOpt ) (* Definition , error ) {
131134 c := NewConstraints (append (s .opts , co ... )... )
132135 def := & Definition {
@@ -212,17 +215,21 @@ func marshal(ctx context.Context, v Vertex, def *Definition, s *sourceMapCollect
212215 return def , nil
213216}
214217
218+ // Validate validates the state.
219+ // This validation, unlike most other operations on [State], is not lazily performed.
215220func (s State ) Validate (ctx context.Context , c * Constraints ) error {
216221 return s .Output ().Vertex (ctx , c ).Validate (ctx , c )
217222}
218223
224+ // Output returns the output of the state.
219225func (s State ) Output () Output {
220226 if s .async != nil {
221227 return s .async .Output ()
222228 }
223229 return s .out
224230}
225231
232+ // WithOutput creats a new state with the output set to the given output.
226233func (s State ) WithOutput (o Output ) State {
227234 prev := s
228235 s = State {
@@ -233,6 +240,7 @@ func (s State) WithOutput(o Output) State {
233240 return s
234241}
235242
243+ // WithImageConfig adds the environment variables, working directory, and platform specified in the image config to the state.
236244func (s State ) WithImageConfig (c []byte ) (State , error ) {
237245 var img ocispecs.Image
238246 if err := json .Unmarshal (c , & img ); err != nil {
@@ -259,6 +267,12 @@ func (s State) WithImageConfig(c []byte) (State, error) {
259267 return s , nil
260268}
261269
270+ // Run performs the command specified by the arguments within the contexst of the current [State].
271+ // The command is executed as a container with the [State]'s filesystem as the root filesystem.
272+ // As such any command you run must be present in the [State]'s filesystem.
273+ // Constraints such as [State.Ulimit], [State.ParentCgroup], [State.Network], etc. are applied to the container.
274+ //
275+ // Run is useful when none of the LLB ops are sufficient for the operation that you want to perform.
262276func (s State ) Run (ro ... RunOption ) ExecState {
263277 ei := & ExecInfo {State : s }
264278 for _ , o := range ro {
@@ -277,6 +291,8 @@ func (s State) Run(ro ...RunOption) ExecState {
277291 }
278292}
279293
294+ // File performs a file operation on the current state.
295+ // See [FileAction] for details on the operations that can be performed.
280296func (s State ) File (a * FileAction , opts ... ConstraintsOpt ) State {
281297 var c Constraints
282298 for _ , o := range opts {
@@ -286,21 +302,29 @@ func (s State) File(a *FileAction, opts ...ConstraintsOpt) State {
286302 return s .WithOutput (NewFileOp (s , a , c ).Output ())
287303}
288304
305+ // AddEnv returns a new [State] with the provided environment variable set.
306+ // See [AddEnv]
289307func (s State ) AddEnv (key , value string ) State {
290308 return AddEnv (key , value )(s )
291309}
292310
311+ // AddEnvf is the same as [State.AddEnv] but with a format string.
293312func (s State ) AddEnvf (key , value string , v ... interface {}) State {
294313 return AddEnvf (key , value , v ... )(s )
295314}
296315
316+ // Dir returns a new [State] with the provided working directory set.
317+ // See [Dir]
297318func (s State ) Dir (str string ) State {
298319 return Dir (str )(s )
299320}
321+
322+ // Dirf is the same as [State.Dir] but with a format string.
300323func (s State ) Dirf (str string , v ... interface {}) State {
301324 return Dirf (str , v ... )(s )
302325}
303326
327+ // GetEnv returns the value of the environment variable with the provided key.
304328func (s State ) GetEnv (ctx context.Context , key string , co ... ConstraintsOpt ) (string , bool , error ) {
305329 c := & Constraints {}
306330 for _ , f := range co {
@@ -314,6 +338,8 @@ func (s State) GetEnv(ctx context.Context, key string, co ...ConstraintsOpt) (st
314338 return v , ok , nil
315339}
316340
341+ // Env returns a new [State] with the provided environment variable set.
342+ // See [Env]
317343func (s State ) Env (ctx context.Context , co ... ConstraintsOpt ) ([]string , error ) {
318344 c := & Constraints {}
319345 for _ , f := range co {
@@ -326,6 +352,7 @@ func (s State) Env(ctx context.Context, co ...ConstraintsOpt) ([]string, error)
326352 return env .ToArray (), nil
327353}
328354
355+ // GetDir returns the current working directory for the state.
329356func (s State ) GetDir (ctx context.Context , co ... ConstraintsOpt ) (string , error ) {
330357 c := & Constraints {}
331358 for _ , f := range co {
@@ -342,18 +369,28 @@ func (s State) GetArgs(ctx context.Context, co ...ConstraintsOpt) ([]string, err
342369 return getArgs (s )(ctx , c )
343370}
344371
372+ // Reset is used to return a new [State] with all of the current state and the
373+ // provided [State] as the parent. In effect you can think of this as creating
374+ // a new state with all the output from the current state but reparented to the
375+ // provided state. See [Reset] for more details.
345376func (s State ) Reset (s2 State ) State {
346377 return Reset (s2 )(s )
347378}
348379
380+ // User sets the user for this state.
381+ // See [User] for more details.
349382func (s State ) User (v string ) State {
350383 return User (v )(s )
351384}
352385
386+ // Hostname sets the hostname for this state.
387+ // See [Hostname] for more details.
353388func (s State ) Hostname (v string ) State {
354389 return Hostname (v )(s )
355390}
356391
392+ // GetHostname returns the hostname set on the state.
393+ // See [Hostname] for more details.
357394func (s State ) GetHostname (ctx context.Context , co ... ConstraintsOpt ) (string , error ) {
358395 c := & Constraints {}
359396 for _ , f := range co {
@@ -362,10 +399,14 @@ func (s State) GetHostname(ctx context.Context, co ...ConstraintsOpt) (string, e
362399 return getHostname (s )(ctx , c )
363400}
364401
402+ // Platform sets the platform for the state. Platforms are used to determine
403+ // image variants to pull and run as well as the platform metadata to set on the
404+ // image config.
365405func (s State ) Platform (p ocispecs.Platform ) State {
366406 return platform (p )(s )
367407}
368408
409+ // GetPlatform returns the platform for the state.
369410func (s State ) GetPlatform (ctx context.Context , co ... ConstraintsOpt ) (* ocispecs.Platform , error ) {
370411 c := & Constraints {}
371412 for _ , f := range co {
@@ -374,21 +415,30 @@ func (s State) GetPlatform(ctx context.Context, co ...ConstraintsOpt) (*ocispecs
374415 return getPlatform (s )(ctx , c )
375416}
376417
418+ // Network sets the network mode for the state.
419+ // Network modes are used by [State.Run] to determine the network mode used when running the container.
420+ // Network modes are not applied to image configs.
377421func (s State ) Network (n pb.NetMode ) State {
378422 return Network (n )(s )
379423}
380424
425+ // GetNetwork returns the network mode for the state.
381426func (s State ) GetNetwork (ctx context.Context , co ... ConstraintsOpt ) (pb.NetMode , error ) {
382427 c := & Constraints {}
383428 for _ , f := range co {
384429 f .SetConstraintsOption (c )
385430 }
386431 return getNetwork (s )(ctx , c )
387432}
433+
434+ // Security sets the security mode for the state.
435+ // Security modes are used by [State.Run] to the privileges that processes in the container will run with.
436+ // Security modes are not applied to image configs.
388437func (s State ) Security (n pb.SecurityMode ) State {
389438 return Security (n )(s )
390439}
391440
441+ // GetSecurity returns the security mode for the state.
392442func (s State ) GetSecurity (ctx context.Context , co ... ConstraintsOpt ) (pb.SecurityMode , error ) {
393443 c := & Constraints {}
394444 for _ , f := range co {
@@ -397,21 +447,32 @@ func (s State) GetSecurity(ctx context.Context, co ...ConstraintsOpt) (pb.Securi
397447 return getSecurity (s )(ctx , c )
398448}
399449
450+ // With applies [StateOption]s to the [State].
451+ // Each applied [StateOption] creates a new [State] object with the previous as its parent.
400452func (s State ) With (so ... StateOption ) State {
401453 for _ , o := range so {
402454 s = o (s )
403455 }
404456 return s
405457}
406458
459+ // AddExtraHost adds a host name to IP mapping to any containers created from this state.
407460func (s State ) AddExtraHost (host string , ip net.IP ) State {
408461 return extraHost (host , ip )(s )
409462}
410463
464+ // AddUlimit sets the hard/soft for the given ulimit.
465+ // The ulimit is applied to containers created from this state.
466+ // Ulimits are Linux specific and only applies to containers created from this state such as via `[State.Run]`
467+ // Ulimits do not apply to image configs.
411468func (s State ) AddUlimit (name UlimitName , soft int64 , hard int64 ) State {
412469 return ulimit (name , soft , hard )(s )
413470}
414471
472+ // WithCgroupParent sets the parent cgroup for any containers created from this state.
473+ // This is useful when you want to apply resource constraints to a group of containers.
474+ // Cgroups are Linux specific and only applies to containers created from this state such as via `[State.Run]`
475+ // Cgroups do not apply to image configs.
415476func (s State ) WithCgroupParent (cp string ) State {
416477 return cgroupParent (cp )(s )
417478}
0 commit comments