/
type.go
251 lines (216 loc) · 6.84 KB
/
type.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
// Copyright 2021 Tetrate
//
// 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 manifest
import (
"fmt"
"strconv"
"strings"
"time"
"github.com/Masterminds/semver"
)
type Manifest struct {
IstioDistributions []*IstioDistribution `json:"istio_distributions"`
// the end of life of Istio minor versions
// key: "x.y", "1.7" for example
// value: "YYYY-MM-DD"
IstioMinorVersionsEOLDates map[string]string `json:"istio_minor_versions_eol_dates"`
}
type IstioDistribution struct {
// Distributions are tagged with `x.y.z-${flavor}-v${flavor_version}` where
// - ${flavor} is either "tetrate" or "tetratefips"
// - ${flavor_version} is ""numeric"" and the version of that distribution
Version string `json:"version,omitempty"`
Flavor string `json:"flavor,omitempty"`
FlavorVersion int64 `json:"flavor_version,omitempty"`
// Supported k8s versions of this distribution.
K8SVersions []string `json:"k8s_versions,omitempty"`
// Indicates if this is a security update.
IsSecurityPatch bool `json:"is_security_patch,omitempty"`
// Release notes for this distribution.
ReleaseNotes []string `json:"release_notes,omitempty"`
// EndOfLife of this distribution (format: "YYYY-MM-DD")
EndOfLife string `json:"end_of_life,omitempty"`
}
const (
IstioDistributionFlavorTetrate = "tetrate"
IstioDistributionFlavorTetrateFIPS = "tetratefips"
IstioDistributionFlavorIstio = "istio"
)
func (x *Manifest) GetEOLDates() (map[string]time.Time, error) {
ret := make(map[string]time.Time, len(x.IstioMinorVersionsEOLDates))
for k, v := range x.IstioMinorVersionsEOLDates {
t, err := parseManifestEOLDate(v)
if err != nil {
return nil, err
}
ret[k] = t
}
return ret, nil
}
func (x *Manifest) SetEOLInIstioDistributions() error {
for _, dist := range x.IstioDistributions {
iVer, err := semver.NewVersion(dist.Version)
if err != nil {
return err
}
for v, date := range x.IstioMinorVersionsEOLDates {
dVer, err := semver.NewVersion(v)
if err != nil {
return err
}
if (dVer.Major() == iVer.Major()) && (dVer.Minor() == iVer.Minor()) {
dist.EndOfLife = date
}
}
}
return nil
}
func parseManifestEOLDate(in string) (time.Time, error) {
const layout = "2006-01-02"
return time.Parse(layout, in)
}
func (x *IstioDistribution) String() string {
return fmt.Sprintf("%s-%s-v%d", x.Version, x.Flavor, x.FlavorVersion)
}
func (x *IstioDistribution) Equal(j *IstioDistribution) bool {
return x.Version == j.Version &&
x.Flavor == j.Flavor &&
x.FlavorVersion == j.FlavorVersion
}
func (x *IstioDistribution) ExistInManifest(ms *Manifest) (bool, error) {
for _, d := range ms.IstioDistributions {
if d.Equal(x) {
return true, nil
}
}
return false, nil
}
func (x *IstioDistribution) Patch() (int, error) {
ts := strings.Split(x.Version, ".")
if len(ts) != 3 {
return 0, fmt.Errorf("invalid vesion: cannot parse %s in the form of 'x.y.z'", x.Version)
}
ret, err := strconv.Atoi(ts[2])
if err != nil {
return 0, fmt.Errorf("invalid vesion: cannot parse %s: failed to convert %s to int", x.Version, ts[2])
}
return ret, nil
}
func (x *IstioDistribution) Group() (string, error) {
ts := strings.Split(x.Version, ".")
if len(ts) != 3 {
return "", fmt.Errorf("invalid vesion: cannot parse %s in the form of 'x.y.z'", x.Version)
}
return fmt.Sprintf("%s.%s-%s", ts[0], ts[1], x.Flavor), nil
}
func (x *IstioDistribution) IsUpstream() bool {
// manifest.json denotes upstream by flavor 'istio'. Whereas the actual upstream images
// in the cluster is of the form 'x.y.z' with no flavor set
return x.Flavor == ""
}
// compare two (patch version, flavor version) tuples in the same group
func (x *IstioDistribution) GreaterThan(y *IstioDistribution) (bool, error) {
xg, err := x.Group()
if err != nil {
return false, err
}
yg, err := y.Group()
if err != nil {
return false, err
}
if xg != yg {
return false, fmt.Errorf("cannot compare two versions (%s, %s) in the differnet group: %s and %s",
x.String(), y.String(), xg, yg)
}
xp, err := x.Patch()
if err != nil {
return false, err
}
yp, err := y.Patch()
if err != nil {
return false, err
}
if xp == yp {
return x.FlavorVersion > y.FlavorVersion, nil
}
return xp > yp, nil
}
func IstioDistributionFromString(in string) (*IstioDistribution, error) {
if !strings.Contains(in, "-") {
// handle the upstream version schema: 'x.y.z'
if err := verifyUpstreamVersionString(in); err != nil {
return nil, err
}
return &IstioDistribution{Version: in}, nil
}
parts := strings.SplitN(in, "-", 2)
if len(parts) != 2 {
return nil, fmt.Errorf("invalid version schema: %s", in)
}
if err := verifyUpstreamVersionString(parts[0]); err != nil {
return nil, err
}
flavor, flavorVersion, err := parseFlavor(parts[1])
return &IstioDistribution{Version: parts[0], Flavor: flavor, FlavorVersion: flavorVersion}, err
}
func parseFlavor(in string) (string, int64, error) {
parts := strings.SplitN(in, "-", 2)
if len(parts) != 2 {
return "", 0, fmt.Errorf("invalid version schema: %s", in)
}
flavor := parts[0]
flavorVersion, err := strconv.ParseInt(strings.TrimPrefix(parts[1], "v"), 10, 64)
if err != nil {
return "", 0, fmt.Errorf("invalid version schema: %s: %w", in, err)
}
return flavor, flavorVersion, nil
}
func verifyUpstreamVersionString(in string) error {
ts := strings.Split(in, ".")
if len(ts) != 3 {
return fmt.Errorf("invalid vesion: cannot parse %s in the form of 'x.y.z'", in)
}
for _, t := range ts {
if _, err := strconv.Atoi(t); err != nil {
return fmt.Errorf("invalid vesion: cannot parse %s in the form of 'x.y.z'", in)
}
}
return nil
}
// get the istio distribution with latest patch version and latest flavor version
func GetLatestDistribution(current *IstioDistribution, ms *Manifest) (foundLatest *IstioDistribution, includeSecurityPatch bool, err error) {
tg, err := current.Group()
if err != nil {
return nil, false, err
}
for _, d := range ms.IstioDistributions {
dg, err := d.Group()
if err != nil {
return nil, false, err
}
if tg == dg {
// if there are any version between current and latest version has security patch
// includeSecurityPatch should return true
if ok, _ := d.GreaterThan(current); ok && d.IsSecurityPatch {
includeSecurityPatch = true
}
if foundLatest == nil {
foundLatest = d
} else if ok, _ := d.GreaterThan(foundLatest); ok {
foundLatest = d
}
}
}
return
}