registry.go 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. package clients
  2. import (
  3. "context"
  4. "fmt"
  5. "git.linuxforward.com/byop/byop-engine/models"
  6. "github.com/sirupsen/logrus"
  7. )
  8. // RegistryClient defines the interface for interacting with a Docker registry.
  9. type RegistryClient interface {
  10. // PushImage pushes an image. fullImageURI includes the tag.
  11. // registryURL is the base URL of the registry (e.g., "docker.io", "myregistry.com") used for auth.
  12. PushImage(ctx context.Context, job models.BuildJob, fullImageURI string, registryURL string, username string, password string) error
  13. // CheckImageExists checks if an image exists in the registry
  14. CheckImageExists(ctx context.Context, fullImageURI string, registryURL string, username string, password string) (bool, error)
  15. // Authenticate() error // Future method
  16. }
  17. // SimpleRegistryClient is a basic implementation of RegistryClient that uses BuildKitClient for pushing.
  18. type SimpleRegistryClient struct {
  19. entry *logrus.Entry
  20. buildkitClient BuildMachineClient // Client to perform the actual build and push operations
  21. }
  22. // NewSimpleRegistryClient creates a new SimpleRegistryClient.
  23. // It requires a BuildKitClient to delegate the push operation.
  24. func NewSimpleRegistryClient(bkc BuildMachineClient) *SimpleRegistryClient {
  25. if bkc == nil {
  26. // Or handle this more gracefully, perhaps by returning an error
  27. panic("BuildKitClient cannot be nil for SimpleRegistryClient")
  28. }
  29. return &SimpleRegistryClient{
  30. entry: logrus.WithField("component", "SimpleRegistryClient"),
  31. buildkitClient: bkc,
  32. }
  33. }
  34. // PushImage delegates the image push operation to the configured BuildKitClient.
  35. func (src *SimpleRegistryClient) PushImage(ctx context.Context, job models.BuildJob, fullImageURI string, registryURL string, username string, password string) error {
  36. src.entry.Infof("Job %d: SimpleRegistryClient delegating push for %s to BuildKitClient", job.ID, fullImageURI)
  37. // Delegate to BuildKitClient's PushImage method
  38. // Note: The BuildKitClient.PushImage method itself handles the BuildKit session and solve options for pushing.
  39. err := src.buildkitClient.PushImage(ctx, job, fullImageURI, registryURL, username, password)
  40. if err != nil {
  41. return fmt.Errorf("job %d: BuildKitClient failed to push image %s: %w", job.ID, fullImageURI, err)
  42. }
  43. src.entry.Infof("Job %d: Image %s successfully pushed by BuildKitClient via SimpleRegistryClient.", job.ID, fullImageURI)
  44. return nil
  45. }
  46. // CheckImageExists checks if an image exists in the registry by delegating to BuildKitClient
  47. func (src *SimpleRegistryClient) CheckImageExists(ctx context.Context, fullImageURI string, registryURL string, username string, password string) (bool, error) {
  48. src.entry.Infof("SimpleRegistryClient checking if image exists: %s", fullImageURI)
  49. // Delegate to BuildKitClient's CheckImageExists method
  50. exists, err := src.buildkitClient.CheckImageExists(ctx, fullImageURI, registryURL, username, password)
  51. if err != nil {
  52. return false, fmt.Errorf("BuildKitClient failed to check image existence %s: %w", fullImageURI, err)
  53. }
  54. src.entry.Infof("Image %s existence check result: %t", fullImageURI, exists)
  55. return exists, nil
  56. }