-
Notifications
You must be signed in to change notification settings - Fork 42
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
exec trunk install #217
Merged
exec trunk install #217
Changes from 11 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
1288e11
add trunk to pg image
709d0a4
compile trunk against openssl3
3d42a17
Merge branch 'main' into cor-534
ff38498
add lower level exec
74c9b35
impl exec on controller
02c3a9e
handle error from kube api
2ca4e6a
handle install
046f013
update pg image
c7423e5
simplify exec cmd
876f78a
fmt
141f60a
update crd
972b657
Merge branch 'main' into cor-534
ChuckHend 45d9bc6
Merge branch 'main' into cor-534
ChuckHend File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
use k8s_openapi::{api::core::v1::Pod, apimachinery::pkg::apis::meta::v1::Status}; | ||
use kube::{api::Api, client::Client, core::subresource::AttachParams}; | ||
use tokio::io::AsyncReadExt; | ||
|
||
use crate::Error; | ||
use tracing::error; | ||
|
||
#[derive(Debug)] | ||
pub struct ExecOutput { | ||
pub stdout: Option<String>, | ||
pub stderr: Option<String>, | ||
pub status: Option<Status>, | ||
} | ||
|
||
impl ExecOutput { | ||
pub fn new(stdout: Option<String>, stderr: Option<String>, status: Option<Status>) -> Self { | ||
Self { | ||
stdout, | ||
stderr, | ||
status, | ||
} | ||
} | ||
} | ||
|
||
pub struct ExecCommand { | ||
pods_api: Api<Pod>, | ||
pod_name: String, | ||
} | ||
|
||
impl ExecCommand { | ||
pub fn new(pod_name: String, namespace: String, client: Client) -> Self { | ||
let pods_api: Api<Pod> = Api::namespaced(client, &namespace); | ||
Self { pod_name, pods_api } | ||
} | ||
|
||
pub async fn execute(&self, command: &[String]) -> Result<ExecOutput, Error> { | ||
let attach_params = AttachParams { | ||
container: Some("postgres".to_string()), | ||
tty: false, | ||
stdin: true, | ||
stdout: true, | ||
stderr: true, | ||
max_stdin_buf_size: Some(1024), | ||
max_stdout_buf_size: Some(1024), | ||
max_stderr_buf_size: Some(1024), | ||
}; | ||
|
||
let mut attached_process = self | ||
.pods_api | ||
.exec(self.pod_name.as_str(), command, &attach_params) | ||
.await?; | ||
|
||
let mut stdout_reader = attached_process.stdout().unwrap(); | ||
let mut result_stdout = String::new(); | ||
stdout_reader.read_to_string(&mut result_stdout).await.unwrap(); | ||
|
||
let mut stderr_reader = attached_process.stderr().unwrap(); | ||
let mut result_stderr = String::new(); | ||
stderr_reader.read_to_string(&mut result_stderr).await.unwrap(); | ||
|
||
|
||
let status = attached_process.take_status().unwrap().await.unwrap(); | ||
// https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#spec-and-status | ||
let response = ExecOutput::new(Some(result_stdout), Some(result_stderr), Some(status.clone())); | ||
|
||
match status.status.expect("no status reported").as_str() { | ||
"Success" => Ok(response), | ||
"Failure" => { | ||
error!("Error executing command: {:?}. response: {:?}", command, response); | ||
Err(Error::KubeExecError(format!( | ||
"Error executing command: {:?}. response: {:?}", | ||
command, response | ||
))) | ||
} | ||
_ => { | ||
error!( | ||
"Undefined response from kube API {:?}, command: {:?}", | ||
response, command | ||
); | ||
Err(Error::KubeExecError(format!( | ||
"Error executing command: {:?}. response: {:?}", | ||
command, response | ||
))) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -126,6 +126,55 @@ name asc, | |
enabled desc | ||
"#; | ||
|
||
/// handles installing extensions | ||
pub async fn install_extension( | ||
cdb: &CoreDB, | ||
extensions: &[Extension], | ||
ctx: Arc<Context>, | ||
) -> Result<(), Error> { | ||
debug!("extensions to install: {:?}", extensions); | ||
let client = ctx.client.clone(); | ||
|
||
let pod_name = cdb | ||
.primary_pod(client.clone()) | ||
.await | ||
.unwrap() | ||
.metadata | ||
.name | ||
.unwrap(); | ||
|
||
let mut errors: Vec<Error> = Vec::new(); | ||
for ext in extensions.iter() { | ||
let version = ext.locations[0].version.clone().unwrap(); | ||
let cmd = vec![ | ||
"trunk".to_owned(), | ||
"install".to_owned(), | ||
ext.name.clone(), | ||
"--version".to_owned(), | ||
version, | ||
]; | ||
let result = cdb.exec(pod_name.clone(), client.clone(), &cmd).await; | ||
|
||
match result { | ||
Ok(result) => { | ||
debug!("installed extension: {}", result.stdout.clone().unwrap()); | ||
} | ||
Err(err) => { | ||
error!("error installing extension, {}", err); | ||
errors.push(err); | ||
} | ||
} | ||
} | ||
let num_success = extensions.len() - errors.len(); | ||
info!( | ||
"Successfully installed {} / {} extensions", | ||
num_success, | ||
extensions.len() | ||
); | ||
Ok(()) | ||
} | ||
|
||
|
||
/// handles create/drop extensions | ||
pub async fn toggle_extensions( | ||
cdb: &CoreDB, | ||
|
@@ -302,32 +351,6 @@ pub async fn get_all_extensions(cdb: &CoreDB, ctx: Arc<Context>) -> Result<Vec<E | |
Ok(ext_spec) | ||
} | ||
|
||
/// reconcile extensions between the spec and the database | ||
pub async fn reconcile_extensions(coredb: &CoreDB, ctx: Arc<Context>) -> Result<Vec<Extension>, Error> { | ||
// always get the current state of extensions in the database | ||
// this is due to out of band changes - manual create/drop extension | ||
let actual_extensions = get_all_extensions(coredb, ctx.clone()).await?; | ||
let desired_extensions = coredb.spec.extensions.clone(); | ||
|
||
// most of the time there will be no changes | ||
let extensions_changed = diff_extensions(&desired_extensions, &actual_extensions); | ||
|
||
if extensions_changed.is_empty() { | ||
// no further work when no changes | ||
return Ok(actual_extensions); | ||
} | ||
|
||
// otherwise, need to determine the plan to apply | ||
let (changed_extensions, extensions_to_install) = extension_plan(&desired_extensions, &actual_extensions); | ||
|
||
toggle_extensions(coredb, &changed_extensions, ctx.clone()).await?; | ||
debug!("extensions to install: {:?}", extensions_to_install); | ||
// TODO: trunk install >extensions_to_install< on container | ||
|
||
// return final state of extensions | ||
get_all_extensions(coredb, ctx.clone()).await | ||
} | ||
|
||
// returns any elements that are in the desired, and not in actual | ||
// any Extensions returned by this function need either create or drop extension | ||
// cheap way to determine if there have been any sort of changes to extensions | ||
|
@@ -367,6 +390,32 @@ fn extension_plan(have_changed: &[Extension], actual: &[Extension]) -> (Vec<Exte | |
(changed, to_install) | ||
} | ||
|
||
/// reconcile extensions between the spec and the database | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. moved this to the bottom of the file so its easier to find. its the "top level" command in the file |
||
pub async fn reconcile_extensions(coredb: &CoreDB, ctx: Arc<Context>) -> Result<Vec<Extension>, Error> { | ||
// always get the current state of extensions in the database | ||
// this is due to out of band changes - manual create/drop extension | ||
let actual_extensions = get_all_extensions(coredb, ctx.clone()).await?; | ||
let desired_extensions = coredb.spec.extensions.clone(); | ||
|
||
// most of the time there will be no changes | ||
let extensions_changed = diff_extensions(&desired_extensions, &actual_extensions); | ||
|
||
if extensions_changed.is_empty() { | ||
// no further work when no changes | ||
return Ok(actual_extensions); | ||
} | ||
|
||
// otherwise, need to determine the plan to apply | ||
let (changed_extensions, extensions_to_install) = extension_plan(&desired_extensions, &actual_extensions); | ||
|
||
toggle_extensions(coredb, &changed_extensions, ctx.clone()).await?; | ||
install_extension(coredb, &extensions_to_install, ctx.clone()).await?; | ||
|
||
// return final state of extensions | ||
get_all_extensions(coredb, ctx.clone()).await | ||
} | ||
|
||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
update to the image that has trunk cli installed