mirror of
https://github.com/kubevela/kubevela.git
synced 2026-08-27 16:17:34 +00:00
fix bugs and add tests
Signed-off-by: 楚岳 <wangyike.wyk@alibaba-inc.com>
This commit is contained in:
@@ -50,6 +50,7 @@ const (
|
||||
addonAllClusterPolicy = "deploy-addon-to-all-clusters"
|
||||
renderOutputCuePath = "output"
|
||||
renderAuxiliaryOutputsPath = "outputs"
|
||||
defaultCuePackageHeader = "main"
|
||||
)
|
||||
|
||||
type addonCueTemplateRender struct {
|
||||
@@ -340,6 +341,13 @@ func renderResources(addon *InstallPackage, args map[string]interface{}) ([]comm
|
||||
}
|
||||
|
||||
for _, tmpl := range addon.CUETemplates {
|
||||
isMainCueTemplate, err := checkCueFileHasPackageHeader(tmpl)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if isMainCueTemplate {
|
||||
continue
|
||||
}
|
||||
comp, err := renderCompAccordingCUETemplate(tmpl, addon, args)
|
||||
if err != nil && strings.Contains(err.Error(), "var(path=output) not exist") {
|
||||
continue
|
||||
@@ -373,3 +381,14 @@ func isDeployToRuntime(addon *InstallPackage) bool {
|
||||
}
|
||||
return addon.DeployTo.RuntimeCluster || addon.DeployTo.LegacyRuntimeCluster
|
||||
}
|
||||
|
||||
func checkCueFileHasPackageHeader(cueTemplate ElementFile) (bool, error) {
|
||||
cueFile, err := parser.ParseFile(cueTemplate.Name, cueTemplate.Data, parser.ParseComments)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
if cueFile.PackageName() == defaultCuePackageHeader {
|
||||
return true, nil
|
||||
}
|
||||
return false, nil
|
||||
}
|
||||
|
||||
@@ -451,3 +451,99 @@ func TestRenderCueResourceError(t *testing.T) {
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, len(comp), 2)
|
||||
}
|
||||
|
||||
func TestCheckCueFileHasPackageHeader(t *testing.T) {
|
||||
testCueTemplateWithPkg := `
|
||||
package main
|
||||
|
||||
kustomizeController: {
|
||||
// About this name, refer to #429 for details.
|
||||
name: "fluxcd-kustomize-controller"
|
||||
type: "webservice"
|
||||
dependsOn: ["fluxcd-ns"]
|
||||
properties: {
|
||||
imagePullPolicy: "IfNotPresent"
|
||||
image: _base + "fluxcd/kustomize-controller:v0.26.0"
|
||||
env: [
|
||||
{
|
||||
name: "RUNTIME_NAMESPACE"
|
||||
value: _targetNamespace
|
||||
},
|
||||
]
|
||||
livenessProbe: {
|
||||
httpGet: {
|
||||
path: "/healthz"
|
||||
port: 9440
|
||||
}
|
||||
timeoutSeconds: 5
|
||||
}
|
||||
readinessProbe: {
|
||||
httpGet: {
|
||||
path: "/readyz"
|
||||
port: 9440
|
||||
}
|
||||
timeoutSeconds: 5
|
||||
}
|
||||
volumeMounts: {
|
||||
emptyDir: [
|
||||
{
|
||||
name: "temp"
|
||||
mountPath: "/tmp"
|
||||
},
|
||||
]
|
||||
}
|
||||
}
|
||||
traits: [
|
||||
{
|
||||
type: "service-account"
|
||||
properties: {
|
||||
name: "sa-kustomize-controller"
|
||||
create: true
|
||||
privileges: _rules
|
||||
}
|
||||
},
|
||||
{
|
||||
type: "labels"
|
||||
properties: {
|
||||
"control-plane": "controller"
|
||||
// This label is kept to avoid breaking existing
|
||||
// KubeVela e2e tests (makefile e2e-setup).
|
||||
"app": "kustomize-controller"
|
||||
}
|
||||
},
|
||||
{
|
||||
type: "command"
|
||||
properties: {
|
||||
args: controllerArgs
|
||||
}
|
||||
},
|
||||
]
|
||||
}
|
||||
`
|
||||
|
||||
testCueTemplateWithoutPkg := `
|
||||
output: {
|
||||
type: "helm"
|
||||
name: "nginx-ingress"
|
||||
properties: {
|
||||
repoType: "helm"
|
||||
url: "https://kubernetes.github.io/ingress-nginx"
|
||||
chart: "ingress-nginx"
|
||||
version: "4.2.0"
|
||||
values: {
|
||||
controller: service: type: parameter["serviceType"]
|
||||
}
|
||||
}
|
||||
}
|
||||
`
|
||||
|
||||
cueTemplate := ElementFile{Name: "test-file.cue", Data: testCueTemplateWithPkg}
|
||||
ok, err := checkCueFileHasPackageHeader(cueTemplate)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, true, ok)
|
||||
|
||||
cueTemplate = ElementFile{Name: "test-file-without-pkg.cue", Data: testCueTemplateWithoutPkg}
|
||||
ok, err = checkCueFileHasPackageHeader(cueTemplate)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, false, ok)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user