github.com/redhat-appstudio/release-service@v0.0.0-20240507045911-a8558ef3422a/metadata/metadata.go (about) 1 /* 2 Copyright 2022. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package metadata 18 19 import ( 20 "k8s.io/apimachinery/pkg/apis/meta/v1" 21 "strings" 22 ) 23 24 // AddAnnotations copies the map into the resource's Annotations map. 25 // When the destination map is nil, then the map will be created. 26 // The unexported function addEntries is called with args passed. 27 func AddAnnotations(obj v1.Object, entries map[string]string) { 28 if obj.GetAnnotations() == nil { 29 obj.SetAnnotations(map[string]string{}) 30 } 31 addEntries(entries, obj.GetAnnotations()) 32 } 33 34 // AddLabels copies the map into the resource's Labels map. 35 // When the destination map is nil, then the map will be created. 36 // The unexported function addEntries is called with args passed. 37 func AddLabels(obj v1.Object, entries map[string]string) { 38 if obj.GetLabels() == nil { 39 obj.SetLabels(map[string]string{}) 40 } 41 addEntries(entries, obj.GetLabels()) 42 } 43 44 // GetAnnotationsWithPrefix is a method that returns a map of key/value pairs matching a prefix string. 45 // The unexported function filterByPrefix is called with args passed. 46 func GetAnnotationsWithPrefix(obj v1.Object, prefix string) map[string]string { 47 return filterByPrefix(obj.GetAnnotations(), prefix) 48 } 49 50 // GetLabelsWithPrefix is a method that returns a map of key/value pairs matching a prefix string. 51 // The unexported function filterByPrefix is called with args passed. 52 func GetLabelsWithPrefix(obj v1.Object, prefix string) map[string]string { 53 return filterByPrefix(obj.GetLabels(), prefix) 54 } 55 56 // addEntries copies key/value pairs in the source map adding them into the destination map. 57 // The unexported function safeCopy is used to copy, and avoids clobbering existing keys in the destination map. 58 func addEntries(source, destination map[string]string) { 59 for key, val := range source { 60 safeCopy(destination, key, val) 61 } 62 } 63 64 // filterByPrefix returns a map of key/value pairs contained in src that matches the prefix. 65 // When the prefix is empty/nil, the source map is returned. 66 // When source key does not contain the prefix string, no copy happens. 67 func filterByPrefix(entries map[string]string, prefix string) map[string]string { 68 if len(prefix) == 0 { 69 return entries 70 } 71 dst := map[string]string{} 72 for key, val := range entries { 73 if strings.HasPrefix(key, prefix) { 74 dst[key] = val 75 } 76 } 77 return dst 78 } 79 80 // safeCopy conditionally copies a given key/value pair into a map. 81 // When a key is already present in the map, no copy happens. 82 func safeCopy(dst map[string]string, key, val string) { 83 if _, err := dst[key]; !err { 84 dst[key] = val 85 } 86 }