-
Notifications
You must be signed in to change notification settings - Fork 28
/
gardener.go
128 lines (112 loc) · 4.22 KB
/
gardener.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
// SPDX-FileCopyrightText: 2024 SAP SE or an SAP affiliate company and Gardener contributors
//
// SPDX-License-Identifier: Apache-2.0
package util
import (
"context"
"fmt"
"time"
"github.com/Masterminds/semver/v3"
gardencorev1beta1 "github.com/gardener/gardener/pkg/apis/core/v1beta1"
"github.com/pkg/errors"
"sigs.k8s.io/controller-runtime/pkg/client"
"github.com/gardener/test-infra/pkg/common"
)
// ConvertStringToVersion converts a string to gardener experable versions
func ConvertStringToVersion(v string) gardencorev1beta1.ExpirableVersion {
return gardencorev1beta1.ExpirableVersion{
Version: v,
ExpirationDate: nil,
}
}
// ConvertStringArrayToVersions converts a string array of versions to gardener experable versions
func ConvertStringArrayToVersions(versions []string) []gardencorev1beta1.ExpirableVersion {
expVersions := make([]gardencorev1beta1.ExpirableVersion, len(versions))
for i, v := range versions {
expVersions[i] = ConvertStringToVersion(v)
}
return expVersions
}
// ContainsCloudprovider checks whether a cloudprovier is part of an array of cloudproviders
func ContainsCloudprovider(cloudproviders []common.CloudProvider, cloudprovider common.CloudProvider) bool {
for _, cp := range cloudproviders {
if cp == cloudprovider {
return true
}
}
return false
}
// GetCloudProfile returns the cloudprofile
func GetCloudProfile(k8sClient client.Client, profileName string) (gardencorev1beta1.CloudProfile, error) {
var cloudprofile gardencorev1beta1.CloudProfile
if err := k8sClient.Get(context.TODO(), client.ObjectKey{Name: profileName}, &cloudprofile); err != nil {
return cloudprofile, err
}
return cloudprofile, nil
}
// GetLatestVersion returns the latest image from a list of expirable versions
func GetLatestVersion(rawVersions []gardencorev1beta1.ExpirableVersion) (gardencorev1beta1.ExpirableVersion, error) {
if len(rawVersions) == 0 {
return gardencorev1beta1.ExpirableVersion{}, errors.New("no kubernetes versions found")
}
var (
latestExpVersion gardencorev1beta1.ExpirableVersion
latestVersion *semver.Version
)
for _, rawVersion := range rawVersions {
v, err := semver.NewVersion(rawVersion.Version)
if err != nil {
return gardencorev1beta1.ExpirableVersion{}, err
}
if latestVersion == nil || v.GreaterThan(latestVersion) {
latestVersion = v
latestExpVersion = rawVersion
}
}
return latestExpVersion, nil
}
// FilterPatchVersions keeps only versions with newest patch versions. E.g. 1.15.1, 1.14.4, 1.14.3, will result in 1.15.1, 1.14.4
func FilterPatchVersions(cloudProfileVersions []gardencorev1beta1.ExpirableVersion) ([]gardencorev1beta1.ExpirableVersion, error) {
type versionWrapper struct {
expirableVersion gardencorev1beta1.ExpirableVersion
semverVersion *semver.Version
}
newestPatchVersionMap := make(map[string]versionWrapper)
for _, rawVersion := range cloudProfileVersions {
parsedVersion, err := semver.NewVersion(rawVersion.Version)
if err != nil {
return nil, err
}
majorMinor := fmt.Sprintf("%d.%d", parsedVersion.Major(), parsedVersion.Minor())
if newestPatch, ok := newestPatchVersionMap[majorMinor]; !ok || newestPatch.semverVersion.LessThan(parsedVersion) {
newestPatchVersionMap[majorMinor] = versionWrapper{
expirableVersion: rawVersion,
semverVersion: parsedVersion,
}
}
}
newestPatchVersions := make([]gardencorev1beta1.ExpirableVersion, 0)
for _, version := range newestPatchVersionMap {
newestPatchVersions = append(newestPatchVersions, version.expirableVersion)
}
return newestPatchVersions, nil
}
// FilterExpiredVersions removes all expired versions from the list.
func FilterExpiredVersions(versions []gardencorev1beta1.ExpirableVersion) []gardencorev1beta1.ExpirableVersion {
filtered := make([]gardencorev1beta1.ExpirableVersion, 0)
for _, v := range versions {
if v.ExpirationDate == nil || v.ExpirationDate.Time.After(time.Now()) {
filtered = append(filtered, v)
}
}
return filtered
}
// DecMinorVersion decreases the minor version of 1 and sets the patch version to 0.
func DecMinorVersion(v *semver.Version) (*semver.Version, error) {
minor := uint64(0)
if v.Minor() > 0 {
minor = v.Minor() - 1
}
vPrev := fmt.Sprintf("%d.%d.%d", v.Major(), minor, 0)
return semver.NewVersion(vPrev)
}