chart.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. "gopkg.in/yaml.v3"
  21. chart "helm.sh/helm/v3/pkg/chart"
  22. loader "helm.sh/helm/v3/pkg/chart/loader"
  23. "k8s.io/apimachinery/pkg/runtime"
  24. )
  25. func ConvertToChart(name string, objects map[string]runtime.Object) (*chart.Chart, error) {
  26. files := []*loader.BufferedFile{
  27. &loader.BufferedFile{
  28. Name: "README.md",
  29. Data: []byte("This chart was created by converting a Compose file"),
  30. }}
  31. chart := `name: {{.Name}}
  32. description: A generated Helm Chart for {{.Name}} from Skippbox Kompose
  33. version: 0.0.1
  34. apiVersion: v1
  35. keywords:
  36. - {{.Name}}
  37. sources:
  38. home:
  39. `
  40. t, err := template.New("ChartTmpl").Parse(chart)
  41. if err != nil {
  42. return nil, err
  43. }
  44. type ChartDetails struct {
  45. Name string
  46. }
  47. var chartData bytes.Buffer
  48. err = t.Execute(&chartData, ChartDetails{Name: name})
  49. if err != nil {
  50. return nil, err
  51. }
  52. files = append(files, &loader.BufferedFile{
  53. Name: "Chart.yaml",
  54. Data: chartData.Bytes(),
  55. })
  56. for name, o := range objects {
  57. j, err := json.Marshal(o)
  58. if err != nil {
  59. return nil, err
  60. }
  61. buf, err := jsonToYaml(j, 2)
  62. if err != nil {
  63. return nil, err
  64. }
  65. files = append(files, &loader.BufferedFile{
  66. Name: filepath.Join("templates", name),
  67. Data: buf,
  68. })
  69. }
  70. return loader.LoadFiles(files)
  71. }
  72. // Convert JSON to YAML.
  73. func jsonToYaml(j []byte, spaces int) ([]byte, error) {
  74. // Convert the JSON to an object.
  75. var jsonObj interface{}
  76. // We are using yaml.Unmarshal here (instead of json.Unmarshal) because the
  77. // Go JSON library doesn't try to pick the right number type (int, float,
  78. // etc.) when unmarshling to interface{}, it just picks float64
  79. // universally. go-yaml does go through the effort of picking the right
  80. // number type, so we can preserve number type throughout this process.
  81. err := yaml.Unmarshal(j, &jsonObj)
  82. if err != nil {
  83. return nil, err
  84. }
  85. var b bytes.Buffer
  86. encoder := yaml.NewEncoder(&b)
  87. encoder.SetIndent(spaces)
  88. if err := encoder.Encode(jsonObj); err != nil {
  89. return nil, err
  90. }
  91. return b.Bytes(), nil
  92. }