2
0

sync.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. package environment_groups
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/porter-dev/porter/internal/kubernetes"
  6. "github.com/porter-dev/porter/internal/telemetry"
  7. )
  8. // SyncLatestVersionToNamespaceInput contains all information required to sync an environment group from the porter-env-group namespace
  9. // to the given target namespace
  10. type SyncLatestVersionToNamespaceInput struct {
  11. BaseEnvironmentGroupName string
  12. TargetNamespace string
  13. }
  14. // SyncLatestVersionToNamespaceOutput returns the literal configmap name (as opposed to the environment group name) which can be used in kubernetes manifests
  15. // for loading the configmap (or secret) into a deployment, or job
  16. type SyncLatestVersionToNamespaceOutput struct {
  17. // EnvironmentGroupVersionedName is the name of the secret and configmap which should be able to be used in kubernetes manifests. This must already exist as both a configmap and a secret in the given namespace before being returned.
  18. // EnvironmentGroupVersionedName will be of the format "<environment-group-name>.<version>"
  19. EnvironmentGroupVersionedName string
  20. }
  21. // SyncLatestVersionToNamespace gets the latest version of a given environment group, and makes a copy of it in the target
  22. // namespace. If the versions match, no changes will be made. In either case, the name of an environment group in the target namespace will be returned
  23. // unless an error has occurred.
  24. func SyncLatestVersionToNamespace(ctx context.Context, a *kubernetes.Agent, inp SyncLatestVersionToNamespaceInput, additionalLabels map[string]string) (SyncLatestVersionToNamespaceOutput, error) {
  25. ctx, span := telemetry.NewSpan(ctx, "sync-env-group-version-to-namespace")
  26. defer span.End()
  27. var output SyncLatestVersionToNamespaceOutput
  28. if inp.BaseEnvironmentGroupName == "" {
  29. return output, nil
  30. }
  31. if inp.TargetNamespace == "" {
  32. return output, telemetry.Error(ctx, span, nil, "must provide a target environment group namespace to sync to")
  33. }
  34. telemetry.WithAttributes(span,
  35. telemetry.AttributeKV{Key: "environment-group-name", Value: inp.BaseEnvironmentGroupName},
  36. telemetry.AttributeKV{Key: "target-environment-namespace", Value: inp.TargetNamespace},
  37. )
  38. baseEnvironmentGroup, err := latestBaseEnvironmentGroup(ctx, a, inp.BaseEnvironmentGroupName)
  39. if err != nil {
  40. return output, telemetry.Error(ctx, span, err, "unable to find latest environment group version")
  41. }
  42. envGroupInp := EnvironmentGroupInTargetNamespaceInput{
  43. Name: baseEnvironmentGroup.Name,
  44. Version: baseEnvironmentGroup.Version,
  45. Namespace: inp.TargetNamespace,
  46. }
  47. targetEnvironmentGroup, err := EnvironmentGroupInTargetNamespace(ctx, a, envGroupInp)
  48. if err != nil {
  49. return output, telemetry.Error(ctx, span, err, "unable to get environement group in target namespace")
  50. }
  51. if targetEnvironmentGroup.Name == baseEnvironmentGroup.Name && targetEnvironmentGroup.Version == baseEnvironmentGroup.Version {
  52. return SyncLatestVersionToNamespaceOutput{
  53. EnvironmentGroupVersionedName: fmt.Sprintf("%s.%d", baseEnvironmentGroup.Name, baseEnvironmentGroup.Version),
  54. }, nil
  55. }
  56. targetConfigmapName, err := createEnvironmentGroupInTargetNamespace(ctx, a, inp.TargetNamespace, baseEnvironmentGroup, additionalLabels)
  57. if err != nil {
  58. return output, telemetry.Error(ctx, span, err, "unable to create environment group in target namespace")
  59. }
  60. output = SyncLatestVersionToNamespaceOutput{
  61. EnvironmentGroupVersionedName: targetConfigmapName,
  62. }
  63. return output, nil
  64. }