-
Notifications
You must be signed in to change notification settings - Fork 872
/
appfile.go
161 lines (142 loc) · 4.49 KB
/
appfile.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*
Copyright 2021 The KubeVela Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package api
import (
"encoding/json"
"errors"
"os"
"path/filepath"
"strings"
"time"
"sigs.k8s.io/yaml"
"github.com/oam-dev/kubevela/apis/core.oam.dev/common"
"github.com/oam-dev/kubevela/apis/core.oam.dev/v1beta1"
"github.com/oam-dev/kubevela/pkg/builtin"
cmdutil "github.com/oam-dev/kubevela/pkg/utils/util"
"github.com/oam-dev/kubevela/references/appfile/template"
)
// error msg used in Appfile
var (
ErrImageNotDefined = errors.New("image not defined")
)
// DefaultAppfilePath defines the default file path that used by `vela up` command
const (
DefaultJSONAppfilePath = "./vela.json"
DefaultAppfilePath = "./vela.yaml"
DefaultUnknowFormatAppfilePath = "./Appfile"
)
const (
// DefaultHealthScopeKey is the key in application for default health scope
DefaultHealthScopeKey = "healthscopes.core.oam.dev"
)
// AppFile defines the spec of KubeVela Appfile
type AppFile struct {
Name string `json:"name"`
CreateTime time.Time `json:"createTime,omitempty"`
UpdateTime time.Time `json:"updateTime,omitempty"`
Services map[string]Service `json:"services"`
Secrets map[string]string `json:"secrets,omitempty"`
initialized bool
}
// NewAppFile init an empty AppFile struct
func NewAppFile() *AppFile {
return &AppFile{
Services: make(map[string]Service),
Secrets: make(map[string]string),
}
}
// Load will load appfile from default path
func Load() (*AppFile, error) {
if _, err := os.Stat(DefaultAppfilePath); err == nil {
return LoadFromFile(DefaultAppfilePath)
}
if _, err := os.Stat(DefaultJSONAppfilePath); err == nil {
return LoadFromFile(DefaultJSONAppfilePath)
}
return LoadFromFile(DefaultUnknowFormatAppfilePath)
}
// JSONToYaml will convert JSON format appfile to yaml and load the AppFile struct
func JSONToYaml(data []byte, appFile *AppFile) (*AppFile, error) {
j, e := yaml.JSONToYAML(data)
if e != nil {
return nil, e
}
err := yaml.Unmarshal(j, appFile)
if err != nil {
return nil, err
}
return appFile, nil
}
// LoadFromFile will read the file and load the AppFile struct
func LoadFromFile(filename string) (*AppFile, error) {
b, err := os.ReadFile(filepath.Clean(filename))
if err != nil {
return nil, err
}
return LoadFromBytes(b)
}
// LoadFromBytes will load AppFile from bytes
func LoadFromBytes(b []byte) (*AppFile, error) {
af := NewAppFile()
var err error
if json.Valid(b) {
af, err = JSONToYaml(b, af)
} else {
err = yaml.Unmarshal(b, af)
}
if err != nil {
return nil, err
}
return af, nil
}
// ExecuteAppfileTasks will execute built-in tasks(such as image builder, etc.) and generate locally executed application
func (app *AppFile) ExecuteAppfileTasks(io cmdutil.IOStreams) error {
if app.initialized {
return nil
}
for name, svc := range app.Services {
newSvc, err := builtin.RunBuildInTasks(svc, io)
if err != nil {
return err
}
app.Services[name] = newSvc
}
app.initialized = true
return nil
}
// ConvertToApplication renders Appfile into Application, Scopes and other K8s Resources.
func (app *AppFile) ConvertToApplication(namespace string, io cmdutil.IOStreams, tm template.Manager, silence bool) (*v1beta1.Application, error) {
if err := app.ExecuteAppfileTasks(io); err != nil {
if strings.Contains(err.Error(), "'image' : not found") {
return nil, ErrImageNotDefined
}
return nil, err
}
// auxiliaryObjects currently include OAM Scope Custom Resources and ConfigMaps
servApp := new(v1beta1.Application)
servApp.SetNamespace(namespace)
servApp.SetName(app.Name)
servApp.Spec.Components = []common.ApplicationComponent{}
if !silence {
io.Infof("parsing application components")
}
for serviceName, svc := range app.GetServices() {
comp, err := svc.RenderServiceToApplicationComponent(tm, serviceName)
if err != nil {
return nil, err
}
servApp.Spec.Components = append(servApp.Spec.Components, comp)
}
servApp.SetGroupVersionKind(v1beta1.SchemeGroupVersion.WithKind("Application"))
return servApp, nil
}