mirror of
https://github.com/kubevela/kubevela.git
synced 2026-05-15 22:07:13 +00:00
* Feat allow users to specify component revision name * Add unit tests Signed-off-by: LeoLiuYan <929908264@qq.com> * Add ExternalRevision field to ComponentManifest * Add handleComponentRevisionNameUnspecified func * Add unit tests for webhook Signed-off-by: LeoLiuYan <929908264@qq.com> * Add more unit tests * Feat allow users to specify component revision name * Add unit tests Signed-off-by: LeoLiuYan <929908264@qq.com> * Add ExternalRevision field to ComponentManifest * Add handleComponentRevisionNameUnspecified func * Add unit tests for webhook Signed-off-by: LeoLiuYan <929908264@qq.com> * Add more unit tests * Try to fix webhook unit tests * fix race (#2040) * fix test bug * More unit tst * More unit test and trigger ci Co-authored-by: yangsoon <yangsoonlx@gmail.com> Co-authored-by: Yin Da <yd219913@alibaba-inc.com>
147 lines
6.3 KiB
Go
147 lines
6.3 KiB
Go
/*
|
|
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 application
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
appsv1 "k8s.io/api/apps/v1"
|
|
apierrors "k8s.io/apimachinery/pkg/api/errors"
|
|
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
|
"k8s.io/apimachinery/pkg/util/validation/field"
|
|
"sigs.k8s.io/controller-runtime/pkg/client"
|
|
|
|
"github.com/oam-dev/kubevela/apis/core.oam.dev/v1beta1"
|
|
"github.com/oam-dev/kubevela/apis/types"
|
|
"github.com/oam-dev/kubevela/pkg/appfile"
|
|
"github.com/oam-dev/kubevela/pkg/controller/core.oam.dev/v1alpha2/application"
|
|
"github.com/oam-dev/kubevela/pkg/oam"
|
|
"github.com/oam-dev/kubevela/pkg/oam/util"
|
|
"github.com/oam-dev/kubevela/pkg/webhook/common/rollout"
|
|
)
|
|
|
|
// ValidateCreate validates the Application on creation
|
|
func (h *ValidatingHandler) ValidateCreate(ctx context.Context, app *v1beta1.Application) field.ErrorList {
|
|
var componentErrs field.ErrorList
|
|
// try to generate an app file
|
|
appParser := appfile.NewApplicationParser(h.Client, h.dm, h.pd)
|
|
|
|
af, err := appParser.GenerateAppFile(ctx, app)
|
|
if err != nil {
|
|
componentErrs = append(componentErrs, field.Invalid(field.NewPath("spec"), app, err.Error()))
|
|
// cannot generate appfile, no need to validate further
|
|
return componentErrs
|
|
}
|
|
if i, err := appParser.ValidateComponentNames(ctx, af); err != nil {
|
|
componentErrs = append(componentErrs, field.Invalid(field.NewPath(fmt.Sprintf("components[%d].name", i)), app, err.Error()))
|
|
}
|
|
if err := appParser.ValidateCUESchematicAppfile(af); err != nil {
|
|
componentErrs = append(componentErrs, field.Invalid(field.NewPath("schematic"), app, err.Error()))
|
|
}
|
|
if v := app.GetAnnotations()[oam.AnnotationAppRollout]; len(v) != 0 && v != "true" {
|
|
componentErrs = append(componentErrs, field.Invalid(field.NewPath("annotation:app.oam.dev/rollout-template"), app, "the annotation value of rollout-template must be true"))
|
|
}
|
|
if app.Spec.RolloutPlan != nil {
|
|
componentErrs = append(componentErrs, rollout.ValidateCreate(h.Client, app.Spec.RolloutPlan, field.NewPath("rolloutPlan"))...)
|
|
}
|
|
componentErrs = append(componentErrs, h.validateExternalRevisionName(ctx, app)...)
|
|
return componentErrs
|
|
}
|
|
|
|
// ValidateUpdate validates the Application on update
|
|
func (h *ValidatingHandler) ValidateUpdate(ctx context.Context, newApp, oldApp *v1beta1.Application) field.ErrorList {
|
|
// check if the newApp is valid
|
|
componentErrs := h.ValidateCreate(ctx, newApp)
|
|
// TODO: add more validating
|
|
return componentErrs
|
|
}
|
|
|
|
func (h *ValidatingHandler) validateExternalRevisionName(ctx context.Context, app *v1beta1.Application) field.ErrorList {
|
|
var componentErrs field.ErrorList
|
|
|
|
for index, comp := range app.Spec.Components {
|
|
if len(comp.ExternalRevision) == 0 {
|
|
continue
|
|
}
|
|
|
|
revisionName := comp.ExternalRevision
|
|
cr := &appsv1.ControllerRevision{}
|
|
if err := h.Client.Get(ctx, client.ObjectKey{Namespace: app.Namespace, Name: revisionName}, cr); err != nil {
|
|
if !apierrors.IsNotFound(err) {
|
|
componentErrs = append(componentErrs, field.Invalid(field.NewPath(fmt.Sprintf("components[%d].externalRevision", index)), app, err.Error()))
|
|
}
|
|
continue
|
|
}
|
|
|
|
labeledControllerComponent := cr.GetLabels()[oam.LabelControllerRevisionComponent]
|
|
labeledRevisionHash := cr.GetLabels()[oam.LabelComponentRevisionHash]
|
|
if labeledControllerComponent != comp.Name {
|
|
componentErrs = append(componentErrs, field.Invalid(field.NewPath(fmt.Sprintf("components[%d].externalRevision", index)), app, fmt.Sprintf("label:%s for revision:%s should be equal with component name", oam.LabelControllerRevisionComponent, revisionName)))
|
|
continue
|
|
}
|
|
if len(labeledRevisionHash) == 0 {
|
|
componentErrs = append(componentErrs, field.Invalid(field.NewPath(fmt.Sprintf("components[%d].externalRevision", index)), app, fmt.Sprintf("label:%s for revision:%s should exist", oam.LabelComponentRevisionHash, revisionName)))
|
|
continue
|
|
}
|
|
|
|
comp, err := util.RawExtension2Component(cr.Data)
|
|
if err != nil {
|
|
componentErrs = append(componentErrs, field.Invalid(field.NewPath(fmt.Sprintf("components[%d].externalRevision", index)), app, "cann't covert revision to component"))
|
|
continue
|
|
}
|
|
workload, err := util.RawExtension2Unstructured(&comp.Spec.Workload)
|
|
if err != nil {
|
|
componentErrs = append(componentErrs, field.Invalid(field.NewPath(fmt.Sprintf("components[%d].externalRevision", index)), app, "cann't extract workload"))
|
|
continue
|
|
}
|
|
packagedWorkloadResources := make([]*unstructured.Unstructured, 0)
|
|
if comp.Spec.Helm != nil {
|
|
helmRelease, err := util.RawExtension2Unstructured(&comp.Spec.Helm.Release)
|
|
if err != nil {
|
|
componentErrs = append(componentErrs, field.Invalid(field.NewPath(fmt.Sprintf("components[%d].externalRevision", index)), app, "cann't extract helmRelease"))
|
|
continue
|
|
}
|
|
helmRepository, err := util.RawExtension2Unstructured(&comp.Spec.Helm.Repository)
|
|
if err != nil {
|
|
componentErrs = append(componentErrs, field.Invalid(field.NewPath(fmt.Sprintf("components[%d].externalRevision", index)), app, "cann't extract helmRepository"))
|
|
continue
|
|
}
|
|
if helmRelease != nil {
|
|
packagedWorkloadResources = append(packagedWorkloadResources, helmRelease)
|
|
}
|
|
if helmRepository != nil {
|
|
packagedWorkloadResources = append(packagedWorkloadResources, helmRepository)
|
|
}
|
|
}
|
|
// recalculate hash
|
|
hash, err := application.ComputeComponentRevisionHash(&types.ComponentManifest{
|
|
StandardWorkload: workload,
|
|
PackagedWorkloadResources: packagedWorkloadResources,
|
|
})
|
|
if err != nil {
|
|
componentErrs = append(componentErrs, field.Invalid(field.NewPath(fmt.Sprintf("components[%d].externalRevision", index)), app, "cann't recalculate hash"))
|
|
continue
|
|
}
|
|
if hash != labeledRevisionHash {
|
|
componentErrs = append(componentErrs, field.Invalid(field.NewPath(fmt.Sprintf("components[%d].externalRevision", index)), app, "hash value which specified in labels and revision data's hash should be equal"))
|
|
continue
|
|
}
|
|
}
|
|
return componentErrs
|
|
}
|