build.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. package v2beta1
  2. import (
  3. "fmt"
  4. "strings"
  5. "github.com/mitchellh/mapstructure"
  6. apiTypes "github.com/porter-dev/porter/api/types"
  7. "github.com/porter-dev/porter/internal/integrations/preview"
  8. "github.com/porter-dev/switchboard/pkg/types"
  9. )
  10. func (b *Build) GetName() string {
  11. if b == nil || b.Name == nil {
  12. return ""
  13. }
  14. return *b.Name
  15. }
  16. func (b *Build) GetContext() string {
  17. if b == nil || b.Context == nil || *b.Context == "" {
  18. return "."
  19. }
  20. return *b.Context
  21. }
  22. func (b *Build) GetMethod() string {
  23. if b == nil || b.Method == nil {
  24. return ""
  25. }
  26. return *b.Method
  27. }
  28. func (b *Build) GetBuilder() string {
  29. if b == nil || b.Builder == nil {
  30. return ""
  31. }
  32. return *b.Builder
  33. }
  34. func (b *Build) GetBuildpacks() []string {
  35. if b == nil || b.Buildpacks == nil {
  36. return []string{}
  37. }
  38. var bp []string
  39. for _, b := range b.Buildpacks {
  40. if b == nil {
  41. continue
  42. }
  43. bp = append(bp, *b)
  44. }
  45. return bp
  46. }
  47. func (b *Build) GetDockerfile() string {
  48. if b == nil || b.Dockerfile == nil {
  49. return ""
  50. }
  51. return *b.Dockerfile
  52. }
  53. func (b *Build) GetImage() string {
  54. if b == nil || b.Image == nil {
  55. return ""
  56. }
  57. return *b.Image
  58. }
  59. func (b *Build) GetRawEnv() map[string]string {
  60. env := make(map[string]string)
  61. if b == nil || b.Env == nil {
  62. return env
  63. }
  64. for k, v := range b.Env.Raw {
  65. if k == nil || v == nil {
  66. continue
  67. }
  68. env[*k] = *v
  69. }
  70. return env
  71. }
  72. func (b *Build) GetEnvGroups() []string {
  73. var eg []string
  74. if b == nil || b.Env == nil {
  75. return eg
  76. }
  77. for _, g := range b.Env.ImportFrom {
  78. if g == nil {
  79. continue
  80. }
  81. ns, name, valid := strings.Cut(*g, "/")
  82. if !valid || ns == "" || name == "" {
  83. printWarningMessage(fmt.Sprintf("ignoring invalid env group name: %s", *g))
  84. continue
  85. }
  86. eg = append(eg, *g)
  87. }
  88. return eg
  89. }
  90. func (b *Build) getV1BuildImage() (*types.Resource, error) {
  91. config := &preview.BuildDriverConfig{}
  92. if b.GetMethod() == "pack" {
  93. config.Build.Method = "pack"
  94. config.Build.Builder = b.GetBuilder()
  95. config.Build.Buildpacks = b.GetBuildpacks()
  96. } else if b.GetMethod() == "docker" {
  97. config.Build.Method = "docker"
  98. config.Build.Dockerfile = b.GetDockerfile()
  99. } else if b.GetMethod() == "registry" {
  100. config.Build.Method = "registry"
  101. config.Build.Image = b.GetImage()
  102. } else {
  103. return nil, fmt.Errorf("invalid build method: %s", b.GetMethod())
  104. }
  105. config.Build.Context = b.GetContext()
  106. config.Build.Env = b.GetRawEnv()
  107. for _, eg := range b.GetEnvGroups() {
  108. ns, name, _ := strings.Cut(eg, "/")
  109. config.EnvGroups = append(config.EnvGroups, apiTypes.EnvGroupMeta{
  110. Namespace: ns,
  111. Name: name,
  112. })
  113. }
  114. rawConfig := make(map[string]any)
  115. err := mapstructure.Decode(config, &rawConfig)
  116. if err != nil {
  117. return nil, err
  118. }
  119. return &types.Resource{
  120. Name: fmt.Sprintf("%s-build-image", b.GetName()),
  121. Driver: "build-image",
  122. Source: map[string]any{
  123. "name": "web",
  124. },
  125. Target: map[string]any{
  126. "app_name": b.GetName(),
  127. },
  128. DependsOn: []string{
  129. "get-env",
  130. },
  131. Config: rawConfig,
  132. }, nil
  133. }
  134. func (b *Build) getV1PushImage() (*types.Resource, error) {
  135. config := &preview.PushDriverConfig{}
  136. config.Push.Image = fmt.Sprintf("{ .%s-build-image.image }", b.GetName())
  137. rawConfig := make(map[string]any)
  138. err := mapstructure.Decode(config, &rawConfig)
  139. if err != nil {
  140. return nil, err
  141. }
  142. return &types.Resource{
  143. Name: b.GetName(),
  144. Driver: "push-image",
  145. DependsOn: []string{
  146. "get-env",
  147. fmt.Sprintf("%s-build-image", b.GetName()),
  148. },
  149. Target: map[string]any{
  150. "app_name": b.GetName(),
  151. },
  152. Config: rawConfig,
  153. }, nil
  154. }