Code Monkey home page Code Monkey logo

Comments (3)

clux avatar clux commented on May 17, 2024

Tried to write something that sets .status.conditions yesterday. I find the setup really awkward to deal with for most of the reasons outline in this comment from the already linked thread.

Regardless, here's some sample code that goes down both the .status.conditions and .status.phases path:

#[derive(Deserialize, Serialize, Clone, Debug)]
#[serde(rename_all = "camelCase")]
pub struct ManifestStatus {
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub conditions: Vec<ApplyCondition>,
    phase: ApplyPhase,
    start_time: String,
}

/// ApplyCondition
///
/// Meant to act like normal kubernetes conditions like PodCondition:
///
///  - lastProbeTime: null
///    lastTransitionTime: "2019-07-31T13:07:30Z"
///    message: 'containers with unready status: [product-config]'
///    reason: ContainersNotReady
///    status: "False"
///    type: ContainersReady
///
/// We do not post lastProbeTime / lastHeartbeatTime because they are expensive.
#[derive(Deserialize, Serialize, Clone, Debug)]
pub struct ApplyCondition {
    /// Whether or not in a good state
    ///
    /// This must default to true when in a good state
    pub status: bool,
    /// Error reason type if not in a good state
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub reason: Option<String>,
    /// One sentence error message if not in a good state
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub message: Option<String>,

    /// The type of condition we export
    ///
    /// This is an extensible enum. Conditions we don't know about gets thrown away.
    #[serde(rename = "type")]
    pub type_: ConditionType,

    /// When the condition was last written in a RFC 3339 format
    ///
    /// Format == `1996-12-19T16:39:57-08:00`, but we hardcode Utc herein.
    #[serde(rename = "lastTransitionTime")]
    pub last_transition: String
}

/// Allowed condition types we always export all of.
///
/// This does not describe a state machine. We set conditions as they happen.
#[derive(Deserialize, Serialize, Clone, Debug, PartialEq, Eq, Default)]
pub enum ConditionType {
    /// Applied to kubernetes
    ///
    /// If this is false, it might contain information about:
    /// - invalid yaml, or not passing admission controllers logic
    /// - network errors
    Applied,

    /// Rollout of current shipcatmanifest succeeded
    ///
    /// If this is false, it's likely the deployment(s) failing to come online.
    /// Best effort information given in message, but this won't replace DeploymentConditions
    RolledOut,

    /// Cach all event that we don't emit (forwards compat on api side)
    #[serde(other, skip_serializing)]
    Unknown
}

/// A high-level summary of where the service is in its lifecycle.
///
/// It is not intended to be a comprehensive rollup of state, nor intended to be
/// a comprehensive state machine.
#[derive(Deserialize, Serialize, Clone, Debug, PartialEq, Eq)]
pub enum ApplyPhase {
    /// The service started the apply process and has passed manifest validation
    Pending,
    /// The service configuration has been sent to kubernetes without errors.
    Running,
    /// The applied configuration has rolled out in all resources.
    Succeeded,
    /// The rollout did not complete successfully
    Failed,

    /// Cach all event that we don't emit (forwards compat on api side)
    #[serde(other, skip_serializing)]
    Unknown
}


impl Default for ConditionType {
    fn default() -> Self {
        ConditionType::Unknown
    }
}

which obviously needs to come with a bunch of helper functions to create, get, update, the conditions:

impl ApplyCondition {
    fn resolved_secret_ok() -> Self {
        ApplyCondition {
            status: true,
            type_: ConditionType::SecretsResolved,
            last_transition: Utc::now().to_rfc3339(),
            ..Default::default(),
        }
    }
}

This was my attempt to codify conditions and phases we normally pass through, and follow best practices at the same time.

Funnily, I can use enums in rust to enumerate the known states, even though kube devs are like:

Enums aren't extensible. Every addition is a breaking, non-backward-compatible API change.

Nuh, uh. That's a language assumption.

At any rate, despite all the fuzz in that thread, it doesn't look like conditions are going to be removed. Will write some more code and see if I can port something general in to this library, but this might all need to be at the controller level.

from kube.

clux avatar clux commented on May 17, 2024

Well. Conditions suck.

You cannot really patch_status to update particular condition entry AFAIKT:

  • merge patch obviously won't support this (merges a vector)
  • json patch doesn't support this (cannot replace array entry by field name) - see rfc6902
  • strategic merge patch support is not even planned to have support for custom resources - kubernetes#58414

I'm gonna have to prod more directly.

from kube.

clux avatar clux commented on May 17, 2024

Have asked around and tested out this a bit now and am just going to say that unless you can wait for at least kubernetes 1.15 for server-side apply (and even that might not do it work custom resources that can't have strategic merge patch support), don't bother with a standard .conditions vector format for custom resources.

Named field fixes most of the problems, makes your code simpler, and makes consuming them simpler. Optionality give them same API benefits, and are way easier to merge.

From the linked PR, I have been making a struct with named fields for Conditions, and updating the status with merge patch which lets you null things out if you are careful with serde annotations.

Marking this as a WONTFIX.

from kube.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.