list.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. package environment_groups
  2. import (
  3. "net/http"
  4. "strings"
  5. "time"
  6. "github.com/porter-dev/porter/api/server/authz"
  7. "github.com/porter-dev/porter/api/server/handlers"
  8. "github.com/porter-dev/porter/api/server/shared"
  9. "github.com/porter-dev/porter/api/server/shared/apierrors"
  10. "github.com/porter-dev/porter/api/server/shared/config"
  11. "github.com/porter-dev/porter/api/types"
  12. environmentgroups "github.com/porter-dev/porter/internal/kubernetes/environment_groups"
  13. "github.com/porter-dev/porter/internal/models"
  14. "github.com/porter-dev/porter/internal/telemetry"
  15. )
  16. type ListEnvironmentGroupsHandler struct {
  17. handlers.PorterHandlerReadWriter
  18. authz.KubernetesAgentGetter
  19. }
  20. func NewListEnvironmentGroupsHandler(
  21. config *config.Config,
  22. decoderValidator shared.RequestDecoderValidator,
  23. writer shared.ResultWriter,
  24. ) *ListEnvironmentGroupsHandler {
  25. return &ListEnvironmentGroupsHandler{
  26. PorterHandlerReadWriter: handlers.NewDefaultPorterHandler(config, decoderValidator, writer),
  27. KubernetesAgentGetter: authz.NewOutOfClusterAgentGetter(config),
  28. }
  29. }
  30. type ListEnvironmentGroupsResponse struct {
  31. EnvironmentGroups []EnvironmentGroupListItem `json:"environment_groups,omitempty"`
  32. }
  33. type EnvironmentGroupListItem struct {
  34. Name string `json:"name"`
  35. LatestVersion int `json:"latest_version"`
  36. Variables map[string]string `json:"variables,omitempty"`
  37. SecretVariables map[string]string `json:"secret_variables,omitempty"`
  38. CreatedAtUTC time.Time `json:"created_at"`
  39. LinkedApplications []string `json:"linked_applications,omitempty"`
  40. }
  41. func (c *ListEnvironmentGroupsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  42. ctx, span := telemetry.NewSpan(r.Context(), "serve-list-env-groups")
  43. defer span.End()
  44. project, _ := ctx.Value(types.ProjectScope).(*models.Project)
  45. cluster, _ := ctx.Value(types.ClusterScope).(*models.Cluster)
  46. agent, err := c.GetAgent(r, cluster, "")
  47. if err != nil {
  48. err = telemetry.Error(ctx, span, err, "unable to connect to cluster")
  49. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusServiceUnavailable))
  50. return
  51. }
  52. allEnvGroupVersions, err := environmentgroups.ListEnvironmentGroups(ctx, agent, environmentgroups.WithNamespace(environmentgroups.Namespace_EnvironmentGroups), environmentgroups.WithoutDefaultAppEnvironmentGroups())
  53. if err != nil {
  54. err = telemetry.Error(ctx, span, err, "unable to list all environment groups")
  55. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  56. return
  57. }
  58. envGroupSet := make(map[string]struct{})
  59. for _, envGroup := range allEnvGroupVersions {
  60. if envGroup.Name == "" {
  61. continue
  62. }
  63. if _, ok := envGroupSet[envGroup.Name]; !ok {
  64. envGroupSet[envGroup.Name] = struct{}{}
  65. }
  66. }
  67. var envGroups []EnvironmentGroupListItem
  68. for envGroupName := range envGroupSet {
  69. latestVersion, err := environmentgroups.LatestBaseEnvironmentGroup(ctx, agent, envGroupName)
  70. if err != nil {
  71. err = telemetry.Error(ctx, span, err, "unable to get latest environment groups")
  72. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  73. return
  74. }
  75. var linkedApplications []string
  76. if !project.GetFeatureFlag(models.ValidateApplyV2, c.Config().LaunchDarklyClient) {
  77. applications, err := environmentgroups.LinkedApplications(ctx, agent, latestVersion.Name, true)
  78. if err != nil {
  79. err = telemetry.Error(ctx, span, err, "unable to get linked applications")
  80. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  81. return
  82. }
  83. applicationSetForEnvGroup := make(map[string]struct{})
  84. for _, app := range applications {
  85. if app.Namespace == "" {
  86. continue
  87. }
  88. if _, ok := applicationSetForEnvGroup[app.Namespace]; !ok {
  89. applicationSetForEnvGroup[app.Namespace] = struct{}{}
  90. }
  91. }
  92. for appNamespace := range applicationSetForEnvGroup {
  93. porterAppName := strings.TrimPrefix(appNamespace, "porter-stack-")
  94. linkedApplications = append(linkedApplications, porterAppName)
  95. }
  96. } else {
  97. applications, err := environmentgroups.LinkedApplications(ctx, agent, latestVersion.Name, false)
  98. if err != nil {
  99. err = telemetry.Error(ctx, span, err, "unable to get linked applications")
  100. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  101. return
  102. }
  103. for _, app := range applications {
  104. linkedApplications = append(linkedApplications, app.Name)
  105. }
  106. }
  107. secrets := make(map[string]string)
  108. for k, v := range latestVersion.SecretVariables {
  109. secrets[k] = string(v)
  110. }
  111. envGroups = append(envGroups, EnvironmentGroupListItem{
  112. Name: latestVersion.Name,
  113. LatestVersion: latestVersion.Version,
  114. Variables: latestVersion.Variables,
  115. SecretVariables: secrets,
  116. CreatedAtUTC: latestVersion.CreatedAtUTC,
  117. LinkedApplications: linkedApplications,
  118. })
  119. }
  120. c.WriteResult(w, r, ListEnvironmentGroupsResponse{EnvironmentGroups: envGroups})
  121. }