mirror of
https://github.com/kubevela/kubevela.git
synced 2026-08-19 04:26:39 +00:00
fix bugs on pkg/appfile (#409)
* fix bugs on pkg/appfile Signed-off-by: Hongchao Deng <hongchaodeng1@gmail.com> * fix
This commit is contained in:
+38
-17
@@ -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
|
||||
|
||||
}
|
||||
|
||||
+27
-12
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user