registry.go 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. package models
  2. import (
  3. "strings"
  4. "github.com/porter-dev/porter/api/types"
  5. "github.com/porter-dev/porter/internal/models/integrations"
  6. "gorm.io/gorm"
  7. )
  8. // Registry is an integration that can connect to a Docker image registry via
  9. // a specific auth mechanism
  10. type Registry struct {
  11. gorm.Model
  12. // Name of the registry
  13. Name string `json:"name"`
  14. // URL of the registry
  15. URL string `json:"url"`
  16. // The project that this integration belongs to
  17. ProjectID uint `json:"project_id"`
  18. // The infra id, if registry was provisioned with Porter
  19. InfraID uint `json:"infra_id"`
  20. // CloudProvider is the cloud provider that hosts the registry. Accepted values: [AWS, GCP, AZURE]
  21. CloudProvider string `json:"cloud_provider" gorm:"default:''"`
  22. // CloudProviderCredentialIdentifier is a reference to find the credentials required for access the registry's API.
  23. // For AWS EKS clusters, this will be an ARN for the final target role in the assume role chain.
  24. CloudProviderCredentialIdentifier string `json:"cloud_provider_credential_identifier" gorm:"default:''"`
  25. // ------------------------------------------------------------------
  26. // All fields below this line are encrypted before storage
  27. // ------------------------------------------------------------------
  28. GCPIntegrationID uint
  29. AWSIntegrationID uint
  30. AzureIntegrationID uint
  31. DOIntegrationID uint
  32. BasicIntegrationID uint
  33. // A token cache that can be used by an auth mechanism (integration), if desired
  34. TokenCache integrations.RegTokenCache
  35. }
  36. func (r *Registry) ToRegistryType() *types.Registry {
  37. var serv types.RegistryService
  38. if r.AWSIntegrationID != 0 {
  39. serv = types.ECR
  40. } else if r.GCPIntegrationID != 0 {
  41. if strings.Contains(r.URL, "pkg.dev") {
  42. serv = types.GAR
  43. } else {
  44. serv = types.GCR
  45. }
  46. } else if r.DOIntegrationID != 0 {
  47. serv = types.DOCR
  48. } else if r.AzureIntegrationID != 0 {
  49. serv = types.ACR
  50. } else if strings.Contains(r.URL, "index.docker.io") {
  51. serv = types.DockerHub
  52. }
  53. uri := r.URL
  54. // remove the protocol
  55. if splStr := strings.Split(uri, "://"); len(splStr) > 1 {
  56. uri = splStr[1]
  57. }
  58. return &types.Registry{
  59. ID: r.ID,
  60. ProjectID: r.ProjectID,
  61. Name: r.Name,
  62. URL: uri,
  63. Service: string(serv),
  64. InfraID: r.InfraID,
  65. GCPIntegrationID: r.GCPIntegrationID,
  66. AWSIntegrationID: r.AWSIntegrationID,
  67. AzureIntegrationID: r.AzureIntegrationID,
  68. DOIntegrationID: r.DOIntegrationID,
  69. BasicIntegrationID: r.BasicIntegrationID,
  70. }
  71. }