From 38dc6fe73ea0faca3975d7f1dc6f4fd516d06486 Mon Sep 17 00:00:00 2001 From: Hongchao Deng Date: Mon, 19 Oct 2020 22:55:57 -0700 Subject: [PATCH] fix bugs on pkg/appfile (#409) * fix bugs on pkg/appfile Signed-off-by: Hongchao Deng * fix --- pkg/appfile/appfile.go | 55 +++++++++++++++++++++++++++++------------- pkg/appfile/service.go | 39 +++++++++++++++++++++--------- 2 files changed, 65 insertions(+), 29 deletions(-) diff --git a/pkg/appfile/appfile.go b/pkg/appfile/appfile.go index 31296eb8c..f2b69cecf 100644 --- a/pkg/appfile/appfile.go +++ b/pkg/appfile/appfile.go @@ -24,6 +24,13 @@ type AppFile struct { Secrets map[string]string `json:"secrets"` } +func NewAppFile() *AppFile { + return &AppFile{ + Services: make(map[string]Service), + Secrets: make(map[string]string), + } +} + func Load() (*AppFile, error) { return LoadFromFile(DefaultAppfilePath) } @@ -33,26 +40,27 @@ func LoadFromFile(filename string) (*AppFile, error) { if err != nil { return nil, err } - af := &AppFile{} + af := NewAppFile() err = yaml.Unmarshal(b, af) if err != nil { return nil, err } - return af, af.Validate() -} - -func (app *AppFile) Validate() error { - if app.Name == "" { - return errors.New("name is required") - } - if len(app.Services) == 0 { - return errors.New("at least one service component is required") - } - return nil + return af, nil } // BuildOAM renders Appfile into AppConfig, Components. It also builds images for services if defined. func (app *AppFile) BuildOAM(ns string, io cmdutil.IOStreams) ( + []*v1alpha2.Component, *v1alpha2.ApplicationConfiguration, error) { + return app.buildOAM(ns, io, true) +} + +// RenderOAM renders Appfile into AppConfig, Components. +func (app *AppFile) RenderOAM(ns string, io cmdutil.IOStreams) ( + []*v1alpha2.Component, *v1alpha2.ApplicationConfiguration, error) { + return app.buildOAM(ns, io, false) +} + +func (app *AppFile) buildOAM(ns string, io cmdutil.IOStreams, buildImage bool) ( []*v1alpha2.Component, *v1alpha2.ApplicationConfiguration, error) { io.Info("Loading templates ...") tm, err := template.Load() @@ -71,14 +79,28 @@ func (app *AppFile) BuildOAM(ns string, io cmdutil.IOStreams) ( for sname, svc := range app.GetServices() { build := svc.GetBuild() + var image string + if build != nil { + image = build.Image - io.Infof("\nBuilding service (%s)...\n", sname) - if err := build.BuildImage(io); err != nil { - return nil, nil, err + if buildImage { + io.Infof("\nBuilding service (%s)...\n", sname) + if err := build.BuildImage(io); err != nil { + return nil, nil, err + } + } + } + if image == "" { + v, ok := svc["image"] + if ok { + image = v.(string) + } else { + return nil, nil, errors.New("no image is defined") + } } io.Infof("\nRendering component configs for service (%s)...\n", sname) - acComp, comp, err := svc.RenderService(tm, app.Name, ns, build.Image) + acComp, comp, err := svc.RenderService(tm, sname, ns, image) if err != nil { return nil, nil, err } @@ -87,5 +109,4 @@ func (app *AppFile) BuildOAM(ns string, io cmdutil.IOStreams) ( } return comps, appConfig, nil - } diff --git a/pkg/appfile/service.go b/pkg/appfile/service.go index 3bc07a93a..f8dec6ba9 100644 --- a/pkg/appfile/service.go +++ b/pkg/appfile/service.go @@ -18,6 +18,29 @@ import ( type Service map[string]interface{} +const DefaultWorkloadType = "webservice" + +func (s Service) GetType() string { + t, ok := s["type"] + if !ok { + return DefaultWorkloadType + } + return t.(string) +} + +func (s Service) GetConfig() map[string]interface{} { + config := make(map[string]interface{}) +outerLoop: + for k, v := range s { + switch k { + case "build", "type": // skip + continue outerLoop + } + config[k] = v + } + return config +} + func (s Service) GetBuild() *Build { v, ok := s["build"] if !ok { @@ -44,22 +67,14 @@ func (s Service) RenderService(tm template.Manager, name, ns, image string) ( workloadKeys := map[string]interface{}{} traitKeys := map[string]interface{}{} - wtype := "webservice" - -outerLoop: - for k, v := range s { - switch k { - case "build": // skip - continue outerLoop - case "type": - wtype = v.(string) - } + wtype := s.GetType() + for k, v := range s.GetConfig() { if tm.IsTrait(k) { traitKeys[k] = v - } else if tm.IsWorkload(k) { - workloadKeys[k] = v + continue } + workloadKeys[k] = v } // render component