...

Source file src/k8s.io/kubectl/pkg/cmd/version/skew_warning.go

Documentation: k8s.io/kubectl/pkg/cmd/version

     1  /*
     2  Copyright 2021 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  	"fmt"
    21  	"io"
    22  	"k8s.io/apimachinery/pkg/util/version"
    23  	apimachineryversion "k8s.io/apimachinery/pkg/version"
    24  	"math"
    25  )
    26  
    27  // supportedMinorVersionSkew is the maximum supported difference between the client and server minor versions.
    28  // For example: client versions 1.18, 1.19, and 1.20 would be within the supported version skew for server version 1.19,
    29  // and server versions 1.18, 1.19, and 1.20 would be within the supported version skew for client version 1.19.
    30  const supportedMinorVersionSkew = 1
    31  
    32  // printVersionSkewWarning prints a warning message if the difference between the client and version is greater than
    33  // the supported version skew.
    34  func printVersionSkewWarning(w io.Writer, clientVersion, serverVersion apimachineryversion.Info) error {
    35  	parsedClientVersion, err := version.ParseSemantic(clientVersion.GitVersion)
    36  	if err != nil {
    37  		return err
    38  	}
    39  
    40  	parsedServerVersion, err := version.ParseSemantic(serverVersion.GitVersion)
    41  	if err != nil {
    42  		return err
    43  	}
    44  
    45  	majorVersionDifference := math.Abs(float64(parsedClientVersion.Major()) - float64(parsedServerVersion.Major()))
    46  	minorVersionDifference := math.Abs(float64(parsedClientVersion.Minor()) - float64(parsedServerVersion.Minor()))
    47  
    48  	if majorVersionDifference > 0 || minorVersionDifference > supportedMinorVersionSkew {
    49  		fmt.Fprintf(w, "WARNING: version difference between client (%d.%d) and server (%d.%d) exceeds the supported minor version skew of +/-%d\n",
    50  			parsedClientVersion.Major(), parsedClientVersion.Minor(), parsedServerVersion.Major(), parsedServerVersion.Minor(), supportedMinorVersionSkew)
    51  	}
    52  
    53  	return nil
    54  }
    55  

View as plain text