/
utils.go
110 lines (94 loc) · 3.59 KB
/
utils.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
// Copyright 2020-2023 Politecnico di Torino
//
// 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 forge
import (
"fmt"
"strings"
petname "github.com/dustinkirkland/golang-petname"
"golang.org/x/text/cases"
"golang.org/x/text/language"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
clv1alpha2 "github.com/netgroup-polito/CrownLabs/operators/api/v1alpha2"
)
const (
// StringSeparator -> the separator used to concatenate string.
StringSeparator = "-"
)
func init() {
petname.NonDeterministicMode()
}
// ObjectMeta returns the namespace/name pair given an instance object.
func ObjectMeta(instance *clv1alpha2.Instance) metav1.ObjectMeta {
return metav1.ObjectMeta{
Name: canonicalName(instance.GetName()),
Namespace: instance.GetNamespace(),
}
}
// ObjectMetaWithSuffix returns the namespace/name pair given an instance object and a name suffix.
func ObjectMetaWithSuffix(instance *clv1alpha2.Instance, suffix string) metav1.ObjectMeta {
return metav1.ObjectMeta{
Name: canonicalName(instance.GetName()) + StringSeparator + suffix,
Namespace: instance.GetNamespace(),
}
}
// NamespacedName returns the namespace/name pair given an instance object.
func NamespacedName(instance *clv1alpha2.Instance) types.NamespacedName {
return types.NamespacedName{
Name: canonicalName(instance.GetName()),
Namespace: instance.GetNamespace(),
}
}
// NamespacedNameWithSuffix returns the namespace/name pair given an instance object and a name suffix.
func NamespacedNameWithSuffix(instance *clv1alpha2.Instance, suffix string) types.NamespacedName {
return types.NamespacedName{
Name: canonicalName(instance.GetName()) + StringSeparator + suffix,
Namespace: instance.GetNamespace(),
}
}
// NamespacedNameToObjectMeta returns the ObjectMeta corresponding to a NamespacedName.
func NamespacedNameToObjectMeta(namespacedName types.NamespacedName) metav1.ObjectMeta {
return metav1.ObjectMeta{
Name: namespacedName.Name,
Namespace: namespacedName.Namespace,
}
}
// canonicalName returns a canonical name given a resource name, to
// prevent issues with DNS style requirements.
func canonicalName(name string) string {
return strings.ReplaceAll(name, ".", StringSeparator)
}
// CanonicalSandboxName returns a name given a tenant name.
func CanonicalSandboxName(name string) string {
return fmt.Sprintf("sandbox-%s", canonicalName(name))
}
// RandomInstancePrettyName generates a random name of 2 capitalized words.
func RandomInstancePrettyName() string {
return cases.Title(language.AmericanEnglish).String(petname.Generate(2, " "))
}
// CapResourceQuantity compares a resource.Quantity value with a given cap and returns the lower.
func CapResourceQuantity(quantity, capQuantity resource.Quantity) resource.Quantity {
if quantity.Cmp(capQuantity) < 0 {
return quantity
}
return capQuantity
}
// CapIntegerQuantity compares an unsigned integer value with a given cap and returns the lower.
func CapIntegerQuantity(quantity, capQuantity uint32) uint32 {
if quantity < capQuantity {
return quantity
}
return capQuantity
}