Skip to content

Commit bd7c80b

Browse files
committed
Extract tsuru.yaml from docker context
1 parent cdd5968 commit bd7c80b

File tree

5 files changed

+77
-0
lines changed

5 files changed

+77
-0
lines changed

pkg/build/buildkit/build.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,25 @@ func generateContainerfile(w io.Writer, image string, tsuruYamlHooks *build.Tsur
230230
return err
231231
}
232232

233+
func findAndReadTsuruYaml(tmpDir string) (string, error) {
234+
contextDir := filepath.Join(tmpDir, "context")
235+
236+
// Try all possible Tsuru YAML filenames
237+
for _, filename := range build.TsuruYamlNames {
238+
tsuruYamlPath := filepath.Join(contextDir, filename)
239+
if _, err := os.Stat(tsuruYamlPath); err == nil {
240+
tsuruYamlData, err := os.ReadFile(tsuruYamlPath)
241+
if err != nil {
242+
return "", fmt.Errorf("failed to read %s: %w", filename, err)
243+
}
244+
return string(tsuruYamlData), nil
245+
}
246+
}
247+
248+
// No Tsuru YAML file found, return empty string (not an error)
249+
return "", nil
250+
}
251+
233252
func (b *BuildKit) buildFromContainerImage(ctx context.Context, c *client.Client, r *pb.BuildRequest, w console.File) (*pb.TsuruConfig, error) {
234253
if err := ctx.Err(); err != nil {
235254
return nil, err
@@ -462,6 +481,16 @@ func (b *BuildKit) buildFromContainerFile(ctx context.Context, c *client.Client,
462481
return nil, err
463482
}
464483

484+
if tc.TsuruYaml == "" {
485+
tsuruYamlData, err := findAndReadTsuruYaml(tmpDir)
486+
if err != nil {
487+
return nil, err
488+
}
489+
if tsuruYamlData != "" {
490+
tc.TsuruYaml = tsuruYamlData
491+
}
492+
}
493+
465494
tc.ImageConfig = ic
466495

467496
return tc, nil

pkg/build/buildkit/build_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -697,6 +697,43 @@ kubernetes:
697697
}, appFiles)
698698
})
699699

700+
t.Run("Extract tsuru.yaml from context when is not present on container fs", func(t *testing.T) {
701+
destImage := baseRegistry(t, "my-app", "")
702+
703+
dockerfile, err := os.ReadFile("./testdata/tsuru-files-from-context/Dockerfile")
704+
require.NoError(t, err)
705+
706+
req := &pb.BuildRequest{
707+
Kind: pb.BuildKind_BUILD_KIND_APP_BUILD_WITH_CONTAINER_FILE,
708+
App: &pb.TsuruApp{
709+
Name: "my-app",
710+
},
711+
DestinationImages: []string{destImage},
712+
Containerfile: string(dockerfile),
713+
Data: compressGZIP(t, "./testdata/tsuru-files-from-context/"),
714+
PushOptions: &pb.PushOptions{
715+
InsecureRegistry: registryHTTP,
716+
},
717+
}
718+
719+
appFiles, err := NewBuildKit(bc, BuildKitOptions{TempDir: t.TempDir()}).Build(context.TODO(), req, os.Stdout)
720+
require.NoError(t, err)
721+
assert.Equal(t, &pb.TsuruConfig{
722+
Procfile: "",
723+
TsuruYaml: `healthcheck:
724+
path: /healthz
725+
interval_seconds: 3
726+
timeout_seconds: 1
727+
ports:
728+
- port: 443
729+
`,
730+
ImageConfig: &pb.ContainerImageConfig{
731+
Cmd: []string{"echo", "Hello from Tsuru files from context!"},
732+
WorkingDir: "/tmp",
733+
},
734+
}, appFiles)
735+
})
736+
700737
t.Run("Dockerfile mounting the app's env vars", func(t *testing.T) {
701738
destImage := baseRegistry(t, "my-app", "")
702739

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
FROM busybox:latest
2+
3+
WORKDIR /tmp
4+
CMD ["echo", "Hello from Tsuru files from context!"]
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
web: my-server --addr 0.0.0.0:${PORT}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
healthcheck:
2+
path: /healthz
3+
interval_seconds: 3
4+
timeout_seconds: 1
5+
ports:
6+
- port: 443

0 commit comments

Comments
 (0)