diff --git a/KubeOS-Rust/proxy/src/controller/apiclient.rs b/KubeOS-Rust/proxy/src/controller/apiclient.rs index 85e839f0eaa44cb40db6cf097c8113983c871c58..e250bf65924777fc14fe03a39433a66a1224490d 100644 --- a/KubeOS-Rust/proxy/src/controller/apiclient.rs +++ b/KubeOS-Rust/proxy/src/controller/apiclient.rs @@ -39,7 +39,12 @@ impl Default for OSInstanceSpecPatch { OSInstanceSpecPatch { api_version: OSINSTANCE_API_VERSION.to_string(), kind: OSINSTANCE_KIND.to_string(), - spec: OSInstanceSpec { nodestatus: NODE_STATUS_IDLE.to_string(), sysconfigs: None, upgradeconfigs: None ,namespacedname:None}, + spec: OSInstanceSpec { + nodestatus: NODE_STATUS_IDLE.to_string(), + sysconfigs: None, + upgradeconfigs: None, + namespacedname: None, + }, } } } @@ -102,7 +107,12 @@ impl ApplyApi for ControllerClient { labels: Some(labels), ..ObjectMeta::default() }, - spec: OSInstanceSpec { nodestatus: NODE_STATUS_IDLE.to_string(), sysconfigs: None, upgradeconfigs: None ,namespacedname: None}, + spec: OSInstanceSpec { + nodestatus: NODE_STATUS_IDLE.to_string(), + sysconfigs: None, + upgradeconfigs: None, + namespacedname: None, + }, status: None, }; let osi_api = Api::namespaced(self.client.clone(), namespace); diff --git a/KubeOS-Rust/proxy/src/controller/apiserver_mock.rs b/KubeOS-Rust/proxy/src/controller/apiserver_mock.rs index 679096d3de517488ba99dd53fe5a808d626e0368..a59ff913a22c76dfec02739b361d62dbdedff1cb 100644 --- a/KubeOS-Rust/proxy/src/controller/apiserver_mock.rs +++ b/KubeOS-Rust/proxy/src/controller/apiserver_mock.rs @@ -71,6 +71,9 @@ impl ApiServerVerifier { match cases { Testcases::OSInstanceNotExist(osi) => { self.handler_osinstance_get_not_exist(osi.clone()) + .await + .unwrap() + .handler_node_get(osi.clone()) .await .unwrap() .handler_osinstance_creation(osi.clone()) @@ -531,7 +534,7 @@ impl OSInstance { nodestatus: NODE_STATUS_IDLE.to_string(), sysconfigs: Some(Configs { version: Some(String::from("v1")), configs: None }), upgradeconfigs: Some(Configs { version: Some(String::from("v1")), configs: None }), - namespacedname:Some(NamespacedName{namespace:String::from("default"),name:String::from("test")}) + namespacedname: Some(NamespacedName { namespace: String::from("default"), name: String::from("test") }), }, status: Some(OSInstanceStatus { sysconfigs: Some(Configs { version: Some(String::from("v1")), configs: None }), @@ -606,7 +609,7 @@ impl OS { os.meta_mut().namespace = Some("default".into()); os } - + pub fn set_os_osversion_v2_opstype_config() -> Self { let mut os = OS::set_os_default(); os.spec.osversion = String::from("KubeOS v2"); @@ -677,10 +680,10 @@ impl Default for OSSpec { clientkey: Some(String::from("")), sysconfigs: Some(Configs { version: Some(String::from("v1")), configs: None }), upgradeconfigs: Some(Configs { version: Some(String::from("v1")), configs: None }), - nodeselector:None, - timeinterval:None, - timewindow:None, - executionmode:None, + nodeselector: None, + timeinterval: None, + timewindow: None, + executionmode: None, } } } diff --git a/KubeOS-Rust/proxy/src/controller/controller.rs b/KubeOS-Rust/proxy/src/controller/controller.rs index 787a0e1c9db1fe0f79a77e75220f529d78d45b46..8eab905fbdb6caab085b85295e92a0b17aaab69e 100644 --- a/KubeOS-Rust/proxy/src/controller/controller.rs +++ b/KubeOS-Rust/proxy/src/controller/controller.rs @@ -30,8 +30,8 @@ use super::{ crd::{Configs, Content, OSInstance, OS}, utils::{check_version, get_config_version, ConfigOperation, ConfigType}, values::{ - LABEL_UPGRADING, NODE_STATUS_CONFIG, NODE_STATUS_IDLE, OPERATION_TYPE_ROLLBACK, OPERATION_TYPE_UPGRADE, - OSINSTANCE_NAMESPACE, REQUEUE_ERROR, REQUEUE_NORMAL,LABEL_CONFIGURING, NO_REQUEUE, + LABEL_CONFIGURING, LABEL_UPGRADING, NODE_STATUS_CONFIG, NODE_STATUS_IDLE, NO_REQUEUE, OPERATION_TYPE_ROLLBACK, + OPERATION_TYPE_UPGRADE, OSINSTANCE_NAMESPACE, REQUEUE_ERROR, REQUEUE_NORMAL, }, }; @@ -50,15 +50,18 @@ pub async fn reconcile( let controller_res = proxy_controller.get_resources(&node_name).await?; let node = controller_res.node; let mut osinstance = controller_res.osinstance; - if let Some(namespacedname) = osinstance.spec.namespacedname.as_ref(){ - debug!("osinstance correspending os name is {}, namespace is {}",namespacedname.name,namespacedname.namespace); - if !(namespacedname.name == os_cr.name() && namespacedname.namespace == namespace){ - debug!("current os cr name:{}, namespace:{} is not belong to this node",os_cr.name(),namespace); - return Ok(NO_REQUEUE) + if let Some(namespacedname) = osinstance.spec.namespacedname.as_ref() { + debug!( + "osinstance correspending os name is {}, namespace is {}", + namespacedname.name, namespacedname.namespace + ); + if !(namespacedname.name == os_cr.name() && namespacedname.namespace == namespace) { + debug!("current os cr name:{}, namespace:{} is not belong to this node", os_cr.name(), namespace); + return Ok(NO_REQUEUE); } - }else { + } else { debug!("osinstance correspending os name is None, not in upgrading or configuring"); - return Ok(REQUEUE_NORMAL) + return Ok(REQUEUE_NORMAL); } let node_os_image = &node @@ -110,10 +113,15 @@ pub async fn reconcile( .await?; return Ok(REQUEUE_NORMAL); } - proxy_controller.set_config(&mut osinstance, ConfigType::SysConfig).await?; - proxy_controller - .refresh_node(node, osinstance, &get_config_version(os_cr.spec.sysconfigs.as_ref()), ConfigType::SysConfig) - .await?; + proxy_controller.set_config(&mut osinstance, ConfigType::SysConfig).await?; + proxy_controller + .refresh_node( + node, + osinstance, + &get_config_version(os_cr.spec.sysconfigs.as_ref()), + ConfigType::SysConfig, + ) + .await?; } } else { if os_cr.spec.opstype == NODE_STATUS_CONFIG { @@ -186,8 +194,13 @@ impl ProxyController { Ok(()) }, Err(kube::Error::Api(ErrorResponse { reason, .. })) if &reason == "NotFound" => { - info!("Create OSInstance {}", node_name); - self.controller_client.create_osinstance(node_name, OSINSTANCE_NAMESPACE).await?; + let node_api: Api = Api::all(self.k8s_client.clone()); + if node_api.get(node_name).await.is_ok() { + info!("Create OSInstance {}", node_name); + self.controller_client.create_osinstance(node_name, OSINSTANCE_NAMESPACE).await?; + } else { + info!("Skip creating OSInstance because of node {} not found", node_name); + } Ok(()) }, Err(err) => Err(Error::KubeClient { source: err }), @@ -216,7 +229,7 @@ impl ProxyController { debug!("delete label {}", LABEL_UPGRADING); labels.remove(LABEL_UPGRADING); node = node_api.replace(&node.name(), &PostParams::default(), &node).await?; - }else if labels.contains_key(LABEL_CONFIGURING){ + } else if labels.contains_key(LABEL_CONFIGURING) { debug!("delete label {}", LABEL_CONFIGURING); labels.remove(LABEL_CONFIGURING); node = node_api.replace(&node.name(), &PostParams::default(), &node).await?; diff --git a/KubeOS-Rust/proxy/src/controller/crd.rs b/KubeOS-Rust/proxy/src/controller/crd.rs index 36e14d59094dc02828e1a3da8b8a994cf2ac9b25..1b3dabfe0bbc8e36d6db3b69bbce4083369222cd 100644 --- a/KubeOS-Rust/proxy/src/controller/crd.rs +++ b/KubeOS-Rust/proxy/src/controller/crd.rs @@ -32,11 +32,10 @@ pub struct OSSpec { pub clientkey: Option, pub sysconfigs: Option, pub upgradeconfigs: Option, - pub nodeselector:Option, + pub nodeselector: Option, pub timewindow: Option, - pub timeinterval: Option, - pub executionmode:Option, - + pub timeinterval: Option, + pub executionmode: Option, } #[derive(CustomResource, Debug, Clone, Deserialize, Serialize, JsonSchema)] @@ -83,12 +82,12 @@ pub struct Content { } #[derive(Clone, Deserialize, Serialize, Debug, Eq, PartialEq, JsonSchema)] -pub struct NamespacedName{ +pub struct NamespacedName { pub namespace: String, pub name: String, } #[derive(Clone, Deserialize, Serialize, Debug, Eq, PartialEq, JsonSchema)] -pub struct TimeWindow{ - pub starttime:String, - pub endtime:String, -} \ No newline at end of file +pub struct TimeWindow { + pub starttime: String, + pub endtime: String, +}