tests_fuzz/utils/
pod_failure.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 std::collections::BTreeMap;
16
17use kube::Api;
18
19use crate::error::Result;
20use crate::utils::crd::common::{Mode, SelectorBuilder};
21use crate::utils::crd::pod::{Action, PodChaos, PodChaosSpecBuilder};
22
23/// Injects a pod failure into a specific datanode within a Kubernetes cluster.
24///
25/// This function constructs a `PodChaos` custom resource to simulate a pod failure for the specified
26/// datanode, and creates this resource in the Kubernetes cluster using the provided client.
27pub async fn inject_datanode_pod_failure(
28    client: kube::Client,
29    namespace: &str,
30    cluster_name: &str,
31    datanode_id: u64,
32    duration_secs: usize,
33) -> Result<String> {
34    let mut selector = BTreeMap::new();
35    let pod_name = format!("{}-datanode-{}", cluster_name, datanode_id);
36    selector.insert(
37        "statefulset.kubernetes.io/pod-name".into(),
38        pod_name.clone(),
39    );
40    let selector = SelectorBuilder::default()
41        .label_selectors(selector)
42        .build()
43        .unwrap();
44
45    let spec = PodChaosSpecBuilder::default()
46        .duration(format!("{duration_secs}s"))
47        .selector(selector)
48        .action(Action::PodFailure)
49        .mode(Mode::One)
50        .build()
51        .unwrap();
52    let chaos_name = format!("{pod_name}-pod-failure");
53    let cr = PodChaos::new(&chaos_name, spec);
54    let api: Api<PodChaos> = Api::namespaced(client, namespace);
55    api.create(&Default::default(), &cr).await.unwrap();
56
57    Ok(chaos_name)
58}
59
60/// Recovers a pod from a failure by deleting the associated PodChaos resource.
61///
62/// This function deletes the PodChaos custom resource with the specified name, effectively
63/// recovering the pod from the injected failure.
64pub async fn recover_pod_failure(client: kube::Client, namespace: &str, name: &str) -> Result<()> {
65    let api: Api<PodChaos> = Api::namespaced(client, namespace);
66    api.delete(name, &Default::default()).await.unwrap();
67    Ok(())
68}