shared.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. package deploy
  2. import (
  3. "context"
  4. "fmt"
  5. api "github.com/porter-dev/porter/api/client"
  6. "github.com/porter-dev/porter/api/types"
  7. )
  8. // SharedOpts are common options for build, create, and deploy agents
  9. type SharedOpts struct {
  10. ProjectID uint
  11. ClusterID uint
  12. Namespace string
  13. LocalPath string
  14. LocalDockerfile string
  15. OverrideTag string
  16. Method DeployBuildType
  17. AdditionalEnv map[string]string
  18. EnvGroups []types.EnvGroupMeta
  19. UseCache bool
  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. originalEnvConfig, err := GetNestedMap(config, "container", "env", "normal")
  29. if err != nil || originalEnvConfig == nil {
  30. originalEnvConfig = make(map[string]interface{})
  31. }
  32. for _, group := range envGroups {
  33. if group.Name == "" {
  34. return fmt.Errorf("env group name cannot be empty")
  35. }
  36. envGroup, err := client.GetEnvGroup(
  37. context.Background(),
  38. projectID,
  39. clusterID,
  40. namespace,
  41. &types.GetEnvGroupRequest{
  42. Name: group.Name,
  43. Version: group.Version,
  44. },
  45. )
  46. if err != nil {
  47. return err
  48. }
  49. envConfig, err := GetNestedMap(config, "container", "env", "normal")
  50. if err != nil || envConfig == nil {
  51. envConfig = make(map[string]interface{})
  52. }
  53. for k, v := range envGroup.Variables {
  54. // If original env config already have the value, do not override
  55. if _, ok := originalEnvConfig[k]; ok {
  56. continue
  57. }
  58. envConfig[k] = v
  59. }
  60. containerMap, _ := config["container"].(map[string]interface{})
  61. envMap, _ := containerMap["env"].(map[string]interface{})
  62. envMap["normal"] = envConfig
  63. }
  64. return nil
  65. }