...

Source file src/go.etcd.io/etcd/server/v3/etcdserver/api/capability.go

Documentation: go.etcd.io/etcd/server/v3/etcdserver/api

     1  // Copyright 2015 The etcd 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 api
    16  
    17  import (
    18  	"sync"
    19  
    20  	"go.etcd.io/etcd/api/v3/version"
    21  	"go.etcd.io/etcd/server/v3/etcdserver/api/membership"
    22  	"go.uber.org/zap"
    23  
    24  	"github.com/coreos/go-semver/semver"
    25  )
    26  
    27  type Capability string
    28  
    29  const (
    30  	AuthCapability  Capability = "auth"
    31  	V3rpcCapability Capability = "v3rpc"
    32  )
    33  
    34  var (
    35  	// capabilityMaps is a static map of version to capability map.
    36  	capabilityMaps = map[string]map[Capability]bool{
    37  		"3.0.0": {AuthCapability: true, V3rpcCapability: true},
    38  		"3.1.0": {AuthCapability: true, V3rpcCapability: true},
    39  		"3.2.0": {AuthCapability: true, V3rpcCapability: true},
    40  		"3.3.0": {AuthCapability: true, V3rpcCapability: true},
    41  		"3.4.0": {AuthCapability: true, V3rpcCapability: true},
    42  		"3.5.0": {AuthCapability: true, V3rpcCapability: true},
    43  	}
    44  
    45  	enableMapMu sync.RWMutex
    46  	// enabledMap points to a map in capabilityMaps
    47  	enabledMap map[Capability]bool
    48  
    49  	curVersion *semver.Version
    50  )
    51  
    52  func init() {
    53  	enabledMap = map[Capability]bool{
    54  		AuthCapability:  true,
    55  		V3rpcCapability: true,
    56  	}
    57  }
    58  
    59  // UpdateCapability updates the enabledMap when the cluster version increases.
    60  func UpdateCapability(lg *zap.Logger, v *semver.Version) {
    61  	if v == nil {
    62  		// if recovered but version was never set by cluster
    63  		return
    64  	}
    65  	enableMapMu.Lock()
    66  	if curVersion != nil && !membership.IsValidVersionChange(v, curVersion) {
    67  		enableMapMu.Unlock()
    68  		return
    69  	}
    70  	curVersion = v
    71  	enabledMap = capabilityMaps[curVersion.String()]
    72  	enableMapMu.Unlock()
    73  
    74  	if lg != nil {
    75  		lg.Info(
    76  			"enabled capabilities for version",
    77  			zap.String("cluster-version", version.Cluster(v.String())),
    78  		)
    79  	}
    80  }
    81  
    82  func IsCapabilityEnabled(c Capability) bool {
    83  	enableMapMu.RLock()
    84  	defer enableMapMu.RUnlock()
    85  	if enabledMap == nil {
    86  		return false
    87  	}
    88  	return enabledMap[c]
    89  }
    90  
    91  func EnableCapability(c Capability) {
    92  	enableMapMu.Lock()
    93  	defer enableMapMu.Unlock()
    94  	enabledMap[c] = true
    95  }
    96  

View as plain text