github.phpd.cn/cilium/cilium@v1.6.12/pkg/k8s/version/version.go (about)

     1  // Copyright 2016-2020 Authors of Cilium
     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 version keeps track of the Kubernetes version the client is
    16  // connected to
    17  package version
    18  
    19  import (
    20  	"fmt"
    21  
    22  	"github.com/cilium/cilium/pkg/lock"
    23  	"github.com/cilium/cilium/pkg/option"
    24  	"github.com/cilium/cilium/pkg/versioncheck"
    25  
    26  	go_version "github.com/hashicorp/go-version"
    27  	"k8s.io/client-go/kubernetes"
    28  )
    29  
    30  // ServerCapabilities is a list of server capabilities derived based on version
    31  type ServerCapabilities struct {
    32  	// Patch is the ability to use PATCH to modify a resource
    33  	Patch bool
    34  
    35  	// UpdateStatus is the ability to update the status separately as a
    36  	// sub-resource
    37  	UpdateStatus bool
    38  
    39  	// MinimalVersionMet is true when the minimal version of Kubernetes
    40  	// required to run Cilium has been met
    41  	MinimalVersionMet bool
    42  
    43  	// FieldTypeInCRDSchema is set to true if Kubernetes supports having
    44  	// the field Type set in the CRD Schema.
    45  	FieldTypeInCRDSchema bool
    46  }
    47  
    48  type cachedVersion struct {
    49  	mutex        lock.RWMutex
    50  	capabilities ServerCapabilities
    51  	version      *go_version.Version
    52  }
    53  
    54  var (
    55  	cached = cachedVersion{}
    56  
    57  	patchConstraint           = versioncheck.MustCompile(">= 1.13.0")
    58  	updateStatusConstraint    = versioncheck.MustCompile(">= 1.11.0")
    59  	isGThanRootTypeConstraint = versioncheck.MustCompile(">= 1.12.0")
    60  
    61  	// MinimalVersionConstraint is the minimal version required to run
    62  	// Cilium
    63  	MinimalVersionConstraint = versioncheck.MustCompile(">= 1.8.0")
    64  )
    65  
    66  // Version returns the version of the Kubernetes apiserver
    67  func Version() *go_version.Version {
    68  	cached.mutex.RLock()
    69  	c := cached.version
    70  	cached.mutex.RUnlock()
    71  	return c
    72  }
    73  
    74  // Capabilities returns the capabilities of the Kubernetes apiserver
    75  func Capabilities() ServerCapabilities {
    76  	cached.mutex.RLock()
    77  	c := cached.capabilities
    78  	cached.mutex.RUnlock()
    79  	return c
    80  }
    81  
    82  func updateVersion(version *go_version.Version) {
    83  	cached.mutex.Lock()
    84  	defer cached.mutex.Unlock()
    85  
    86  	cached.version = version
    87  
    88  	cached.capabilities.Patch = patchConstraint.Check(version) || option.Config.K8sForceJSONPatch
    89  	cached.capabilities.UpdateStatus = updateStatusConstraint.Check(version)
    90  	cached.capabilities.MinimalVersionMet = MinimalVersionConstraint.Check(version)
    91  	cached.capabilities.FieldTypeInCRDSchema = isGThanRootTypeConstraint.Check(version)
    92  }
    93  
    94  // Force forces the use of a specific version
    95  func Force(version string) error {
    96  	ver, err := go_version.NewVersion(version)
    97  	if err != nil {
    98  		return err
    99  	}
   100  	updateVersion(ver)
   101  	return nil
   102  }
   103  
   104  // Update retrieves the version of the Kubernetes apiserver and derives the
   105  // capabilities. This function must be called after connectivity to the
   106  // apiserver has been established.
   107  func Update(client kubernetes.Interface) error {
   108  	sv, err := client.Discovery().ServerVersion()
   109  	if err != nil {
   110  		return err
   111  	}
   112  
   113  	// Try GitVersion first. In case of error fallback to MajorMinor
   114  	if sv.GitVersion != "" {
   115  		// This is a string like "v1.9.0"
   116  		ver, err := go_version.NewVersion(sv.GitVersion)
   117  		if err == nil {
   118  			updateVersion(ver)
   119  			return nil
   120  		}
   121  	}
   122  
   123  	if sv.Major != "" && sv.Minor != "" {
   124  		ver, err := go_version.NewVersion(fmt.Sprintf("%s.%s", sv.Major, sv.Minor))
   125  		if err == nil {
   126  			updateVersion(ver)
   127  			return nil
   128  		}
   129  	}
   130  
   131  	if err != nil {
   132  		return fmt.Errorf("cannot parse k8s server version from %+v: %s", sv, err)
   133  	}
   134  	return fmt.Errorf("cannot parse k8s server version from %+v", sv)
   135  }