chart.go 2.7 KB

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