github.com/oam-dev/kubevela@v1.9.11/pkg/controller/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  	"fmt"
    21  	"strconv"
    22  	"strings"
    23  
    24  	"github.com/mitchellh/hashstructure/v2"
    25  
    26  	"github.com/oam-dev/kubevela/apis/core.oam.dev/v1beta1"
    27  	"github.com/oam-dev/kubevela/pkg/oam/util"
    28  )
    29  
    30  // GetAppNextRevision will generate the next revision name and revision number for application
    31  func GetAppNextRevision(app *v1beta1.Application) (string, int64) {
    32  	if app == nil {
    33  		// should never happen
    34  		return "", 0
    35  	}
    36  	var nextRevision int64 = 1
    37  	if app.Status.LatestRevision != nil {
    38  		// revision will always bump and increment no matter what the way user is running.
    39  		nextRevision = app.Status.LatestRevision.Revision + 1
    40  	}
    41  	return ConstructRevisionName(app.Name, nextRevision), nextRevision
    42  }
    43  
    44  // ConstructRevisionName will generate a revisionName given the componentName and revision
    45  // will be <componentName>-v<RevisionNumber>, for example: comp-v1
    46  func ConstructRevisionName(componentName string, revision int64) string {
    47  	return strings.Join([]string{componentName, fmt.Sprintf("v%d", revision)}, "-")
    48  }
    49  
    50  // ExtractComponentName will extract the componentName from a revisionName
    51  var ExtractComponentName = util.ExtractComponentName
    52  
    53  // ExtractRevision will extract the revision from a revisionName
    54  func ExtractRevision(revisionName string) (int, error) {
    55  	splits := strings.Split(revisionName, "-")
    56  	// the revision is the last string without the prefix "v"
    57  	return strconv.Atoi(strings.TrimPrefix(splits[len(splits)-1], "v"))
    58  }
    59  
    60  // ComputeSpecHash computes the hash value of a k8s resource spec
    61  func ComputeSpecHash(spec interface{}) (string, error) {
    62  	// compute a hash value of any resource spec
    63  	specHash, err := hashstructure.Hash(spec, hashstructure.FormatV2, nil)
    64  	if err != nil {
    65  		return "", err
    66  	}
    67  	specHashLabel := strconv.FormatUint(specHash, 16)
    68  	return specHashLabel, nil
    69  }