2021-03-26 15:24:19 +08:00
|
|
|
/*
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2020-11-01 10:44:17 +08:00
|
|
|
package utils
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strconv"
|
2020-12-04 10:03:25 +08:00
|
|
|
"strings"
|
2021-02-20 04:11:26 +08:00
|
|
|
"time"
|
2020-11-01 10:44:17 +08:00
|
|
|
|
2021-03-16 11:39:55 +08:00
|
|
|
"github.com/mitchellh/hashstructure/v2"
|
2021-02-20 04:11:26 +08:00
|
|
|
"k8s.io/apimachinery/pkg/util/wait"
|
2020-11-26 12:27:25 +08:00
|
|
|
|
2021-03-25 08:15:20 +08:00
|
|
|
"github.com/oam-dev/kubevela/apis/core.oam.dev/v1beta1"
|
2021-02-21 15:10:15 +08:00
|
|
|
"github.com/oam-dev/kubevela/pkg/oam/util"
|
2020-11-01 10:44:17 +08:00
|
|
|
)
|
|
|
|
|
2021-02-20 04:11:26 +08:00
|
|
|
// DefaultBackoff is the backoff we use in controller
|
|
|
|
var DefaultBackoff = wait.Backoff{
|
|
|
|
Duration: 1 * time.Second,
|
|
|
|
Factor: 2,
|
|
|
|
Steps: 5,
|
|
|
|
Jitter: 0.1,
|
|
|
|
}
|
|
|
|
|
2021-03-11 15:56:38 +08:00
|
|
|
// GetAppNextRevision will generate the next revision name and revision number for application
|
2021-03-25 08:15:20 +08:00
|
|
|
func GetAppNextRevision(app *v1beta1.Application) (string, int64) {
|
2021-03-10 16:47:17 +08:00
|
|
|
if app == nil {
|
|
|
|
// should never happen
|
|
|
|
return "", 0
|
|
|
|
}
|
|
|
|
var nextRevision int64 = 1
|
|
|
|
if app.Status.LatestRevision != nil {
|
2021-03-20 03:30:31 +08:00
|
|
|
// revision will always bump and increment no matter what the way user is running.
|
|
|
|
nextRevision = app.Status.LatestRevision.Revision + 1
|
2021-03-10 16:47:17 +08:00
|
|
|
}
|
|
|
|
return ConstructRevisionName(app.Name, nextRevision), nextRevision
|
|
|
|
}
|
|
|
|
|
2021-02-21 15:10:15 +08:00
|
|
|
// ConstructRevisionName will generate a revisionName given the componentName and revision
|
|
|
|
// 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 the componentName from a revisionName
|
2021-06-24 15:06:58 +08:00
|
|
|
var ExtractComponentName = util.ExtractComponentName
|
2021-02-21 15:10:15 +08:00
|
|
|
|
|
|
|
// ExtractRevision will extract the revision from a revisionName
|
|
|
|
func ExtractRevision(revisionName string) (int, error) {
|
|
|
|
splits := strings.Split(revisionName, "-")
|
|
|
|
// the revision is the last string without the prefix "v"
|
|
|
|
return strconv.Atoi(strings.TrimPrefix(splits[len(splits)-1], "v"))
|
|
|
|
}
|
|
|
|
|
2021-03-16 11:39:55 +08:00
|
|
|
// ComputeSpecHash computes the hash value of a k8s resource spec
|
|
|
|
func ComputeSpecHash(spec interface{}) (string, error) {
|
|
|
|
// compute a hash value of any resource spec
|
|
|
|
specHash, err := hashstructure.Hash(spec, hashstructure.FormatV2, nil)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
specHashLabel := strconv.FormatUint(specHash, 16)
|
|
|
|
return specHashLabel, nil
|
|
|
|
}
|