parse.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. package stacks
  2. import (
  3. "fmt"
  4. "strings"
  5. "github.com/porter-dev/porter/api/server/shared/config"
  6. "github.com/porter-dev/porter/api/types"
  7. "github.com/porter-dev/porter/internal/helm/loader"
  8. "github.com/porter-dev/porter/internal/templater/utils"
  9. "github.com/stefanmcshane/helm/pkg/chart"
  10. "gopkg.in/yaml.v2"
  11. )
  12. type PorterStackYAML struct {
  13. Version *string `yaml:"version"`
  14. Build *Build `yaml:"build"`
  15. Env map[string]string `yaml:"env"`
  16. Apps map[string]*App `yaml:"apps"`
  17. Release *string `yaml:"release"`
  18. }
  19. type Build struct {
  20. Context *string `yaml:"context" validate:"dir"`
  21. Method *string `yaml:"method" validate:"required,oneof=pack docker registry"`
  22. Builder *string `yaml:"builder" validate:"required_if=Method pack"`
  23. Buildpacks []*string `yaml:"buildpacks"`
  24. Dockerfile *string `yaml:"dockerfile" validate:"required_if=Method docker"`
  25. Image *string `yaml:"image" validate:"required_if=Method registry"`
  26. }
  27. type App struct {
  28. Run *string `yaml:"run" validate:"required"`
  29. Config map[string]interface{} `yaml:"config"`
  30. Type *string `yaml:"type" validate:"required, oneof=web worker job"`
  31. }
  32. func parse(porterYaml []byte, imageInfo *types.ImageInfo, config *config.Config, projectID uint) (*chart.Chart, map[string]interface{}, error) {
  33. parsed := &PorterStackYAML{}
  34. err := yaml.Unmarshal(porterYaml, parsed)
  35. if err != nil {
  36. return nil, nil, fmt.Errorf("%s: %w", "error parsing porter.yaml", err)
  37. }
  38. values, err := buildStackValues(parsed, imageInfo)
  39. if err != nil {
  40. return nil, nil, fmt.Errorf("%s: %w", "error building values from porter.yaml", err)
  41. }
  42. convertedValues := convertMap(values)
  43. chart, err := buildStackChart(parsed, config, projectID)
  44. if err != nil {
  45. return nil, nil, fmt.Errorf("%s: %w", "error building chart from porter.yaml", err)
  46. }
  47. return chart, convertedValues.(map[string]interface{}), nil
  48. }
  49. func buildStackValues(parsed *PorterStackYAML, imageInfo *types.ImageInfo) (map[string]interface{}, error) {
  50. values := make(map[string]interface{})
  51. for name, app := range parsed.Apps {
  52. appType := getType(name, app)
  53. defaultValues := getDefaultValues(app, parsed.Env, appType)
  54. helm_values := utils.CoalesceValues(defaultValues, app.Config)
  55. values[name] = helm_values
  56. if imageInfo != nil {
  57. values["global"] = map[string]interface{}{
  58. "image": map[string]interface{}{
  59. "repository": imageInfo.Repository,
  60. "tag": imageInfo.Tag,
  61. },
  62. }
  63. }
  64. }
  65. return values, nil
  66. }
  67. func getType(name string, app *App) string {
  68. if app.Type != nil {
  69. return *app.Type
  70. }
  71. if strings.Contains(name, "web") {
  72. return "web"
  73. }
  74. return "worker"
  75. }
  76. func getDefaultValues(app *App, env map[string]string, appType string) map[string]interface{} {
  77. var defaultValues map[string]interface{}
  78. var runCommand string
  79. if app.Run != nil {
  80. runCommand = *app.Run
  81. }
  82. if appType == "web" {
  83. defaultValues = map[string]interface{}{
  84. "ingress": map[string]interface{}{
  85. "enabled": false,
  86. },
  87. "container": map[string]interface{}{
  88. "command": runCommand,
  89. "env": map[string]interface{}{
  90. "normal": CopyEnv(env),
  91. },
  92. },
  93. }
  94. } else {
  95. defaultValues = map[string]interface{}{
  96. "container": map[string]interface{}{
  97. "command": runCommand,
  98. "env": map[string]interface{}{
  99. "normal": CopyEnv(env),
  100. },
  101. },
  102. }
  103. }
  104. return defaultValues
  105. }
  106. func buildStackChart(parsed *PorterStackYAML, config *config.Config, projectID uint) (*chart.Chart, error) {
  107. deps := make([]*chart.Dependency, 0)
  108. for alias, app := range parsed.Apps {
  109. appType := getType(alias, app)
  110. selectedRepo := config.ServerConf.DefaultApplicationHelmRepoURL
  111. selectedVersion, err := getLatestTemplateVersion(appType, config, projectID)
  112. if err != nil {
  113. return nil, err
  114. }
  115. deps = append(deps, &chart.Dependency{
  116. Name: appType,
  117. Alias: alias,
  118. Version: selectedVersion,
  119. Repository: selectedRepo,
  120. })
  121. }
  122. chart, err := createChartFromDependencies(deps)
  123. if err != nil {
  124. return nil, err
  125. }
  126. return chart, nil
  127. }
  128. func createChartFromDependencies(deps []*chart.Dependency) (*chart.Chart, error) {
  129. metadata := &chart.Metadata{
  130. Name: "umbrella",
  131. Description: "Web application that is exposed to external traffic.",
  132. Version: "0.96.0",
  133. APIVersion: "v2",
  134. Home: "https://getporter.dev/",
  135. Icon: "https://user-images.githubusercontent.com/65516095/111255214-07d3da80-85ed-11eb-99e2-fddcbdb99bdb.png",
  136. Keywords: []string{
  137. "porter",
  138. "application",
  139. "service",
  140. "umbrella",
  141. },
  142. Type: "application",
  143. Dependencies: deps,
  144. }
  145. // create a new chart object with the metadata
  146. c := &chart.Chart{
  147. Metadata: metadata,
  148. }
  149. return c, nil
  150. }
  151. func getLatestTemplateVersion(templateName string, config *config.Config, projectID uint) (string, error) {
  152. repoIndex, err := loader.LoadRepoIndexPublic(config.ServerConf.DefaultApplicationHelmRepoURL)
  153. if err != nil {
  154. return "", fmt.Errorf("%s: %w", "unable to load porter chart repo", err)
  155. }
  156. templates := loader.RepoIndexToPorterChartList(repoIndex, config.ServerConf.DefaultApplicationHelmRepoURL)
  157. if err != nil {
  158. return "", fmt.Errorf("%s: %w", "unable to load porter chart list", err)
  159. }
  160. var version string
  161. // find the matching template name
  162. for _, template := range templates {
  163. if templateName == template.Name {
  164. version = template.Versions[0]
  165. break
  166. }
  167. }
  168. if version == "" {
  169. return "", fmt.Errorf("matching template version not found")
  170. }
  171. return version, nil
  172. }
  173. func convertMap(m interface{}) interface{} {
  174. switch m := m.(type) {
  175. case map[string]interface{}:
  176. for k, v := range m {
  177. m[k] = convertMap(v)
  178. }
  179. case map[interface{}]interface{}:
  180. result := map[string]interface{}{}
  181. for k, v := range m {
  182. result[k.(string)] = convertMap(v)
  183. }
  184. return result
  185. case []interface{}:
  186. for i, v := range m {
  187. m[i] = convertMap(v)
  188. }
  189. }
  190. return m
  191. }
  192. func CopyEnv(env map[string]string) map[string]string {
  193. envCopy := make(map[string]string)
  194. if env == nil {
  195. return envCopy
  196. }
  197. for k, v := range env {
  198. if k == "" || v == "" {
  199. continue
  200. }
  201. envCopy[k] = v
  202. }
  203. return envCopy
  204. }