Skip to content

Commit 77e4c5a

Browse files
authored
Merge pull request moby#3874 from cpuguy83/llb_docs
Add some doc strings for LLB functions
2 parents 3c3a6e9 + 206b3a1 commit 77e4c5a

File tree

7 files changed

+96
-0
lines changed

7 files changed

+96
-0
lines changed

client/llb/diff.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ func (m *DiffOp) Inputs() (out []Output) {
9090
return out
9191
}
9292

93+
// Diff returns a state that represents the diff of the lower and upper states.
94+
// The returned State is useful for use with [Merge] where you can merge the lower state with the diff.
9395
func Diff(lower, upper State, opts ...ConstraintsOpt) State {
9496
if lower.Output() == nil {
9597
if upper.Output() == nil {

client/llb/exec.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -649,6 +649,7 @@ type SSHInfo struct {
649649
Optional bool
650650
}
651651

652+
// AddSecret is a RunOption that adds a secret to the exec.
652653
func AddSecret(dest string, opts ...SecretOption) RunOption {
653654
return runOptionFunc(func(ei *ExecInfo) {
654655
s := &SecretInfo{ID: dest, Target: dest, Mode: 0400}
@@ -696,6 +697,7 @@ func SecretAsEnv(v bool) SecretOption {
696697
})
697698
}
698699

700+
// SecretFileOpt sets the secret's target file uid, gid and permissions.
699701
func SecretFileOpt(uid, gid, mode int) SecretOption {
700702
return secretOptionFunc(func(si *SecretInfo) {
701703
si.UID = uid
@@ -704,12 +706,15 @@ func SecretFileOpt(uid, gid, mode int) SecretOption {
704706
})
705707
}
706708

709+
// ReadonlyRootFS sets the execs's root filesystem to be read-only.
707710
func ReadonlyRootFS() RunOption {
708711
return runOptionFunc(func(ei *ExecInfo) {
709712
ei.ReadonlyRootFS = true
710713
})
711714
}
712715

716+
// WithProxy is a RunOption that sets the proxy environment variables in the resulting exec.
717+
// For example `HTTP_PROXY` is a standard environment variable for unix systems that programs may read.
713718
func WithProxy(ps ProxyEnv) RunOption {
714719
return runOptionFunc(func(ei *ExecInfo) {
715720
ei.ProxyEnv = &ps

client/llb/fileop.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ func NewFileOp(s State, action *FileAction, c Constraints) *FileOp {
4848
}
4949

5050
// CopyInput is either llb.State or *FileActionWithState
51+
// It is used by [Copy] to to specify the source of the copy operation.
5152
type CopyInput interface {
5253
isFileOpCopyInput()
5354
}
@@ -131,6 +132,10 @@ type fileActionWithState struct {
131132

132133
func (fas *fileActionWithState) isFileOpCopyInput() {}
133134

135+
// Mkdir creates a FileAction which creates a directory at the given path.
136+
// Example:
137+
//
138+
// llb.Scratch().File(llb.Mkdir("/foo", 0755))
134139
func Mkdir(p string, m os.FileMode, opt ...MkdirOption) *FileAction {
135140
var mi MkdirInfo
136141
for _, o := range opt {
@@ -181,6 +186,7 @@ func (fn mkdirOptionFunc) SetMkdirOption(mi *MkdirInfo) {
181186

182187
var _ MkdirOption = &MkdirInfo{}
183188

189+
// WithParents is an option for Mkdir which creates parent directories if they do not exist.
184190
func WithParents(b bool) MkdirOption {
185191
return mkdirOptionFunc(func(mi *MkdirInfo) {
186192
mi.MakeParents = b
@@ -282,6 +288,10 @@ func (up *UserOpt) marshal(base pb.InputIndex) *pb.UserOpt {
282288
return &pb.UserOpt{User: &pb.UserOpt_ByID{ByID: uint32(up.UID)}}
283289
}
284290

291+
// Mkfile creates a FileAction which creates a file at the given path with the provided contents.
292+
// Example:
293+
//
294+
// llb.Scratch().File(llb.Mkfile("/foo", 0644, []byte("hello world!")))
285295
func Mkfile(p string, m os.FileMode, dt []byte, opts ...MkfileOption) *FileAction {
286296
var mi MkfileInfo
287297
for _, o := range opts {
@@ -332,6 +342,10 @@ func (a *fileActionMkfile) toProtoAction(ctx context.Context, parent string, bas
332342
}, nil
333343
}
334344

345+
// Rm creates a FileAction which removes a file or directory at the given path.
346+
// Example:
347+
//
348+
// llb.Scratch().File(Mkfile("/foo", 0644, []byte("not around for long..."))).File(llb.Rm("/foo"))
335349
func Rm(p string, opts ...RmOption) *FileAction {
336350
var mi RmInfo
337351
for _, o := range opts {
@@ -394,6 +408,25 @@ func (a *fileActionRm) toProtoAction(ctx context.Context, parent string, base pb
394408
}, nil
395409
}
396410

411+
// Copy produces a FileAction which copies a file or directory from the source to the destination.
412+
// The "input" parameter is the contents to copy from.
413+
// "src" is the path to copy from within the "input".
414+
// "dest" is the path to copy to within the destination (the state being operated on).
415+
// See [CopyInput] for the valid types of input.
416+
//
417+
// Example:
418+
//
419+
// st := llb.Local(".")
420+
// llb.Scratch().File(llb.Copy(st, "/foo", "/bar"))
421+
//
422+
// The example copies the local (client) directory "./foo" to a new empty directory at /bar.
423+
//
424+
// Note: Copying directories can have different behavior based on if the destination exists or not.
425+
// When the destination already exists, the contents of the source directory is copied underneath the destination, including the directory itself.
426+
// You may need to supply a copy option to copy the dir contents only.
427+
// You may also need to pass in a [CopyOption] which creates parent directories if they do not exist.
428+
//
429+
// See [CopyOption] for more details on what options are available.
397430
func Copy(input CopyInput, src, dest string, opts ...CopyOption) *FileAction {
398431
var state *State
399432
var fas *fileActionWithState

client/llb/merge.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,31 @@ func (m *MergeOp) Inputs() []Output {
7070
return m.inputs
7171
}
7272

73+
// Merge merges multiple states into a single state. This is useful in
74+
// conjunction with [Diff] to create set of patches which are independent of
75+
// each other to a base state without affecting the cache of other merged
76+
// states.
77+
// As an example, lets say you have a rootfs with the following directories:
78+
//
79+
// / /bin /etc /opt /tmp
80+
//
81+
// Now lets say you want to copy a directory /etc/foo from one state and a
82+
// binary /bin/bar from another state.
83+
// [Copy] makes a duplicate of file on top of another directory.
84+
// Merge creates a directory whose contents is an overlay of 2 states on top of each other.
85+
//
86+
// With "Merge" you can do this:
87+
//
88+
// fooState := Diff(rootfs, fooState)
89+
// barState := Diff(rootfs, barState)
90+
//
91+
// Then merge the results with:
92+
//
93+
// Merge(rootfs, fooDiff, barDiff)
94+
//
95+
// The resulting state will have both /etc/foo and /bin/bar, but because Merge
96+
// was used, changing the contents of "fooDiff" does not require copying
97+
// "barDiff" again.
7398
func Merge(inputs []State, opts ...ConstraintsOpt) State {
7499
// filter out any scratch inputs, which have no effect when merged
75100
var filteredInputs []State

client/llb/source.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,10 @@ func (s *SourceOp) Inputs() []Output {
9191
return nil
9292
}
9393

94+
// Image returns a state that represents a docker image in a registry.
95+
// Example:
96+
//
97+
// st := llb.Image("busybox:latest")
9498
func Image(ref string, opts ...ImageOption) State {
9599
r, err := reference.ParseNormalizedNamed(ref)
96100
if err == nil {
@@ -215,6 +219,17 @@ type ImageInfo struct {
215219
RecordType string
216220
}
217221

222+
// Git returns a state that represents a git repository.
223+
// Example:
224+
//
225+
// st := llb.Git("https://github.com/moby/buildkit.git#v0.11.6")
226+
//
227+
// The example fetches the v0.11.6 tag of the buildkit repository.
228+
// You can also use a commit hash or a branch name.
229+
//
230+
// Other URL formats are supported such as "[email protected]:moby/buildkit.git", "git://...", "ssh://..."
231+
// Formats that utilize SSH may need to supply credentials as a [GitOption].
232+
// You may need to check the source code for a full list of supported formats.
218233
func Git(remote, ref string, opts ...GitOption) State {
219234
url := strings.Split(remote, "#")[0]
220235

@@ -346,10 +361,12 @@ func MountSSHSock(sshID string) GitOption {
346361
})
347362
}
348363

364+
// Scratch returns a state that represents an empty filesystem.
349365
func Scratch() State {
350366
return NewState(nil)
351367
}
352368

369+
// Local returns a state that represents a directory local to the client.
353370
func Local(name string, opts ...LocalOption) State {
354371
gi := &LocalInfo{}
355372

client/llb/sourcemap.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,16 @@ import (
77
digest "github.com/opencontainers/go-digest"
88
)
99

10+
// SourceMap maps a source file/location to an LLB state/definition.
11+
// SourceMaps are used to provide information for debugging and helpful error messages to the user.
12+
// As an example, lets say you have a Dockerfile with the following content:
13+
//
14+
// FROM alpine
15+
// RUN exit 1
16+
//
17+
// When the "RUN" statement exits with a non-zero exit code buildkit will treat
18+
// it as an error and is able to provide the user with a helpful error message
19+
// pointing to exactly the line in the Dockerfile that caused the error.
1020
type SourceMap struct {
1121
State *State
1222
Definition *Definition

client/llb/state.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ func NewState(o Output) State {
4949
return s
5050
}
5151

52+
// State represents all operations that must be done to produce a given output.
53+
// States are immutable, and all operations return a new state linked to the previous one.
54+
// State is the core type of the LLB API and is used to build a graph of operations.
55+
// The graph is then marshaled into a definition that can be executed by a backend (such as buildkitd).
5256
type State struct {
5357
out Output
5458
prev *State

0 commit comments

Comments
 (0)