Fix: fix gen doc for container image (#4935)

Signed-off-by: FogDong <dongtianxin.tx@alibaba-inc.com>

Signed-off-by: FogDong <dongtianxin.tx@alibaba-inc.com>
This commit is contained in:
Tianxin Dong
2022-10-27 19:03:13 +08:00
committed by GitHub
parent ea12c0f11e
commit 01c759dcd1
3 changed files with 54 additions and 6 deletions

View File

@@ -44,6 +44,7 @@ import (
"github.com/oam-dev/kubevela/pkg/utils/common"
"github.com/oam-dev/kubevela/pkg/utils/helm"
util2 "github.com/oam-dev/kubevela/pkg/utils/util"
"github.com/oam-dev/kubevela/references/docgen/fix"
)
// DescriptionUndefined indicates the description is not defined
@@ -215,10 +216,17 @@ func GetTraitsFromClusterWithValidateOption(ctx context.Context, namespace strin
var templateErrors []error
for _, td := range traitDefs.Items {
tmp, err := GetCapabilityByTraitDefinitionObject(td)
if err != nil {
templateErrors = append(templateErrors, errors.Wrapf(err, "handle trait template `%s` failed", td.Name))
continue
var tmp *types.Capability
var err error
// FIXME: remove this temporary fix when https://github.com/cue-lang/cue/issues/2047 is fixed
if td.Name == "container-image" {
tmp = fix.CapContainerImage
} else {
tmp, err = GetCapabilityByTraitDefinitionObject(td)
if err != nil {
templateErrors = append(templateErrors, errors.Wrapf(err, "handle trait template `%s` failed", td.Name))
continue
}
}
tmp.Namespace = namespace
if validateFlag {

View File

@@ -0,0 +1,34 @@
/*
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 fix
import (
"encoding/json"
"github.com/oam-dev/kubevela/apis/types"
)
var (
// CapContainerImage is the cap for container image
CapContainerImage *types.Capability
)
// FIXME: remove this temporary fix when https://github.com/cue-lang/cue/issues/2047 is fixed
func init() {
legacyJSON := `{"name":"container-image","type":"trait","template":"#PatchParams: {\n\t// +usage=Specify the name of the target container, if not set, use the component name\n\tcontainerName: *\"\" | string\n\t// +usage=Specify the image of the container\n\timage: string\n\t// +usage=Specify the image pull policy of the container\n\timagePullPolicy: *\"\" | \"IfNotPresent\" | \"Always\" | \"Never\"\n}\nPatchContainer: {\n\t_params: #PatchParams\n\tname: _params.containerName\n\t_baseContainers: context.output.spec.template.spec.containers\n\t_matchContainers_: [ for _container_ in _baseContainers if _container_.name == name {_container_}]\n\t_baseContainer: *_|_ | {...}\n\tif len(_matchContainers_) == 0 {\n\t\terr: \"container \\(name) not found\"\n\t}\n\tif len(_matchContainers_) \u003e 0 {\n\t\t// +patchStrategy=retainKeys\n\t\timage: _params.image\n\n\t\tif _params.imagePullPolicy != \"\" {\n\t\t\t// +patchStrategy=retainKeys\n\t\t\timagePullPolicy: _params.imagePullPolicy\n\t\t}\n\t}\n}\npatch: spec: template: spec: {\n\tif parameter.containers == _|_ {\n\t\t// +patchKey=name\n\t\tcontainers: [{\n\t\t\tPatchContainer \u0026 {_params: {\n\t\t\t\tif parameter.containerName == \"\" {\n\t\t\t\t\tcontainerName: context.name\n\t\t\t\t}\n\t\t\t\tif parameter.containerName != \"\" {\n\t\t\t\t\tcontainerName: parameter.containerName\n\t\t\t\t}\n\t\t\t\timage: parameter.image\n\t\t\t\timagePullPolicy: parameter.imagePullPolicy\n\t\t\t}}\n\t\t}]\n\t}\n\tif parameter.containers != _|_ {\n\t\t// +patchKey=name\n\t\tcontainers: [ for c in parameter.containers {\n\t\t\tif c.containerName == \"\" {\n\t\t\t\terr: \"containerName must be set for containers\"\n\t\t\t}\n\t\t\tif c.containerName != \"\" {\n\t\t\t\tPatchContainer \u0026 {_params: c}\n\t\t\t}\n\t\t}]\n\t}\n}\nparameter: *#PatchParams | close({\n\t// +usage=Specify the container image for multiple containers\n\tcontainers: [...#PatchParams]\n})\nerrs: [ for c in patch.spec.template.spec.containers if c.err != _|_ {c.err}]\n","parameters":[{"name":"containerName","default":"","usage":"Specify the name of the target container, if not set, use the component name","type":16},{"name":"image","required":true,"default":"","usage":"Specify the image of the container","type":16},{"name":"imagePullPolicy","default":"","usage":"Specify the image pull policy of the container","type":16}],"description":"Set the image of the container.","category":"cue","appliesTo":["deployments.apps","statefulsets.apps","daemonsets.apps","jobs.batch"],"kubetemplate":null}`
_ = json.Unmarshal([]byte(legacyJSON), &CapContainerImage)
}

View File

@@ -53,6 +53,7 @@ import (
pkgUtils "github.com/oam-dev/kubevela/pkg/utils"
"github.com/oam-dev/kubevela/pkg/utils/common"
"github.com/oam-dev/kubevela/pkg/utils/terraform"
"github.com/oam-dev/kubevela/references/docgen/fix"
)
// ParseReference is used to include the common function `parseParameter`
@@ -74,7 +75,7 @@ func (ref *ParseReference) getCapabilities(ctx context.Context, c common.Args) (
case ref.Local != nil:
lcaps, err := ParseLocalFiles(ref.Local.Path, c)
if err != nil {
return nil, fmt.Errorf("failed to get capability from local file %s: %w", ref.DefinitionName, err)
return nil, fmt.Errorf("failed to get capability from local file %s: %w", ref.Local.Path, err)
}
for _, lcap := range lcaps {
caps = append(caps, *lcap)
@@ -519,6 +520,11 @@ func ParseLocalFiles(localFilePath string, c common.Args) ([]*types.Capability,
if !strings.HasSuffix(info.Name(), ".yaml") && !strings.HasSuffix(info.Name(), ".cue") {
return nil
}
// FIXME: remove this temporary fix when https://github.com/cue-lang/cue/issues/2047 is fixed
if strings.Contains(path, "container-image") {
lcaps = append(lcaps, fix.CapContainerImage)
return nil
}
lcap, err := ParseLocalFile(path, c)
if err != nil {
return err
@@ -588,7 +594,7 @@ func ParseLocalFile(localFilePath string, c common.Args) (*types.Capability, err
}
lcap, err := ParseCapabilityFromUnstructured(mapper, pd, def.Unstructured)
if err != nil {
return nil, errors.Wrapf(err, "fail to parse definition to capability")
return nil, errors.Wrapf(err, "fail to parse definition to capability %s", def.GetName())
}
return &lcap, nil