Skip to content

Commit

Permalink
fix(buildkit): support app deploy without user-defined Procfile
Browse files Browse the repository at this point in the history
  • Loading branch information
nettoclaudio committed Jan 12, 2023
1 parent 833b24c commit 5034325
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 2 deletions.
27 changes: 25 additions & 2 deletions pkg/build/buildkit/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,19 @@ func (b *BuildKit) buildFromAppSourceFiles(ctx context.Context, r *pb.BuildReque
return nil, err
}

// NOTE(nettoclaudio): Some platforms doesn't require an user-defined Procfile (e.g. go, java, static, etc).
// So we need to retrieve the default Procfile from the platform image.
if appFiles.Procfile == "" {
fmt.Fprintln(w, "User-defined Procfile not found, trying to extract it from platform's container image")

tc, err := b.extractTsuruConfigsFromContainerImage(ctx, r.DestinationImages[0])
if err != nil {
return nil, err
}

appFiles.Procfile = tc.Procfile
}

return appFiles, nil
}

Expand Down Expand Up @@ -157,7 +170,7 @@ func (b *BuildKit) buildFromContainerImage(ctx context.Context, r *pb.BuildReque
return nil, err
}

appFiles, err := b.extractTsuruConfigsFromContainerImage(ctx, tmpDir)
appFiles, err := b.callBuildKitToExtractTsuruConfigs(ctx, tmpDir)
if err != nil {
return nil, err
}
Expand All @@ -166,7 +179,17 @@ func (b *BuildKit) buildFromContainerImage(ctx context.Context, r *pb.BuildReque
return appFiles, nil
}

func (b *BuildKit) extractTsuruConfigsFromContainerImage(ctx context.Context, localContextDir string) (*pb.TsuruConfig, error) {
func (b *BuildKit) extractTsuruConfigsFromContainerImage(ctx context.Context, image string) (*pb.TsuruConfig, error) {
tmpDir, cleanFunc, err := generateBuildLocalDir(ctx, b.opts.TempDir, fmt.Sprintf("FROM %s", image), nil, nil)
if err != nil {
return nil, err
}
defer cleanFunc()

return b.callBuildKitToExtractTsuruConfigs(ctx, tmpDir)
}

func (b *BuildKit) callBuildKitToExtractTsuruConfigs(ctx context.Context, localContextDir string) (*pb.TsuruConfig, error) {
eg, ctx := errgroup.WithContext(ctx)
pr, pw := io.Pipe() // reader/writer for tar output

Expand Down
26 changes: 26 additions & 0 deletions pkg/build/buildkit/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,32 @@ func TestBuildKit_Build_FromSourceFiles(t *testing.T) {
})
}

func TestBuildKit_Build_AppDeployFromSourceFiles_NoUserDefinedProcfile(t *testing.T) {
destImage := baseRegistry(t, "my-static-app", "latest")

req := &pb.BuildRequest{
Kind: pb.BuildKind_BUILD_KIND_APP_BUILD_WITH_SOURCE_UPLOAD,
App: &pb.TsuruApp{
Name: "my-app",
},
SourceImage: "tsuru/static:2.3",
DestinationImages: []string{destImage},
Data: appArchiveData(t, "./testdata/static/"),
PushOptions: &pb.PushOptions{InsecureRegistry: registryHTTP},
}

bc := newBuildKitClient(t)
defer bc.Close()

appFiles, err := NewBuildKit(bc, BuildKitOptions{TempDir: t.TempDir()}).
Build(context.TODO(), req, os.Stdout)

require.NoError(t, err)
assert.Equal(t, &pb.TsuruConfig{
Procfile: "web: /usr/sbin/nginx -g \"daemon off;\"\n",
}, appFiles)
}

func TestBuildKit_Build_FromContainerImages(t *testing.T) {
dc := newDockerClient(t)
defer dc.Close()
Expand Down
1 change: 1 addition & 0 deletions pkg/build/buildkit/testdata/static/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<h1>Hello world!</h1>

0 comments on commit 5034325

Please sign in to comment.