Skip to content

Commit 4f8030f

Browse files
committed
refactor(packager): embed scripts via go:embed, fix JSON escaping, share MarshalToString, add integration harness (phase 6)
1 parent c12431f commit 4f8030f

8 files changed

Lines changed: 640 additions & 372 deletions

File tree

internal/testutil/llb.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Package testutil provides small shared helpers for tests across the aikit
2+
// packages. It lives in a non-test file so it can be imported from _test.go
3+
// files in other packages.
4+
package testutil
5+
6+
import "github.com/moby/buildkit/client/llb"
7+
8+
// MarshalToString concatenates the marshaled protobuf operations of an LLB
9+
// definition into a single string. Tests use it to assert that an expected
10+
// command or source identifier appears somewhere in the produced LLB graph.
11+
func MarshalToString(def *llb.Definition) string {
12+
if def == nil {
13+
return ""
14+
}
15+
var combined []byte
16+
for _, d := range def.ToPB().Def {
17+
combined = append(combined, d...)
18+
}
19+
return string(combined)
20+
}

pkg/packager/build.go

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package packager
66
import (
77
"context"
88
"fmt"
9+
"sort"
910

1011
"github.com/kaito-project/aikit/pkg/utils"
1112
"github.com/moby/buildkit/client/llb"
@@ -111,12 +112,14 @@ func BuildModelpack(ctx context.Context, c client.Client) (*client.Result, error
111112

112113
artifactType := v1.ArtifactTypeModelManifest
113114
mtManifest := v1.MediaTypeModelConfig
114-
script := generateModelpackScript(cfg.packMode, artifactType, mtManifest, cfg.name, cfg.refName)
115+
env := modelpackScriptEnv(cfg.packMode, artifactType, mtManifest, cfg.name, cfg.refName)
115116

116-
run := llb.Image(bashImage).Run(
117-
llb.Args([]string{"bash", "-c", script}),
117+
runOpts := []llb.RunOption{
118+
llb.Args([]string{"bash", "-c", modelpackScript}),
118119
llb.AddMount("/src", modelState, llb.Readonly),
119-
)
120+
}
121+
runOpts = append(runOpts, envRunOptions(env)...)
122+
run := llb.Image(bashImage).Run(runOpts...)
120123
final := llb.Scratch().File(llb.Copy(run.Root(), "/layout/", "/"))
121124

122125
return solveAndBuildResult(ctx, c, final, "packager:modelpack")
@@ -146,17 +149,34 @@ func BuildGeneric(ctx context.Context, c client.Client) (*client.Result, error)
146149
}
147150

148151
artifactType := "application/vnd.unknown.artifact.v1"
149-
script := generateGenericScript(cfg.packMode, artifactType, cfg.name, cfg.refName, cfg.debug)
152+
env := genericScriptEnv(cfg.packMode, artifactType, cfg.name, cfg.refName, cfg.debug)
150153

151-
run := llb.Image(bashImage).Run(
152-
llb.Args([]string{"bash", "-c", script}),
154+
runOpts := []llb.RunOption{
155+
llb.Args([]string{"bash", "-c", genericScript}),
153156
llb.AddMount("/src", srcState, llb.Readonly),
154-
)
157+
}
158+
runOpts = append(runOpts, envRunOptions(env)...)
159+
run := llb.Image(bashImage).Run(runOpts...)
155160
final := llb.Scratch().File(llb.Copy(run.Root(), "/layout/", "/"))
156161

157162
return solveAndBuildResult(ctx, c, final, "packager:generic")
158163
}
159164

165+
// envRunOptions converts an environment map into a deterministically ordered
166+
// slice of llb.RunOption values (sorted by key so the produced LLB is stable).
167+
func envRunOptions(env map[string]string) []llb.RunOption {
168+
keys := make([]string, 0, len(env))
169+
for k := range env {
170+
keys = append(keys, k)
171+
}
172+
sort.Strings(keys)
173+
opts := make([]llb.RunOption, 0, len(keys))
174+
for _, k := range keys {
175+
opts = append(opts, llb.AddEnv(k, env[k]))
176+
}
177+
return opts
178+
}
179+
160180
func getBuildArg(opts map[string]string, k string) string {
161181
return utils.GetBuildArg(opts, k)
162182
}

0 commit comments

Comments
 (0)