-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
650 additions
and
581 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
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 |
---|---|---|
|
@@ -11,4 +11,4 @@ publish = false | |
links = "helm" | ||
|
||
[build-dependencies] | ||
gobuild.workspace = true | ||
bindgen.workspace = true |
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 |
---|---|---|
@@ -1,27 +1,32 @@ | ||
use std::env; | ||
use std::{env, path::PathBuf, process::Command}; | ||
|
||
use gobuild::BuildMode; | ||
fn main() { | ||
let out_path = PathBuf::from(env::var("OUT_DIR").unwrap()); | ||
|
||
const ENV_GO_HELM_WRAPPER: &str = "GO_HELM_WRAPPER"; | ||
println!("cargo:rerun-if-changed=go-helm-wrapper/main.go"); | ||
|
||
fn main() { | ||
// cgo requires an explicit dependency on libresolv on some platforms (such as Red Hat Enterprise Linux 8 and derivatives) | ||
println!("cargo:rustc-link-lib=resolv"); | ||
println!("cargo:rerun-if-env-changed={ENV_GO_HELM_WRAPPER}"); | ||
match env::var(ENV_GO_HELM_WRAPPER) { | ||
Ok(go_helm_wrapper) => { | ||
// Reuse pre-built helm wrapper if possible | ||
eprintln!("Reusing pre-built go-helm-wrapper ({go_helm_wrapper:?})"); | ||
println!("cargo:rustc-link-lib=static:+verbatim={go_helm_wrapper}"); | ||
} | ||
Err(env::VarError::NotPresent) => { | ||
gobuild::Build::new() | ||
.file("go-helm-wrapper/main.go") | ||
.buildmode(BuildMode::CArchive) | ||
.compile("go-helm-wrapper"); | ||
} | ||
Err(err @ env::VarError::NotUnicode(..)) => { | ||
panic!("{ENV_GO_HELM_WRAPPER} must be valid unicode: {err}"); | ||
} | ||
} | ||
let mut cmd = Command::new("go"); | ||
cmd.arg("build") | ||
.args(["-buildmode", "c-archive"]) | ||
.arg("-o") | ||
.arg(out_path.join("libgo-helm-wrapper.a")) | ||
.arg("go-helm-wrapper/main.go") | ||
.env("CGO_ENABLED", "1"); | ||
cmd.status().expect("Failed to build go-helm-wrapper"); | ||
|
||
let bindings = bindgen::builder() | ||
.header(out_path.join("libgo-helm-wrapper.h").to_str().unwrap()) | ||
.parse_callbacks(Box::new(bindgen::CargoCallbacks)) | ||
.generate() | ||
.expect("Failed to generate Rust bindings from Go header file"); | ||
|
||
bindings | ||
.write_to_file(out_path.join("bindings.rs")) | ||
.expect("Failed to write bindings"); | ||
|
||
println!("cargo:rustc-link-lib=static=go-helm-wrapper"); | ||
println!( | ||
"cargo:rustc-link-search=native={}", | ||
out_path.to_str().unwrap() | ||
); | ||
} |
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 |
---|---|---|
@@ -1,37 +1,109 @@ | ||
use std::{marker::PhantomData, os::raw::c_char}; | ||
#![allow(non_upper_case_globals)] | ||
#![allow(non_camel_case_types)] | ||
#![allow(improper_ctypes)] | ||
#![allow(non_snake_case)] | ||
|
||
#[repr(C)] | ||
pub struct GoString<'a> { | ||
p: *const u8, | ||
n: i64, | ||
_lifetime: PhantomData<&'a str>, | ||
include!(concat!(env!("OUT_DIR"), "/bindings.rs")); | ||
|
||
use std::ffi::{c_char, CStr, CString}; | ||
|
||
pub const HELM_ERROR_PREFIX: &str = "ERROR:"; | ||
|
||
pub fn install_helm_release( | ||
release_name: &str, | ||
chart_name: &str, | ||
chart_version: &str, | ||
values_yaml: &str, | ||
namespace: &str, | ||
suppress_output: bool, | ||
) -> String { | ||
let release_name = CString::new(release_name).unwrap(); | ||
let chart_name = CString::new(chart_name).unwrap(); | ||
let chart_version = CString::new(chart_version).unwrap(); | ||
let values_yaml = CString::new(values_yaml).unwrap(); | ||
let namespace = CString::new(namespace).unwrap(); | ||
|
||
unsafe { | ||
let c = go_install_helm_release( | ||
release_name.as_ptr() as *mut c_char, | ||
chart_name.as_ptr() as *mut c_char, | ||
chart_version.as_ptr() as *mut c_char, | ||
values_yaml.as_ptr() as *mut c_char, | ||
namespace.as_ptr() as *mut c_char, | ||
suppress_output as u8, | ||
); | ||
|
||
cstr_ptr_to_string(c) | ||
} | ||
} | ||
|
||
pub fn uninstall_helm_release( | ||
release_name: &str, | ||
namespace: &str, | ||
suppress_output: bool, | ||
) -> String { | ||
let release_name = CString::new(release_name).unwrap(); | ||
let namespace = CString::new(namespace).unwrap(); | ||
|
||
unsafe { | ||
let c = go_uninstall_helm_release( | ||
release_name.as_ptr() as *mut c_char, | ||
namespace.as_ptr() as *mut c_char, | ||
suppress_output as u8, | ||
); | ||
|
||
cstr_ptr_to_string(c) | ||
} | ||
} | ||
|
||
pub fn check_helm_release_exists(release_name: &str, namespace: &str) -> bool { | ||
let release_name = CString::new(release_name).unwrap(); | ||
let namespace = CString::new(namespace).unwrap(); | ||
|
||
unsafe { | ||
go_helm_release_exists( | ||
release_name.as_ptr() as *mut c_char, | ||
namespace.as_ptr() as *mut c_char, | ||
) != 0 | ||
} | ||
} | ||
|
||
impl<'a> From<&'a str> for GoString<'a> { | ||
fn from(str: &'a str) -> Self { | ||
GoString { | ||
p: str.as_ptr(), | ||
n: str.len() as i64, | ||
_lifetime: PhantomData, | ||
} | ||
pub fn list_helm_releases(namespace: &str) -> String { | ||
let namespace = CString::new(namespace).unwrap(); | ||
|
||
unsafe { | ||
let c = go_helm_list_releases(namespace.as_ptr() as *mut c_char); | ||
cstr_ptr_to_string(c) | ||
} | ||
} | ||
|
||
extern "C" { | ||
pub fn go_install_helm_release( | ||
release_name: GoString, | ||
chart_name: GoString, | ||
chart_version: GoString, | ||
values_yaml: GoString, | ||
namespace: GoString, | ||
suppress_output: bool, | ||
) -> *const c_char; | ||
pub fn go_uninstall_helm_release( | ||
release_name: GoString, | ||
namespace: GoString, | ||
suppress_output: bool, | ||
) -> *const c_char; | ||
pub fn go_helm_release_exists(release_name: GoString, namespace: GoString) -> bool; | ||
pub fn go_helm_list_releases(namespace: GoString) -> *const c_char; | ||
pub fn go_add_helm_repo(name: GoString, url: GoString) -> *const c_char; | ||
pub fn add_helm_repository(repository_name: &str, repository_url: &str) -> String { | ||
let repository_name = CString::new(repository_name).unwrap(); | ||
let repository_url = CString::new(repository_url).unwrap(); | ||
|
||
unsafe { | ||
let c = go_add_helm_repo( | ||
repository_name.as_ptr() as *mut c_char, | ||
repository_url.as_ptr() as *mut c_char, | ||
); | ||
|
||
cstr_ptr_to_string(c) | ||
} | ||
} | ||
|
||
/// Checks if the result string is an error, and if so, returns the error message as a string. | ||
pub fn to_helm_error(result: &str) -> Option<String> { | ||
if !result.is_empty() && result.starts_with(HELM_ERROR_PREFIX) { | ||
return Some(result.replace(HELM_ERROR_PREFIX, "")); | ||
} | ||
|
||
None | ||
} | ||
|
||
unsafe fn cstr_ptr_to_string(c: *mut c_char) -> String { | ||
let cstr = CStr::from_ptr(c); | ||
let s = String::from_utf8_lossy(cstr.to_bytes()).to_string(); | ||
free_go_string(cstr.as_ptr() as *mut c_char); | ||
|
||
s | ||
} |
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
Oops, something went wrong.