|
| 1 | +package packager |
| 2 | + |
| 3 | +import ( |
| 4 | + "io/ioutil" |
| 5 | + "os" |
| 6 | + "path" |
| 7 | + |
| 8 | + conversion "github.com/docker/cli/cli/command/stack/kubernetes" |
| 9 | + "github.com/docker/cli/kubernetes/compose/v1beta2" |
| 10 | + "github.com/docker/lunchbox/types" |
| 11 | + yaml "gopkg.in/yaml.v2" |
| 12 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 13 | +) |
| 14 | + |
| 15 | +type helmMaintainer struct { |
| 16 | + Name string |
| 17 | +} |
| 18 | + |
| 19 | +type helmMeta struct { |
| 20 | + Name string |
| 21 | + Version string |
| 22 | + Description string |
| 23 | + Keywords []string |
| 24 | + Maintainers []helmMaintainer |
| 25 | +} |
| 26 | + |
| 27 | +func toHelmMeta(meta *types.AppMetadata) (*helmMeta, error) { |
| 28 | + return &helmMeta{ |
| 29 | + Name: meta.Application.Name, |
| 30 | + Version: meta.Version, |
| 31 | + Description: meta.Application.Description, |
| 32 | + Keywords: meta.Application.Labels, |
| 33 | + Maintainers: []helmMaintainer{{Name: meta.Application.Author}}, |
| 34 | + }, nil |
| 35 | +} |
| 36 | + |
| 37 | +// Helm renders an app as an Helm Chart |
| 38 | +func Helm(appname string, composeFiles []string, settingsFile []string, env map[string]string) error { |
| 39 | + appname, cleanup, err := Extract(appname) |
| 40 | + if err != nil { |
| 41 | + return err |
| 42 | + } |
| 43 | + defer cleanup() |
| 44 | + rendered, err := Render(appname, composeFiles, settingsFile, env) |
| 45 | + if err != nil { |
| 46 | + return err |
| 47 | + } |
| 48 | + metaFile := path.Join(appname, "metadata.yml") |
| 49 | + metaContent, err := ioutil.ReadFile(metaFile) |
| 50 | + if err != nil { |
| 51 | + return err |
| 52 | + } |
| 53 | + var meta types.AppMetadata |
| 54 | + err = yaml.Unmarshal(metaContent, &meta) |
| 55 | + if err != nil { |
| 56 | + return err |
| 57 | + } |
| 58 | + targetDir := appName(appname) + ".helm" |
| 59 | + os.Mkdir(targetDir, 0755) |
| 60 | + hmeta, err := toHelmMeta(&meta) |
| 61 | + if err != nil { |
| 62 | + return err |
| 63 | + } |
| 64 | + hmetadata, err := yaml.Marshal(hmeta) |
| 65 | + if err != nil { |
| 66 | + return err |
| 67 | + } |
| 68 | + err = ioutil.WriteFile(path.Join(targetDir, "Chart.yaml"), hmetadata, 0644) |
| 69 | + if err != nil { |
| 70 | + return err |
| 71 | + } |
| 72 | + os.Mkdir(path.Join(targetDir, "templates"), 0755) |
| 73 | + stackSpec := conversion.FromComposeConfig(rendered) |
| 74 | + stack := v1beta2.Stack{ |
| 75 | + TypeMeta: metav1.TypeMeta{ |
| 76 | + Kind: "stacks.compose.docker.com", |
| 77 | + APIVersion: "v1beta2", |
| 78 | + }, |
| 79 | + ObjectMeta: metav1.ObjectMeta{ |
| 80 | + Name: appName(appname), |
| 81 | + Namespace: "default", // FIXME |
| 82 | + }, |
| 83 | + Spec: stackSpec, |
| 84 | + } |
| 85 | + stackData, err := yaml.Marshal(stack) |
| 86 | + if err != nil { |
| 87 | + return err |
| 88 | + } |
| 89 | + err = ioutil.WriteFile(path.Join(targetDir, "templates", "stack.yaml"), stackData, 0644) |
| 90 | + return err |
| 91 | +} |
0 commit comments