1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
use crate::Context;

use kube::{runtime::controller::Action, Client};
use std::{sync::Arc, time::Duration};
use tracing::warn;

use crate::exec::ExecCommand;

#[derive(Debug)]
pub struct PsqlOutput {
    pub stdout: Option<String>,
    pub stderr: Option<String>,
    // k8s_openapi::apimachinery::pkg::apis::meta::v1::Status
    pub success: bool,
}

impl PsqlOutput {
    pub fn new(stdout: Option<String>, stderr: Option<String>, success: bool) -> Self {
        Self {
            stdout,
            stderr,
            success,
        }
    }
}

pub struct PsqlCommand {
    pod_name: String,
    namespace: String,
    database: String,
    command: String,
    client: Client,
}

impl PsqlCommand {
    pub fn new(
        pod_name: String,
        namespace: String,
        command: String,
        database: String,
        context: Arc<Context>,
    ) -> Self {
        Self {
            pod_name,
            namespace,
            database,
            command,
            client: context.client.clone(),
        }
    }

    pub async fn execute(&self) -> Result<PsqlOutput, Action> {
        let psql_command = vec![
            String::from("psql"),
            format!(
                "postgres://?dbname={}&application_name=tembo-system",
                self.database.clone()
            ),
            String::from("-c"),
            self.command.clone(),
        ];
        let command = ExecCommand::new(
            self.pod_name.clone(),
            self.namespace.clone(),
            self.client.clone(),
        );
        let output = match command.execute(&psql_command).await {
            Ok(output) => output,
            Err(e) => {
                warn!(
                    "{}: Failed to kubectl exec a psql command: {:?}",
                    self.namespace, e
                );
                return Err(Action::requeue(Duration::from_secs(10)));
            }
        };

        if !output.success
            && output.stderr.clone().is_some()
            && output
                .stderr
                .clone()
                .unwrap()
                .contains("the database system is shutting down")
        {
            warn!(
                "Failed to execute psql command because DB is shutting down. Requeueing. {}",
                self.namespace
            );
            return Err(Action::requeue(Duration::from_secs(10)));
        }

        Ok(PsqlOutput::new(
            output.stdout,
            output.stderr,
            output.success,
        ))
    }
}