shared.go 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. package deploy
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/fatih/color"
  6. api "github.com/porter-dev/porter/api/client"
  7. "github.com/porter-dev/porter/api/types"
  8. )
  9. // SharedOpts are common options for build, create, and deploy agents
  10. type SharedOpts struct {
  11. ProjectID uint
  12. ClusterID uint
  13. Namespace string
  14. LocalPath string
  15. LocalDockerfile string
  16. OverrideTag string
  17. Method DeployBuildType
  18. AdditionalEnv map[string]string
  19. EnvGroups []types.EnvGroupMeta
  20. }
  21. func coalesceEnvGroups(
  22. client *api.Client,
  23. projectID, clusterID uint,
  24. namespace string,
  25. envGroups []types.EnvGroupMeta,
  26. config map[string]interface{},
  27. ) error {
  28. for _, group := range envGroups {
  29. if group.Name == "" {
  30. return fmt.Errorf("env group name cannot be empty")
  31. }
  32. envGroup, err := client.GetEnvGroup(
  33. context.Background(),
  34. projectID,
  35. clusterID,
  36. namespace,
  37. &types.GetEnvGroupRequest{
  38. Name: group.Name,
  39. Version: group.Version,
  40. },
  41. )
  42. if err != nil && err.Error() == "env group not found" {
  43. if group.Namespace == "" {
  44. return fmt.Errorf("env group namespace cannot be empty")
  45. }
  46. color.New(color.FgBlue, color.Bold).
  47. Printf("Env group '%s' does not exist in the target namespace '%s'\n", group.Name, namespace)
  48. color.New(color.FgBlue, color.Bold).
  49. Printf("Cloning env group '%s' from namespace '%s' to target namespace '%s'\n",
  50. group.Name, group.Namespace, namespace)
  51. envGroup, err = client.CloneEnvGroup(
  52. context.Background(), projectID, clusterID, group.Namespace,
  53. &types.CloneEnvGroupRequest{
  54. Name: group.Name,
  55. Namespace: namespace,
  56. },
  57. )
  58. if err != nil {
  59. return err
  60. }
  61. } else if err != nil {
  62. return err
  63. }
  64. envConfig, err := getNestedMap(config, "container", "env", "normal")
  65. if err != nil || envConfig == nil {
  66. envConfig = make(map[string]interface{})
  67. }
  68. for k, v := range envGroup.Variables {
  69. envConfig[k] = v
  70. }
  71. containerMap, _ := config["container"].(map[string]interface{})
  72. envMap, _ := containerMap["env"].(map[string]interface{})
  73. envMap["normal"] = envConfig
  74. }
  75. return nil
  76. }