helpers.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. package stacks
  2. import (
  3. "fmt"
  4. "github.com/porter-dev/porter/internal/encryption"
  5. "github.com/porter-dev/porter/internal/models"
  6. )
  7. func CloneSourceConfigs(sourceConfigs []models.StackSourceConfig) ([]models.StackSourceConfig, error) {
  8. res := make([]models.StackSourceConfig, 0)
  9. // for now, only write source configs which are deployed as a docker image
  10. // TODO: add parsing/writes for git-based sources
  11. for _, sourceConfig := range sourceConfigs {
  12. uid, err := encryption.GenerateRandomBytes(16)
  13. if err != nil {
  14. return nil, err
  15. }
  16. res = append(res, models.StackSourceConfig{
  17. UID: uid,
  18. Name: sourceConfig.Name,
  19. ImageRepoURI: sourceConfig.ImageRepoURI,
  20. ImageTag: sourceConfig.ImageTag,
  21. })
  22. }
  23. return res, nil
  24. }
  25. func CloneAppResources(
  26. appResources []models.StackResource,
  27. prevSourceConfigs []models.StackSourceConfig,
  28. newSourceConfigs []models.StackSourceConfig,
  29. ) ([]models.StackResource, error) {
  30. res := make([]models.StackResource, 0)
  31. // for now, only write source configs which are deployed as a docker image
  32. // TODO: add parsing/writes for git-based sources
  33. for _, appResource := range appResources {
  34. uid, err := encryption.GenerateRandomBytes(16)
  35. if err != nil {
  36. return nil, err
  37. }
  38. var linkedSourceConfigUID string
  39. for _, prevSourceConfig := range prevSourceConfigs {
  40. if prevSourceConfig.UID == appResource.StackSourceConfigUID {
  41. // find the corresponding new source config
  42. for _, newSourceConfig := range newSourceConfigs {
  43. // If the source config was created previous to the implemntation of StableSourceConfigID
  44. // it means that we should check by the name and not the StableSourceConfigID.
  45. // This will happen only once for old source configs.
  46. if prevSourceConfig.StableSourceConfigID != "" {
  47. if newSourceConfig.StableSourceConfigID == prevSourceConfig.StableSourceConfigID {
  48. linkedSourceConfigUID = newSourceConfig.UID
  49. }
  50. } else {
  51. if newSourceConfig.Name == prevSourceConfig.Name {
  52. linkedSourceConfigUID = newSourceConfig.UID
  53. }
  54. }
  55. }
  56. }
  57. }
  58. if linkedSourceConfigUID == "" {
  59. return nil, fmt.Errorf("source config does not exist in source config list")
  60. }
  61. res = append(res, models.StackResource{
  62. Name: appResource.Name,
  63. UID: uid,
  64. StackSourceConfigUID: linkedSourceConfigUID,
  65. TemplateRepoURL: appResource.TemplateRepoURL,
  66. TemplateName: appResource.TemplateName,
  67. TemplateVersion: appResource.TemplateVersion,
  68. HelmRevisionID: appResource.HelmRevisionID,
  69. })
  70. }
  71. return res, nil
  72. }
  73. func CloneEnvGroups(envGroups []models.StackEnvGroup) ([]models.StackEnvGroup, error) {
  74. res := make([]models.StackEnvGroup, 0)
  75. for _, envGroup := range envGroups {
  76. uid, err := encryption.GenerateRandomBytes(16)
  77. if err != nil {
  78. return nil, err
  79. }
  80. res = append(res, models.StackEnvGroup{
  81. UID: uid,
  82. Name: envGroup.Name,
  83. EnvGroupVersion: envGroup.EnvGroupVersion,
  84. Namespace: envGroup.Namespace,
  85. ProjectID: envGroup.ProjectID,
  86. ClusterID: envGroup.ClusterID,
  87. })
  88. }
  89. return res, nil
  90. }