1 /* 2 Copyright 2023 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 cache 18 19 import ( 20 "k8s.io/apimachinery/pkg/types" 21 ) 22 23 // ObjectName is a reference to an object of some implicit kind 24 type ObjectName struct { 25 Namespace string 26 Name string 27 } 28 29 // NewObjectName constructs a new one 30 func NewObjectName(namespace, name string) ObjectName { 31 return ObjectName{Namespace: namespace, Name: name} 32 } 33 34 // Parts is the inverse of the constructor 35 func (objName ObjectName) Parts() (namespace, name string) { 36 return objName.Namespace, objName.Name 37 } 38 39 // String returns the standard string encoding, 40 // which is designed to match the historical behavior of MetaNamespaceKeyFunc. 41 // Note this behavior is different from the String method of types.NamespacedName. 42 func (objName ObjectName) String() string { 43 if len(objName.Namespace) > 0 { 44 return objName.Namespace + "/" + objName.Name 45 } 46 return objName.Name 47 } 48 49 // ParseObjectName tries to parse the standard encoding 50 func ParseObjectName(str string) (ObjectName, error) { 51 var objName ObjectName 52 var err error 53 objName.Namespace, objName.Name, err = SplitMetaNamespaceKey(str) 54 return objName, err 55 } 56 57 // NamespacedNameAsObjectName rebrands the given NamespacedName as an ObjectName 58 func NamespacedNameAsObjectName(nn types.NamespacedName) ObjectName { 59 return NewObjectName(nn.Namespace, nn.Name) 60 } 61 62 // AsNamespacedName rebrands as a NamespacedName 63 func (objName ObjectName) AsNamespacedName() types.NamespacedName { 64 return types.NamespacedName{Namespace: objName.Namespace, Name: objName.Name} 65 } 66