@@ -53,6 +53,8 @@ func NewState(o Output) State {
53
53
// States are immutable, and all operations return a new state linked to the previous one.
54
54
// State is the core type of the LLB API and is used to build a graph of operations.
55
55
// 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.
56
58
type State struct {
57
59
out Output
58
60
prev * State
@@ -127,6 +129,7 @@ func (s State) SetMarshalDefaults(co ...ConstraintsOpt) State {
127
129
return s
128
130
}
129
131
132
+ // Marshal marshals the state and all its parents into a [Definition].
130
133
func (s State ) Marshal (ctx context.Context , co ... ConstraintsOpt ) (* Definition , error ) {
131
134
c := NewConstraints (append (s .opts , co ... )... )
132
135
def := & Definition {
@@ -212,17 +215,21 @@ func marshal(ctx context.Context, v Vertex, def *Definition, s *sourceMapCollect
212
215
return def , nil
213
216
}
214
217
218
+ // Validate validates the state.
219
+ // This validation, unlike most other operations on [State], is not lazily performed.
215
220
func (s State ) Validate (ctx context.Context , c * Constraints ) error {
216
221
return s .Output ().Vertex (ctx , c ).Validate (ctx , c )
217
222
}
218
223
224
+ // Output returns the output of the state.
219
225
func (s State ) Output () Output {
220
226
if s .async != nil {
221
227
return s .async .Output ()
222
228
}
223
229
return s .out
224
230
}
225
231
232
+ // WithOutput creats a new state with the output set to the given output.
226
233
func (s State ) WithOutput (o Output ) State {
227
234
prev := s
228
235
s = State {
@@ -233,6 +240,7 @@ func (s State) WithOutput(o Output) State {
233
240
return s
234
241
}
235
242
243
+ // WithImageConfig adds the environment variables, working directory, and platform specified in the image config to the state.
236
244
func (s State ) WithImageConfig (c []byte ) (State , error ) {
237
245
var img ocispecs.Image
238
246
if err := json .Unmarshal (c , & img ); err != nil {
@@ -259,6 +267,12 @@ func (s State) WithImageConfig(c []byte) (State, error) {
259
267
return s , nil
260
268
}
261
269
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.
262
276
func (s State ) Run (ro ... RunOption ) ExecState {
263
277
ei := & ExecInfo {State : s }
264
278
for _ , o := range ro {
@@ -277,6 +291,8 @@ func (s State) Run(ro ...RunOption) ExecState {
277
291
}
278
292
}
279
293
294
+ // File performs a file operation on the current state.
295
+ // See [FileAction] for details on the operations that can be performed.
280
296
func (s State ) File (a * FileAction , opts ... ConstraintsOpt ) State {
281
297
var c Constraints
282
298
for _ , o := range opts {
@@ -286,21 +302,29 @@ func (s State) File(a *FileAction, opts ...ConstraintsOpt) State {
286
302
return s .WithOutput (NewFileOp (s , a , c ).Output ())
287
303
}
288
304
305
+ // AddEnv returns a new [State] with the provided environment variable set.
306
+ // See [AddEnv]
289
307
func (s State ) AddEnv (key , value string ) State {
290
308
return AddEnv (key , value )(s )
291
309
}
292
310
311
+ // AddEnvf is the same as [State.AddEnv] but with a format string.
293
312
func (s State ) AddEnvf (key , value string , v ... interface {}) State {
294
313
return AddEnvf (key , value , v ... )(s )
295
314
}
296
315
316
+ // Dir returns a new [State] with the provided working directory set.
317
+ // See [Dir]
297
318
func (s State ) Dir (str string ) State {
298
319
return Dir (str )(s )
299
320
}
321
+
322
+ // Dirf is the same as [State.Dir] but with a format string.
300
323
func (s State ) Dirf (str string , v ... interface {}) State {
301
324
return Dirf (str , v ... )(s )
302
325
}
303
326
327
+ // GetEnv returns the value of the environment variable with the provided key.
304
328
func (s State ) GetEnv (ctx context.Context , key string , co ... ConstraintsOpt ) (string , bool , error ) {
305
329
c := & Constraints {}
306
330
for _ , f := range co {
@@ -314,6 +338,8 @@ func (s State) GetEnv(ctx context.Context, key string, co ...ConstraintsOpt) (st
314
338
return v , ok , nil
315
339
}
316
340
341
+ // Env returns a new [State] with the provided environment variable set.
342
+ // See [Env]
317
343
func (s State ) Env (ctx context.Context , co ... ConstraintsOpt ) ([]string , error ) {
318
344
c := & Constraints {}
319
345
for _ , f := range co {
@@ -326,6 +352,7 @@ func (s State) Env(ctx context.Context, co ...ConstraintsOpt) ([]string, error)
326
352
return env .ToArray (), nil
327
353
}
328
354
355
+ // GetDir returns the current working directory for the state.
329
356
func (s State ) GetDir (ctx context.Context , co ... ConstraintsOpt ) (string , error ) {
330
357
c := & Constraints {}
331
358
for _ , f := range co {
@@ -342,18 +369,28 @@ func (s State) GetArgs(ctx context.Context, co ...ConstraintsOpt) ([]string, err
342
369
return getArgs (s )(ctx , c )
343
370
}
344
371
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.
345
376
func (s State ) Reset (s2 State ) State {
346
377
return Reset (s2 )(s )
347
378
}
348
379
380
+ // User sets the user for this state.
381
+ // See [User] for more details.
349
382
func (s State ) User (v string ) State {
350
383
return User (v )(s )
351
384
}
352
385
386
+ // Hostname sets the hostname for this state.
387
+ // See [Hostname] for more details.
353
388
func (s State ) Hostname (v string ) State {
354
389
return Hostname (v )(s )
355
390
}
356
391
392
+ // GetHostname returns the hostname set on the state.
393
+ // See [Hostname] for more details.
357
394
func (s State ) GetHostname (ctx context.Context , co ... ConstraintsOpt ) (string , error ) {
358
395
c := & Constraints {}
359
396
for _ , f := range co {
@@ -362,10 +399,14 @@ func (s State) GetHostname(ctx context.Context, co ...ConstraintsOpt) (string, e
362
399
return getHostname (s )(ctx , c )
363
400
}
364
401
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.
365
405
func (s State ) Platform (p ocispecs.Platform ) State {
366
406
return platform (p )(s )
367
407
}
368
408
409
+ // GetPlatform returns the platform for the state.
369
410
func (s State ) GetPlatform (ctx context.Context , co ... ConstraintsOpt ) (* ocispecs.Platform , error ) {
370
411
c := & Constraints {}
371
412
for _ , f := range co {
@@ -374,21 +415,30 @@ func (s State) GetPlatform(ctx context.Context, co ...ConstraintsOpt) (*ocispecs
374
415
return getPlatform (s )(ctx , c )
375
416
}
376
417
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.
377
421
func (s State ) Network (n pb.NetMode ) State {
378
422
return Network (n )(s )
379
423
}
380
424
425
+ // GetNetwork returns the network mode for the state.
381
426
func (s State ) GetNetwork (ctx context.Context , co ... ConstraintsOpt ) (pb.NetMode , error ) {
382
427
c := & Constraints {}
383
428
for _ , f := range co {
384
429
f .SetConstraintsOption (c )
385
430
}
386
431
return getNetwork (s )(ctx , c )
387
432
}
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.
388
437
func (s State ) Security (n pb.SecurityMode ) State {
389
438
return Security (n )(s )
390
439
}
391
440
441
+ // GetSecurity returns the security mode for the state.
392
442
func (s State ) GetSecurity (ctx context.Context , co ... ConstraintsOpt ) (pb.SecurityMode , error ) {
393
443
c := & Constraints {}
394
444
for _ , f := range co {
@@ -397,21 +447,32 @@ func (s State) GetSecurity(ctx context.Context, co ...ConstraintsOpt) (pb.Securi
397
447
return getSecurity (s )(ctx , c )
398
448
}
399
449
450
+ // With applies [StateOption]s to the [State].
451
+ // Each applied [StateOption] creates a new [State] object with the previous as its parent.
400
452
func (s State ) With (so ... StateOption ) State {
401
453
for _ , o := range so {
402
454
s = o (s )
403
455
}
404
456
return s
405
457
}
406
458
459
+ // AddExtraHost adds a host name to IP mapping to any containers created from this state.
407
460
func (s State ) AddExtraHost (host string , ip net.IP ) State {
408
461
return extraHost (host , ip )(s )
409
462
}
410
463
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.
411
468
func (s State ) AddUlimit (name UlimitName , soft int64 , hard int64 ) State {
412
469
return ulimit (name , soft , hard )(s )
413
470
}
414
471
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.
415
476
func (s State ) WithCgroupParent (cp string ) State {
416
477
return cgroupParent (cp )(s )
417
478
}
0 commit comments