Skip to content

Commit 552e0e7

Browse files
committed
Adding more docs to client/llb
Just some more method/fn documentation for various llb opts/options. Signed-off-by: Brian Goff <[email protected]>
1 parent bc23425 commit 552e0e7

File tree

4 files changed

+92
-0
lines changed

4 files changed

+92
-0
lines changed

client/llb/fileop.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ type capAdder interface {
6161
addCaps(*FileOp)
6262
}
6363

64+
// FileAction is used to specify a file operation on a [State].
65+
// It can be used to create a directory, create a file, or remove a file, etc.
66+
// This is used by [State.File]
67+
// Typically a FileAction is created by calling one of the helper functions such as [Mkdir], [Copy], [Rm], [Mkfile]
6468
type FileAction struct {
6569
state *State
6670
prev *FileAction

client/llb/meta.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,15 @@ var (
2929
keySecurity = contextKeyT("llb.security")
3030
)
3131

32+
// AddEnvf is the same as [AddEnv] but allows for a format string.
33+
// This is the equivalent of `[State.AddEnvf]`
3234
func AddEnvf(key, value string, v ...interface{}) StateOption {
3335
return addEnvf(key, value, true, v...)
3436
}
3537

38+
// AddEnv returns a [StateOption] whichs adds an environment variable to the state.
39+
// Use this with [State.With] to create a new state with the environment variable set.
40+
// This is the equivalent of `[State.AddEnv]`
3641
func AddEnv(key, value string) StateOption {
3742
return addEnvf(key, value, false)
3843
}
@@ -52,10 +57,14 @@ func addEnvf(key, value string, replace bool, v ...interface{}) StateOption {
5257
}
5358
}
5459

60+
// Dir returns a [StateOption] sets the working directory for the state which will be used to resolve
61+
// relative paths as well as the working directory for [State.Run].
62+
// See [State.With] for where to use this.
5563
func Dir(str string) StateOption {
5664
return dirf(str, false)
5765
}
5866

67+
// Dirf is the same as [Dir] but allows for a format string.
5968
func Dirf(str string, v ...interface{}) StateOption {
6069
return dirf(str, true, v...)
6170
}
@@ -81,12 +90,18 @@ func dirf(value string, replace bool, v ...interface{}) StateOption {
8190
}
8291
}
8392

93+
// User returns a [StateOption] which sets the user for the state which will be used by [State.Run].
94+
// This is the equivalent of [State.User]
95+
// See [State.With] for where to use this.
8496
func User(str string) StateOption {
8597
return func(s State) State {
8698
return s.WithValue(keyUser, str)
8799
}
88100
}
89101

102+
// Reset returns a [StateOption] which creates a new [State] with just the
103+
// output of the current [State] and the provided [State] is set as the parent.
104+
// This is the equivalent of [State.Reset]
90105
func Reset(other State) StateOption {
91106
return func(s State) State {
92107
s = NewState(s.Output())
@@ -147,6 +162,9 @@ func getUser(s State) func(context.Context, *Constraints) (string, error) {
147162
}
148163
}
149164

165+
// Hostname returns a [StateOption] which sets the hostname used for containers created by [State.Run].
166+
// This is the equivalent of [State.Hostname]
167+
// See [State.With] for where to use this.
150168
func Hostname(str string) StateOption {
151169
return func(s State) State {
152170
return s.WithValue(keyHostname, str)
@@ -283,6 +301,9 @@ func getCgroupParent(s State) func(context.Context, *Constraints) (string, error
283301
}
284302
}
285303

304+
// Network returns a [StateOption] which sets the network mode used for containers created by [State.Run].
305+
// This is the equivalent of [State.Network]
306+
// See [State.With] for where to use this.
286307
func Network(v pb.NetMode) StateOption {
287308
return func(s State) State {
288309
return s.WithValue(keyNetwork, v)
@@ -302,6 +323,9 @@ func getNetwork(s State) func(context.Context, *Constraints) (pb.NetMode, error)
302323
}
303324
}
304325

326+
// Security returns a [StateOption] which sets the security mode used for containers created by [State.Run].
327+
// This is the equivalent of [State.Security]
328+
// See [State.With] for where to use this.
305329
func Security(v pb.SecurityMode) StateOption {
306330
return func(s State) State {
307331
return s.WithValue(keySecurity, v)

client/llb/source.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,9 @@ type ImageInfo struct {
230230
// Other URL formats are supported such as "[email protected]:moby/buildkit.git", "git://...", "ssh://..."
231231
// Formats that utilize SSH may need to supply credentials as a [GitOption].
232232
// You may need to check the source code for a full list of supported formats.
233+
//
234+
// By default the git repository is cloned with `--depth=1` to reduce the amount of data downloaded.
235+
// Additionally the ".git" directory is removed after the clone, you can keep ith with the [KeepGitDir] [GitOption].
233236
func Git(remote, ref string, opts ...GitOption) State {
234237
url := strings.Split(remote, "#")[0]
235238

client/llb/state.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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.
5658
type 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].
130133
func (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.
215220
func (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.
219225
func (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.
226233
func (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.
236244
func (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.
262276
func (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.
280296
func (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]
289307
func (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.
293312
func (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]
297318
func (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.
300323
func (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.
304328
func (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]
317343
func (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.
329356
func (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.
345376
func (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.
349382
func (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.
353388
func (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.
357394
func (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.
365405
func (s State) Platform(p ocispecs.Platform) State {
366406
return platform(p)(s)
367407
}
368408

409+
// GetPlatform returns the platform for the state.
369410
func (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.
377421
func (s State) Network(n pb.NetMode) State {
378422
return Network(n)(s)
379423
}
380424

425+
// GetNetwork returns the network mode for the state.
381426
func (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.
388437
func (s State) Security(n pb.SecurityMode) State {
389438
return Security(n)(s)
390439
}
391440

441+
// GetSecurity returns the security mode for the state.
392442
func (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.
400452
func (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.
407460
func (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.
411468
func (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.
415476
func (s State) WithCgroupParent(cp string) State {
416477
return cgroupParent(cp)(s)
417478
}

0 commit comments

Comments
 (0)