Skip to content

Commit f230f64

Browse files
authored
Merge pull request #3 from g-gaston/metadata-cert-manager-fork
🌱 Check for build metadata in cert-manager version
2 parents ce3e578 + bcbbc38 commit f230f64

File tree

4 files changed

+139
-21
lines changed

4 files changed

+139
-21
lines changed

cmd/clusterctl/client/cluster/cert_manager.go

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ import (
2121
_ "embed"
2222
"time"
2323

24+
"github.com/blang/semver"
2425
"github.com/pkg/errors"
2526
corev1 "k8s.io/api/core/v1"
2627
apierrors "k8s.io/apimachinery/pkg/api/errors"
2728
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
28-
"k8s.io/apimachinery/pkg/util/version"
2929
"sigs.k8s.io/cluster-api/cmd/clusterctl/client/repository"
3030
"sigs.k8s.io/controller-runtime/pkg/client"
3131

@@ -34,6 +34,7 @@ import (
3434
"sigs.k8s.io/cluster-api/cmd/clusterctl/internal/util"
3535
logf "sigs.k8s.io/cluster-api/cmd/clusterctl/log"
3636
utilresource "sigs.k8s.io/cluster-api/util/resource"
37+
"sigs.k8s.io/cluster-api/util/version"
3738
utilyaml "sigs.k8s.io/cluster-api/util/yaml"
3839
)
3940

@@ -48,10 +49,8 @@ const (
4849
certManagerVersionAnnotation = "certmanager.clusterctl.cluster.x-k8s.io/version"
4950
)
5051

51-
var (
52-
//go:embed assets/cert-manager-test-resources.yaml
53-
certManagerTestManifest []byte
54-
)
52+
//go:embed assets/cert-manager-test-resources.yaml
53+
var certManagerTestManifest []byte
5554

5655
// CertManagerUpgradePlan defines the upgrade plan if cert-manager needs to be
5756
// upgraded to a different version.
@@ -300,6 +299,12 @@ func (cm *certManagerClient) shouldUpgrade(objs []unstructured.Unstructured) (st
300299
return "", "", false, err
301300
}
302301

302+
desiredVersion := config.Version()
303+
desiredSemVersion, err := semver.ParseTolerant(desiredVersion)
304+
if err != nil {
305+
return "", "", false, errors.Wrapf(err, "failed to parse config version [%s] for cert-manager component", desiredVersion)
306+
}
307+
303308
needUpgrade := false
304309
currentVersion := ""
305310
for i := range objs {
@@ -322,16 +327,12 @@ func (cm *certManagerClient) shouldUpgrade(objs []unstructured.Unstructured) (st
322327
}
323328
}
324329

325-
objSemVersion, err := version.ParseSemantic(objVersion)
330+
objSemVersion, err := semver.ParseTolerant(objVersion)
326331
if err != nil {
327332
return "", "", false, errors.Wrapf(err, "failed to parse version for cert-manager component %s/%s", obj.GetKind(), obj.GetName())
328333
}
329334

330-
c, err := objSemVersion.Compare(config.Version())
331-
if err != nil {
332-
return "", "", false, errors.Wrapf(err, "failed to compare target version for cert-manager component %s/%s", obj.GetKind(), obj.GetName())
333-
}
334-
335+
c := version.Compare(objSemVersion, desiredSemVersion, version.WithBuildTags())
335336
switch {
336337
case c < 0:
337338
// if version < current, then upgrade
@@ -346,7 +347,7 @@ func (cm *certManagerClient) shouldUpgrade(objs []unstructured.Unstructured) (st
346347
break
347348
}
348349
}
349-
return currentVersion, config.Version(), needUpgrade, nil
350+
return currentVersion, desiredVersion, needUpgrade, nil
350351
}
351352

352353
func (cm *certManagerClient) getWaitTimeout() time.Duration {

cmd/clusterctl/client/cluster/cert_manager_test.go

Lines changed: 107 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@ func Test_shouldUpgrade(t *testing.T) {
207207
}
208208
tests := []struct {
209209
name string
210+
configVersion string
210211
args args
211212
wantFromVersion string
212213
wantToVersion string
@@ -247,6 +248,111 @@ func Test_shouldUpgrade(t *testing.T) {
247248
want: false,
248249
wantErr: false,
249250
},
251+
{
252+
name: "Version is equal but current version has no build metadata, should upgrade",
253+
configVersion: "v1.5.3+h4fd4",
254+
args: args{
255+
objs: []unstructured.Unstructured{
256+
{
257+
Object: map[string]interface{}{
258+
"metadata": map[string]interface{}{
259+
"annotations": map[string]interface{}{
260+
clusterctlv1.CertManagerVersionAnnotation: "v1.5.3",
261+
},
262+
},
263+
},
264+
},
265+
},
266+
},
267+
wantFromVersion: "v1.5.3",
268+
wantToVersion: "v1.5.3+h4fd4",
269+
want: true,
270+
wantErr: false,
271+
},
272+
{
273+
name: "Version is equal but different build metadata with hash, should upgrade",
274+
configVersion: "v1.5.3+h4fd4",
275+
args: args{
276+
objs: []unstructured.Unstructured{
277+
{
278+
Object: map[string]interface{}{
279+
"metadata": map[string]interface{}{
280+
"annotations": map[string]interface{}{
281+
clusterctlv1.CertManagerVersionAnnotation: "v1.5.3+h4fd5",
282+
},
283+
},
284+
},
285+
},
286+
},
287+
},
288+
wantFromVersion: "v1.5.3+h4fd5",
289+
wantToVersion: "v1.5.3+h4fd4",
290+
want: true,
291+
wantErr: false,
292+
},
293+
{
294+
name: "Version is equal and same build metadata with hash, should not upgrade",
295+
configVersion: "v1.5.3+h4fd5",
296+
args: args{
297+
objs: []unstructured.Unstructured{
298+
{
299+
Object: map[string]interface{}{
300+
"metadata": map[string]interface{}{
301+
"annotations": map[string]interface{}{
302+
clusterctlv1.CertManagerVersionAnnotation: "v1.5.3+h4fd5",
303+
},
304+
},
305+
},
306+
},
307+
},
308+
},
309+
wantFromVersion: "v1.5.3+h4fd5",
310+
wantToVersion: "v1.5.3+h4fd5",
311+
want: false,
312+
wantErr: false,
313+
},
314+
{
315+
name: "Version is equal but older numbered build metadata, should not upgrade",
316+
configVersion: "v1.5.3+build.1",
317+
args: args{
318+
objs: []unstructured.Unstructured{
319+
{
320+
Object: map[string]interface{}{
321+
"metadata": map[string]interface{}{
322+
"annotations": map[string]interface{}{
323+
clusterctlv1.CertManagerVersionAnnotation: "v1.5.3+build.2",
324+
},
325+
},
326+
},
327+
},
328+
},
329+
},
330+
wantFromVersion: "v1.5.3+build.2",
331+
wantToVersion: "v1.5.3+build.1",
332+
want: false,
333+
wantErr: false,
334+
},
335+
{
336+
name: "Version is equal but newer numbered build metadata, should upgrade",
337+
configVersion: "v1.5.3+build.3",
338+
args: args{
339+
objs: []unstructured.Unstructured{
340+
{
341+
Object: map[string]interface{}{
342+
"metadata": map[string]interface{}{
343+
"annotations": map[string]interface{}{
344+
clusterctlv1.CertManagerVersionAnnotation: "v1.5.3+build.2",
345+
},
346+
},
347+
},
348+
},
349+
},
350+
},
351+
wantFromVersion: "v1.5.3+build.2",
352+
wantToVersion: "v1.5.3+build.3",
353+
want: true,
354+
wantErr: false,
355+
},
250356
{
251357
name: "Version is older, should upgrade",
252358
args: args{
@@ -313,7 +419,7 @@ func Test_shouldUpgrade(t *testing.T) {
313419
t.Run(tt.name, func(t *testing.T) {
314420
g := NewWithT(t)
315421
proxy := test.NewFakeProxy()
316-
fakeConfigClient := newFakeConfig()
422+
fakeConfigClient := newFakeConfig().WithCertManager("", tt.configVersion, "")
317423
pollImmediateWaiter := func(interval, timeout time.Duration, condition wait.ConditionFunc) error {
318424
return nil
319425
}

util/version/version.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -162,14 +162,13 @@ func (v buildIdentifier) compare(o buildIdentifier) int {
162162
return 1
163163
}
164164
} else { // both are strings
165-
switch {
166-
case v.IdentifierStr < o.IdentifierStr:
167-
return -1
168-
case v.IdentifierStr == o.IdentifierStr:
165+
// In order to support random build indentifiers, like commit hashes,
166+
// always return -1 when the strings are different to always signal
167+
// there is version change
168+
if v.IdentifierStr == o.IdentifierStr {
169169
return 0
170-
default:
171-
return 1
172170
}
171+
return -1
173172
}
174173
}
175174

util/version/version_test.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ func TestCompareWithBuildIdentifiers(t *testing.T) {
156156
expected: -1,
157157
},
158158
{
159-
name: "compare with pre release versions and build identifiers",
159+
name: "compare with pre release versions and build identifiers",
160160
a: func() semver.Version {
161161
v, _ := semver.ParseTolerant("v1.20.1-alpha.1+xyz.1")
162162
return v
@@ -240,7 +240,7 @@ func TestCompareWithBuildIdentifiers(t *testing.T) {
240240
expected: 1,
241241
},
242242
{
243-
name: "compare with build identifiers - smaller non numeric",
243+
name: "compare with build identifiers - different non numeric",
244244
a: func() semver.Version {
245245
v, _ := semver.ParseTolerant("v1.20.1+xyz.a")
246246
return v
@@ -251,6 +251,18 @@ func TestCompareWithBuildIdentifiers(t *testing.T) {
251251
}(),
252252
expected: -1,
253253
},
254+
{
255+
name: "compare with build identifiers - equal non numeric",
256+
a: func() semver.Version {
257+
v, _ := semver.ParseTolerant("v1.20.1+xyz.a")
258+
return v
259+
}(),
260+
b: func() semver.Version {
261+
v, _ := semver.ParseTolerant("v1.20.1+xyz.a")
262+
return v
263+
}(),
264+
expected: 0,
265+
},
254266
{
255267
name: "compare with build identifiers - smaller - a is numeric b is not",
256268
a: func() semver.Version {

0 commit comments

Comments
 (0)