get.go 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. func LatestBaseEnvironmentGroup(ctx context.Context, a *kubernetes.Agent, environmentGroupName string) (EnvironmentGroup, error) {
  9. ctx, span := telemetry.NewSpan(ctx, "latest-base-env-group")
  10. defer span.End()
  11. telemetry.WithAttributes(span, telemetry.AttributeKV{Key: "environment-group-name", Value: environmentGroupName})
  12. var eg EnvironmentGroup
  13. baseEnvironmentGroupVersions, err := ListEnvironmentGroups(ctx, a, WithEnvironmentGroupName(environmentGroupName), WithNamespace(Namespace_EnvironmentGroups))
  14. if err != nil {
  15. return eg, telemetry.Error(ctx, span, err, "unable to list base environment groups")
  16. }
  17. var highestVersionEnvironmentGroup EnvironmentGroup
  18. for _, baseEnvironmentGroup := range baseEnvironmentGroupVersions {
  19. if baseEnvironmentGroup.Version > highestVersionEnvironmentGroup.Version {
  20. highestVersionEnvironmentGroup = baseEnvironmentGroup
  21. }
  22. }
  23. telemetry.WithAttributes(span,
  24. telemetry.AttributeKV{Key: "highest-version", Value: highestVersionEnvironmentGroup.Version},
  25. telemetry.AttributeKV{Key: "highest-version-name", Value: highestVersionEnvironmentGroup.Name},
  26. )
  27. return highestVersionEnvironmentGroup, nil
  28. }
  29. // EnvironmentGroupInTargetNamespaceInput contains all information required to check if an environment group exists in a target namespace.
  30. // If you are looking for envrionment groups in the base namespace, consider using LatestBaseEnvironmentGroup or ListBaseEnvironmentGroups instead
  31. type EnvironmentGroupInTargetNamespaceInput struct {
  32. // Name is the environment group name which can be found on the configmap label
  33. Name string
  34. Version int
  35. Namespace string
  36. }
  37. // EnvironmentGroupInTargetNamespace checks if an environment group of a specific name and version exists in a target namespace.
  38. // If an environment group exists, it will be returned
  39. func EnvironmentGroupInTargetNamespace(ctx context.Context, a *kubernetes.Agent, inp EnvironmentGroupInTargetNamespaceInput) (EnvironmentGroup, error) {
  40. ctx, span := telemetry.NewSpan(ctx, "env-group-in-target-namespace")
  41. defer span.End()
  42. var eg EnvironmentGroup
  43. if inp.Name == "" {
  44. return eg, telemetry.Error(ctx, span, nil, "must provide an environment group name")
  45. }
  46. if inp.Version == 0 {
  47. return eg, telemetry.Error(ctx, span, nil, "must provide an environment group version to check for")
  48. }
  49. if inp.Namespace == "" {
  50. return eg, telemetry.Error(ctx, span, nil, "must provide a namespace to check")
  51. }
  52. telemetry.WithAttributes(span,
  53. telemetry.AttributeKV{Key: "environment-group-name", Value: inp.Name},
  54. telemetry.AttributeKV{Key: "environment-group-version", Value: inp.Version},
  55. telemetry.AttributeKV{Key: "namespace", Value: inp.Namespace},
  56. )
  57. environmentGroups, err := ListEnvironmentGroups(ctx, a, WithEnvironmentGroupName(inp.Name), WithEnvironmentGroupVersion(inp.Version), WithNamespace(inp.Namespace))
  58. if err != nil {
  59. return eg, telemetry.Error(ctx, span, err, "unable to list environment groups in target namespace")
  60. }
  61. if len(environmentGroups) > 1 {
  62. telemetry.WithAttributes(span,
  63. telemetry.AttributeKV{Key: "expected-results", Value: 1},
  64. telemetry.AttributeKV{Key: "actual-results", Value: len(environmentGroups)},
  65. )
  66. return eg, telemetry.Error(ctx, span, nil, "unexpected number of versions found in namespace")
  67. }
  68. if len(environmentGroups) == 1 {
  69. return environmentGroups[0], nil
  70. }
  71. return eg, nil
  72. }