Skip to content

Commit 2ce7c89

Browse files
committed
Separate operator and controller names (#492)
## Description Fixes #482 Co-authored-by: Teo Klestrup Röijezon <[email protected]>
1 parent db45745 commit 2ce7c89

File tree

5 files changed

+44
-15
lines changed

5 files changed

+44
-15
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,13 @@ All notable changes to this project will be documented in this file.
77
### Changed
88

99
- BREAKING: `get_recommended_labels` and `with_recommended_labels` now takes a struct of named arguments ([#501]).
10+
- BREAKING: `get_recommended_labels` (and co) now takes the operator and controller names separately ([#492]).
11+
- BREAKING: `ClusterResources` now takes the operator and controller names separately ([#492]).
12+
- When upgrading, please use FQDN-style names for the operators (`{operator}.stackable.tech`).
1013
- Bump opentelemetry crates ([#502]).
1114
- Bump clap to 4.0 ([#503]).
1215

16+
[#492]: https://github.com/stackabletech/operator-rs/pull/492
1317
[#501]: https://github.com/stackabletech/operator-rs/pull/501
1418
[#502]: https://github.com/stackabletech/operator-rs/pull/502
1519
[#503]: https://github.com/stackabletech/operator-rs/pull/503

src/builder/meta.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,8 @@ mod tests {
324324
owner: &pod,
325325
app_name: "test_app",
326326
app_version: "1.0",
327-
managed_by: "app-operator",
327+
operator_name: "app.stackable.tech",
328+
controller_name: "appcluster",
328329
role: "role",
329330
role_group: "rolegroup",
330331
})

src/cluster_resources.rs

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use crate::{
1717
},
1818
kube::{Resource, ResourceExt},
1919
labels::{APP_INSTANCE_LABEL, APP_MANAGED_BY_LABEL, APP_NAME_LABEL},
20+
utils::format_full_controller_name,
2021
};
2122
use k8s_openapi::{
2223
api::{core::v1::Secret, core::v1::ServiceAccount, rbac::v1::RoleBinding},
@@ -72,7 +73,8 @@ impl ClusterResource for Secret {}
7273
/// use std::sync::Arc;
7374
///
7475
/// const APP_NAME: &str = "app";
75-
/// const FIELD_MANAGER_SCOPE: &str = "appcluster";
76+
/// const OPERATOR_NAME: &str = "app.stackable.tech";
77+
/// const CONTROLLER_NAME: &str = "appcluster";
7678
///
7779
/// #[derive(Clone, CustomResource, Debug, Deserialize, JsonSchema, Serialize)]
7880
/// #[kube(
@@ -101,7 +103,8 @@ impl ClusterResource for Secret {}
101103
///
102104
/// let mut cluster_resources = ClusterResources::new(
103105
/// APP_NAME,
104-
/// FIELD_MANAGER_SCOPE,
106+
/// OPERATOR_NAME,
107+
/// CONTROLLER_NAME,
105108
/// &app.object_ref(&()),
106109
/// )
107110
/// .map_err(|source| Error::CreateClusterResources { source })?;
@@ -166,19 +169,24 @@ impl ClusterResources {
166169
///
167170
/// * `app_name` - The lower-case application name used in the resource labels, e.g.
168171
/// "zookeeper"
169-
/// * `manager` - The manager of these cluster resources, e.g.
170-
/// "zookeeper-operator_zk-controller". The added resources must contain the content of this
171-
/// field in the `app.kubernetes.io/managed-by` label. It must be different for each
172-
/// controller in the operator, otherwise resources created by another controller are detected
173-
/// as orphaned in this cluster and are deleted. This value is also used for the field manager
174-
/// scope when applying resources.
172+
/// * `operator_name` - The FQDN-style name of the operator, such as ""zookeeper.stackable.tech""
173+
/// * `controller_name` - The name of the lower-case name of the primary resource that
174+
/// the controller manages, such as "zookeepercluster"
175175
/// * `cluster` - A reference to the cluster containing the name and namespace of the cluster
176176
///
177+
/// The combination of (`operator_name`, `controller_name`) must be unique for each controller in the cluster,
178+
/// otherwise resources created by another controller are detected as orphaned and deleted.
179+
///
177180
/// # Errors
178181
///
179182
/// If `cluster` does not contain a namespace and a name then an `Error::MissingObjectKey` is
180183
/// returned.
181-
pub fn new(app_name: &str, manager: &str, cluster: &ObjectReference) -> OperatorResult<Self> {
184+
pub fn new(
185+
app_name: &str,
186+
operator_name: &str,
187+
controller_name: &str,
188+
cluster: &ObjectReference,
189+
) -> OperatorResult<Self> {
182190
let namespace = cluster
183191
.namespace
184192
.to_owned()
@@ -192,7 +200,7 @@ impl ClusterResources {
192200
namespace,
193201
app_instance,
194202
app_name: app_name.into(),
195-
manager: manager.into(),
203+
manager: format_full_controller_name(operator_name, controller_name),
196204
resource_ids: Default::default(),
197205
})
198206
}

src/labels.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::utils::format_full_controller_name;
12
use const_format::concatcp;
23
use kube::api::{Resource, ResourceExt};
34
use std::collections::BTreeMap;
@@ -35,8 +36,10 @@ pub struct ObjectLabels<'a, T> {
3536
/// This version should include the Stackable version, such as `0.1.0-stackable0.1.0`. In the case of custom product images,
3637
/// the tag should be appended, such as `<productversion>-<tag>`. However, this is pure documentation and should not be parsed.
3738
pub app_version: &'a str,
38-
/// The name of the operator and controller managing the object
39-
pub managed_by: &'a str,
39+
/// The DNS-style name of the operator managing the object (such as `zookeeper.stackable.tech`)
40+
pub operator_name: &'a str,
41+
/// The name of the controller inside of the operator managing the object (such as `zookeepercluster`)
42+
pub controller_name: &'a str,
4043
/// The role that this object belongs to
4144
pub role: &'a str,
4245
/// The role group that this object belongs to
@@ -49,7 +52,8 @@ pub fn get_recommended_labels<T>(
4952
owner,
5053
app_name,
5154
app_version,
52-
managed_by,
55+
operator_name,
56+
controller_name,
5357
role,
5458
role_group,
5559
}: ObjectLabels<T>,
@@ -62,7 +66,10 @@ where
6266
// TODO: Add operator version label
6367
// TODO: part-of is empty for now, decide on how this can be used in a proper fashion
6468
labels.insert(APP_VERSION_LABEL.to_string(), app_version.to_string());
65-
labels.insert(APP_MANAGED_BY_LABEL.to_string(), managed_by.to_string());
69+
labels.insert(
70+
APP_MANAGED_BY_LABEL.to_string(),
71+
format_full_controller_name(operator_name, controller_name),
72+
);
6673

6774
labels
6875
}

src/utils.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,12 @@ pub fn print_startup_string(
4545
pkg_version, git_information, target, rustc_version, built_time
4646
)
4747
}
48+
49+
/// Returns the fully qualified controller name, which should be used when a single controller needs to be referred to uniquely.
50+
///
51+
/// `operator` should be a FQDN-style operator name (for example: `zookeeper.stackable.tech`).
52+
/// `controller` should typically be the lower-case version of the primary resource that the
53+
/// controller manages (for example: `zookeeperclusters`).
54+
pub(crate) fn format_full_controller_name(operator: &str, controller: &str) -> String {
55+
format!("{operator}/{controller}")
56+
}

0 commit comments

Comments
 (0)