Matthias Andreas Benkard | 832a54e | 2019-01-29 09:27:38 +0100 | [diff] [blame] | 1 | /* |
| 2 | Copyright 2014 The Kubernetes Authors. |
| 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 generic |
| 18 | |
| 19 | import ( |
| 20 | metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 21 | "k8s.io/apimachinery/pkg/fields" |
| 22 | ) |
| 23 | |
| 24 | // ObjectMetaFieldsSet returns a fields that represent the ObjectMeta. |
| 25 | func ObjectMetaFieldsSet(objectMeta *metav1.ObjectMeta, hasNamespaceField bool) fields.Set { |
| 26 | if !hasNamespaceField { |
| 27 | return fields.Set{ |
| 28 | "metadata.name": objectMeta.Name, |
| 29 | } |
| 30 | } |
| 31 | return fields.Set{ |
| 32 | "metadata.name": objectMeta.Name, |
| 33 | "metadata.namespace": objectMeta.Namespace, |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | // AdObjectMetaField add fields that represent the ObjectMeta to source. |
| 38 | func AddObjectMetaFieldsSet(source fields.Set, objectMeta *metav1.ObjectMeta, hasNamespaceField bool) fields.Set { |
| 39 | source["metadata.name"] = objectMeta.Name |
| 40 | if hasNamespaceField { |
| 41 | source["metadata.namespace"] = objectMeta.Namespace |
| 42 | } |
| 43 | return source |
| 44 | } |
| 45 | |
| 46 | // MergeFieldsSets merges a fields'set from fragment into the source. |
| 47 | func MergeFieldsSets(source fields.Set, fragment fields.Set) fields.Set { |
| 48 | for k, value := range fragment { |
| 49 | source[k] = value |
| 50 | } |
| 51 | return source |
| 52 | } |