github.com/oam-dev/kubevela@v1.9.11/pkg/webhook/core.oam.dev/v1beta1/application/validation.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 application
    18  
    19  import (
    20  	"context"
    21  	"fmt"
    22  	"time"
    23  
    24  	"github.com/kubevela/pkg/controller/sharding"
    25  	"github.com/kubevela/pkg/util/singleton"
    26  	"k8s.io/apimachinery/pkg/util/validation/field"
    27  	utilfeature "k8s.io/apiserver/pkg/util/feature"
    28  	"sigs.k8s.io/controller-runtime/pkg/client"
    29  
    30  	"github.com/oam-dev/kubevela/apis/core.oam.dev/v1beta1"
    31  	"github.com/oam-dev/kubevela/pkg/appfile"
    32  	"github.com/oam-dev/kubevela/pkg/features"
    33  )
    34  
    35  // ValidateWorkflow validates the Application workflow
    36  func (h *ValidatingHandler) ValidateWorkflow(_ context.Context, app *v1beta1.Application) field.ErrorList {
    37  	var errs field.ErrorList
    38  	if app.Spec.Workflow != nil {
    39  		stepName := make(map[string]interface{})
    40  		for _, step := range app.Spec.Workflow.Steps {
    41  			if _, ok := stepName[step.Name]; ok {
    42  				errs = append(errs, field.Invalid(field.NewPath("spec", "workflow", "steps"), step.Name, "duplicated step name"))
    43  			}
    44  			stepName[step.Name] = nil
    45  			if step.Timeout != "" {
    46  				errs = append(errs, h.ValidateTimeout(step.Name, step.Timeout)...)
    47  			}
    48  			for _, sub := range step.SubSteps {
    49  				if _, ok := stepName[sub.Name]; ok {
    50  					errs = append(errs, field.Invalid(field.NewPath("spec", "workflow", "steps", "subSteps"), sub.Name, "duplicated step name"))
    51  				}
    52  				stepName[sub.Name] = nil
    53  				if step.Timeout != "" {
    54  					errs = append(errs, h.ValidateTimeout(step.Name, step.Timeout)...)
    55  				}
    56  			}
    57  		}
    58  	}
    59  	return errs
    60  }
    61  
    62  // ValidateTimeout validates the timeout of steps
    63  func (h *ValidatingHandler) ValidateTimeout(name, timeout string) field.ErrorList {
    64  	var errs field.ErrorList
    65  	_, err := time.ParseDuration(timeout)
    66  	if err != nil {
    67  		errs = append(errs, field.Invalid(field.NewPath("spec", "workflow", "steps", "timeout"), name, "invalid timeout, please use the format of timeout like 1s, 1m, 1h or 1d"))
    68  	}
    69  	return errs
    70  }
    71  
    72  // appRevBypassCacheClient
    73  type appRevBypassCacheClient struct {
    74  	client.Client
    75  }
    76  
    77  // Get retrieve appRev directly from request if sharding enabled
    78  func (in *appRevBypassCacheClient) Get(ctx context.Context, key client.ObjectKey, obj client.Object, _ ...client.GetOption) error {
    79  	if _, ok := obj.(*v1beta1.ApplicationRevision); ok && sharding.EnableSharding {
    80  		return singleton.KubeClient.Get().Get(ctx, key, obj)
    81  	}
    82  	return in.Client.Get(ctx, key, obj)
    83  }
    84  
    85  // ValidateComponents validates the Application components
    86  func (h *ValidatingHandler) ValidateComponents(ctx context.Context, app *v1beta1.Application) field.ErrorList {
    87  	if sharding.EnableSharding && !utilfeature.DefaultMutableFeatureGate.Enabled(features.ValidateComponentWhenSharding) {
    88  		return nil
    89  	}
    90  	var componentErrs field.ErrorList
    91  	// try to generate an app file
    92  	cli := &appRevBypassCacheClient{Client: h.Client}
    93  	appParser := appfile.NewApplicationParser(cli, h.pd)
    94  
    95  	af, err := appParser.GenerateAppFile(ctx, app)
    96  	if err != nil {
    97  		componentErrs = append(componentErrs, field.Invalid(field.NewPath("spec"), app, err.Error()))
    98  		// cannot generate appfile, no need to validate further
    99  		return componentErrs
   100  	}
   101  	if i, err := appParser.ValidateComponentNames(app); err != nil {
   102  		componentErrs = append(componentErrs, field.Invalid(field.NewPath(fmt.Sprintf("components[%d].name", i)), app, err.Error()))
   103  	}
   104  	if err := appParser.ValidateCUESchematicAppfile(af); err != nil {
   105  		componentErrs = append(componentErrs, field.Invalid(field.NewPath("schematic"), app, err.Error()))
   106  	}
   107  	return componentErrs
   108  }
   109  
   110  // ValidateCreate validates the Application on creation
   111  func (h *ValidatingHandler) ValidateCreate(ctx context.Context, app *v1beta1.Application) field.ErrorList {
   112  	var errs field.ErrorList
   113  
   114  	errs = append(errs, h.ValidateWorkflow(ctx, app)...)
   115  	errs = append(errs, h.ValidateComponents(ctx, app)...)
   116  	return errs
   117  }
   118  
   119  // ValidateUpdate validates the Application on update
   120  func (h *ValidatingHandler) ValidateUpdate(ctx context.Context, newApp, _ *v1beta1.Application) field.ErrorList {
   121  	// check if the newApp is valid
   122  	errs := h.ValidateCreate(ctx, newApp)
   123  	// TODO: add more validating
   124  	return errs
   125  }