This is the 15th day of my participation in the November Gwen Challenge. Check out the event details: The last Gwen Challenge 2021

Recall how LLB was used in dockerfile.build:Yuan Xiaobai thought about how to find an example with appropriate difficulty to learn LLB. State after he had a general understanding of LLB.

After a search, is really huang Tian pays off to those who wait, really found:By its name, this test case tests a scenario that is the default platform. throughs := llb.Image("foo").Run(llb.Shlex("bar"))Assemble the build language, based on image – “foo”, run the script “bar”, and clean up the states.MarshalRemember that you can pass in the default platform, so now it’s the default, that’s the default platform that you use; Then use thellbsolver.LoadThis is the first time it appears, and the type returned is solver.Edge, which is the first time it appears, so what does this have to do with the Definition State before? Finally, we compare the default platform with the assembled image ID.

To figure out what role llB. State plays in the process and how it relates all these states together. Yuan Xiaobai is ready to start from the most critical sentence:

s := llb.Image("foo").Run(llb.Shlex("bar"))
Copy the code

After several nights of struggle, the relationship was sorted out:As can be seen from the figure, the association structure between instances is indeed somewhat convoluted.

  • The first half of the sentencellb.Image("foo"), creates a SourceOp, or source operation
  • After half sentence.Run(llb.shlex("bar")), creates an ExecOp to perform the operation
  • The Output object of the SourceOp contains a Vertex that points to the SourceOp itself. The Output object of the SourceOp contains a Vertex that points to the SourceOp itself. What is the relationship between SoruceOp and output? What is the emphasis respectively? ExecOp also implements Output interface, but the corresponding attribute is root. This is because the root file system is required to execute the operation, and SourceOp also has an Output pointing to its Vertex. What is this Vertex that comes up a lot?
  • The most convoluted part is the State structure
    • The first half of the sentence creates S1(State) as shown in figure 1, naming output
    • The Dir(“/”) operation is performed separately on the basis of S1, but instead of generating a new State, the withValuer operation generates S2, whose prev attribute links S1
    • In the last part of the sentence, S3 is not directly generated, but es1(ExecState). Originally, there are different states corresponding to different Op, but in order to associate with the previous State, in fact, ES1 has a corresponding S3. The operation here is the same as S2, with its own State and the previous node S2

So comb down, really clear many, but the overall feeling is still very vague. In order to find some clues, Yuan Xiaobai decided to comb and summarize again from the point of view of the source code, to see if you can get some inspiration.

Next: Take a closer look at the code implementation of Moby Buildkit #16 – Magic llb.State