forked from openshift/library-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
version.go
90 lines (73 loc) · 2.17 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
package status
import (
"os"
"sync"
corev1client "k8s.io/client-go/kubernetes/typed/core/v1"
"github.com/openshift/library-go/pkg/operator/events"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
type versionGetter struct {
lock sync.Mutex
versions map[string]string
notificationChannels []chan struct{}
}
const (
operandImageVersionEnvVarName = "OPERAND_IMAGE_VERSION"
operatorImageVersionEnvVarName = "OPERATOR_IMAGE_VERSION"
)
func NewVersionGetter() VersionGetter {
return &versionGetter{
versions: map[string]string{},
}
}
func (v *versionGetter) SetVersion(operandName, version string) {
v.lock.Lock()
defer v.lock.Unlock()
v.versions[operandName] = version
for i := range v.notificationChannels {
ch := v.notificationChannels[i]
// don't let a slow consumer block the rest
go func() {
ch <- struct{}{}
}()
}
}
func (v *versionGetter) GetVersions() map[string]string {
v.lock.Lock()
defer v.lock.Unlock()
ret := map[string]string{}
for k, v := range v.versions {
ret[k] = v
}
return ret
}
func (v *versionGetter) VersionChangedChannel() <-chan struct{} {
v.lock.Lock()
defer v.lock.Unlock()
channel := make(chan struct{}, 50)
v.notificationChannels = append(v.notificationChannels, channel)
return channel
}
func VersionForOperandFromEnv() string {
return os.Getenv(operandImageVersionEnvVarName)
}
func VersionForOperatorFromEnv() string {
return os.Getenv(operatorImageVersionEnvVarName)
}
func VersionForOperand(namespace, imagePullSpec string, configMapGetter corev1client.ConfigMapsGetter, eventRecorder events.Recorder) string {
versionMap := map[string]string{}
versionMapping, err := configMapGetter.ConfigMaps(namespace).Get("version-mapping", metav1.GetOptions{})
if err != nil && !apierrors.IsNotFound(err) {
eventRecorder.Warningf("VersionMappingFailure", "unable to get version mapping: %v", err)
return ""
}
if versionMapping != nil {
for version, image := range versionMapping.Data {
versionMap[image] = version
}
}
// we have the actual daemonset and we need the pull spec
operandVersion := versionMap[imagePullSpec]
return operandVersion
}