-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
version.go
129 lines (108 loc) · 3.54 KB
/
version.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// Copyright 2016-2019 Authors of Cilium
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package version keeps track of the Kubernetes version the client is
// connected to
package version
import (
"fmt"
"github.com/cilium/cilium/pkg/lock"
"github.com/cilium/cilium/pkg/option"
"github.com/cilium/cilium/pkg/versioncheck"
go_version "github.com/hashicorp/go-version"
"k8s.io/client-go/kubernetes"
)
// ServerCapabilities is a list of server capabilities derived based on version
type ServerCapabilities struct {
// Patch is the ability to use PATCH to modify a resource
Patch bool
// UpdateStatus is the ability to update the status separately as a
// sub-resource
UpdateStatus bool
// MinimalVersionMet is true when the minimal version of Kubernetes
// required to run Cilium has been met
MinimalVersionMet bool
}
type cachedVersion struct {
mutex lock.RWMutex
capabilities ServerCapabilities
version *go_version.Version
}
var (
cached = cachedVersion{}
patchConstraint = versioncheck.MustCompile(">= 1.13.0")
updateStatusConstraint = versioncheck.MustCompile(">= 1.11.0")
// MinimalVersionConstraint is the minimal version required to run
// Cilium
MinimalVersionConstraint = versioncheck.MustCompile(">= 1.8.0")
)
// Version returns the version of the Kubernetes apiserver
func Version() *go_version.Version {
cached.mutex.RLock()
c := cached.version
cached.mutex.RUnlock()
return c
}
// Capabilities returns the capabilities of the Kubernetes apiserver
func Capabilities() ServerCapabilities {
cached.mutex.RLock()
c := cached.capabilities
cached.mutex.RUnlock()
return c
}
func updateVersion(version *go_version.Version) {
cached.mutex.Lock()
defer cached.mutex.Unlock()
cached.version = version
cached.capabilities.Patch = patchConstraint.Check(version) || option.Config.K8sForceJSONPatch
cached.capabilities.UpdateStatus = updateStatusConstraint.Check(version)
cached.capabilities.MinimalVersionMet = MinimalVersionConstraint.Check(version)
}
// Force forces the use of a specific version
func Force(version string) error {
ver, err := go_version.NewVersion(version)
if err != nil {
return err
}
updateVersion(ver)
return nil
}
// Update retrieves the version of the Kubernetes apiserver and derives the
// capabilities. This function must be called after connectivity to the
// apiserver has been established.
func Update(client kubernetes.Interface) error {
sv, err := client.Discovery().ServerVersion()
if err != nil {
return err
}
// Try GitVersion first. In case of error fallback to MajorMinor
if sv.GitVersion != "" {
// This is a string like "v1.9.0"
ver, err := go_version.NewVersion(sv.GitVersion)
if err == nil {
updateVersion(ver)
return nil
}
}
if sv.Major != "" && sv.Minor != "" {
ver, err := go_version.NewVersion(fmt.Sprintf("%s.%s", sv.Major, sv.Minor))
if err == nil {
updateVersion(ver)
return nil
}
}
if err != nil {
return fmt.Errorf("cannot parse k8s server version from %+v: %s", sv, err)
}
return fmt.Errorf("cannot parse k8s server version from %+v", sv)
}