22// Copyright Open Network Fabric Authors
33
44use futures:: { StreamExt , TryStreamExt } ;
5+ use kube:: api:: { Patch , PatchParams } ;
56use kube:: runtime:: { WatchStreamExt , watcher} ;
67use kube:: { Api , Client } ;
8+ use serde_json:: json;
79
810use tracectl:: trace_target;
911use tracing:: { error, info} ;
1012
11- use crate :: gateway_agent_crd:: GatewayAgent ;
13+ use crate :: gateway_agent_crd:: { GatewayAgent , GatewayAgentStatus } ;
1214
1315trace_target ! ( "k8s-client" , LevelFilter :: INFO , & [ "management" ] ) ;
1416
@@ -20,6 +22,12 @@ pub enum WatchError {
2022 WatcherError ( #[ from] kube:: runtime:: watcher:: Error ) ,
2123}
2224
25+ #[ derive( Debug , thiserror:: Error ) ]
26+ pub enum PatchError {
27+ #[ error( "Client error: {0}" ) ]
28+ ClientError ( #[ from] kube:: Error ) ,
29+ }
30+
2331/// Watch `GatewayAgent` CRD and call callback for all changes
2432///
2533/// # Errors
@@ -63,3 +71,29 @@ pub async fn watch_gateway_agent_crd(
6371 }
6472 }
6573}
74+
75+ /// Patch `GatewayAgent` object with current status
76+ ///
77+ /// # Errors
78+ /// Returns an error if the patch request fails.
79+ pub async fn patch_gateway_status (
80+ gateway_object_name : & str ,
81+ status : & GatewayAgentStatus ,
82+ ) -> Result < ( ) , PatchError > {
83+ let client = Client :: try_default ( ) . await ?;
84+ let api: Api < GatewayAgent > = Api :: namespaced ( client. clone ( ) , "fab" ) ;
85+
86+ let patch_json = json ! ( {
87+ "status" : status,
88+ } ) ;
89+
90+ api. patch_status (
91+ gateway_object_name,
92+ & PatchParams :: default ( ) ,
93+ & Patch :: Merge ( & patch_json) ,
94+ )
95+ . await ?;
96+
97+ Ok ( ( ) )
98+ }
99+ // Process status update
0 commit comments