dockerfile_builder.go 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. package clients
  2. import (
  3. "context"
  4. "fmt"
  5. "os"
  6. "path/filepath"
  7. "strings"
  8. "git.linuxforward.com/byop/byop-engine/models"
  9. "github.com/docker/cli/cli/config/configfile"
  10. clitypes "github.com/docker/cli/cli/config/types"
  11. "github.com/moby/buildkit/client"
  12. "github.com/moby/buildkit/session"
  13. "github.com/moby/buildkit/session/auth/authprovider"
  14. "github.com/sirupsen/logrus"
  15. "github.com/tonistiigi/fsutil"
  16. "golang.org/x/sync/errgroup"
  17. )
  18. // DockerfileBuilder implements BuildMachineClient using Dockerfile-based builds
  19. // Inspired by the buildkit example provided
  20. type DockerfileBuilder struct {
  21. buildkitHost string
  22. entry *logrus.Entry
  23. }
  24. // NewDockerfileBuilder creates a new DockerfileBuilder
  25. func NewDockerfileBuilder(buildkitHost string) BuildMachineClient {
  26. return &DockerfileBuilder{
  27. buildkitHost: buildkitHost,
  28. entry: logrus.WithField("component", "DockerfileBuilder"),
  29. }
  30. }
  31. // BuildImage builds a Docker image using BuildKit with Dockerfile frontend
  32. 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) {
  33. db.entry.Infof("Job %d: Building image %s:%s using Dockerfile approach", job.ID, imageName, imageTag)
  34. c, err := client.New(ctx, db.buildkitHost)
  35. if err != nil {
  36. return "", fmt.Errorf("job %d: failed to create BuildKit client: %w", job.ID, err)
  37. }
  38. defer c.Close()
  39. // If we have generated Dockerfile content, write it to the build context
  40. if job.DockerfileContent != "" {
  41. dockerfilePath = filepath.Join(contextPath, "Dockerfile")
  42. if err := os.WriteFile(dockerfilePath, []byte(job.DockerfileContent), 0644); err != nil {
  43. return "", fmt.Errorf("job %d: failed to write generated Dockerfile: %w", job.ID, err)
  44. }
  45. db.entry.Infof("Job %d: Wrote generated Dockerfile to %s", job.ID, dockerfilePath)
  46. // Debug: Log first few lines of Dockerfile to verify content
  47. lines := strings.Split(job.DockerfileContent, "\n")
  48. if len(lines) > 10 {
  49. lines = lines[:10]
  50. }
  51. db.entry.Infof("Job %d: Generated Dockerfile first 10 lines:\n%s", job.ID, strings.Join(lines, "\n"))
  52. // Debug: Check if go.sum exists in build context
  53. goSumPath := filepath.Join(contextPath, "go.sum")
  54. if _, err := os.Stat(goSumPath); err == nil {
  55. db.entry.Infof("Job %d: go.sum EXISTS in build context %s", job.ID, contextPath)
  56. } else {
  57. db.entry.Infof("Job %d: go.sum DOES NOT EXIST in build context %s", job.ID, contextPath)
  58. }
  59. }
  60. solveOpt, err := db.newSolveOpt(ctx, job, contextPath, dockerfilePath, imageName, imageTag, noCache, buildArgs)
  61. if err != nil {
  62. return "", fmt.Errorf("job %d: failed to create solve options: %w", job.ID, err)
  63. }
  64. ch := make(chan *client.SolveStatus)
  65. eg, gctx := errgroup.WithContext(ctx)
  66. var buildOutput strings.Builder
  67. var solveResp *client.SolveResponse
  68. // Start the build
  69. eg.Go(func() error {
  70. var err error
  71. solveResp, err = c.Solve(gctx, nil, *solveOpt, ch)
  72. if err != nil {
  73. return fmt.Errorf("BuildKit solve failed: %w", err)
  74. }
  75. return nil
  76. })
  77. // Collect build output
  78. eg.Go(func() error {
  79. for status := range ch {
  80. for _, v := range status.Vertexes {
  81. if v.Error != "" {
  82. buildOutput.WriteString(fmt.Sprintf("Vertex Error: %s: %s\n", v.Name, v.Error))
  83. }
  84. }
  85. for _, l := range status.Logs {
  86. buildOutput.Write(l.Data)
  87. }
  88. }
  89. return nil
  90. })
  91. if err := eg.Wait(); err != nil {
  92. db.entry.Errorf("Job %d: Build failed: %v. Output:\n%s", job.ID, err, buildOutput.String())
  93. return buildOutput.String(), fmt.Errorf("build failed: %w", err)
  94. }
  95. db.entry.Infof("Job %d: Image %s:%s built successfully", job.ID, imageName, imageTag)
  96. // Return digest if available
  97. if solveResp != nil && solveResp.ExporterResponse != nil {
  98. if digest, ok := solveResp.ExporterResponse["containerimage.digest"]; ok {
  99. return digest, nil
  100. }
  101. }
  102. return buildOutput.String(), nil
  103. }
  104. // newSolveOpt creates solve options for Dockerfile builds, similar to the provided example
  105. func (db *DockerfileBuilder) newSolveOpt(ctx context.Context, job models.BuildJob, buildContext, dockerfilePath, imageName, imageTag string, noCache bool, buildArgs map[string]string) (*client.SolveOpt, error) {
  106. if buildContext == "" {
  107. return nil, fmt.Errorf("build context cannot be empty")
  108. }
  109. if dockerfilePath == "" {
  110. dockerfilePath = filepath.Join(buildContext, "Dockerfile")
  111. }
  112. // Create filesystem for build context
  113. contextFS, err := fsutil.NewFS(buildContext)
  114. if err != nil {
  115. return nil, fmt.Errorf("invalid build context: %w", err)
  116. }
  117. // Create filesystem for dockerfile directory
  118. dockerfileFS, err := fsutil.NewFS(filepath.Dir(dockerfilePath))
  119. if err != nil {
  120. return nil, fmt.Errorf("invalid dockerfile directory: %w", err)
  121. }
  122. fullImageName := fmt.Sprintf("%s:%s", imageName, imageTag)
  123. if job.RegistryURL != "" {
  124. fullImageName = fmt.Sprintf("%s/%s:%s", job.RegistryURL, imageName, imageTag)
  125. }
  126. // Frontend attributes for dockerfile build
  127. frontendAttrs := map[string]string{
  128. "filename": filepath.Base(dockerfilePath),
  129. }
  130. if noCache {
  131. frontendAttrs["no-cache"] = ""
  132. }
  133. // Add build args
  134. for key, value := range buildArgs {
  135. frontendAttrs["build-arg:"+key] = value
  136. }
  137. solveOpt := &client.SolveOpt{
  138. Exports: []client.ExportEntry{
  139. {
  140. Type: client.ExporterImage,
  141. Attrs: map[string]string{
  142. "name": fullImageName,
  143. },
  144. },
  145. },
  146. LocalMounts: map[string]fsutil.FS{
  147. "context": contextFS,
  148. "dockerfile": dockerfileFS,
  149. },
  150. Frontend: "dockerfile.v0", // Use dockerfile frontend
  151. FrontendAttrs: frontendAttrs,
  152. }
  153. // Setup authentication if registry credentials are provided
  154. if job.RegistryURL != "" && job.RegistryUser != "" && job.RegistryPassword != "" {
  155. authConfig := authprovider.DockerAuthProviderConfig{
  156. ConfigFile: &configfile.ConfigFile{
  157. AuthConfigs: map[string]clitypes.AuthConfig{
  158. job.RegistryURL: {
  159. Username: job.RegistryUser,
  160. Password: job.RegistryPassword,
  161. },
  162. },
  163. },
  164. }
  165. solveOpt.Session = []session.Attachable{
  166. authprovider.NewDockerAuthProvider(authConfig),
  167. }
  168. }
  169. return solveOpt, nil
  170. }
  171. // PushImage pushes the built image to registry
  172. func (db *DockerfileBuilder) PushImage(ctx context.Context, job models.BuildJob, fullImageURI string, registryURL string, username string, password string) error {
  173. db.entry.Infof("Job %d: Pushing image %s to registry", job.ID, fullImageURI)
  174. c, err := client.New(ctx, db.buildkitHost)
  175. if err != nil {
  176. return fmt.Errorf("job %d: failed to create BuildKit client for push: %w", job.ID, err)
  177. }
  178. defer c.Close()
  179. // For Dockerfile-based builds, we need to rebuild with push export
  180. // This is similar to the approach in the provided example
  181. contextFS, err := fsutil.NewFS(job.BuildContext)
  182. if err != nil {
  183. return fmt.Errorf("job %d: failed to create context FS for push: %w", job.ID, err)
  184. }
  185. dockerfilePath := job.Dockerfile
  186. if job.DockerfileContent != "" {
  187. // Write the generated Dockerfile content
  188. dockerfilePath = filepath.Join(job.BuildContext, "Dockerfile")
  189. if err := os.WriteFile(dockerfilePath, []byte(job.DockerfileContent), 0644); err != nil {
  190. return fmt.Errorf("job %d: failed to write Dockerfile for push: %w", job.ID, err)
  191. }
  192. }
  193. dockerfileFS, err := fsutil.NewFS(filepath.Dir(dockerfilePath))
  194. if err != nil {
  195. return fmt.Errorf("job %d: failed to create dockerfile FS for push: %w", job.ID, err)
  196. }
  197. // Parse build args
  198. buildArgs := make(map[string]string)
  199. if job.BuildArgs != "" {
  200. // Parse JSON build args if needed
  201. // For simplicity, assume it's already a map or handle JSON parsing
  202. }
  203. frontendAttrs := map[string]string{
  204. "filename": filepath.Base(dockerfilePath),
  205. }
  206. if job.NoCache {
  207. frontendAttrs["no-cache"] = ""
  208. }
  209. for key, value := range buildArgs {
  210. frontendAttrs["build-arg:"+key] = value
  211. }
  212. solveOpt := &client.SolveOpt{
  213. Exports: []client.ExportEntry{
  214. {
  215. Type: client.ExporterImage,
  216. Attrs: map[string]string{
  217. "name": fullImageURI,
  218. "push": "true",
  219. },
  220. },
  221. },
  222. LocalMounts: map[string]fsutil.FS{
  223. "context": contextFS,
  224. "dockerfile": dockerfileFS,
  225. },
  226. Frontend: "dockerfile.v0",
  227. FrontendAttrs: frontendAttrs,
  228. }
  229. // Setup authentication for push
  230. if username != "" && password != "" {
  231. authConfig := authprovider.DockerAuthProviderConfig{
  232. ConfigFile: &configfile.ConfigFile{
  233. AuthConfigs: map[string]clitypes.AuthConfig{
  234. registryURL: {
  235. Username: username,
  236. Password: password,
  237. },
  238. },
  239. },
  240. }
  241. solveOpt.Session = []session.Attachable{
  242. authprovider.NewDockerAuthProvider(authConfig),
  243. }
  244. }
  245. ch := make(chan *client.SolveStatus)
  246. _, err = c.Solve(ctx, nil, *solveOpt, ch)
  247. if err != nil {
  248. return fmt.Errorf("job %d: failed to push image: %w", job.ID, err)
  249. }
  250. db.entry.Infof("Job %d: Successfully pushed image %s", job.ID, fullImageURI)
  251. return nil
  252. }
  253. // CheckImageExists checks if an image exists in the registry
  254. func (db *DockerfileBuilder) CheckImageExists(ctx context.Context, fullImageURI string, registryURL string, username string, password string) (bool, error) {
  255. // This would require registry API calls, not implemented in this example
  256. db.entry.Infof("CheckImageExists called for %s (not implemented)", fullImageURI)
  257. return false, fmt.Errorf("CheckImageExists not implemented for DockerfileBuilder")
  258. }
  259. // Prune cleans up build resources
  260. func (db *DockerfileBuilder) Prune(ctx context.Context, job models.BuildJob) error {
  261. db.entry.Infof("Job %d: Prune called (no-op for DockerfileBuilder)", job.ID)
  262. return nil
  263. }
  264. // Close releases any resources held by the client
  265. func (db *DockerfileBuilder) Close() error {
  266. db.entry.Info("DockerfileBuilder closed")
  267. return nil
  268. }