1
- //! This module provides various types and functions to construct valid
2
- //! Kubernetes labels. Labels are key/value pairs, where the key must meet
3
- //! certain requirementens regarding length and character set. The value can
4
- //! contain a limited set of ASCII characters.
1
+ //! This module provides various types and functions to construct valid Kubernetes labels. Labels
2
+ //! are key/value pairs, where the key must meet certain requirements regarding length and character
3
+ //! set. The value can contain a limited set of ASCII characters.
5
4
//!
6
- //! Additionally, the [`Label`] struct provides various helper functions to
7
- //! construct commonly used labels across the Stackable Data Platform, like
8
- //! the role_group or component.
5
+ //! Additionally, the [`Label`] struct provides various helper functions to construct commonly used
6
+ //! labels across the Stackable Data Platform, like the `role_group` or `component`.
9
7
//!
10
- //! See <https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/>
11
- //! for more information on Kubernetes labels.
8
+ //! See <https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/> for more
9
+ //! information on Kubernetes labels.
12
10
13
11
use crate :: kvp:: { KeyValuePair , KeyValuePairError , KeyValuePairs } ;
14
12
@@ -18,30 +16,26 @@ mod value;
18
16
pub use selector:: * ;
19
17
pub use value:: * ;
20
18
21
- /// A type alias for errors returned when construction or manipulation of a set
22
- /// of labels fails.
19
+ /// A type alias for errors returned when construction or manipulation of a set of labels fails.
23
20
pub type LabelError = KeyValuePairError < LabelValueError > ;
24
21
25
- /// A specialized implementation of a key/value pair representing Kubernetes
26
- /// labels.
22
+ /// A specialized implementation of a key/value pair representing Kubernetes labels.
27
23
///
28
24
/// ```
29
25
/// # use stackable_operator::kvp::Label;
30
26
/// let label = Label::try_from(("stackable.tech/vendor", "Stackable")).unwrap();
31
27
/// assert_eq!(label.to_string(), "stackable.tech/vendor=Stackable");
32
28
/// ```
33
29
///
34
- /// The validation of the label value can fail due to multiple reasons. It can
35
- /// only contain a limited set and combination of ASCII characters. See
36
- /// <https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/>
37
- /// for more information on Kubernetes labels.
30
+ /// The validation of the label value can fail due to multiple reasons. It can only contain a
31
+ /// limited set and combination of ASCII characters. See [the documentation][1] for more information
32
+ /// on Kubernetes labels.
33
+ ///
34
+ /// [1]: https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/
38
35
pub type Label = KeyValuePair < LabelValue > ;
39
36
40
37
/// A validated set/list of Kubernetes labels.
41
38
///
42
- /// It provides selected associated functions to manipulate the set of labels,
43
- /// like inserting or extending.
44
- ///
45
39
/// ## Examples
46
40
///
47
41
/// ### Converting a BTreeMap into a list of labels
@@ -72,88 +66,89 @@ pub type Labels = KeyValuePairs<LabelValue>;
72
66
73
67
/// Well-known labels used by other tools or standard conventions.
74
68
pub mod well_known {
75
- use crate :: {
76
- kvp:: consts:: {
77
- K8S_APP_COMPONENT_KEY , K8S_APP_MANAGED_BY_KEY , K8S_APP_ROLE_GROUP_KEY ,
78
- K8S_APP_VERSION_KEY , STACKABLE_VENDOR_KEY , STACKABLE_VENDOR_VALUE ,
79
- } ,
80
- utils:: format_full_controller_name,
81
- } ;
82
-
83
- use super :: { Label , LabelError } ;
84
-
85
- /// Creates the `app.kubernetes.io/component` label with `role` as the
86
- /// value. This function will return an error if `role` violates the required
87
- /// Kubernetes restrictions.
88
- pub fn component ( component : & str ) -> Result < Label , LabelError > {
89
- Label :: try_from ( ( K8S_APP_COMPONENT_KEY , component) )
90
- }
69
+ use crate :: utils:: format_full_controller_name;
91
70
92
- /// Creates the `app.kubernetes.io/role-group` label with `role_group` as
93
- /// the value. This function will return an error if `role_group` violates
94
- /// the required Kubernetes restrictions.
95
- pub fn role_group ( role_group : & str ) -> Result < Label , LabelError > {
96
- Label :: try_from ( ( K8S_APP_ROLE_GROUP_KEY , role_group) )
71
+ use super :: { Label , LabelError , Labels } ;
72
+
73
+ /// Creates the `app.kubernetes.io/component` label.
74
+ ///
75
+ /// It is used to specify the component within the architecture, e.g. `database`.
76
+ ///
77
+ /// This function will return an error if `role` violates the required Kubernetes restrictions.
78
+ pub fn component ( component : & str ) -> Result < Label , LabelError > {
79
+ Label :: try_from ( ( "app.kubernetes.io/component" , component) )
97
80
}
98
81
99
- /// Creates the `app.kubernetes.io/managed-by` label with the formated
100
- /// full controller name based on `operator_name` and `controller_name` as
101
- /// the value. This function will return an error if the formatted controller
102
- /// name violates the required Kubernetes restrictions.
82
+ /// Creates the `app.kubernetes.io/managed-by` label with the formatted full controller name as
83
+ /// a value.
84
+ ///
85
+ /// The controller name is based on `operator_name` and `controller_name`. It is used to
86
+ /// indicate what tool is being used to manage the operation of an application, e.g. `helm`.
87
+ ///
88
+ /// This function will return an error if the formatted controller name violates the required
89
+ /// Kubernetes restrictions.
103
90
pub fn managed_by ( operator_name : & str , controller_name : & str ) -> Result < Label , LabelError > {
104
91
Label :: try_from ( (
105
- K8S_APP_MANAGED_BY_KEY ,
92
+ "app.kubernetes.io/managed-by" ,
106
93
format_full_controller_name ( operator_name, controller_name) . as_str ( ) ,
107
94
) )
108
95
}
109
96
110
- /// Creates the `app.kubernetes.io/version` label with `version` as the
111
- /// value. This function will return an error if `role_group` violates the
112
- /// required Kubernetes restrictions.
97
+ /// Creates the `app.kubernetes.io/version` label.
98
+ ///
99
+ /// It is used to indicate the current version of the application. The value can represent a
100
+ /// semantic version or a revision, e.g. `5.7.21`.
101
+ ///
102
+ /// This function will return an error if `role_group` violates the required Kubernetes
103
+ /// restrictions.
113
104
pub fn version ( version : & str ) -> Result < Label , LabelError > {
114
- Label :: try_from ( ( K8S_APP_VERSION_KEY , version) )
105
+ Label :: try_from ( ( "app.kubernetes.io/version" , version) )
115
106
}
116
107
117
- /// Creates the `stackable.tech/vendor: Stackable` label, tagging the object as
118
- /// created by a Stackable operator.
108
+ /// Creates the `stackable.tech/vendor: Stackable` label, tagging the object as created by a
109
+ /// Stackable operator.
119
110
pub fn vendor_stackable ( ) -> Label {
120
- Label :: try_from ( ( STACKABLE_VENDOR_KEY , STACKABLE_VENDOR_VALUE ) )
111
+ Label :: try_from ( ( "stackable.tech/vendor" , "Stackable" ) )
121
112
. expect ( "failed to parse hard-coded Stackable vendor label" )
122
113
}
123
114
115
+ /// Creates the `stackable.tech/role-group` label.
116
+ ///
117
+ /// This function will return an error if `role_group` violates the required Kubernetes
118
+ /// restrictions.
119
+ pub fn role_group ( role_group : & str ) -> Result < Label , LabelError > {
120
+ Label :: try_from ( ( "stackable.tech/role-group" , role_group) )
121
+ }
122
+
124
123
/// Common sets of labels that apply for different use-cases.
125
124
pub mod sets {
126
125
use kube:: { Resource , ResourceExt } ;
127
126
128
- use crate :: kvp:: {
129
- consts:: { K8S_APP_INSTANCE_KEY , K8S_APP_NAME_KEY } ,
130
- ObjectLabels ,
131
- } ;
127
+ use crate :: kvp:: ObjectLabels ;
132
128
133
- use super :: super :: { Label , LabelError , Labels } ;
129
+ use super :: { Label , LabelError , Labels } ;
134
130
135
- /// Returns the recommended set of labels. The set includes these well-known
136
- /// Kubernetes labels:
131
+ /// Returns the recommended set of labels.
132
+ ///
133
+ /// The set includes these well-known Kubernetes labels:
137
134
///
138
- /// - `app.kubernetes.io/role-group`
139
135
/// - `app.kubernetes.io/managed-by`
140
136
/// - `app.kubernetes.io/component`
141
137
/// - `app.kubernetes.io/instance`
142
138
/// - `app.kubernetes.io/version`
143
139
/// - `app.kubernetes.io/name`
144
140
///
145
- /// Additionally, it includes Stackable-specific labels. These are :
141
+ /// Additionally, it includes these Stackable-specific labels:
146
142
///
143
+ /// - `stackable.tech/role-group`
147
144
/// - `stackable.tech/vendor`
148
145
///
149
- /// This function returns a result, because the parameter `object_labels`
150
- /// can contain invalid data or can exceed the maximum allowed number of
151
- /// characters.
146
+ /// This function returns a [`Result`], because the parameter `object_labels` can contain
147
+ /// invalid data or can exceed the maximum allowed number of characters.
152
148
pub fn recommended < R > ( object_labels : ObjectLabels < R > ) -> Result < Labels , LabelError >
153
149
where
154
150
R : Resource ,
155
151
{
156
- // Well-known Kubernetes labels
157
152
let mut labels = role_group_selector (
158
153
object_labels. owner ,
159
154
object_labels. app_name ,
@@ -171,10 +166,11 @@ pub mod well_known {
171
166
Ok ( labels)
172
167
}
173
168
174
- /// Returns the set of labels required to select the resource based on the
175
- /// role group. The set contains role selector labels, see
176
- /// [`role_selector`] for more details. Additionally, it contains
177
- /// the `app.kubernetes.io/role-group` label with `role_group` as the value.
169
+ /// Returns the set of labels required to select the resource based on the role group.
170
+ ///
171
+ /// The set contains role selector labels, see [`role_selector`] for more details.
172
+ /// Additionally, it contains the `stackable.tech/role-group` label with `role_group` as the
173
+ /// value.
178
174
pub fn role_group_selector < R > (
179
175
owner : & R ,
180
176
app_name : & str ,
@@ -189,10 +185,10 @@ pub mod well_known {
189
185
Ok ( labels)
190
186
}
191
187
192
- /// Returns the set of labels required to select the resource based on the
193
- /// role. The set contains the common labels, see [`common`] for
194
- /// more details. Additionally, it contains the `app.kubernetes.io/component`
195
- /// label with `role` as the value.
188
+ /// Returns the set of labels required to select the resource based on the role.
189
+ ///
190
+ /// The set contains the common labels, see [`common`] for more details. Additionally, it
191
+ /// contains the `app.kubernetes.io/component` label with `role` as the value.
196
192
///
197
193
/// This function returns a result, because the parameters `owner`, `app_name`,
198
194
/// and `role` can contain invalid data or can exceed the maximum allowed
@@ -206,20 +202,20 @@ pub mod well_known {
206
202
Ok ( labels)
207
203
}
208
204
209
- /// Returns a common set of labels, which are required to identify resources
210
- /// that belong to a certain owner object, for example a `ZookeeperCluster`.
205
+ /// Returns a common set of labels, which are required to identify resources that belong to
206
+ /// a certain owner object, for example a `ZookeeperCluster`.
207
+ ///
211
208
/// The set contains these well-known labels:
212
209
///
213
210
/// - `app.kubernetes.io/instance` and
214
211
/// - `app.kubernetes.io/name`
215
212
///
216
- /// This function returns a result, because the parameters `app_name` and
217
- /// `app_instance` can contain invalid data or can exceed the maximum
218
- /// allowed number of characters.
213
+ /// This function returns a result, because the parameters `app_name` and `app_instance` can
214
+ /// contain invalid data or can exceed the maximum allowed number of characters.
219
215
pub fn common ( app_name : & str , app_instance : & str ) -> Result < Labels , LabelError > {
220
216
Ok ( Labels :: from_iter ( [
221
- Label :: try_from ( ( K8S_APP_INSTANCE_KEY , app_instance) ) ?,
222
- Label :: try_from ( ( K8S_APP_NAME_KEY , app_name) ) ?,
217
+ Label :: try_from ( ( "app.kubernetes.io/instance" , app_instance) ) ?,
218
+ Label :: try_from ( ( "app.kubernetes.io/name" , app_name) ) ?,
223
219
] ) )
224
220
}
225
221
}
0 commit comments