github.com/oam-dev/kubevela@v1.9.11/pkg/webhook/utils/utils.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 utils
    18  
    19  import (
    20  	"context"
    21  	"regexp"
    22  	"strings"
    23  
    24  	"cuelang.org/go/cue/cuecontext"
    25  	cueErrors "cuelang.org/go/cue/errors"
    26  	"github.com/pkg/errors"
    27  	"k8s.io/apimachinery/pkg/runtime"
    28  	"k8s.io/apimachinery/pkg/types"
    29  	"k8s.io/apimachinery/pkg/util/validation"
    30  	"sigs.k8s.io/controller-runtime/pkg/client"
    31  
    32  	"github.com/oam-dev/kubevela/apis/core.oam.dev/v1beta1"
    33  	"github.com/oam-dev/kubevela/pkg/controller/core.oam.dev/v1beta1/core"
    34  )
    35  
    36  // ContextRegex to match '**: reference "context" not found'
    37  var ContextRegex = `^.+:\sreference\s\"context\"\snot\sfound$`
    38  
    39  // ValidateDefinitionRevision validate whether definition will modify the immutable object definitionRevision
    40  func ValidateDefinitionRevision(ctx context.Context, cli client.Client, def runtime.Object, defRevNamespacedName types.NamespacedName) error {
    41  	if errs := validation.IsQualifiedName(defRevNamespacedName.Name); len(errs) != 0 {
    42  		return errors.Errorf("invalid definitionRevision name %s:%s", defRevNamespacedName.Name, strings.Join(errs, ","))
    43  	}
    44  
    45  	defRev := new(v1beta1.DefinitionRevision)
    46  	if err := cli.Get(ctx, defRevNamespacedName, defRev); err != nil {
    47  		return client.IgnoreNotFound(err)
    48  	}
    49  
    50  	newRev, _, err := core.GatherRevisionInfo(def)
    51  	if err != nil {
    52  		return err
    53  	}
    54  	if defRev.Spec.RevisionHash != newRev.Spec.RevisionHash {
    55  		return errors.New("the definition's spec is different with existing definitionRevision's spec")
    56  	}
    57  	if !core.DeepEqualDefRevision(defRev, newRev) {
    58  		return errors.New("the definition's spec is different with existing definitionRevision's spec")
    59  	}
    60  	return nil
    61  }
    62  
    63  // ValidateCueTemplate validate cueTemplate
    64  func ValidateCueTemplate(cueTemplate string) error {
    65  
    66  	val := cuecontext.New().CompileString(cueTemplate)
    67  	if e := checkError(val.Err()); e != nil {
    68  		return e
    69  	}
    70  
    71  	err := val.Validate()
    72  	if e := checkError(err); e != nil {
    73  		return e
    74  	}
    75  	return nil
    76  }
    77  
    78  func checkError(err error) error {
    79  	re := regexp.MustCompile(ContextRegex)
    80  	if err != nil {
    81  		// ignore context not found error
    82  		for _, e := range cueErrors.Errors(err) {
    83  			if !re.MatchString(e.Error()) {
    84  				return cueErrors.New(e.Error())
    85  			}
    86  		}
    87  	}
    88  	return nil
    89  }