sigs.k8s.io/cluster-api@v1.7.1/cmd/clusterctl/client/tree/annotations.go (about) 1 /* 2 Copyright 2020 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 tree 18 19 import ( 20 "strconv" 21 22 "sigs.k8s.io/controller-runtime/pkg/client" 23 ) 24 25 const ( 26 // ShowObjectConditionsAnnotation documents that the presentation layer should show all the conditions for the object. 27 ShowObjectConditionsAnnotation = "tree.cluster.x-k8s.io.io/show-conditions" 28 29 // ObjectMetaNameAnnotation contains the meta name that should be used for the object in the presentation layer, 30 // e.g. control plane for KCP. 31 ObjectMetaNameAnnotation = "tree.cluster.x-k8s.io.io/meta-name" 32 33 // VirtualObjectAnnotation documents that the object does not correspond to any real object, but instead is 34 // a virtual object introduced to provide a better representation of the cluster status, e.g. workers. 35 VirtualObjectAnnotation = "tree.cluster.x-k8s.io.io/virtual-object" 36 37 // GroupingObjectAnnotation is an annotation that should be applied to a node in order to trigger the grouping action 38 // when adding the node's children. e.g. if you have a control-plane node, and you apply this annotation, then 39 // the control-plane machines added as a children of this node will be grouped in case the ready condition 40 // has the same Status, Severity and Reason. 41 GroupingObjectAnnotation = "tree.cluster.x-k8s.io.io/grouping-object" 42 43 // GroupObjectAnnotation is an annotation that documents that a node is the result of a grouping operation, and 44 // thus the node is representing group of sibling nodes, e.g. a group of machines. 45 GroupObjectAnnotation = "tree.cluster.x-k8s.io.io/group-object" 46 47 // GroupItemsAnnotation contains the list of names for the objects included in a group object. 48 GroupItemsAnnotation = "tree.cluster.x-k8s.io.io/group-items" 49 50 // GroupItemsSeparator is the separator used in the GroupItemsAnnotation. 51 GroupItemsSeparator = ", " 52 53 // ObjectZOrderAnnotation contains an integer that defines the sorting of child objects when the object tree is printed. 54 // Objects are sorted by their z-order from highest to lowest, and then by their name in alphabetical order if the 55 // z-order is the same. Objects with no z-order set are assumed to have a default z-order of 0. 56 ObjectZOrderAnnotation = "tree.cluster.x-k8s.io.io/z-order" 57 ) 58 59 // GetMetaName returns the object meta name that should be used for the object in the presentation layer, if defined. 60 func GetMetaName(obj client.Object) string { 61 if val, ok := getAnnotation(obj, ObjectMetaNameAnnotation); ok { 62 return val 63 } 64 return "" 65 } 66 67 // IsGroupingObject returns true in case the object is responsible to trigger the grouping action 68 // when adding the object's children. e.g. A control-plane object, could be responsible of grouping 69 // the control-plane machines while added as a children objects. 70 func IsGroupingObject(obj client.Object) bool { 71 if val, ok := getBoolAnnotation(obj, GroupingObjectAnnotation); ok { 72 return val 73 } 74 return false 75 } 76 77 // IsGroupObject returns true if the object is the result of a grouping operation, and 78 // thus the object is representing group of sibling object, e.g. a group of machines. 79 func IsGroupObject(obj client.Object) bool { 80 if val, ok := getBoolAnnotation(obj, GroupObjectAnnotation); ok { 81 return val 82 } 83 return false 84 } 85 86 // GetGroupItems returns the list of names for the objects included in a group object. 87 func GetGroupItems(obj client.Object) string { 88 if val, ok := getAnnotation(obj, GroupItemsAnnotation); ok { 89 return val 90 } 91 return "" 92 } 93 94 // GetZOrder return the zOrder of the object. Objects with no zOrder have a default zOrder of 0. 95 func GetZOrder(obj client.Object) int { 96 if val, ok := getAnnotation(obj, ObjectZOrderAnnotation); ok { 97 if zOrder, err := strconv.ParseInt(val, 10, 0); err == nil { 98 return int(zOrder) 99 } 100 } 101 return 0 102 } 103 104 // IsVirtualObject returns true if the object does not correspond to any real object, but instead it is 105 // a virtual object introduced to provide a better representation of the cluster status. 106 func IsVirtualObject(obj client.Object) bool { 107 if val, ok := getBoolAnnotation(obj, VirtualObjectAnnotation); ok { 108 return val 109 } 110 return false 111 } 112 113 // IsShowConditionsObject returns true if the presentation layer should show all the conditions for the object. 114 func IsShowConditionsObject(obj client.Object) bool { 115 if val, ok := getBoolAnnotation(obj, ShowObjectConditionsAnnotation); ok { 116 return val 117 } 118 return false 119 } 120 121 func getAnnotation(obj client.Object, annotation string) (string, bool) { 122 if obj == nil { 123 return "", false 124 } 125 val, ok := obj.GetAnnotations()[annotation] 126 return val, ok 127 } 128 129 func getBoolAnnotation(obj client.Object, annotation string) (bool, bool) { 130 val, ok := getAnnotation(obj, annotation) 131 if ok { 132 if boolVal, err := strconv.ParseBool(val); err == nil { 133 return boolVal, true 134 } 135 } 136 return false, false 137 } 138 139 func addAnnotation(obj client.Object, annotation, value string) { 140 annotations := obj.GetAnnotations() 141 if annotations == nil { 142 annotations = map[string]string{} 143 } 144 annotations[annotation] = value 145 obj.SetAnnotations(annotations) 146 }