2021-01-28 18:36:52 +08:00
|
|
|
package appfile
|
|
|
|
|
|
|
|
import (
|
2021-03-05 15:14:18 +08:00
|
|
|
"context"
|
2021-03-15 15:54:43 +08:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2021-03-05 15:14:18 +08:00
|
|
|
|
2021-01-28 18:36:52 +08:00
|
|
|
"github.com/crossplane/crossplane-runtime/apis/core/v1alpha1"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
kerrors "k8s.io/apimachinery/pkg/api/errors"
|
2021-03-15 15:54:43 +08:00
|
|
|
"k8s.io/apimachinery/pkg/runtime"
|
2021-01-28 18:36:52 +08:00
|
|
|
"k8s.io/apimachinery/pkg/runtime/schema"
|
|
|
|
"sigs.k8s.io/controller-runtime/pkg/client"
|
|
|
|
|
|
|
|
"github.com/oam-dev/kubevela/apis/core.oam.dev/v1alpha2"
|
|
|
|
"github.com/oam-dev/kubevela/apis/types"
|
|
|
|
"github.com/oam-dev/kubevela/pkg/appfile/config"
|
2021-03-15 15:54:43 +08:00
|
|
|
"github.com/oam-dev/kubevela/pkg/appfile/helm"
|
2021-01-28 18:36:52 +08:00
|
|
|
"github.com/oam-dev/kubevela/pkg/dsl/definition"
|
|
|
|
"github.com/oam-dev/kubevela/pkg/dsl/process"
|
|
|
|
"github.com/oam-dev/kubevela/pkg/oam"
|
|
|
|
"github.com/oam-dev/kubevela/pkg/oam/discoverymapper"
|
|
|
|
"github.com/oam-dev/kubevela/pkg/oam/util"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// AppfileBuiltinConfig defines the built-in config variable
|
|
|
|
AppfileBuiltinConfig = "config"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Workload is component
|
|
|
|
type Workload struct {
|
|
|
|
Name string
|
|
|
|
Type string
|
|
|
|
CapabilityCategory types.CapabilityCategory
|
|
|
|
Params map[string]interface{}
|
|
|
|
Traits []*Trait
|
|
|
|
Scopes []Scope
|
2021-02-01 10:43:44 +08:00
|
|
|
|
|
|
|
Template string
|
|
|
|
HealthCheckPolicy string
|
|
|
|
CustomStatusFormat string
|
2021-03-15 15:54:43 +08:00
|
|
|
|
|
|
|
Helm *v1alpha2.Helm
|
2021-03-16 17:52:51 +08:00
|
|
|
DefinitionReference v1alpha2.WorkloadGVK
|
2021-03-20 03:30:31 +08:00
|
|
|
// TODO: remove all the duplicate fields above as workload now contains the whole template
|
|
|
|
FullTemplate *util.Template
|
2021-03-23 16:30:49 +08:00
|
|
|
|
|
|
|
engine definition.AbstractEngine
|
2021-01-28 18:36:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetUserConfigName get user config from AppFile, it will contain config file in it.
|
|
|
|
func (wl *Workload) GetUserConfigName() string {
|
|
|
|
if wl.Params == nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
t, ok := wl.Params[AppfileBuiltinConfig]
|
|
|
|
if !ok {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
ts, ok := t.(string)
|
|
|
|
if !ok {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return ts
|
|
|
|
}
|
|
|
|
|
|
|
|
// EvalContext eval workload template and set result to context
|
|
|
|
func (wl *Workload) EvalContext(ctx process.Context) error {
|
2021-03-23 16:30:49 +08:00
|
|
|
return wl.engine.Complete(ctx, wl.Template, wl.Params)
|
2021-02-01 10:43:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// EvalStatus eval workload status
|
|
|
|
func (wl *Workload) EvalStatus(ctx process.Context, cli client.Client, ns string) (string, error) {
|
2021-03-23 16:30:49 +08:00
|
|
|
return wl.engine.Status(ctx, cli, ns, wl.CustomStatusFormat)
|
2021-01-28 18:36:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// EvalHealth eval workload health check
|
2021-02-01 10:43:44 +08:00
|
|
|
func (wl *Workload) EvalHealth(ctx process.Context, client client.Client, namespace string) (bool, error) {
|
2021-03-23 16:30:49 +08:00
|
|
|
return wl.engine.HealthCheck(ctx, client, namespace, wl.HealthCheckPolicy)
|
2021-01-28 18:36:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Scope defines the scope of workload
|
|
|
|
type Scope struct {
|
|
|
|
Name string
|
|
|
|
GVK schema.GroupVersionKind
|
|
|
|
}
|
|
|
|
|
|
|
|
// Trait is ComponentTrait
|
|
|
|
type Trait struct {
|
2021-02-01 10:43:44 +08:00
|
|
|
// The Name is name of TraitDefinition, actually it's a type of the trait instance
|
2021-01-28 18:36:52 +08:00
|
|
|
Name string
|
|
|
|
CapabilityCategory types.CapabilityCategory
|
|
|
|
Params map[string]interface{}
|
2021-02-01 10:43:44 +08:00
|
|
|
|
2021-01-28 18:36:52 +08:00
|
|
|
Template string
|
2021-02-01 10:43:44 +08:00
|
|
|
HealthCheckPolicy string
|
|
|
|
CustomStatusFormat string
|
2021-03-20 03:30:31 +08:00
|
|
|
|
|
|
|
FullTemplate *util.Template
|
2021-03-23 16:30:49 +08:00
|
|
|
engine definition.AbstractEngine
|
2021-01-28 18:36:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// EvalContext eval trait template and set result to context
|
|
|
|
func (trait *Trait) EvalContext(ctx process.Context) error {
|
2021-03-23 16:30:49 +08:00
|
|
|
return trait.engine.Complete(ctx, trait.Template, trait.Params)
|
2021-01-28 18:36:52 +08:00
|
|
|
}
|
|
|
|
|
2021-01-29 19:21:47 +08:00
|
|
|
// EvalStatus eval trait status
|
|
|
|
func (trait *Trait) EvalStatus(ctx process.Context, cli client.Client, ns string) (string, error) {
|
2021-03-23 16:30:49 +08:00
|
|
|
return trait.engine.Status(ctx, cli, ns, trait.CustomStatusFormat)
|
2021-01-29 19:21:47 +08:00
|
|
|
}
|
|
|
|
|
2021-01-28 18:36:52 +08:00
|
|
|
// EvalHealth eval trait health check
|
2021-02-01 10:43:44 +08:00
|
|
|
func (trait *Trait) EvalHealth(ctx process.Context, client client.Client, namespace string) (bool, error) {
|
2021-03-23 16:30:49 +08:00
|
|
|
return trait.engine.HealthCheck(ctx, client, namespace, trait.HealthCheckPolicy)
|
2021-01-28 18:36:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Appfile describes application
|
|
|
|
type Appfile struct {
|
2021-03-10 16:47:17 +08:00
|
|
|
Name string
|
|
|
|
RevisionName string
|
|
|
|
Workloads []*Workload
|
2021-01-28 18:36:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// TemplateValidate validate Template format
|
|
|
|
func (af *Appfile) TemplateValidate() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parser is an application parser
|
|
|
|
type Parser struct {
|
|
|
|
client client.Client
|
|
|
|
dm discoverymapper.DiscoveryMapper
|
2021-03-23 16:30:49 +08:00
|
|
|
pd *definition.PackageDiscover
|
2021-01-28 18:36:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewApplicationParser create appfile parser
|
2021-03-23 16:30:49 +08:00
|
|
|
func NewApplicationParser(cli client.Client, dm discoverymapper.DiscoveryMapper, pd *definition.PackageDiscover) *Parser {
|
2021-01-28 18:36:52 +08:00
|
|
|
return &Parser{
|
|
|
|
client: cli,
|
|
|
|
dm: dm,
|
2021-03-23 16:30:49 +08:00
|
|
|
pd: pd,
|
2021-01-28 18:36:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// GenerateAppFile converts an application to an Appfile
|
2021-03-05 15:14:18 +08:00
|
|
|
func (p *Parser) GenerateAppFile(ctx context.Context, name string, app *v1alpha2.Application) (*Appfile, error) {
|
2021-01-28 18:36:52 +08:00
|
|
|
appfile := new(Appfile)
|
|
|
|
appfile.Name = name
|
|
|
|
var wds []*Workload
|
|
|
|
for _, comp := range app.Spec.Components {
|
2021-03-05 15:14:18 +08:00
|
|
|
wd, err := p.parseWorkload(ctx, comp)
|
2021-01-28 18:36:52 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
wds = append(wds, wd)
|
|
|
|
}
|
|
|
|
appfile.Workloads = wds
|
|
|
|
return appfile, nil
|
|
|
|
}
|
|
|
|
|
2021-03-05 15:14:18 +08:00
|
|
|
func (p *Parser) parseWorkload(ctx context.Context, comp v1alpha2.ApplicationComponent) (*Workload, error) {
|
2021-03-23 16:30:49 +08:00
|
|
|
|
2021-03-20 03:30:31 +08:00
|
|
|
// TODO: pass in p.dm
|
2021-03-23 16:30:49 +08:00
|
|
|
templ, err := util.LoadTemplate(ctx, p.client, comp.WorkloadType, types.TypeComponentDefinition)
|
2021-01-28 18:36:52 +08:00
|
|
|
if err != nil && !kerrors.IsNotFound(err) {
|
|
|
|
return nil, errors.WithMessagef(err, "fetch type of %s", comp.Name)
|
|
|
|
}
|
|
|
|
settings, err := util.RawExtension2Map(&comp.Settings)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.WithMessagef(err, "fail to parse settings for %s", comp.Name)
|
|
|
|
}
|
2021-03-23 16:30:49 +08:00
|
|
|
workload := &Workload{
|
|
|
|
Traits: []*Trait{},
|
|
|
|
Name: comp.Name,
|
|
|
|
Type: comp.WorkloadType,
|
|
|
|
CapabilityCategory: templ.CapabilityCategory,
|
|
|
|
Template: templ.TemplateStr,
|
|
|
|
HealthCheckPolicy: templ.Health,
|
|
|
|
CustomStatusFormat: templ.CustomStatus,
|
|
|
|
DefinitionReference: templ.Reference,
|
|
|
|
Helm: templ.Helm,
|
|
|
|
FullTemplate: templ,
|
|
|
|
Params: settings,
|
|
|
|
engine: definition.NewWorkloadAbstractEngine(comp.Name, p.pd),
|
|
|
|
}
|
2021-01-28 18:36:52 +08:00
|
|
|
for _, traitValue := range comp.Traits {
|
|
|
|
properties, err := util.RawExtension2Map(&traitValue.Properties)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Errorf("fail to parse properties of %s for %s", traitValue.Name, comp.Name)
|
|
|
|
}
|
2021-03-05 15:14:18 +08:00
|
|
|
trait, err := p.parseTrait(ctx, traitValue.Name, properties)
|
2021-01-28 18:36:52 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.WithMessagef(err, "component(%s) parse trait(%s)", comp.Name, traitValue.Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
workload.Traits = append(workload.Traits, trait)
|
|
|
|
}
|
|
|
|
for scopeType, instanceName := range comp.Scopes {
|
2021-03-05 15:14:18 +08:00
|
|
|
gvk, err := util.GetScopeGVK(ctx, p.client, p.dm, scopeType)
|
2021-01-28 18:36:52 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
workload.Scopes = append(workload.Scopes, Scope{
|
|
|
|
Name: instanceName,
|
|
|
|
GVK: gvk,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return workload, nil
|
|
|
|
}
|
|
|
|
|
2021-03-05 15:14:18 +08:00
|
|
|
func (p *Parser) parseTrait(ctx context.Context, name string, properties map[string]interface{}) (*Trait, error) {
|
2021-03-20 03:30:31 +08:00
|
|
|
// TODO: pass in p.dm
|
2021-03-05 15:14:18 +08:00
|
|
|
templ, err := util.LoadTemplate(ctx, p.client, name, types.TypeTrait)
|
2021-01-28 18:36:52 +08:00
|
|
|
if kerrors.IsNotFound(err) {
|
|
|
|
return nil, errors.Errorf("trait definition of %s not found", name)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &Trait{
|
|
|
|
Name: name,
|
|
|
|
CapabilityCategory: templ.CapabilityCategory,
|
|
|
|
Params: properties,
|
|
|
|
Template: templ.TemplateStr,
|
2021-02-01 10:43:44 +08:00
|
|
|
HealthCheckPolicy: templ.Health,
|
|
|
|
CustomStatusFormat: templ.CustomStatus,
|
2021-03-20 03:30:31 +08:00
|
|
|
FullTemplate: templ,
|
2021-03-23 16:30:49 +08:00
|
|
|
engine: definition.NewTraitAbstractEngine(name, p.pd),
|
2021-01-28 18:36:52 +08:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GenerateApplicationConfiguration converts an appFile to applicationConfig & Components
|
|
|
|
func (p *Parser) GenerateApplicationConfiguration(app *Appfile, ns string) (*v1alpha2.ApplicationConfiguration,
|
|
|
|
[]*v1alpha2.Component, error) {
|
|
|
|
appconfig := &v1alpha2.ApplicationConfiguration{}
|
|
|
|
appconfig.SetGroupVersionKind(v1alpha2.ApplicationConfigurationGroupVersionKind)
|
|
|
|
appconfig.Name = app.Name
|
|
|
|
appconfig.Namespace = ns
|
|
|
|
|
|
|
|
if appconfig.Labels == nil {
|
|
|
|
appconfig.Labels = map[string]string{}
|
|
|
|
}
|
2021-02-21 15:10:15 +08:00
|
|
|
appconfig.Labels[oam.LabelAppName] = app.Name
|
2021-01-28 18:36:52 +08:00
|
|
|
|
|
|
|
var components []*v1alpha2.Component
|
|
|
|
for _, wl := range app.Workloads {
|
2021-03-15 15:54:43 +08:00
|
|
|
var comp *v1alpha2.Component
|
|
|
|
var acComp *v1alpha2.ApplicationConfigurationComponent
|
|
|
|
var err error
|
|
|
|
switch wl.CapabilityCategory {
|
|
|
|
case types.HelmCategory:
|
2021-03-16 17:52:51 +08:00
|
|
|
comp, acComp, err = generateComponentFromHelmModule(p.client, wl, app.Name, app.RevisionName, ns)
|
2021-03-15 15:54:43 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
comp, acComp, err = generateComponentFromCUEModule(p.client, wl, app.Name, app.RevisionName, ns)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
2021-01-28 18:36:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
components = append(components, comp)
|
|
|
|
appconfig.Spec.Components = append(appconfig.Spec.Components, *acComp)
|
|
|
|
}
|
|
|
|
return appconfig, components, nil
|
|
|
|
}
|
|
|
|
|
2021-03-15 15:54:43 +08:00
|
|
|
func generateComponentFromCUEModule(c client.Client, wl *Workload, appName, revision, ns string) (*v1alpha2.Component, *v1alpha2.ApplicationConfigurationComponent, error) {
|
|
|
|
pCtx, err := PrepareProcessContext(c, wl, appName, revision, ns)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
for _, tr := range wl.Traits {
|
|
|
|
if err := tr.EvalContext(pCtx); err != nil {
|
|
|
|
return nil, nil, errors.Wrapf(err, "evaluate template trait=%s app=%s", tr.Name, wl.Name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
var comp *v1alpha2.Component
|
|
|
|
var acComp *v1alpha2.ApplicationConfigurationComponent
|
|
|
|
comp, acComp, err = evalWorkloadWithContext(pCtx, wl, appName, wl.Name)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
comp.Name = wl.Name
|
|
|
|
acComp.ComponentName = comp.Name
|
|
|
|
|
|
|
|
for _, sc := range wl.Scopes {
|
|
|
|
acComp.Scopes = append(acComp.Scopes, v1alpha2.ComponentScope{ScopeReference: v1alpha1.TypedReference{
|
|
|
|
APIVersion: sc.GVK.GroupVersion().String(),
|
|
|
|
Kind: sc.GVK.Kind,
|
|
|
|
Name: sc.Name,
|
|
|
|
}})
|
|
|
|
}
|
|
|
|
|
|
|
|
comp.Namespace = ns
|
|
|
|
if comp.Labels == nil {
|
|
|
|
comp.Labels = map[string]string{}
|
|
|
|
}
|
|
|
|
comp.Labels[oam.LabelAppName] = appName
|
|
|
|
comp.SetGroupVersionKind(v1alpha2.ComponentGroupVersionKind)
|
|
|
|
|
|
|
|
return comp, acComp, nil
|
|
|
|
}
|
|
|
|
|
2021-03-16 17:52:51 +08:00
|
|
|
func generateComponentFromHelmModule(c client.Client, wl *Workload, appName, revision, ns string) (*v1alpha2.Component, *v1alpha2.ApplicationConfigurationComponent, error) {
|
|
|
|
gv, err := schema.ParseGroupVersion(wl.DefinitionReference.APIVersion)
|
2021-03-15 15:54:43 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
2021-03-16 17:52:51 +08:00
|
|
|
targetWokrloadGVK := gv.WithKind(wl.DefinitionReference.Kind)
|
2021-03-15 15:54:43 +08:00
|
|
|
|
|
|
|
// NOTE this is a hack way to enable using CUE module capabilities on Helm module workload
|
|
|
|
// construct an empty base workload according to its GVK
|
|
|
|
wl.Template = fmt.Sprintf(`
|
|
|
|
output: {
|
|
|
|
apiVersion: "%s"
|
|
|
|
kind: "%s"
|
|
|
|
}`, targetWokrloadGVK.GroupVersion().String(), targetWokrloadGVK.Kind)
|
|
|
|
|
|
|
|
// re-use the way CUE module generates comp & acComp
|
|
|
|
comp, acComp, err := generateComponentFromCUEModule(c, wl, appName, revision, ns)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
release, repo, err := helm.RenderHelmReleaseAndHelmRepo(wl.Helm, wl.Name, appName, ns, wl.Params)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
rlsBytes, err := json.Marshal(release.Object)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
repoBytes, err := json.Marshal(repo.Object)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
comp.Spec.Helm = &v1alpha2.Helm{
|
|
|
|
Release: runtime.RawExtension{Raw: rlsBytes},
|
|
|
|
Repository: runtime.RawExtension{Raw: repoBytes},
|
|
|
|
}
|
|
|
|
return comp, acComp, nil
|
|
|
|
}
|
|
|
|
|
2021-01-28 18:36:52 +08:00
|
|
|
// evalWorkloadWithContext evaluate the workload's template to generate component and ACComponent
|
2021-02-01 10:43:44 +08:00
|
|
|
func evalWorkloadWithContext(pCtx process.Context, wl *Workload, appName, compName string) (*v1alpha2.Component, *v1alpha2.ApplicationConfigurationComponent, error) {
|
2021-01-28 18:36:52 +08:00
|
|
|
base, assists := pCtx.Output()
|
|
|
|
componentWorkload, err := base.Unstructured()
|
|
|
|
if err != nil {
|
2021-02-04 22:09:09 +08:00
|
|
|
return nil, nil, errors.Wrapf(err, "evaluate base template component=%s app=%s", compName, appName)
|
2021-01-28 18:36:52 +08:00
|
|
|
}
|
2021-02-01 10:43:44 +08:00
|
|
|
|
|
|
|
labels := map[string]string{
|
|
|
|
oam.WorkloadTypeLabel: wl.Type,
|
|
|
|
oam.LabelAppName: appName,
|
|
|
|
oam.LabelAppComponent: compName,
|
2021-01-28 18:36:52 +08:00
|
|
|
}
|
2021-02-01 10:43:44 +08:00
|
|
|
util.AddLabels(componentWorkload, labels)
|
2021-01-28 18:36:52 +08:00
|
|
|
|
|
|
|
component := &v1alpha2.Component{}
|
2021-03-20 03:30:31 +08:00
|
|
|
// we need to marshal the workload to byte array before sending them to the k8s
|
2021-02-20 04:11:26 +08:00
|
|
|
component.Spec.Workload = util.Object2RawExtension(componentWorkload)
|
2021-01-28 18:36:52 +08:00
|
|
|
|
|
|
|
acComponent := &v1alpha2.ApplicationConfigurationComponent{}
|
|
|
|
for _, assist := range assists {
|
|
|
|
tr, err := assist.Ins.Unstructured()
|
|
|
|
if err != nil {
|
2021-02-04 22:09:09 +08:00
|
|
|
return nil, nil, errors.Wrapf(err, "evaluate trait=%s template for component=%s app=%s", assist.Name, compName, appName)
|
2021-01-28 18:36:52 +08:00
|
|
|
}
|
2021-02-01 10:43:44 +08:00
|
|
|
labels := map[string]string{
|
|
|
|
oam.TraitTypeLabel: assist.Type,
|
|
|
|
oam.LabelAppName: appName,
|
|
|
|
oam.LabelAppComponent: compName,
|
|
|
|
}
|
|
|
|
if assist.Name != "" {
|
|
|
|
labels[oam.TraitResource] = assist.Name
|
|
|
|
}
|
|
|
|
util.AddLabels(tr, labels)
|
2021-01-28 18:36:52 +08:00
|
|
|
acComponent.Traits = append(acComponent.Traits, v1alpha2.ComponentTrait{
|
2021-02-20 04:11:26 +08:00
|
|
|
// we need to marshal the trait to byte array before sending them to the k8s
|
|
|
|
Trait: util.Object2RawExtension(tr),
|
2021-01-28 18:36:52 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
return component, acComponent, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// PrepareProcessContext prepares a DSL process Context
|
2021-03-10 16:47:17 +08:00
|
|
|
func PrepareProcessContext(k8sClient client.Client, wl *Workload, applicationName, revision string, namespace string) (process.Context, error) {
|
|
|
|
pCtx := process.NewContext(wl.Name, applicationName, revision)
|
2021-01-28 18:36:52 +08:00
|
|
|
userConfig := wl.GetUserConfigName()
|
|
|
|
if userConfig != "" {
|
|
|
|
cg := config.Configmap{Client: k8sClient}
|
|
|
|
// TODO(wonderflow): envName should not be namespace when we have serverside env
|
|
|
|
var envName = namespace
|
|
|
|
data, err := cg.GetConfigData(config.GenConfigMapName(applicationName, wl.Name, userConfig), envName)
|
|
|
|
if err != nil {
|
2021-02-04 22:09:09 +08:00
|
|
|
return nil, errors.Wrapf(err, "get config=%s for app=%s in namespace=%s", userConfig, applicationName, namespace)
|
2021-01-28 18:36:52 +08:00
|
|
|
}
|
|
|
|
pCtx.SetConfigs(data)
|
|
|
|
}
|
|
|
|
if err := wl.EvalContext(pCtx); err != nil {
|
2021-02-04 22:09:09 +08:00
|
|
|
return nil, errors.Wrapf(err, "evaluate base template app=%s in namespace=%s", applicationName, namespace)
|
2021-01-28 18:36:52 +08:00
|
|
|
}
|
|
|
|
return pCtx, nil
|
|
|
|
}
|