Skip to main content

tests_fuzz/utils/
network_chaos.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, Selector, SelectorBuilder};
21use crate::utils::crd::network::{
22    Action, Direction, NetworkChaos, NetworkChaosSpecBuilder, TargetBuilder,
23};
24
25fn build_datanode_selector(namespace: &str, cluster_name: &str) -> Selector {
26    let mut selector = BTreeMap::new();
27    selector.insert(
28        "app.greptime.io/component".into(),
29        format!("{cluster_name}-datanode"),
30    );
31
32    SelectorBuilder::default()
33        .namespaces(vec![namespace.to_string()])
34        .label_selectors(selector)
35        .build()
36        .unwrap()
37}
38
39fn build_metasrv_selector(namespace: &str, cluster_name: &str) -> Selector {
40    let mut selector = BTreeMap::new();
41    selector.insert(
42        "app.greptime.io/component".into(),
43        format!("{cluster_name}-meta"),
44    );
45
46    SelectorBuilder::default()
47        .namespaces(vec![namespace.to_string()])
48        .label_selectors(selector)
49        .build()
50        .unwrap()
51}
52
53/// Injects a network partition between a datanode pod and metasrv.
54pub async fn inject_datanode_metasrv_network_partition(
55    client: kube::Client,
56    namespace: &str,
57    cluster_name: &str,
58    duration_secs: usize,
59) -> Result<String> {
60    let selector = build_datanode_selector(namespace, cluster_name);
61    let target = TargetBuilder::default()
62        .mode(Mode::All)
63        .selector(build_metasrv_selector(namespace, cluster_name))
64        .build()
65        .unwrap();
66
67    let spec = NetworkChaosSpecBuilder::default()
68        .action(Action::Partition)
69        .direction(Direction::Both)
70        .mode(Mode::All)
71        .selector(selector)
72        .target(target)
73        .duration(format!("{duration_secs}s"))
74        .build()
75        .unwrap();
76
77    let chaos_name = "datanode-metasrv-network-partition".to_string();
78    let cr = NetworkChaos::new(&chaos_name, spec);
79    let api: Api<NetworkChaos> = Api::namespaced(client, namespace);
80    api.create(&Default::default(), &cr).await.unwrap();
81
82    Ok(chaos_name)
83}
84
85/// Recovers network chaos by deleting the associated NetworkChaos resource.
86pub async fn recover_network_chaos(
87    client: kube::Client,
88    namespace: &str,
89    name: &str,
90) -> Result<()> {
91    let api: Api<NetworkChaos> = Api::namespaced(client, namespace);
92    api.delete(name, &Default::default()).await.unwrap();
93    Ok(())
94}