From d888e04f4ed63541dc76266e74098599e8eceb94 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B6nke=20Liebau?= Date: Fri, 18 Dec 2020 23:00:51 +0100 Subject: [PATCH 1/2] Added setting of pod condition "ready" for state "running" - this is what operators usually listen for, when they wait for pod to be up and running. --- src/provider/states.rs | 48 +++++++++++++++++++++++++++++++++ src/provider/states/running.rs | 37 +++++++++++++++++++++---- src/provider/states/starting.rs | 7 ++++- 3 files changed, 86 insertions(+), 6 deletions(-) diff --git a/src/provider/states.rs b/src/provider/states.rs index b1d8fe3..591d366 100644 --- a/src/provider/states.rs +++ b/src/provider/states.rs @@ -1,3 +1,7 @@ +use k8s_openapi::api::core::v1::ContainerStatus as KubeContainerStatus; +use k8s_openapi::api::core::v1::PodCondition as KubePodCondition; +use kubelet::pod::Phase; + pub(crate) mod creating_config; pub(crate) mod creating_service; pub(crate) mod downloading; @@ -22,3 +26,47 @@ macro_rules! fail_fatal { return Transition::Complete(Err(aerr)); }}; } + +/// Create basic Pod status patch with container status and pod conditions +pub fn make_status_with_containers_and_condition( + phase: Phase, + reason: &str, + container_statuses: Vec, + init_container_statuses: Vec, + pod_conditions: Vec, +) -> serde_json::Value { + serde_json::json!( + { + "metadata": { + "resourceVersion": "", + }, + "status": { + "phase": phase, + "reason": reason, + "containerStatuses": container_statuses, + "initContainerStatuses": init_container_statuses, + "conditions": pod_conditions + } + } + ) +} + +/// Create basic Pod status patch. +pub fn make_status_with_condition( + phase: Phase, + reason: &str, + pod_conditions: Vec, +) -> serde_json::Value { + serde_json::json!( + { + "metadata": { + "resourceVersion": "", + }, + "status": { + "phase": phase, + "reason": reason, + "conditions": pod_conditions + } + } + ) +} diff --git a/src/provider/states/running.rs b/src/provider/states/running.rs index 7ea35ed..b53522f 100644 --- a/src/provider/states/running.rs +++ b/src/provider/states/running.rs @@ -1,5 +1,5 @@ use k8s_openapi::api::core::v1::{ - ContainerState, ContainerStateRunning, ContainerStatus as KubeContainerStatus, + ContainerState, ContainerStateRunning, ContainerStatus as KubeContainerStatus, PodCondition, }; use kubelet::pod::Pod; use kubelet::state::prelude::*; @@ -8,12 +8,25 @@ use log::{debug, error, trace}; use crate::provider::states::failed::Failed; use crate::provider::states::installing::Installing; +use crate::provider::states::make_status_with_containers_and_condition; use crate::provider::states::stopping::Stopping; use crate::provider::PodState; +use k8s_openapi::apimachinery::pkg::apis::meta::v1::Time; +use k8s_openapi::chrono; #[derive(Debug, TransitionTo)] #[transition_to(Stopping, Failed, Running, Installing)] -pub struct Running {} +pub struct Running { + pub transition_time: Time, +} + +impl Default for Running { + fn default() -> Self { + Self { + transition_time: Time(chrono::offset::Utc::now()), + } + } +} #[async_trait::async_trait] impl State for Running { @@ -66,7 +79,7 @@ impl State for Running { async fn json_status( &self, - _pod_state: &mut PodState, + pod_state: &mut PodState, pod: &Pod, ) -> anyhow::Result { let state = ContainerState { @@ -83,11 +96,25 @@ impl State for Running { state: Some(state), ..Default::default() }); - Ok(make_status_with_containers( + let condition = PodCondition { + last_probe_time: None, + last_transition_time: Some(self.transition_time.clone()), + message: Some(String::from("Service is running")), + reason: Some(String::from("Running")), + status: "True".to_string(), + type_: "Ready".to_string(), + }; + let status = make_status_with_containers_and_condition( Phase::Running, "Running", container_status, vec![], - )) + vec![condition], + ); + debug!( + "Patching status for running servce [{}] with: [{}]", + pod_state.service_name, status + ); + Ok(status) } } diff --git a/src/provider/states/starting.rs b/src/provider/states/starting.rs index 6c81541..58db677 100644 --- a/src/provider/states/starting.rs +++ b/src/provider/states/starting.rs @@ -144,7 +144,12 @@ impl State for Starting { // Store the child handle in the podstate so that later states // can use it pod_state.process_handle = Some(child); - return Transition::next(self, Running {}); + return Transition::next( + self, + Running { + ..Default::default() + }, + ); } Err(error) => { let error_message = format!("Failed to start process with error {}", error); From 7f38adce56fd30824e3a95f1c03a82993686c205 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B6nke=20Liebau?= Date: Mon, 21 Dec 2020 10:11:44 +0100 Subject: [PATCH 2/2] Addressed review comments. --- src/provider/states.rs | 23 ----------------------- 1 file changed, 23 deletions(-) diff --git a/src/provider/states.rs b/src/provider/states.rs index 591d366..fe97857 100644 --- a/src/provider/states.rs +++ b/src/provider/states.rs @@ -37,9 +37,6 @@ pub fn make_status_with_containers_and_condition( ) -> serde_json::Value { serde_json::json!( { - "metadata": { - "resourceVersion": "", - }, "status": { "phase": phase, "reason": reason, @@ -50,23 +47,3 @@ pub fn make_status_with_containers_and_condition( } ) } - -/// Create basic Pod status patch. -pub fn make_status_with_condition( - phase: Phase, - reason: &str, - pod_conditions: Vec, -) -> serde_json::Value { - serde_json::json!( - { - "metadata": { - "resourceVersion": "", - }, - "status": { - "phase": phase, - "reason": reason, - "conditions": pod_conditions - } - } - ) -}