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