get.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. package environment_groups
  2. import (
  3. "context"
  4. "github.com/porter-dev/porter/internal/kubernetes"
  5. "github.com/porter-dev/porter/internal/telemetry"
  6. )
  7. // LatestBaseEnvironmentGroup returns the most recent version of an environment group stored in the porter-env-group namespace.
  8. // It replaces all secret values with a dummy variable and can be used to return values to the user. If you need access to the true secret values,
  9. // use the private latestBaseEnvironmentGroup function instead.
  10. func LatestBaseEnvironmentGroup(ctx context.Context, a *kubernetes.Agent, environmentGroupName string) (EnvironmentGroup, error) {
  11. ctx, span := telemetry.NewSpan(ctx, "latest-base-env-group")
  12. defer span.End()
  13. telemetry.WithAttributes(span, telemetry.AttributeKV{Key: "environment-group-name", Value: environmentGroupName})
  14. var eg EnvironmentGroup
  15. baseEnvironmentGroupVersions, err := ListEnvironmentGroups(ctx, a, WithEnvironmentGroupName(environmentGroupName), WithNamespace(Namespace_EnvironmentGroups))
  16. if err != nil {
  17. return eg, telemetry.Error(ctx, span, err, "unable to list base environment groups")
  18. }
  19. var highestVersionEnvironmentGroup EnvironmentGroup
  20. for _, baseEnvironmentGroup := range baseEnvironmentGroupVersions {
  21. if baseEnvironmentGroup.Version > highestVersionEnvironmentGroup.Version {
  22. highestVersionEnvironmentGroup = baseEnvironmentGroup
  23. }
  24. }
  25. telemetry.WithAttributes(span,
  26. telemetry.AttributeKV{Key: "highest-version", Value: highestVersionEnvironmentGroup.Version},
  27. telemetry.AttributeKV{Key: "highest-version-name", Value: highestVersionEnvironmentGroup.Name},
  28. )
  29. return highestVersionEnvironmentGroup, nil
  30. }
  31. // latestBaseEnvironmentGroup returns the most recent version of an environment group stored in the porter-env-group namespace.
  32. // This is a private function because it returns all secret values. If you are trying to retrieve the latest base environment group to return to the user,
  33. // use the exported LatestBaseEnvironmentGroup instead.
  34. func latestBaseEnvironmentGroup(ctx context.Context, a *kubernetes.Agent, environmentGroupName string) (EnvironmentGroup, error) {
  35. ctx, span := telemetry.NewSpan(ctx, "latest-base-env-group-private")
  36. defer span.End()
  37. telemetry.WithAttributes(span, telemetry.AttributeKV{Key: "environment-group-name", Value: environmentGroupName})
  38. var eg EnvironmentGroup
  39. baseEnvironmentGroupVersions, err := listEnvironmentGroups(ctx, a, WithEnvironmentGroupName(environmentGroupName), WithNamespace(Namespace_EnvironmentGroups))
  40. if err != nil {
  41. return eg, telemetry.Error(ctx, span, err, "unable to list base environment groups")
  42. }
  43. var highestVersionEnvironmentGroup EnvironmentGroup
  44. for _, baseEnvironmentGroup := range baseEnvironmentGroupVersions {
  45. if baseEnvironmentGroup.Version > highestVersionEnvironmentGroup.Version {
  46. highestVersionEnvironmentGroup = baseEnvironmentGroup
  47. }
  48. }
  49. telemetry.WithAttributes(span,
  50. telemetry.AttributeKV{Key: "highest-version", Value: highestVersionEnvironmentGroup.Version},
  51. telemetry.AttributeKV{Key: "highest-version-name", Value: highestVersionEnvironmentGroup.Name},
  52. )
  53. return highestVersionEnvironmentGroup, nil
  54. }
  55. // EnvironmentGroupInTargetNamespaceInput contains all information required to check if an environment group exists in a target namespace.
  56. // If you are looking for environment groups in the base namespace, consider using LatestBaseEnvironmentGroup or ListBaseEnvironmentGroups instead
  57. type EnvironmentGroupInTargetNamespaceInput struct {
  58. // Name is the environment group name which can be found on the configmap label
  59. Name string
  60. Version int
  61. Namespace string
  62. ExcludeDefaultAppEnvironmentGroup bool
  63. }
  64. // EnvironmentGroupInTargetNamespace checks if an environment group of a specific name and version exists in a target namespace.
  65. // If an environment group exists, it will be returned
  66. func EnvironmentGroupInTargetNamespace(ctx context.Context, a *kubernetes.Agent, inp EnvironmentGroupInTargetNamespaceInput) (EnvironmentGroup, error) {
  67. ctx, span := telemetry.NewSpan(ctx, "env-group-in-target-namespace")
  68. defer span.End()
  69. var eg EnvironmentGroup
  70. if inp.Name == "" {
  71. return eg, telemetry.Error(ctx, span, nil, "must provide an environment group name")
  72. }
  73. if inp.Version == 0 {
  74. return eg, telemetry.Error(ctx, span, nil, "must provide an environment group version to check for")
  75. }
  76. if inp.Namespace == "" {
  77. return eg, telemetry.Error(ctx, span, nil, "must provide a namespace to check")
  78. }
  79. telemetry.WithAttributes(span,
  80. telemetry.AttributeKV{Key: "environment-group-name", Value: inp.Name},
  81. telemetry.AttributeKV{Key: "environment-group-version", Value: inp.Version},
  82. telemetry.AttributeKV{Key: "namespace", Value: inp.Namespace},
  83. )
  84. opts := []EnvironmentGroupOption{
  85. WithEnvironmentGroupName(inp.Name),
  86. WithEnvironmentGroupVersion(inp.Version),
  87. WithNamespace(inp.Namespace),
  88. }
  89. if inp.ExcludeDefaultAppEnvironmentGroup {
  90. opts = append(opts, WithoutDefaultAppEnvironmentGroups())
  91. }
  92. environmentGroups, err := ListEnvironmentGroups(ctx, a, opts...)
  93. if err != nil {
  94. return eg, telemetry.Error(ctx, span, err, "unable to list environment groups in target namespace")
  95. }
  96. if len(environmentGroups) > 1 {
  97. telemetry.WithAttributes(span,
  98. telemetry.AttributeKV{Key: "expected-results", Value: 1},
  99. telemetry.AttributeKV{Key: "actual-results", Value: len(environmentGroups)},
  100. )
  101. return eg, telemetry.Error(ctx, span, nil, "unexpected number of versions found in namespace")
  102. }
  103. if len(environmentGroups) == 1 {
  104. return environmentGroups[0], nil
  105. }
  106. return eg, nil
  107. }