github.com/spotmaxtech/k8s-apimachinery-v0260@v0.0.1/pkg/version/helpers.go (about)

     1  /*
     2  Copyright 2018 The Kubernetes 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 version
    18  
    19  import (
    20  	"regexp"
    21  	"strconv"
    22  	"strings"
    23  )
    24  
    25  type versionType int
    26  
    27  const (
    28  	// Bigger the version type number, higher priority it is
    29  	versionTypeAlpha versionType = iota
    30  	versionTypeBeta
    31  	versionTypeGA
    32  )
    33  
    34  var kubeVersionRegex = regexp.MustCompile("^v([\\d]+)(?:(alpha|beta)([\\d]+))?$")
    35  
    36  func parseKubeVersion(v string) (majorVersion int, vType versionType, minorVersion int, ok bool) {
    37  	var err error
    38  	submatches := kubeVersionRegex.FindStringSubmatch(v)
    39  	if len(submatches) != 4 {
    40  		return 0, 0, 0, false
    41  	}
    42  	switch submatches[2] {
    43  	case "alpha":
    44  		vType = versionTypeAlpha
    45  	case "beta":
    46  		vType = versionTypeBeta
    47  	case "":
    48  		vType = versionTypeGA
    49  	default:
    50  		return 0, 0, 0, false
    51  	}
    52  	if majorVersion, err = strconv.Atoi(submatches[1]); err != nil {
    53  		return 0, 0, 0, false
    54  	}
    55  	if vType != versionTypeGA {
    56  		if minorVersion, err = strconv.Atoi(submatches[3]); err != nil {
    57  			return 0, 0, 0, false
    58  		}
    59  	}
    60  	return majorVersion, vType, minorVersion, true
    61  }
    62  
    63  // CompareKubeAwareVersionStrings compares two kube-like version strings.
    64  // Kube-like version strings are starting with a v, followed by a major version, optional "alpha" or "beta" strings
    65  // followed by a minor version (e.g. v1, v2beta1). Versions will be sorted based on GA/alpha/beta first and then major
    66  // and minor versions. e.g. v2, v1, v1beta2, v1beta1, v1alpha1.
    67  func CompareKubeAwareVersionStrings(v1, v2 string) int {
    68  	if v1 == v2 {
    69  		return 0
    70  	}
    71  	v1major, v1type, v1minor, ok1 := parseKubeVersion(v1)
    72  	v2major, v2type, v2minor, ok2 := parseKubeVersion(v2)
    73  	switch {
    74  	case !ok1 && !ok2:
    75  		return strings.Compare(v2, v1)
    76  	case !ok1 && ok2:
    77  		return -1
    78  	case ok1 && !ok2:
    79  		return 1
    80  	}
    81  	if v1type != v2type {
    82  		return int(v1type) - int(v2type)
    83  	}
    84  	if v1major != v2major {
    85  		return v1major - v2major
    86  	}
    87  	return v1minor - v2minor
    88  }