-
Notifications
You must be signed in to change notification settings - Fork 52
/
version.go
169 lines (145 loc) · 4.24 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package version
import (
"encoding/json"
"fmt"
"os"
"sort"
"strings"
"github.com/blang/semver"
"github.com/go-logr/logr"
)
type UnknownError struct {
version *semver.Version
}
func (e *UnknownError) Error() string {
return fmt.Sprintf("unknown version: %v", e.version)
}
type Operand struct {
UpstreamVersion *semver.Version `json:"upstream-version,omitempty"`
DownstreamVersion *semver.Version `json:"downstream-version,omitempty"`
Image string `json:"image"`
CVE bool `json:"cve"`
Deprecated bool `json:"deprecated"`
}
// Ref returns the string used to represent this Operand in the Infinispan CR spec and status
func (o Operand) Ref() string {
if o.DownstreamVersion != nil {
return o.DownstreamVersion.String()
}
return o.UpstreamVersion.String()
}
func (o Operand) String() string {
return fmt.Sprintf("Upstream=%v, Downstream=%v, Image=%s, CVE=%t, Deprecated=%t", o.UpstreamVersion, o.DownstreamVersion, o.Image, o.CVE, o.Deprecated)
}
func (o Operand) Validate() error {
if o.UpstreamVersion == nil {
return fmt.Errorf("upstream-version field must be specified")
}
if o.Image == "" {
return fmt.Errorf("image field must be specified")
}
return nil
}
func (o Operand) EQ(other Operand) bool {
if o.DownstreamVersion != nil {
return o.DownstreamVersion.EQ(*other.DownstreamVersion)
}
return o.UpstreamVersion.EQ(*other.UpstreamVersion)
}
func (o Operand) GTE(other Operand) bool {
if o.DownstreamVersion != nil {
return o.DownstreamVersion.GTE(*other.DownstreamVersion)
}
return o.UpstreamVersion.GTE(*other.UpstreamVersion)
}
func (o Operand) LT(other Operand) bool {
if o.DownstreamVersion != nil {
return o.DownstreamVersion.LT(*other.DownstreamVersion)
}
return o.UpstreamVersion.LT(*other.UpstreamVersion)
}
func NewUnknownError(v *semver.Version) error {
return &UnknownError{v}
}
type Manager struct {
operandMap map[string]*Operand
Operands []*Operand
}
func (m *Manager) WithRef(version string) (Operand, error) {
operand, exists := m.operandMap[version]
if !exists {
v, err := semver.Parse(version)
if err != nil {
return Operand{}, err
}
return Operand{}, NewUnknownError(&v)
}
return *operand, nil
}
// Latest returns the most recent Operand release
func (m *Manager) Latest() Operand {
return *m.Operands[len(m.Operands)-1]
}
func (m *Manager) Oldest() Operand {
return *m.Operands[0]
}
func (m *Manager) Log(log logr.Logger) {
if bytes, err := json.MarshalIndent(m.Operands, "", " "); err != nil {
log.Error(err, "unable to log VersionManager content")
} else {
log.Info(fmt.Sprintf("Loaded Operand Versions:\n%s", bytes))
}
}
func (m *Manager) Json() (string, error) {
j, err := json.Marshal(m.Operands)
return string(j), err
}
func ManagerFromEnv(envName string) (*Manager, error) {
env := os.Getenv(envName)
if env == "" {
return nil, fmt.Errorf("unable to create version Manager, env '%s' is empty", envName)
}
return ManagerFromJson(env)
}
func ManagerFromJson(jsonStr string) (*Manager, error) {
operands := make([]*Operand, 0)
if err := json.Unmarshal([]byte(jsonStr), &operands); err != nil {
fmt.Println(err)
return nil, err
}
numOperands := len(operands)
manager := &Manager{
operandMap: make(map[string]*Operand, numOperands),
Operands: make([]*Operand, numOperands),
}
for i, o := range operands {
if err := o.Validate(); err != nil {
return nil, err
}
if err := resolveOperandImage(o); err != nil {
return nil, fmt.Errorf("unable to resolve Operand image: %w", err)
}
key := o.Ref()
if _, exists := manager.operandMap[key]; exists {
return nil, fmt.Errorf("multiple operands have the same version reference '%s'", key)
}
manager.operandMap[key] = o
manager.Operands[i] = o
}
sort.Slice(manager.Operands, func(i, j int) bool {
return manager.Operands[i].LT(*manager.Operands[j])
})
return manager, nil
}
func resolveOperandImage(o *Operand) error {
img := o.Image
if strings.HasPrefix(img, "${") && strings.HasSuffix(img, "}") {
envVar := strings.TrimPrefix(strings.TrimSuffix(img, "}"), "${")
envImg := os.Getenv(envVar)
if envImg == "" {
return fmt.Errorf("operand %s image env variable '%s' is empty or has not been set", o.Ref(), envVar)
}
o.Image = envImg
}
return nil
}