-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy pathdockerfile.go
More file actions
464 lines (429 loc) · 14.3 KB
/
Copy pathdockerfile.go
File metadata and controls
464 lines (429 loc) · 14.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
package languages
import (
"fmt"
"strconv"
"strings"
"github.com/zzet/gortex/internal/graph"
"github.com/zzet/gortex/internal/parser"
sitter "github.com/zzet/gortex/internal/parser/tsitter"
"github.com/zzet/gortex/internal/parser/tsitter/dockerfile"
)
// DockerfileExtractor extracts Dockerfile files into graph nodes and
// edges. The extractor produces a graph layer for container builds:
//
// - FROM creates a KindImage stage (per `AS` alias, defaulting to
// the file path when no alias is declared) and a KindImage node
// for the parent base image. EdgeDependsOn links the stage to its
// base. EdgeImports is kept alongside for back-compat with consumers
// that walk import edges.
// - ENV emits both a KindVariable (legacy surface) and a KindConfigKey
// `cfg::env::<NAME>` matching the convention used by os.Getenv /
// os.environ extractors. EdgeUsesEnv from the stage Image to the
// ConfigKey wires the cross-ref between infra-side declaration and
// code-side consumption.
// - ARG emits a KindVariable + KindConfigKey scoped as a build arg
// (Meta["scope"]="build_arg").
// - EXPOSE emits EdgeExposes from the stage Image to a synthetic
// port endpoint `port::<proto>::<n>`.
// - RUN/CMD/ENTRYPOINT/COPY/VOLUME/USER/WORKDIR are kept as
// instruction nodes (RUN as KindFunction, others as KindVariable).
type DockerfileExtractor struct {
lang *sitter.Language
}
func NewDockerfileExtractor() *DockerfileExtractor {
return &DockerfileExtractor{lang: dockerfile.GetLanguage()}
}
func (e *DockerfileExtractor) Language() string { return "dockerfile" }
func (e *DockerfileExtractor) Extensions() []string {
return []string{".dockerfile", "Dockerfile", "Containerfile"}
}
// dockerfileState carries the per-extraction "current stage" cursor.
// Each FROM begins a new stage; ENV/ARG/EXPOSE bind to whichever stage
// is currently in scope. The very first instruction in the file may be
// ARG (for global build args) before any FROM — those are bound to the
// file node directly.
type dockerfileState struct {
stageID string
stageIndex int
hasStage bool
}
func (e *DockerfileExtractor) Extract(filePath string, src []byte) (*parser.ExtractionResult, error) {
tree, err := parser.ParseFile(src, e.lang)
if err != nil {
return nil, err
}
defer tree.Close()
root := tree.RootNode()
result := &parser.ExtractionResult{}
fileNode := &graph.Node{
ID: filePath, Kind: graph.KindFile, Name: filePath,
FilePath: filePath, StartLine: 1, EndLine: int(root.EndPoint().Row) + 1,
Language: "dockerfile",
}
result.Nodes = append(result.Nodes, fileNode)
st := &dockerfileState{}
e.walk(root, src, filePath, fileNode.ID, result, st)
return result, nil
}
func (e *DockerfileExtractor) walk(node *sitter.Node, src []byte, filePath, fileID string, result *parser.ExtractionResult, st *dockerfileState) {
if node == nil {
return
}
nodeType := node.Type()
switch nodeType {
case "from_instruction":
e.extractFrom(node, src, filePath, fileID, result, st)
case "env_instruction":
e.extractEnvArg(node, src, filePath, fileID, result, "ENV", st)
case "arg_instruction":
e.extractEnvArg(node, src, filePath, fileID, result, "ARG", st)
case "expose_instruction":
e.extractExpose(node, src, filePath, fileID, result, st)
case "run_instruction", "cmd_instruction", "entrypoint_instruction", "copy_instruction":
e.extractInstruction(node, src, filePath, fileID, result, nodeType)
}
for i := 0; i < int(node.ChildCount()); i++ {
child := node.Child(i)
if child != nil {
e.walk(child, src, filePath, fileID, result, st)
}
}
}
// extractFrom parses a `FROM <image>[:tag][@digest] [AS <stage>]` line.
// It emits a KindImage node for the parent image, a KindImage node for
// the local stage (using the AS alias when present, otherwise an
// implicit name based on the stage index), and an EdgeDependsOn edge
// from the stage to its base. EdgeImports is preserved so consumers
// that walk import edges keep working.
func (e *DockerfileExtractor) extractFrom(node *sitter.Node, src []byte, filePath, fileID string, result *parser.ExtractionResult, st *dockerfileState) {
line := int(node.StartPoint().Row) + 1
endLine := int(node.EndPoint().Row) + 1
imageName := ""
for i := 0; i < int(node.ChildCount()); i++ {
child := node.Child(i)
if child == nil {
continue
}
if child.Type() == "image_spec" && imageName == "" {
imageName = strings.TrimSpace(child.Content(src))
}
}
rawText := strings.TrimSpace(node.Content(src))
imageName, alias := parseFromText(rawText, imageName)
if imageName == "" {
return
}
stageName := alias
if stageName == "" {
stageName = fmt.Sprintf("stage-%d", st.stageIndex)
}
stageID := imageStageID(filePath, stageName)
stageMeta := map[string]any{
"role": "stage",
"index": st.stageIndex,
}
if alias != "" {
stageMeta["alias"] = alias
} else {
stageMeta["is_default_stage"] = true
}
stageMeta["base_image"] = imageName
result.Nodes = append(result.Nodes, &graph.Node{
ID: stageID, Kind: graph.KindImage, Name: stageName,
FilePath: filePath, StartLine: line, EndLine: endLine,
Language: "dockerfile",
Meta: stageMeta,
})
result.Edges = append(result.Edges, &graph.Edge{
From: fileID, To: stageID, Kind: graph.EdgeDefines,
FilePath: filePath, Line: line,
})
st.stageID = stageID
st.stageIndex++
st.hasStage = true
baseID := imageNodeID(imageName)
imageRef, tag := splitImageRef(imageName)
result.Nodes = append(result.Nodes, &graph.Node{
ID: baseID, Kind: graph.KindImage, Name: imageName,
FilePath: filePath, StartLine: line, EndLine: endLine,
Language: "dockerfile",
Meta: map[string]any{
"role": "base",
"ref": imageRef,
"tag": tag,
},
})
result.Edges = append(result.Edges, &graph.Edge{
From: stageID, To: baseID, Kind: graph.EdgeDependsOn,
FilePath: filePath, Line: line,
})
result.Edges = append(result.Edges, &graph.Edge{
From: fileID,
To: "unresolved::import::" + imageName,
Kind: graph.EdgeImports,
FilePath: filePath,
Line: line,
})
priorStageID := imageStageID(filePath, imageName)
if priorStageID != stageID {
result.Edges = append(result.Edges, &graph.Edge{
From: stageID, To: priorStageID, Kind: graph.EdgeDependsOn,
FilePath: filePath, Line: line,
Meta: map[string]any{"link": "stage_chain"},
})
}
}
func (e *DockerfileExtractor) extractEnvArg(node *sitter.Node, src []byte, filePath, fileID string, result *parser.ExtractionResult, prefix string, st *dockerfileState) {
for i := 0; i < int(node.ChildCount()); i++ {
child := node.Child(i)
if child == nil {
continue
}
ct := child.Type()
if ct == "env_pair" {
nameNode := e.findChildOfType(child, "unquoted_string")
if nameNode == nil {
nameNode = e.findChildOfType(child, "env_key")
}
if nameNode != nil {
varName := nameNode.Content(src)
e.addVariable(varName, prefix, node, filePath, fileID, result, st)
}
} else if ct == "unquoted_string" && i == 1 {
varName := child.Content(src)
if idx := strings.Index(varName, "="); idx > 0 {
varName = varName[:idx]
}
e.addVariable(varName, prefix, node, filePath, fileID, result, st)
}
}
}
func (e *DockerfileExtractor) addVariable(varName, prefix string, node *sitter.Node, filePath, fileID string, result *parser.ExtractionResult, st *dockerfileState) {
if varName == "" {
return
}
line := int(node.StartPoint().Row) + 1
endLine := int(node.EndPoint().Row) + 1
id := filePath + "::" + prefix + "." + varName
result.Nodes = append(result.Nodes, &graph.Node{
ID: id, Kind: graph.KindVariable, Name: prefix + " " + varName,
FilePath: filePath, StartLine: line, EndLine: endLine,
Language: "dockerfile",
})
result.Edges = append(result.Edges, &graph.Edge{
From: fileID, To: id, Kind: graph.EdgeDefines,
FilePath: filePath, Line: line,
})
keyID := configKeyEnvID(varName)
scope := "runtime"
if prefix == "ARG" {
scope = "build_arg"
}
result.Nodes = append(result.Nodes, &graph.Node{
ID: keyID, Kind: graph.KindConfigKey, Name: varName,
FilePath: filePath, StartLine: line, EndLine: endLine,
Language: "dockerfile",
Meta: map[string]any{
"source": "env",
"scope": scope,
"origin": "dockerfile",
},
})
usesFrom := st.stageID
if !st.hasStage {
usesFrom = "" // dropped below
}
if usesFrom != "" {
result.Edges = append(result.Edges, &graph.Edge{
From: usesFrom, To: keyID, Kind: graph.EdgeUsesEnv,
FilePath: filePath, Line: line,
Meta: map[string]any{"scope": scope},
})
} else {
// Pre-FROM ARG (global build arg). Bind to the file node
// so the relationship is still queryable.
result.Edges = append(result.Edges, &graph.Edge{
From: id, To: keyID, Kind: graph.EdgeUsesEnv,
FilePath: filePath, Line: line,
Meta: map[string]any{"scope": scope, "global_arg": true},
})
}
}
func (e *DockerfileExtractor) extractExpose(node *sitter.Node, src []byte, filePath, fileID string, result *parser.ExtractionResult, st *dockerfileState) {
line := int(node.StartPoint().Row) + 1
text := strings.TrimSpace(node.Content(src))
upper := strings.ToUpper(text)
if strings.HasPrefix(upper, "EXPOSE") {
text = strings.TrimSpace(text[len("EXPOSE"):])
}
for _, tok := range strings.Fields(text) {
port, proto := parsePortToken(tok)
if port == 0 {
continue
}
from := st.stageID
if !st.hasStage {
from = fileID
}
result.Edges = append(result.Edges, &graph.Edge{
From: from, To: portTargetID(proto, port),
Kind: graph.EdgeExposes,
FilePath: filePath, Line: line,
Meta: map[string]any{"proto": proto, "port": port},
})
}
}
func (e *DockerfileExtractor) extractInstruction(node *sitter.Node, src []byte, filePath, fileID string, result *parser.ExtractionResult, instrType string) {
label := strings.TrimSuffix(instrType, "_instruction")
label = strings.ToUpper(label)
text := node.Content(src)
if len(text) > 80 {
text = text[:77] + "..."
}
startLine := int(node.StartPoint().Row) + 1
endLine := int(node.EndPoint().Row) + 1
if instrType == "run_instruction" {
name := fmt.Sprintf("run-line-%d", startLine)
id := filePath + "::" + name
result.Nodes = append(result.Nodes, &graph.Node{
ID: id, Kind: graph.KindFunction, Name: name,
FilePath: filePath, StartLine: startLine, EndLine: endLine,
Language: "dockerfile",
Meta: map[string]any{
"instruction": label,
"command": text,
},
})
result.Edges = append(result.Edges, &graph.Edge{
From: fileID, To: id, Kind: graph.EdgeDefines,
FilePath: filePath, Line: startLine,
})
return
}
id := filePath + "::" + label + "::" + strings.ReplaceAll(text, "\n", " ")
if len(id) > 200 {
id = id[:200]
}
result.Nodes = append(result.Nodes, &graph.Node{
ID: id, Kind: graph.KindVariable, Name: label,
FilePath: filePath, StartLine: startLine, EndLine: endLine,
Language: "dockerfile",
Meta: map[string]any{
"instruction": label,
},
})
result.Edges = append(result.Edges, &graph.Edge{
From: fileID, To: id, Kind: graph.EdgeDefines,
FilePath: filePath, Line: startLine,
})
}
func (e *DockerfileExtractor) findChildOfType(node *sitter.Node, childType string) *sitter.Node {
for i := 0; i < int(node.ChildCount()); i++ {
child := node.Child(i)
if child != nil && child.Type() == childType {
return child
}
}
return nil
}
// parseFromText extracts the image reference and optional AS alias
// from a FROM instruction's full text. Handles `FROM <image>[:tag]
// [AS <stage>]` and `FROM --platform=… <image> AS <stage>`. The
// astImage argument is the image_spec captured by the AST walker;
// when present we take it verbatim and only use the text to recover
// the AS alias.
func parseFromText(rawText, astImage string) (image, alias string) {
tokens := strings.Fields(rawText)
if len(tokens) == 0 {
return astImage, ""
}
if strings.EqualFold(tokens[0], "FROM") {
tokens = tokens[1:]
}
stripped := tokens[:0]
for _, t := range tokens {
if strings.HasPrefix(t, "--") {
continue
}
stripped = append(stripped, t)
}
tokens = stripped
if len(tokens) == 0 {
return astImage, ""
}
image = tokens[0]
if astImage != "" {
image = astImage
}
for i := 1; i+1 < len(tokens); i++ {
if strings.EqualFold(tokens[i], "AS") {
alias = tokens[i+1]
break
}
}
return image, alias
}
// splitImageRef breaks `repo[:tag][@digest]` into ref and tag.
// Tag defaults to "latest" when omitted. Digests are appended back
// onto ref so consumers can detect immutability via meta.
func splitImageRef(name string) (ref, tag string) {
digest := ""
if at := strings.Index(name, "@"); at >= 0 {
digest = name[at:]
name = name[:at]
}
tag = "latest"
ref = name
if slash := strings.LastIndex(name, "/"); slash >= 0 {
if colon := strings.LastIndex(name[slash:], ":"); colon >= 0 {
tag = name[slash+colon+1:]
ref = name[:slash+colon]
}
} else if colon := strings.LastIndex(name, ":"); colon >= 0 {
tag = name[colon+1:]
ref = name[:colon]
}
if digest != "" {
ref = ref + digest
}
return ref, tag
}
func parsePortToken(tok string) (int, string) {
proto := "tcp"
if slash := strings.Index(tok, "/"); slash >= 0 {
proto = strings.ToLower(strings.TrimSpace(tok[slash+1:]))
tok = tok[:slash]
}
tok = strings.TrimSpace(tok)
n, err := strconv.Atoi(tok)
if err != nil || n <= 0 {
return 0, proto
}
return n, proto
}
// imageNodeID and imageStageID centralise the ID conventions for
// KindImage nodes so K8s extractors and Dockerfile extractors share
// the same scheme — a Pod referencing `image: nginx:1.25` and a
// Dockerfile `FROM nginx:1.25` both link to the same KindImage node.
func imageNodeID(name string) string {
ref, tag := splitImageRef(name)
return "image::" + ref + ":" + tag
}
func imageStageID(filePath, stage string) string {
return "image::stage::" + filePath + "::" + stage
}
// configKeyEnvID is the ID convention shared between the Dockerfile,
// K8s, and Go-side os.Getenv extractors. Keeping all sites at the
// same string ensures the cross-ref between container env
// declarations and code-side reads materialises through node
// identity instead of through a separate resolver pass.
func configKeyEnvID(name string) string {
return "cfg::env::" + name
}
// portTargetID builds the synthetic port endpoint used as the target
// of EdgeExposes edges. We don't materialise port nodes — these IDs
// are dangling endpoints, mirroring how unresolved::import::* works
// for cross-language imports.
func portTargetID(proto string, port int) string {
return fmt.Sprintf("port::%s::%d", proto, port)
}