Fix(dryrun): add default name and namespace in dry run (#2150)

This commit is contained in:
Tianxin Dong
2021-08-24 17:54:55 +08:00
committed by GitHub
parent 8fc192bfac
commit 8fb64fcb19
6 changed files with 49 additions and 3 deletions

View File

@@ -20,6 +20,7 @@ import (
"context"
"github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
"github.com/oam-dev/kubevela/apis/core.oam.dev/v1beta1"
@@ -63,9 +64,34 @@ func (d *Option) ExecuteDryRun(ctx context.Context, app *v1beta1.Application) ([
if err != nil {
return nil, errors.WithMessage(err, "cannot generate appFile from application")
}
if appFile.Namespace == "" {
appFile.Namespace = corev1.NamespaceDefault
}
comps, err := appFile.GenerateComponentManifests()
if err != nil {
return nil, errors.WithMessage(err, "cannot generate AppConfig and Components")
}
for _, comp := range comps {
if comp.StandardWorkload != nil {
if comp.StandardWorkload.GetName() == "" {
comp.StandardWorkload.SetName(comp.Name)
}
if comp.StandardWorkload.GetNamespace() == "" {
comp.StandardWorkload.SetNamespace(appFile.Namespace)
}
}
for _, trait := range comp.Traits {
if trait.GetName() == "" {
traitType := trait.GetLabels()[oam.TraitTypeLabel]
traitName := oamutil.GenTraitNameCompatible(comp.Name, trait, traitType)
trait.SetName(traitName)
}
if trait.GetNamespace() == "" {
trait.SetNamespace(appFile.Namespace)
}
}
}
return comps, nil
}

View File

@@ -71,6 +71,7 @@ spec:
trait.oam.dev/resource: service
trait.oam.dev/type: myingress
name: myweb-1
namespace: default
spec:
ports:
- port: 80
@@ -88,6 +89,7 @@ spec:
trait.oam.dev/resource: ingress
trait.oam.dev/type: myingress
name: myweb-1
namespace: default
spec:
rules:
- host: www.example.com
@@ -107,6 +109,8 @@ spec:
app.oam.dev/name: livediff-demo
trait.oam.dev/resource: scaler
trait.oam.dev/type: myscaler
name: myweb-1-myscaler-5547bdc57f
namespace: default
spec:
replicaCount: 2
- componentName: myweb-2
@@ -155,6 +159,8 @@ spec:
app.oam.dev/component: myweb-1
app.oam.dev/name: livediff-demo
workload.oam.dev/type: myworker
name: myweb-1
namespace: default
spec:
selector:
matchLabels:
@@ -189,6 +195,8 @@ spec:
app.oam.dev/component: myweb-2
app.oam.dev/name: livediff-demo
workload.oam.dev/type: myworker
name: myweb-2
namespace: default
spec:
selector:
matchLabels:

View File

@@ -16,6 +16,8 @@ StandardWorkload:
app.oam.dev/component: myweb
app.oam.dev/name: app-dryrun
workload.oam.dev/type: myworker
name: myweb
namespace: default
spec:
selector:
matchLabels:
@@ -42,6 +44,7 @@ Traits:
trait.oam.dev/resource: service
trait.oam.dev/type: myingress
name: myweb
namespace: default
spec:
ports:
- port: 80
@@ -58,6 +61,7 @@ Traits:
trait.oam.dev/resource: ingress
trait.oam.dev/type: myingress
name: myweb
namespace: default
spec:
rules:
- host: www.example.com