123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313 |
- package clients
- import (
- "context"
- "fmt"
- "os"
- "path/filepath"
- "strings"
- "git.linuxforward.com/byop/byop-engine/models"
- "github.com/docker/cli/cli/config/configfile"
- clitypes "github.com/docker/cli/cli/config/types"
- "github.com/moby/buildkit/client"
- "github.com/moby/buildkit/session"
- "github.com/moby/buildkit/session/auth/authprovider"
- "github.com/sirupsen/logrus"
- "github.com/tonistiigi/fsutil"
- "golang.org/x/sync/errgroup"
- )
- // DockerfileBuilder implements BuildMachineClient using Dockerfile-based builds
- // Inspired by the buildkit example provided
- type DockerfileBuilder struct {
- buildkitHost string
- entry *logrus.Entry
- }
- // NewDockerfileBuilder creates a new DockerfileBuilder
- func NewDockerfileBuilder(buildkitHost string) BuildMachineClient {
- return &DockerfileBuilder{
- buildkitHost: buildkitHost,
- entry: logrus.WithField("component", "DockerfileBuilder"),
- }
- }
- // BuildImage builds a Docker image using BuildKit with Dockerfile frontend
- func (db *DockerfileBuilder) BuildImage(ctx context.Context, job models.BuildJob, dockerfilePath string, contextPath string, imageName string, imageTag string, noCache bool, buildArgs map[string]string) (string, error) {
- db.entry.Infof("Job %d: Building image %s:%s using Dockerfile approach", job.ID, imageName, imageTag)
- c, err := client.New(ctx, db.buildkitHost)
- if err != nil {
- return "", fmt.Errorf("job %d: failed to create BuildKit client: %w", job.ID, err)
- }
- defer c.Close()
- // If we have generated Dockerfile content, write it to the build context
- if job.DockerfileContent != "" {
- dockerfilePath = filepath.Join(contextPath, "Dockerfile")
- if err := os.WriteFile(dockerfilePath, []byte(job.DockerfileContent), 0644); err != nil {
- return "", fmt.Errorf("job %d: failed to write generated Dockerfile: %w", job.ID, err)
- }
- db.entry.Infof("Job %d: Wrote generated Dockerfile to %s", job.ID, dockerfilePath)
- // Debug: Log first few lines of Dockerfile to verify content
- lines := strings.Split(job.DockerfileContent, "\n")
- if len(lines) > 10 {
- lines = lines[:10]
- }
- db.entry.Infof("Job %d: Generated Dockerfile first 10 lines:\n%s", job.ID, strings.Join(lines, "\n"))
- // Debug: Check if go.sum exists in build context
- goSumPath := filepath.Join(contextPath, "go.sum")
- if _, err := os.Stat(goSumPath); err == nil {
- db.entry.Infof("Job %d: go.sum EXISTS in build context %s", job.ID, contextPath)
- } else {
- db.entry.Infof("Job %d: go.sum DOES NOT EXIST in build context %s", job.ID, contextPath)
- }
- }
- solveOpt, err := db.newSolveOpt(ctx, job, contextPath, dockerfilePath, imageName, imageTag, noCache, buildArgs)
- if err != nil {
- return "", fmt.Errorf("job %d: failed to create solve options: %w", job.ID, err)
- }
- ch := make(chan *client.SolveStatus)
- eg, gctx := errgroup.WithContext(ctx)
- var buildOutput strings.Builder
- var solveResp *client.SolveResponse
- // Start the build
- eg.Go(func() error {
- var err error
- solveResp, err = c.Solve(gctx, nil, *solveOpt, ch)
- if err != nil {
- return fmt.Errorf("BuildKit solve failed: %w", err)
- }
- return nil
- })
- // Collect build output
- eg.Go(func() error {
- for status := range ch {
- for _, v := range status.Vertexes {
- if v.Error != "" {
- buildOutput.WriteString(fmt.Sprintf("Vertex Error: %s: %s\n", v.Name, v.Error))
- }
- }
- for _, l := range status.Logs {
- buildOutput.Write(l.Data)
- }
- }
- return nil
- })
- if err := eg.Wait(); err != nil {
- db.entry.Errorf("Job %d: Build failed: %v. Output:\n%s", job.ID, err, buildOutput.String())
- return buildOutput.String(), fmt.Errorf("build failed: %w", err)
- }
- db.entry.Infof("Job %d: Image %s:%s built successfully", job.ID, imageName, imageTag)
- // Return digest if available
- if solveResp != nil && solveResp.ExporterResponse != nil {
- if digest, ok := solveResp.ExporterResponse["containerimage.digest"]; ok {
- return digest, nil
- }
- }
- return buildOutput.String(), nil
- }
- // newSolveOpt creates solve options for Dockerfile builds, similar to the provided example
- func (db *DockerfileBuilder) newSolveOpt(ctx context.Context, job models.BuildJob, buildContext, dockerfilePath, imageName, imageTag string, noCache bool, buildArgs map[string]string) (*client.SolveOpt, error) {
- if buildContext == "" {
- return nil, fmt.Errorf("build context cannot be empty")
- }
- if dockerfilePath == "" {
- dockerfilePath = filepath.Join(buildContext, "Dockerfile")
- }
- // Create filesystem for build context
- contextFS, err := fsutil.NewFS(buildContext)
- if err != nil {
- return nil, fmt.Errorf("invalid build context: %w", err)
- }
- // Create filesystem for dockerfile directory
- dockerfileFS, err := fsutil.NewFS(filepath.Dir(dockerfilePath))
- if err != nil {
- return nil, fmt.Errorf("invalid dockerfile directory: %w", err)
- }
- fullImageName := fmt.Sprintf("%s:%s", imageName, imageTag)
- if job.RegistryURL != "" {
- fullImageName = fmt.Sprintf("%s/%s:%s", job.RegistryURL, imageName, imageTag)
- }
- // Frontend attributes for dockerfile build
- frontendAttrs := map[string]string{
- "filename": filepath.Base(dockerfilePath),
- }
- if noCache {
- frontendAttrs["no-cache"] = ""
- }
- // Add build args
- for key, value := range buildArgs {
- frontendAttrs["build-arg:"+key] = value
- }
- solveOpt := &client.SolveOpt{
- Exports: []client.ExportEntry{
- {
- Type: client.ExporterImage,
- Attrs: map[string]string{
- "name": fullImageName,
- },
- },
- },
- LocalMounts: map[string]fsutil.FS{
- "context": contextFS,
- "dockerfile": dockerfileFS,
- },
- Frontend: "dockerfile.v0", // Use dockerfile frontend
- FrontendAttrs: frontendAttrs,
- }
- // Setup authentication if registry credentials are provided
- if job.RegistryURL != "" && job.RegistryUser != "" && job.RegistryPassword != "" {
- authConfig := authprovider.DockerAuthProviderConfig{
- ConfigFile: &configfile.ConfigFile{
- AuthConfigs: map[string]clitypes.AuthConfig{
- job.RegistryURL: {
- Username: job.RegistryUser,
- Password: job.RegistryPassword,
- },
- },
- },
- }
- solveOpt.Session = []session.Attachable{
- authprovider.NewDockerAuthProvider(authConfig),
- }
- }
- return solveOpt, nil
- }
- // PushImage pushes the built image to registry
- func (db *DockerfileBuilder) PushImage(ctx context.Context, job models.BuildJob, fullImageURI string, registryURL string, username string, password string) error {
- db.entry.Infof("Job %d: Pushing image %s to registry", job.ID, fullImageURI)
- c, err := client.New(ctx, db.buildkitHost)
- if err != nil {
- return fmt.Errorf("job %d: failed to create BuildKit client for push: %w", job.ID, err)
- }
- defer c.Close()
- // For Dockerfile-based builds, we need to rebuild with push export
- // This is similar to the approach in the provided example
- contextFS, err := fsutil.NewFS(job.BuildContext)
- if err != nil {
- return fmt.Errorf("job %d: failed to create context FS for push: %w", job.ID, err)
- }
- dockerfilePath := job.Dockerfile
- if job.DockerfileContent != "" {
- // Write the generated Dockerfile content
- dockerfilePath = filepath.Join(job.BuildContext, "Dockerfile")
- if err := os.WriteFile(dockerfilePath, []byte(job.DockerfileContent), 0644); err != nil {
- return fmt.Errorf("job %d: failed to write Dockerfile for push: %w", job.ID, err)
- }
- }
- dockerfileFS, err := fsutil.NewFS(filepath.Dir(dockerfilePath))
- if err != nil {
- return fmt.Errorf("job %d: failed to create dockerfile FS for push: %w", job.ID, err)
- }
- // Parse build args
- buildArgs := make(map[string]string)
- if job.BuildArgs != "" {
- // Parse JSON build args if needed
- // For simplicity, assume it's already a map or handle JSON parsing
- }
- frontendAttrs := map[string]string{
- "filename": filepath.Base(dockerfilePath),
- }
- if job.NoCache {
- frontendAttrs["no-cache"] = ""
- }
- for key, value := range buildArgs {
- frontendAttrs["build-arg:"+key] = value
- }
- solveOpt := &client.SolveOpt{
- Exports: []client.ExportEntry{
- {
- Type: client.ExporterImage,
- Attrs: map[string]string{
- "name": fullImageURI,
- "push": "true",
- },
- },
- },
- LocalMounts: map[string]fsutil.FS{
- "context": contextFS,
- "dockerfile": dockerfileFS,
- },
- Frontend: "dockerfile.v0",
- FrontendAttrs: frontendAttrs,
- }
- // Setup authentication for push
- if username != "" && password != "" {
- authConfig := authprovider.DockerAuthProviderConfig{
- ConfigFile: &configfile.ConfigFile{
- AuthConfigs: map[string]clitypes.AuthConfig{
- registryURL: {
- Username: username,
- Password: password,
- },
- },
- },
- }
- solveOpt.Session = []session.Attachable{
- authprovider.NewDockerAuthProvider(authConfig),
- }
- }
- ch := make(chan *client.SolveStatus)
- _, err = c.Solve(ctx, nil, *solveOpt, ch)
- if err != nil {
- return fmt.Errorf("job %d: failed to push image: %w", job.ID, err)
- }
- db.entry.Infof("Job %d: Successfully pushed image %s", job.ID, fullImageURI)
- return nil
- }
- // CheckImageExists checks if an image exists in the registry
- func (db *DockerfileBuilder) CheckImageExists(ctx context.Context, fullImageURI string, registryURL string, username string, password string) (bool, error) {
- // This would require registry API calls, not implemented in this example
- db.entry.Infof("CheckImageExists called for %s (not implemented)", fullImageURI)
- return false, fmt.Errorf("CheckImageExists not implemented for DockerfileBuilder")
- }
- // Prune cleans up build resources
- func (db *DockerfileBuilder) Prune(ctx context.Context, job models.BuildJob) error {
- db.entry.Infof("Job %d: Prune called (no-op for DockerfileBuilder)", job.ID)
- return nil
- }
- // Close releases any resources held by the client
- func (db *DockerfileBuilder) Close() error {
- db.entry.Info("DockerfileBuilder closed")
- return nil
- }
|