github.com/oam-dev/kubevela@v1.9.11/pkg/appfile/validate.go (about)

     1  /*
     2  Copyright 2021 The KubeVela Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package appfile
    18  
    19  import (
    20  	"fmt"
    21  
    22  	"github.com/pkg/errors"
    23  
    24  	"github.com/kubevela/workflow/pkg/cue/process"
    25  
    26  	"github.com/oam-dev/kubevela/apis/core.oam.dev/v1alpha1"
    27  	"github.com/oam-dev/kubevela/apis/types"
    28  	velaprocess "github.com/oam-dev/kubevela/pkg/cue/process"
    29  )
    30  
    31  // ValidateCUESchematicAppfile validates CUE schematic workloads in an Appfile
    32  func (p *Parser) ValidateCUESchematicAppfile(a *Appfile) error {
    33  	for _, wl := range a.ParsedComponents {
    34  		// because helm & kube schematic has no CUE template
    35  		// it only validates CUE schematic workload
    36  		if wl.CapabilityCategory != types.CUECategory || wl.Type == v1alpha1.RefObjectsComponentType {
    37  			continue
    38  		}
    39  		ctxData := GenerateContextDataFromAppFile(a, wl.Name)
    40  		pCtx, err := newValidationProcessContext(wl, ctxData)
    41  		if err != nil {
    42  			return errors.WithMessagef(err, "cannot create the validation process context of app=%s in namespace=%s", a.Name, a.Namespace)
    43  		}
    44  		for _, tr := range wl.Traits {
    45  			if tr.CapabilityCategory != types.CUECategory {
    46  				continue
    47  			}
    48  			if err := tr.EvalContext(pCtx); err != nil {
    49  				return errors.WithMessagef(err, "cannot evaluate trait %q", tr.Name)
    50  			}
    51  		}
    52  	}
    53  	return nil
    54  }
    55  
    56  func newValidationProcessContext(c *Component, ctxData velaprocess.ContextData) (process.Context, error) {
    57  	baseHooks := []process.BaseHook{
    58  		// add more hook funcs here to validate CUE base
    59  	}
    60  	auxiliaryHooks := []process.AuxiliaryHook{
    61  		// add more hook funcs here to validate CUE auxiliaries
    62  		validateAuxiliaryNameUnique(),
    63  	}
    64  
    65  	ctxData.BaseHooks = baseHooks
    66  	ctxData.AuxiliaryHooks = auxiliaryHooks
    67  	pCtx := velaprocess.NewContext(ctxData)
    68  	if err := c.EvalContext(pCtx); err != nil {
    69  		return nil, errors.Wrapf(err, "evaluate base template app=%s in namespace=%s", ctxData.AppName, ctxData.Namespace)
    70  	}
    71  	return pCtx, nil
    72  }
    73  
    74  // validateAuxiliaryNameUnique validates the name of each outputs item which is
    75  // called auxiliary in vela CUE-based DSL.
    76  // Each capability definition can have arbitrary number of outputs and each
    77  // outputs can have more than one auxiliaries.
    78  // Auxiliaries can be referenced by other cap's template to pass data
    79  // within a workload, so their names must be unique.
    80  func validateAuxiliaryNameUnique() process.AuxiliaryHook {
    81  	return process.AuxiliaryHookFn(func(c process.Context, a []process.Auxiliary) error {
    82  		_, existingAuxs := c.Output()
    83  		for _, newAux := range a {
    84  			for _, existingAux := range existingAuxs {
    85  				if existingAux.Name == newAux.Name {
    86  					return errors.Wrap(fmt.Errorf("auxiliary %q already exits", newAux.Name),
    87  						"outputs item name must be unique")
    88  				}
    89  			}
    90  		}
    91  		return nil
    92  	})
    93  }