tests_fuzz/utils/crd/
pod.rs

1// Copyright 2023 Greptime Team
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use derive_builder::Builder;
16use kube::CustomResource;
17use schemars::JsonSchema;
18use serde::{Deserialize, Serialize};
19
20use crate::utils::crd::common::{Mode, Selector};
21
22#[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize, JsonSchema)]
23#[serde(rename_all = "kebab-case")]
24pub enum Action {
25    PodFailure,
26    PodKill,
27    ContainerKill,
28}
29
30#[derive(
31    CustomResource, Debug, PartialEq, Eq, Clone, Serialize, Deserialize, Builder, JsonSchema,
32)]
33#[kube(
34    group = "chaos-mesh.org",
35    version = "v1alpha1",
36    namespaced,
37    kind = "PodChaos",
38    plural = "podchaos",
39    singular = "podchaos",
40    derive = "PartialEq"
41)]
42#[serde(rename_all = "camelCase")]
43pub struct PodChaosSpec {
44    // Specifies the fault type to inject. The supported types include pod-failure, pod-kill, and container-kill.
45    action: Action,
46    // Specifies the mode of the experiment.
47    // The mode options include one (selecting a random Pod),
48    // all (selecting all eligible Pods),
49    // fixed (selecting a specified number of eligible Pods),
50    // fixed-percent (selecting a specified percentage of Pods from the eligible Pods),
51    // and random-max-percent (selecting the maximum percentage of Pods from the eligible Pods).
52    mode: Mode,
53    // Provides parameters for the mode configuration, depending on mode.
54    // For example, when mode is set to `fixed-percent`, value specifies the percentage of Pods.
55    #[serde(skip_serializing_if = "Option::is_none")]
56    #[builder(setter(into), default = "None")]
57    value: Option<String>,
58    // Specifies the target Pod.
59    selector: Selector,
60    // When you configure action to `container-kill`,
61    // this configuration is mandatory to specify the target container name for injecting faults.
62    #[serde(skip_serializing_if = "Vec::is_empty", default)]
63    #[builder(setter(into), default = "Vec::new()")]
64    container_names: Vec<String>,
65    // When you configure action to `pod-kill`,
66    // this configuration is mandatory to specify the duration before deleting Pod.
67    #[serde(skip_serializing_if = "Option::is_none")]
68    #[builder(setter(into), default = "None")]
69    grace_period: Option<i64>,
70    // Specifies the duration of the experiment.
71    #[builder(setter(into), default)]
72    duration: String,
73}
74
75#[cfg(test)]
76mod tests {
77    use std::collections::BTreeMap;
78
79    use super::*;
80    use crate::utils::crd::common::SelectorBuilder;
81
82    #[test]
83    fn test_crd_serialization() {
84        let mut fs = BTreeMap::new();
85        fs.insert("app.kubernetes.io/component".into(), "tikv".into());
86        let selector = SelectorBuilder::default()
87            .field_selectors(fs)
88            .build()
89            .unwrap();
90
91        let spec = PodChaosSpecBuilder::default()
92            .duration("10s")
93            .selector(selector)
94            .action(Action::PodKill)
95            .mode(Mode::One)
96            .build()
97            .unwrap();
98        let crd = PodChaos::new("my-pod-chaos", spec);
99        let serialized = serde_yaml::to_string(&crd).unwrap();
100        let expected = r#"apiVersion: chaos-mesh.org/v1alpha1
101kind: PodChaos
102metadata:
103  name: my-pod-chaos
104spec:
105  action: pod-kill
106  mode: one
107  selector:
108    fieldSelectors:
109      app.kubernetes.io/component: tikv
110  duration: 10s
111"#;
112        assert_eq!(expected, serialized);
113    }
114}