Skip to content

Commit bc47996

Browse files
authored
added apply_patch_status method using Merge strategy (#64)
* added apply_patch_status method using Merge strategy * addressed PR review: implemented merge_patch_status and apply_patch_status for the individual strategies based on non public patch_status. * resolved conflicts * removed apiVersion and Kind from merge_patch_status; not required
1 parent 4e02a06 commit bc47996

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed

src/client.rs

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,67 @@ impl Client {
115115
.await?)
116116
}
117117

118+
/// Patches subresource status in a given Resource using apply strategy.
119+
/// The subresource status must be defined beforehand in the Crd.
120+
pub async fn apply_patch_status<T, S>(&self, resource: &T, status: &S) -> OperatorResult<T>
121+
where
122+
T: Clone + DeserializeOwned + Meta + Resource,
123+
S: Serialize,
124+
{
125+
let new_status = Patch::Apply(serde_json::json!({
126+
"apiVersion": T::API_VERSION,
127+
"kind": T::KIND,
128+
"status": status
129+
}));
130+
131+
Ok(self
132+
.patch_status(resource, new_status, &self.patch_params)
133+
.await?)
134+
}
135+
136+
/// Patches subresource status in a given Resource using merge strategy.
137+
/// The subresource status must be defined beforehand in the Crd.
138+
pub async fn merge_patch_status<T, S>(&self, resource: &T, status: &S) -> OperatorResult<T>
139+
where
140+
T: Clone + DeserializeOwned + Meta + Resource,
141+
S: Serialize,
142+
{
143+
let new_status = Patch::Merge(serde_json::json!({ "status": status }));
144+
145+
Ok(self
146+
.patch_status(resource, new_status, &self.patch_params)
147+
.await?)
148+
}
149+
150+
async fn patch_status<T, S>(
151+
&self,
152+
resource: &T,
153+
patch: Patch<S>,
154+
patch_params: &PatchParams,
155+
) -> OperatorResult<T>
156+
where
157+
T: Clone + DeserializeOwned + Meta + Resource,
158+
S: Serialize,
159+
{
160+
// There are four different strategies:
161+
// 1) Apply (https://kubernetes.io/docs/reference/using-api/api-concepts/#server-side-apply)
162+
// Starting from Kubernetes v1.18, you can enable the Server Side Apply feature so that the control plane tracks managed fields for all newly created objects.
163+
// 2) Json (https://tools.ietf.org/html/rfc6902):
164+
// This is supported on crate feature jsonpatch only
165+
// 3) Merge (https://tools.ietf.org/html/rfc7386):
166+
// For example, if you want to update a list you have to specify the complete list and update everything
167+
// 4) Strategic (not for CustomResource)
168+
// With a strategic merge patch, a list is either replaced or merged depending on its patch strategy.
169+
// The patch strategy is specified by the value of the patchStrategy key in a field tag in the Kubernetes source code.
170+
// For example, the Containers field of PodSpec struct has a patchStrategy of merge.
171+
//
172+
173+
let api = self.get_api(Meta::namespace(resource));
174+
Ok(api
175+
.patch_status(&Meta::name(resource), patch_params, &patch)
176+
.await?)
177+
}
178+
118179
/// This will _update_ an existing resource.
119180
/// The operation is called `replace` in the Kubernetes API.
120181
/// While a `patch` can just update a partial object

0 commit comments

Comments
 (0)