-
Notifications
You must be signed in to change notification settings - Fork 3
/
common_handler.go
89 lines (78 loc) · 2.65 KB
/
common_handler.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
package v1alpha3
import (
"fmt"
"math/rand"
"sort"
"strings"
"time"
"github.com/blang/semver/v4"
releasev1alpha1 "github.com/giantswarm/apiextensions/v3/pkg/apis/release/v1alpha1"
"github.com/giantswarm/k8sclient/v5/pkg/k8sclient"
"github.com/giantswarm/microerror"
"github.com/giantswarm/micrologger"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"github.com/giantswarm/aws-admission-controller/v3/pkg/label"
"github.com/giantswarm/aws-admission-controller/v3/pkg/mutator"
)
type Handler struct {
K8sClient k8sclient.Interface
Logger micrologger.Logger
}
func GetReleaseComponentLabels(release releasev1alpha1.Release) map[string]string {
components := map[string]string{}
for _, component := range release.Spec.Components {
components[component.Name] = component.Version
}
return components
}
func GetNavailabilityZones(m *Handler, n int, azs []string) []string {
randomAZs := azs
// In case there are not enough distinct AZs, we repeat them
for len(randomAZs) < n {
randomAZs = append(randomAZs, azs...)
}
// We shuffle the AZs, pick the first n and sort them alphabetically
rand.Seed(time.Now().UnixNano())
rand.Shuffle(len(randomAZs), func(i, j int) { randomAZs[i], randomAZs[j] = randomAZs[j], randomAZs[i] })
randomAZs = randomAZs[:n]
sort.Strings(randomAZs)
m.Logger.Log("level", "debug", "message", fmt.Sprintf("available AZ's: %v, selected AZ's: %v", azs, randomAZs))
return randomAZs
}
func IsCAPIRelease(meta metav1.Object) (bool, error) {
if meta.GetLabels()[label.Release] == "" {
return false, nil
}
releaseVersion, err := ReleaseVersion(meta, []mutator.PatchOperation{})
if err != nil {
return false, microerror.Maskf(parsingFailedError, "unable to parse release version from object")
}
return IsCAPIVersion(releaseVersion)
}
func ReleaseVersion(meta metav1.Object, patch []mutator.PatchOperation) (*semver.Version, error) {
var version string
var ok bool
// check first if the release version is contained in a patch
for _, p := range patch {
if p.Path == fmt.Sprintf("/metadata/labels/%s", EscapeJSONPatchString(label.Release)) {
version = p.Value.(string)
if version != "" {
return semver.New(version)
} else {
break
}
}
}
// otherwise check the labels
version, ok = meta.GetLabels()[label.Release]
if !ok {
return nil, microerror.Maskf(parsingFailedError, "unable to get release version from Object %s", meta.GetName())
}
return semver.New(version)
}
// Ensure the needed escapes are in place. See https://tools.ietf.org/html/rfc6901#section-3 .
func EscapeJSONPatchString(input string) string {
input = strings.ReplaceAll(input, "~", "~0")
input = strings.ReplaceAll(input, "/", "~1")
return input
}