-
-
Notifications
You must be signed in to change notification settings - Fork 19
feat: Adds a warning when a defined object override was not applied #1264
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
d073166
9a2adee
c48736c
6033b87
1e3efa0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -445,6 +445,10 @@ pub struct ClusterResources<'a> { | |
|
|
||
| /// Arbitrary Kubernetes object overrides specified by the user via the CRD. | ||
| object_overrides: &'a ObjectOverrides, | ||
|
|
||
| /// The indices of the object_overrides entries that matched at least one of | ||
| /// the added resources. | ||
| matched_object_overrides: HashSet<usize>, | ||
| } | ||
|
|
||
| impl<'a> ClusterResources<'a> { | ||
|
|
@@ -499,6 +503,7 @@ impl<'a> ClusterResources<'a> { | |
| resource_ids: HashSet::default(), | ||
| apply_strategy, | ||
| object_overrides, | ||
| matched_object_overrides: HashSet::default(), | ||
| }) | ||
| } | ||
|
|
||
|
|
@@ -570,10 +575,12 @@ impl<'a> ClusterResources<'a> { | |
|
|
||
| let mut mutated = resource.maybe_mutate(&self.apply_strategy); | ||
|
|
||
| // We apply the object overrides of the user at the very end to offer maximum flexibility. | ||
| self.object_overrides | ||
| let matched_object_overrides = self | ||
| .object_overrides | ||
| .apply_to(&mut mutated) | ||
| .context(ApplyObjectOverridesSnafu)?; | ||
| self.matched_object_overrides | ||
| .extend(matched_object_overrides); | ||
|
|
||
| let patched_resource = self | ||
| .apply_strategy | ||
|
|
@@ -657,6 +664,16 @@ impl<'a> ClusterResources<'a> { | |
| /// | ||
| /// * `client` - The client which is used to access Kubernetes | ||
| pub async fn delete_orphaned_resources(self, client: &Client) -> Result<()> { | ||
| // We warn late about unmatched object overrides, as every override is matched against | ||
| // each object individually (by apiVersion, kind, name and namespace). An override that | ||
| // e.g. targets the discovery ConfigMap will therefore not match any of the rolegroup | ||
| // ConfigMaps, so whether an override matched nothing at all can only be determined once | ||
| // all objects have been added. | ||
| // As this function consumes `self` and finalizes the cluster creation, it is the last | ||
| // point at which we can do so without requiring an extra call in every operator. | ||
| // The downside is that the warnings are lost in case reconciliation fails earlier. | ||
| self.warn_about_unmatched_object_overrides(); | ||
|
|
||
| // We can only delete Listeners in case the "crds" feature is enabled, otherwise it's a NOP. | ||
| #[cfg(feature = "crds")] | ||
| let delete_listeners = self | ||
|
|
@@ -681,6 +698,43 @@ impl<'a> ClusterResources<'a> { | |
| Ok(()) | ||
| } | ||
|
|
||
| /// Warns about every object override that did not match any of the added resources. | ||
| fn warn_about_unmatched_object_overrides(&self) { | ||
| for (index, object_override) in self | ||
| .object_overrides | ||
| .unmatched(&self.matched_object_overrides) | ||
| { | ||
| let (api_version, kind) = object_override | ||
| .types | ||
| .as_ref() | ||
| .map_or(("<not set>", "<not set>"), |types| { | ||
| (types.api_version.as_str(), types.kind.as_str()) | ||
| }); | ||
| let name = object_override | ||
| .metadata | ||
| .name | ||
| .as_deref() | ||
| .unwrap_or("<not set>"); | ||
| let namespace = object_override | ||
| .metadata | ||
| .namespace | ||
| .as_deref() | ||
| .unwrap_or("<not set>"); | ||
|
|
||
| warn!( | ||
| index, | ||
| api_version, | ||
| kind, | ||
| metadata.name = name, | ||
| metadata.namespace = namespace, | ||
| cluster_namespace = self.namespace, | ||
| "objectOverride did not match any object created for this cluster and therefore had \ | ||
| no effect. Please check that apiVersion, kind and metadata.name are correct and that \ | ||
| metadata.namespace matches the cluster namespace." | ||
| ); | ||
|
Comment on lines
+724
to
+734
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We generally prefer providing dynamic values as fields/attributes (by mentioning them before the message text in the
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. that makes sense - thank you! Addressed in c48736c |
||
| } | ||
| } | ||
|
|
||
| /// Deletes all deployed resources of the given kind which are labelled as if they belong to | ||
| /// this cluster instance but are not contained in the given list. | ||
| /// | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the call. I understand it better now. I'd like to have a comment here, why the decision was to put in
delete_orphaned_resourcessomething along the lines:Did it here because last step as it's only about resources which didn't had a match. Did so to not clutter Snafu warnings directly related to malformed CRDs.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
addressed in 9a2adee
@Maleware I thought a bit more about this - I think you are right that we should warn earlier. My initial thinking was that an object override would not make an apply step fail but that is wrong. When I tried to add it earlier I ran into the issue that you actually only get to match against all possible overrides, once all applies have been done. There are some checks we could do before, but not all of them.
To keep this small, I'd leave it at this, but I think this deserves further improvements - but from what I looked at, that would be more invasive than this change.