chart.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 helm
  15. import (
  16. "bytes"
  17. "encoding/json"
  18. "html/template"
  19. "path/filepath"
  20. "strings"
  21. "github.com/compose-spec/compose-go/types"
  22. "github.com/docker/compose-cli/kube/resources"
  23. "gopkg.in/yaml.v3"
  24. chart "helm.sh/helm/v3/pkg/chart"
  25. loader "helm.sh/helm/v3/pkg/chart/loader"
  26. util "helm.sh/helm/v3/pkg/chartutil"
  27. "k8s.io/apimachinery/pkg/runtime"
  28. )
  29. //ConvertToChart convert Kube objects to helm chart
  30. func ConvertToChart(name string, objects map[string]runtime.Object) (*chart.Chart, error) {
  31. files := []*loader.BufferedFile{
  32. {
  33. Name: "README.md",
  34. Data: []byte("This chart was created by converting a Compose file"),
  35. }}
  36. chart := `name: {{.Name}}
  37. description: A generated Helm Chart for {{.Name}} from Skippbox Kompose
  38. version: 0.0.1
  39. apiVersion: v1
  40. keywords:
  41. - {{.Name}}
  42. sources:
  43. home:
  44. `
  45. t, err := template.New("ChartTmpl").Parse(chart)
  46. if err != nil {
  47. return nil, err
  48. }
  49. type ChartDetails struct {
  50. Name string
  51. }
  52. var chartData bytes.Buffer
  53. err = t.Execute(&chartData, ChartDetails{Name: name})
  54. if err != nil {
  55. return nil, err
  56. }
  57. files = append(files, &loader.BufferedFile{
  58. Name: "Chart.yaml",
  59. Data: chartData.Bytes(),
  60. })
  61. for name, o := range objects {
  62. j, err := json.Marshal(o)
  63. if err != nil {
  64. return nil, err
  65. }
  66. buf, err := jsonToYaml(j, 2)
  67. if err != nil {
  68. return nil, err
  69. }
  70. files = append(files, &loader.BufferedFile{
  71. Name: filepath.Join("templates", name),
  72. Data: buf,
  73. })
  74. }
  75. return loader.LoadFiles(files)
  76. }
  77. // Convert JSON to YAML.
  78. func jsonToYaml(j []byte, spaces int) ([]byte, error) {
  79. // Convert the JSON to an object.
  80. var jsonObj interface{}
  81. // We are using yaml.Unmarshal here (instead of json.Unmarshal) because the
  82. // Go JSON library doesn't try to pick the right number type (int, float,
  83. // etc.) when unmarshling to interface{}, it just picks float64
  84. // universally. go-yaml does go through the effort of picking the right
  85. // number type, so we can preserve number type throughout this process.
  86. err := yaml.Unmarshal(j, &jsonObj)
  87. if err != nil {
  88. return nil, err
  89. }
  90. var b bytes.Buffer
  91. encoder := yaml.NewEncoder(&b)
  92. encoder.SetIndent(spaces)
  93. if err := encoder.Encode(jsonObj); err != nil {
  94. return nil, err
  95. }
  96. return b.Bytes(), nil
  97. }
  98. // GetChartInMemory get memory representation of helm chart
  99. func GetChartInMemory(project *types.Project) (*chart.Chart, error) {
  100. // replace _ with - in volume names
  101. for k, v := range project.Volumes {
  102. volumeName := strings.ReplaceAll(k, "_", "-")
  103. if volumeName != k {
  104. project.Volumes[volumeName] = v
  105. delete(project.Volumes, k)
  106. }
  107. }
  108. objects, err := resources.MapToKubernetesObjects(project)
  109. if err != nil {
  110. return nil, err
  111. }
  112. //in memory files
  113. return ConvertToChart(project.Name, objects)
  114. }
  115. // SaveChart converts compose project to helm and saves the chart
  116. func SaveChart(project *types.Project, dest string) error {
  117. chart, err := GetChartInMemory(project)
  118. if err != nil {
  119. return err
  120. }
  121. return util.SaveDir(chart, dest)
  122. }
  123. // GenerateChart generates helm chart from Compose project
  124. func GenerateChart(project *types.Project, dirname string) error {
  125. if strings.Contains(dirname, ".") {
  126. splits := strings.SplitN(dirname, ".", 2)
  127. dirname = splits[0]
  128. }
  129. dirname = filepath.Dir(dirname)
  130. return SaveChart(project, dirname)
  131. }