github.com/banzaicloud/operator-tools@v0.28.10/pkg/utils/types.go (about) 1 // Copyright © 2020 Banzai Cloud 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package utils 16 17 import ( 18 "fmt" 19 "hash/fnv" 20 "sort" 21 22 "emperror.dev/errors" 23 "github.com/iancoleman/orderedmap" 24 v1 "k8s.io/apimachinery/pkg/apis/meta/v1" 25 "k8s.io/apimachinery/pkg/types" 26 ) 27 28 // MergeLabels merge into map[string]string map 29 func MergeLabels(labelGroups ...map[string]string) map[string]string { 30 mergedLabels := make(map[string]string) 31 for _, labels := range labelGroups { 32 for k, v := range labels { 33 mergedLabels[k] = v 34 } 35 } 36 return mergedLabels 37 } 38 39 func PointerToBool(b *bool) bool { 40 if b == nil { 41 return false 42 } 43 44 return *b 45 } 46 47 func PointerToUint(i *uint) uint { 48 if i == nil { 49 return 0 50 } 51 52 return *i 53 } 54 55 func PointerToInt(i *int) int { 56 if i == nil { 57 return 0 58 } 59 60 return *i 61 } 62 63 func PointerToInt32(i *int32) int32 { 64 if i == nil { 65 return 0 66 } 67 68 return *i 69 } 70 71 func PointerToString(s *string) string { 72 if s == nil { 73 return "" 74 } 75 76 return *s 77 } 78 79 // IntPointer converts int32 to *int32 80 func IntPointer(i int32) *int32 { 81 return &i 82 } 83 84 // IntPointer converts int64 to *int64 85 func IntPointer64(i int64) *int64 { 86 return &i 87 } 88 89 // BoolPointer converts bool to *bool 90 func BoolPointer(b bool) *bool { 91 return &b 92 } 93 94 // StringPointer converts string to *string 95 func StringPointer(s string) *string { 96 return &s 97 } 98 99 // OrderedStringMap 100 func OrderedStringMap(original map[string]string) *orderedmap.OrderedMap { 101 o := orderedmap.New() 102 for k, v := range original { 103 o.Set(k, v) 104 } 105 o.SortKeys(sort.Strings) 106 return o 107 } 108 109 // Contains check if a string item exists in []string 110 func Contains(s []string, e string) bool { 111 for _, i := range s { 112 if i == e { 113 return true 114 } 115 } 116 return false 117 } 118 119 // Hash32 calculate for string 120 func Hash32(in string) (string, error) { 121 hasher := fnv.New32() 122 _, err := hasher.Write([]byte(in)) 123 if err != nil { 124 return "", errors.WrapIf(err, "failed to calculate hash for the configmap data") 125 } 126 return fmt.Sprintf("%x", hasher.Sum32()), nil 127 } 128 129 func ObjectKeyFromObjectMeta(o v1.Object) types.NamespacedName { 130 return types.NamespacedName{Namespace: o.GetNamespace(), Name: o.GetName()} 131 }