forked from cisco-open/operator-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
types.go
123 lines (103 loc) · 2.49 KB
/
types.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
// Copyright © 2020 Banzai Cloud
//
// 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 utils
import (
"fmt"
"hash/fnv"
"sort"
"emperror.dev/errors"
"github.com/iancoleman/orderedmap"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
)
// MergeLabels merge into map[string]string map
func MergeLabels(labelGroups ...map[string]string) map[string]string {
mergedLabels := make(map[string]string)
for _, labels := range labelGroups {
for k, v := range labels {
mergedLabels[k] = v
}
}
return mergedLabels
}
func PointerToBool(b *bool) bool {
if b == nil {
return false
}
return *b
}
func PointerToUint(i *uint) uint {
if i == nil {
return 0
}
return *i
}
func PointerToInt(i *int) int {
if i == nil {
return 0
}
return *i
}
func PointerToString(s *string) string {
if s == nil {
return ""
}
return *s
}
// IntPointer converts int32 to *int32
func IntPointer(i int32) *int32 {
return &i
}
// IntPointer converts int64 to *int64
func IntPointer64(i int64) *int64 {
return &i
}
// BoolPointer converts bool to *bool
func BoolPointer(b bool) *bool {
return &b
}
// StringPointer converts string to *string
func StringPointer(s string) *string {
return &s
}
// OrderedStringMap
func OrderedStringMap(original map[string]string) *orderedmap.OrderedMap {
o := orderedmap.New()
for k, v := range original {
o.Set(k, v)
}
o.SortKeys(sort.Strings)
return o
}
// Contains check if a string item exists in []string
func Contains(s []string, e string) bool {
for _, i := range s {
if i == e {
return true
}
}
return false
}
// Hash32 calculate for string
func Hash32(in string) (string, error) {
hasher := fnv.New32()
_, err := hasher.Write([]byte(in))
if err != nil {
return "", errors.WrapIf(err, "failed to calculate hash for the configmap data")
}
return fmt.Sprintf("%x", hasher.Sum32()), nil
}
func ObjectKeyFromObjectMeta(o v1.Object) types.NamespacedName {
return types.NamespacedName{Namespace: o.GetNamespace(), Name: o.GetName()}
}