mirror of
https://github.com/kubevela/kubevela.git
synced 2026-05-23 09:43:46 +00:00
* add webhooks * app controller change * add component revision and appconfig revision and test * solidify the component revision logic and fix component revisoin bugs * fix command cli e2e failure * fix the bug caused by rawExtention * fix UT test * retry on component not found * lint * revert component revision create order
61 lines
2.1 KiB
Go
61 lines
2.1 KiB
Go
package common
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"reflect"
|
|
"strings"
|
|
|
|
"github.com/crossplane/crossplane-runtime/pkg/logging"
|
|
v1 "k8s.io/api/apps/v1"
|
|
apierrors "k8s.io/apimachinery/pkg/api/errors"
|
|
"k8s.io/apimachinery/pkg/util/wait"
|
|
"k8s.io/client-go/util/retry"
|
|
"sigs.k8s.io/controller-runtime/pkg/client"
|
|
|
|
"github.com/oam-dev/kubevela/apis/core.oam.dev/v1alpha2"
|
|
"github.com/oam-dev/kubevela/pkg/oam/util"
|
|
)
|
|
|
|
// ConstructRevisionName will generate revisionName from componentName
|
|
// will be <componentName>-v<RevisionNumber>, for example: comp-v1
|
|
func ConstructRevisionName(componentName string, revision int64) string {
|
|
return strings.Join([]string{componentName, fmt.Sprintf("v%d", revision)}, "-")
|
|
}
|
|
|
|
// ExtractComponentName will extract componentName from revisionName
|
|
func ExtractComponentName(revisionName string) string {
|
|
splits := strings.Split(revisionName, "-")
|
|
return strings.Join(splits[0:len(splits)-1], "-")
|
|
}
|
|
|
|
// CompareWithRevision compares a component's spec with the component's latest revision content
|
|
func CompareWithRevision(ctx context.Context, c client.Client, logger logging.Logger, componentName, nameSpace,
|
|
latestRevision string, curCompSpec *v1alpha2.ComponentSpec) (bool, error) {
|
|
oldRev := &v1.ControllerRevision{}
|
|
// retry on NotFound since we update the component last revision first
|
|
err := wait.ExponentialBackoff(retry.DefaultBackoff, func() (bool, error) {
|
|
err := c.Get(ctx, client.ObjectKey{Namespace: nameSpace, Name: latestRevision}, oldRev)
|
|
if err != nil && !apierrors.IsNotFound(err) {
|
|
logger.Info(fmt.Sprintf("get old controllerRevision %s error %v",
|
|
latestRevision, err), "componentName", componentName)
|
|
return false, err
|
|
}
|
|
return true, nil
|
|
})
|
|
if err != nil {
|
|
return true, err
|
|
}
|
|
oldComp, err := util.UnpackRevisionData(oldRev)
|
|
if err != nil {
|
|
logger.Info(fmt.Sprintf("Unmarshal old controllerRevision %s error %v",
|
|
latestRevision, err), "componentName", componentName)
|
|
return true, err
|
|
}
|
|
if reflect.DeepEqual(curCompSpec, &oldComp.Spec) {
|
|
// no need to create a new revision
|
|
return false, nil
|
|
}
|
|
return true, nil
|
|
}
|