charts.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. // +build kube
  2. /*
  3. Copyright 2020 Docker Compose CLI authors
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. */
  14. package charts
  15. import (
  16. "os"
  17. "path/filepath"
  18. "strings"
  19. "github.com/compose-spec/compose-go/types"
  20. "github.com/docker/compose-cli/api/compose"
  21. "github.com/docker/compose-cli/api/context/store"
  22. "github.com/docker/compose-cli/kube/charts/helm"
  23. "github.com/docker/compose-cli/kube/charts/kubernetes"
  24. chart "helm.sh/helm/v3/pkg/chart"
  25. util "helm.sh/helm/v3/pkg/chartutil"
  26. helmenv "helm.sh/helm/v3/pkg/cli"
  27. )
  28. //SDK chart SDK
  29. type SDK struct {
  30. h *helm.Actions
  31. environment map[string]string
  32. }
  33. // NewSDK new chart SDK
  34. func NewSDK(ctx store.KubeContext) (SDK, error) {
  35. return SDK{
  36. environment: environment(),
  37. h: helm.NewHelmActions(nil),
  38. }, nil
  39. }
  40. // Install deploys a Compose stack
  41. func (s SDK) Install(project *types.Project) error {
  42. chart, err := s.GetChartInMemory(project)
  43. if err != nil {
  44. return err
  45. }
  46. return s.h.InstallChart(project.Name, chart)
  47. }
  48. // Uninstall removes a runnign compose stack
  49. func (s SDK) Uninstall(projectName string) error {
  50. return s.h.Uninstall(projectName)
  51. }
  52. // List returns a list of compose stacks
  53. func (s SDK) List() ([]compose.Stack, error) {
  54. return s.h.ListReleases()
  55. }
  56. // GetDefaultEnv initializes Helm EnvSettings
  57. func (s SDK) GetDefaultEnv() *helmenv.EnvSettings {
  58. return helmenv.New()
  59. }
  60. // GetChartInMemory get memory representation of helm chart
  61. func (s SDK) GetChartInMemory(project *types.Project) (*chart.Chart, error) {
  62. // replace _ with - in volume names
  63. for k, v := range project.Volumes {
  64. volumeName := strings.ReplaceAll(k, "_", "-")
  65. if volumeName != k {
  66. project.Volumes[volumeName] = v
  67. delete(project.Volumes, k)
  68. }
  69. }
  70. objects, err := kubernetes.MapToKubernetesObjects(project)
  71. if err != nil {
  72. return nil, err
  73. }
  74. //in memory files
  75. return helm.ConvertToChart(project.Name, objects)
  76. }
  77. // SaveChart converts compose project to helm and saves the chart
  78. func (s SDK) SaveChart(project *types.Project, dest string) error {
  79. chart, err := s.GetChartInMemory(project)
  80. if err != nil {
  81. return err
  82. }
  83. return util.SaveDir(chart, dest)
  84. }
  85. // GenerateChart generates helm chart from Compose project
  86. func (s SDK) GenerateChart(project *types.Project, dirname string) error {
  87. if strings.Contains(dirname, ".") {
  88. splits := strings.SplitN(dirname, ".", 2)
  89. dirname = splits[0]
  90. }
  91. dirname = filepath.Dir(dirname)
  92. return s.SaveChart(project, dirname)
  93. }
  94. func environment() map[string]string {
  95. vars := make(map[string]string)
  96. env := os.Environ()
  97. for _, v := range env {
  98. k := strings.SplitN(v, "=", 2)
  99. vars[k[0]] = k[1]
  100. }
  101. return vars
  102. }