blob: eb34383d7f9714a12af017c461fd0d6dc8a0f744 [file] [log] [blame]
khenaidooffe076b2019-01-15 16:08:08 -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 "github.com/coreos/etcd/version"
21 "github.com/coreos/go-semver/semver"
22 "github.com/coreos/pkg/capnslog"
23)
24
25type Capability string
26
27const (
28 AuthCapability Capability = "auth"
29 V3rpcCapability Capability = "v3rpc"
30)
31
32var (
33 plog = capnslog.NewPackageLogger("github.com/coreos/etcd", "etcdserver/api")
34
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 }
42
43 enableMapMu sync.RWMutex
44 // enabledMap points to a map in capabilityMaps
45 enabledMap map[Capability]bool
46
47 curVersion *semver.Version
48)
49
50func init() {
51 enabledMap = map[Capability]bool{
52 AuthCapability: true,
53 V3rpcCapability: true,
54 }
55}
56
57// UpdateCapability updates the enabledMap when the cluster version increases.
58func UpdateCapability(v *semver.Version) {
59 if v == nil {
60 // if recovered but version was never set by cluster
61 return
62 }
63 enableMapMu.Lock()
64 if curVersion != nil && !curVersion.LessThan(*v) {
65 enableMapMu.Unlock()
66 return
67 }
68 curVersion = v
69 enabledMap = capabilityMaps[curVersion.String()]
70 enableMapMu.Unlock()
71 plog.Infof("enabled capabilities for version %s", version.Cluster(v.String()))
72}
73
74func IsCapabilityEnabled(c Capability) bool {
75 enableMapMu.RLock()
76 defer enableMapMu.RUnlock()
77 if enabledMap == nil {
78 return false
79 }
80 return enabledMap[c]
81}
82
83func EnableCapability(c Capability) {
84 enableMapMu.Lock()
85 defer enableMapMu.Unlock()
86 enabledMap[c] = true
87}