istio.io/istio@v0.0.0-20240520182934-d79c90f27776/istioctl/pkg/install/k8sversion/version.go (about)

     1  // Copyright Istio Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package k8sversion
    16  
    17  import (
    18  	"fmt"
    19  
    20  	goversion "github.com/hashicorp/go-version"
    21  	"k8s.io/apimachinery/pkg/version"
    22  
    23  	"istio.io/istio/operator/pkg/util/clog"
    24  	"istio.io/istio/pkg/kube"
    25  	pkgVersion "istio.io/istio/pkg/version"
    26  )
    27  
    28  const (
    29  	// MinK8SVersion is the minimum k8s version required to run this version of Istio
    30  	// https://istio.io/docs/setup/platform-setup/
    31  	MinK8SVersion               = 26
    32  	UnSupportedK8SVersionLogMsg = "\nThe Kubernetes version %s is not supported by Istio %s. The minimum supported Kubernetes version is 1.%d.\n" +
    33  		"Proceeding with the installation, but you might experience problems. " +
    34  		"See https://istio.io/latest/docs/releases/supported-releases/ for a list of supported versions.\n"
    35  )
    36  
    37  // CheckKubernetesVersion checks if this Istio version is supported in the k8s version
    38  func CheckKubernetesVersion(versionInfo *version.Info) (bool, error) {
    39  	v, err := extractKubernetesVersion(versionInfo)
    40  	if err != nil {
    41  		return false, err
    42  	}
    43  	return MinK8SVersion <= v, nil
    44  }
    45  
    46  // extractKubernetesVersion returns the Kubernetes minor version. For example, `v1.19.1` will return `19`
    47  func extractKubernetesVersion(versionInfo *version.Info) (int, error) {
    48  	ver, err := goversion.NewVersion(versionInfo.String())
    49  	if err != nil {
    50  		return 0, fmt.Errorf("could not parse %v", err)
    51  	}
    52  	// Segments provide slice of int eg: v1.19.1 => [1, 19, 1]
    53  	num := ver.Segments()[1]
    54  	return num, nil
    55  }
    56  
    57  // IsK8VersionSupported checks minimum supported Kubernetes version for Istio.
    58  // If the K8s version is not at least the `MinK8SVersion`, it logs a message warning the user that they
    59  // may experience problems if they proceed with the install.
    60  func IsK8VersionSupported(c kube.Client, l clog.Logger) error {
    61  	serverVersion, err := c.GetKubernetesVersion()
    62  	if err != nil {
    63  		return fmt.Errorf("error getting Kubernetes version: %w", err)
    64  	}
    65  	if !kube.IsAtLeastVersion(c, MinK8SVersion) {
    66  		l.LogAndPrintf(UnSupportedK8SVersionLogMsg, serverVersion.GitVersion, pkgVersion.Info.Version, MinK8SVersion)
    67  	}
    68  	return nil
    69  }