github.com/migueleliasweb/helm@v2.6.1+incompatible/pkg/version/compatible.go (about)

     1  /*
     2  Copyright 2016 The Kubernetes Authors All rights reserved.
     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 // import "k8s.io/helm/pkg/version"
    18  
    19  import (
    20  	"fmt"
    21  	"strings"
    22  
    23  	"github.com/Masterminds/semver"
    24  )
    25  
    26  // IsCompatible tests if a client and server version are compatible.
    27  func IsCompatible(client, server string) bool {
    28  	if isUnreleased(client) || isUnreleased(server) {
    29  		return true
    30  	}
    31  	cv, err := semver.NewVersion(client)
    32  	if err != nil {
    33  		return false
    34  	}
    35  	sv, err := semver.NewVersion(server)
    36  	if err != nil {
    37  		return false
    38  	}
    39  
    40  	constraint := fmt.Sprintf("^%d.%d.x", cv.Major(), cv.Minor())
    41  	if cv.Prerelease() != "" || sv.Prerelease() != "" {
    42  		constraint = cv.String()
    43  	}
    44  
    45  	return IsCompatibleRange(constraint, server)
    46  }
    47  
    48  // IsCompatibleRange compares a version to a constraint.
    49  // It returns true if the version matches the constraint, and false in all other cases.
    50  func IsCompatibleRange(constraint, ver string) bool {
    51  	sv, err := semver.NewVersion(ver)
    52  	if err != nil {
    53  		return false
    54  	}
    55  
    56  	c, err := semver.NewConstraint(constraint)
    57  	if err != nil {
    58  		return false
    59  	}
    60  	return c.Check(sv)
    61  }
    62  
    63  func isUnreleased(v string) bool {
    64  	return strings.HasSuffix(v, "unreleased")
    65  }