@@ -16,6 +16,7 @@ import (
1616 "github.com/docker/app/internal/types"
1717 conversion "github.com/docker/cli/cli/command/stack/kubernetes"
1818 "github.com/docker/cli/cli/compose/loader"
19+ "github.com/docker/cli/kubernetes/compose/v1beta1"
1920 "github.com/docker/cli/kubernetes/compose/v1beta2"
2021 "github.com/pkg/errors"
2122 yaml "gopkg.in/yaml.v2"
@@ -38,6 +39,13 @@ post-process the serialized yaml to replace all 'template_'-prefixed keys
3839with the appropriate content (value or template)
3940*/
4041
42+ const (
43+ // V1Beta1 is the string identifier for the v1beta1 version of the stack spec
44+ V1Beta1 = "v1beta1"
45+ // V1Beta2 is the string identifier for the v1beta2 version of the stack spec
46+ V1Beta2 = "v1beta2"
47+ )
48+
4149type helmMaintainer struct {
4250 Name string
4351}
@@ -207,21 +215,44 @@ func makeChart(appname, targetDir string) error {
207215 return ioutil .WriteFile (filepath .Join (targetDir , "Chart.yaml" ), hmetadata , 0644 )
208216}
209217
210- func helmRender (appname string , targetDir string , composeFiles []string , settingsFile []string , env map [string ]string ) error {
218+ func helmRender (appname string , targetDir string , composeFiles []string , settingsFile []string , env map [string ]string , stackVersion string ) error {
211219 rendered , err := Render (appname , composeFiles , settingsFile , env )
212220 if err != nil {
213221 return err
214222 }
215- stackSpec := conversion .FromComposeConfig (rendered )
216- stack := v1beta2.Stack {
217- TypeMeta : metav1.TypeMeta {
218- Kind : "stacks.compose.docker.com" ,
219- APIVersion : "v1beta2" ,
220- },
221- ObjectMeta : metav1.ObjectMeta {
222- Name : internal .AppNameFromDir (appname ),
223- },
224- Spec : stackSpec ,
223+ var stack interface {}
224+ switch stackVersion {
225+ case V1Beta2 :
226+ stackSpec := conversion .FromComposeConfig (rendered )
227+ stack = v1beta2.Stack {
228+ TypeMeta : metav1.TypeMeta {
229+ Kind : "stacks.compose.docker.com" ,
230+ APIVersion : V1Beta2 ,
231+ },
232+ ObjectMeta : metav1.ObjectMeta {
233+ Name : internal .AppNameFromDir (appname ),
234+ },
235+ Spec : stackSpec ,
236+ }
237+ case V1Beta1 :
238+ composeFile , err := yaml .Marshal (rendered )
239+ if err != nil {
240+ return err
241+ }
242+ stack = v1beta1.Stack {
243+ TypeMeta : metav1.TypeMeta {
244+ Kind : "stacks.compose.docker.com" ,
245+ APIVersion : V1Beta1 ,
246+ },
247+ ObjectMeta : metav1.ObjectMeta {
248+ Name : internal .AppNameFromDir (appname ),
249+ },
250+ Spec : v1beta1.StackSpec {
251+ ComposeFile : string (composeFile ),
252+ },
253+ }
254+ default :
255+ return fmt .Errorf ("invalid stack version %q" , stackVersion )
225256 }
226257 stackData , err := yaml .Marshal (stack )
227258 if err != nil {
@@ -231,7 +262,7 @@ func helmRender(appname string, targetDir string, composeFiles []string, setting
231262}
232263
233264//makeStack converts data into a helm template for a stack
234- func makeStack (appname string , targetDir string , data []byte ) error {
265+ func makeStack (appname string , targetDir string , data []byte , stackVersion string ) error {
235266 parsed , err := loader .ParseYAML (data )
236267 if err != nil {
237268 return errors .Wrap (err , "failed to parse template compose" )
@@ -241,17 +272,41 @@ func makeStack(appname string, targetDir string, data []byte) error {
241272 return errors .Wrap (err , "failed to load template compose" )
242273 }
243274 os .Mkdir (filepath .Join (targetDir , "templates" ), 0755 )
244- stackSpec := templateconversion .FromComposeConfig (rendered )
245- stack := templatev1beta2.Stack {
246- TypeMeta : metav1.TypeMeta {
247- Kind : "stacks.compose.docker.com" ,
248- APIVersion : "v1beta2" ,
249- },
250- ObjectMeta : metav1.ObjectMeta {
251- Name : internal .AppNameFromDir (appname ),
252- Namespace : "default" , // FIXME
253- },
254- Spec : stackSpec ,
275+ var stack interface {}
276+ switch stackVersion {
277+ case V1Beta2 :
278+ stackSpec := templateconversion .FromComposeConfig (rendered )
279+ stack = templatev1beta2.Stack {
280+ TypeMeta : metav1.TypeMeta {
281+ Kind : "stacks.compose.docker.com" ,
282+ APIVersion : V1Beta2 ,
283+ },
284+ ObjectMeta : metav1.ObjectMeta {
285+ Name : internal .AppNameFromDir (appname ),
286+ Namespace : "default" , // FIXME
287+ },
288+ Spec : stackSpec ,
289+ }
290+ case V1Beta1 :
291+ composeFile , err := yaml .Marshal (rendered )
292+ if err != nil {
293+ return err
294+ }
295+ stack = v1beta1.Stack {
296+ TypeMeta : metav1.TypeMeta {
297+ Kind : "stacks.compose.docker.com" ,
298+ APIVersion : V1Beta1 ,
299+ },
300+ ObjectMeta : metav1.ObjectMeta {
301+ Name : internal .AppNameFromDir (appname ),
302+ Namespace : "default" , // FIXME
303+ },
304+ Spec : v1beta1.StackSpec {
305+ ComposeFile : string (composeFile ),
306+ },
307+ }
308+ default :
309+ return fmt .Errorf ("invalid stack version %q" , stackVersion )
255310 }
256311 stackData , err := yaml .Marshal (stack )
257312 if err != nil {
@@ -274,7 +329,7 @@ func makeStack(appname string, targetDir string, data []byte) error {
274329}
275330
276331// Helm renders an app as an Helm Chart
277- func Helm (appname string , composeFiles []string , settingsFile []string , env map [string ]string , render bool ) error {
332+ func Helm (appname string , composeFiles []string , settingsFile []string , env map [string ]string , render bool , stackVersion string ) error {
278333 targetDir := internal .AppNameFromDir (appname ) + ".chart"
279334 if err := os .Mkdir (targetDir , 0755 ); err != nil && ! os .IsExist (err ) {
280335 return errors .Wrap (err , "failed to create Chart directory" )
@@ -284,7 +339,7 @@ func Helm(appname string, composeFiles []string, settingsFile []string, env map[
284339 return err
285340 }
286341 if render {
287- return helmRender (appname , targetDir , composeFiles , settingsFile , env )
342+ return helmRender (appname , targetDir , composeFiles , settingsFile , env , stackVersion )
288343 }
289344 data , err := ioutil .ReadFile (filepath .Join (appname , internal .ComposeFileName ))
290345 if err != nil {
@@ -294,7 +349,7 @@ func Helm(appname string, composeFiles []string, settingsFile []string, env map[
294349 if err != nil {
295350 return errors .Wrap (err , "failed to parse docker-compose.yml, maybe because it is a template" )
296351 }
297- err = makeStack (appname , targetDir , data )
352+ err = makeStack (appname , targetDir , data , stackVersion )
298353 if err != nil {
299354 return err
300355 }
0 commit comments