|
| 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 | + |
| 54 | +// GetMetaName returns the object meta name that should be used for the object in the presentation layer, if defined. |
| 55 | +func GetMetaName(obj client.Object) string { |
| 56 | + if val, ok := getAnnotation(obj, ObjectMetaNameAnnotation); ok { |
| 57 | + return val |
| 58 | + } |
| 59 | + return "" |
| 60 | +} |
| 61 | + |
| 62 | +// IsGroupingObject returns true in case the object is responsible to trigger the grouping action |
| 63 | +// when adding the object's children. e.g. A control-plane object, could be responsible of grouping |
| 64 | +// the control-plane machines while added as a children objects. |
| 65 | +func IsGroupingObject(obj client.Object) bool { |
| 66 | + if val, ok := getBoolAnnotation(obj, GroupingObjectAnnotation); ok { |
| 67 | + return val |
| 68 | + } |
| 69 | + return false |
| 70 | +} |
| 71 | + |
| 72 | +// IsGroupObject return true if the object is the result of a grouping operation, and |
| 73 | +// thus the object is representing group of sibling object, e.g. a group of machines. |
| 74 | +func IsGroupObject(obj client.Object) bool { |
| 75 | + if val, ok := getBoolAnnotation(obj, GroupObjectAnnotation); ok { |
| 76 | + return val |
| 77 | + } |
| 78 | + return false |
| 79 | +} |
| 80 | + |
| 81 | +// GetGroupItems return the list of names for the objects included in a group object. |
| 82 | +func GetGroupItems(obj client.Object) string { |
| 83 | + if val, ok := getAnnotation(obj, GroupItemsAnnotation); ok { |
| 84 | + return val |
| 85 | + } |
| 86 | + return "" |
| 87 | +} |
| 88 | + |
| 89 | +// IsVirtualObject return true if the object does not correspond to any real object, but instead it is |
| 90 | +// a virtual object introduced to provide a better representation of the cluster status. |
| 91 | +func IsVirtualObject(obj client.Object) bool { |
| 92 | + if val, ok := getBoolAnnotation(obj, VirtualObjectAnnotation); ok { |
| 93 | + return val |
| 94 | + } |
| 95 | + return false |
| 96 | +} |
| 97 | + |
| 98 | +// IsShowConditionsObject returns true if the presentation layer should show all the conditions for the object. |
| 99 | +func IsShowConditionsObject(obj client.Object) bool { |
| 100 | + if val, ok := getBoolAnnotation(obj, ShowObjectConditionsAnnotation); ok { |
| 101 | + return val |
| 102 | + } |
| 103 | + return false |
| 104 | +} |
| 105 | + |
| 106 | +func getAnnotation(obj client.Object, annotation string) (string, bool) { |
| 107 | + if obj == nil { |
| 108 | + return "", false |
| 109 | + } |
| 110 | + val, ok := obj.GetAnnotations()[annotation] |
| 111 | + return val, ok |
| 112 | +} |
| 113 | + |
| 114 | +func getBoolAnnotation(obj client.Object, annotation string) (bool, bool) { |
| 115 | + val, ok := getAnnotation(obj, annotation) |
| 116 | + if ok { |
| 117 | + if boolVal, err := strconv.ParseBool(val); err == nil { |
| 118 | + return boolVal, true |
| 119 | + } |
| 120 | + } |
| 121 | + return false, false |
| 122 | +} |
| 123 | + |
| 124 | +func addAnnotation(obj client.Object, annotation, value string) { |
| 125 | + annotations := obj.GetAnnotations() |
| 126 | + if annotations == nil { |
| 127 | + annotations = map[string]string{} |
| 128 | + } |
| 129 | + annotations[annotation] = value |
| 130 | + obj.SetAnnotations(annotations) |
| 131 | +} |
0 commit comments