-
Notifications
You must be signed in to change notification settings - Fork 84
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
Add one-off command #65
Open
peterfication
wants to merge
3
commits into
databricks:master
Choose a base branch
from
store2be:add-one-off
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1145,6 +1145,199 @@ command!( | |
} | ||
); | ||
|
||
command!( | ||
OneOff, | ||
"one-off", | ||
"exec specified command in a new container of the active pod", | ||
|clap: App<'static, 'static>| clap.arg( | ||
Arg::with_name("command") | ||
.help("The command to execute") | ||
.required(true) | ||
.index(1) | ||
).arg( | ||
Arg::with_name("container") | ||
.short("c") | ||
.long("container") | ||
.help("Exec in the image of the specified container") | ||
.takes_value(true) | ||
) | ||
.arg( | ||
Arg::with_name("terminal") | ||
.short("t") | ||
.long("terminal") | ||
.help( | ||
"Run the command in a new terminal. With --terminal ARG, ARG is used as the \ | ||
terminal command, otherwise the default is used ('set terminal <value>' to \ | ||
specify default)" | ||
) | ||
.takes_value(true) | ||
.min_values(0) | ||
), | ||
vec!["one-off"], | ||
|args: Vec<&str>, env: &Env| if args.len() <= 1 { | ||
let mut v = Vec::new(); | ||
let argstart = args.get(0); | ||
match env.current_object { | ||
::KObj::Pod { | ||
name: _, | ||
ref containers, | ||
} => for cont in containers.iter() { | ||
if argstart.is_none() || cont.starts_with(argstart.unwrap()) { | ||
v.push(cont.clone()); | ||
} | ||
}, | ||
_ => {} | ||
} | ||
( | ||
match argstart { | ||
Some(line) => line.len(), | ||
None => 0, | ||
}, | ||
v, | ||
) | ||
} else { | ||
(0, Vec::new()) | ||
}, | ||
|matches, env, writer| { | ||
let cont = match matches.value_of("container") { | ||
Some(c) => match env.current_object { | ||
::KObj::Pod { | ||
name: _, | ||
ref containers, | ||
} => { | ||
if !containers.contains(&c.to_string()) { | ||
clickwrite!( | ||
writer, | ||
"Specified container not found. Please specify one of: \ | ||
{:?}\n", | ||
containers | ||
); | ||
return; | ||
} | ||
c | ||
} | ||
_ => { | ||
clickwrite!(writer, "Need an active pod to run one-off command.\n"); | ||
return; | ||
} | ||
}, | ||
None => match env.current_object { | ||
::KObj::Pod { | ||
name: _, | ||
ref containers, | ||
} => { | ||
if containers.len() == 1 { | ||
containers[0].as_str() | ||
} else { | ||
clickwrite!( | ||
writer, | ||
"Pod has multiple containers, please specify one of: \ | ||
{:?}\n", | ||
containers | ||
); | ||
return; | ||
} | ||
} | ||
_ => { | ||
clickwrite!(writer, "Need an active pod to run one-off command.\n"); | ||
return; | ||
} | ||
}, | ||
}; | ||
|
||
let cmd = matches.value_of("command").unwrap(); // safe as required | ||
if let (Some(ref kluster), Some(ref ns), Some(ref pod)) = ( | ||
env.kluster.as_ref(), | ||
env.current_object_namespace.as_ref(), | ||
env.current_pod(), | ||
) { | ||
let url = format!("/api/v1/namespaces/{}/pods/{}", ns, pod); | ||
let pod_opt: Option<Pod> = env.run_on_kluster(|k| k.get(url.as_str())); | ||
let container_image = if let Some(pod) = pod_opt { | ||
let mut image = ""; | ||
if let Some(ref container_statuses) = pod.status.container_statuses { | ||
for container_status in container_statuses.iter() { | ||
if cont == container_status.name { | ||
image = &container_status.image; | ||
} | ||
} | ||
if image == "" { | ||
clickwrite!(writer, "Couldon't get container image\n"); | ||
return; | ||
} else { | ||
String::from(image) | ||
} | ||
} else { | ||
clickwrite!(writer, "Couldon't get container image\n"); | ||
return; | ||
} | ||
} else { | ||
clickwrite!(writer, "Couldon't get container image\n"); | ||
return; | ||
}; | ||
|
||
if matches.is_present("terminal") { | ||
let terminal = if let Some(t) = matches.value_of("terminal") { | ||
t | ||
} else if let Some(ref t) = env.click_config.terminal { | ||
t | ||
} else { | ||
"xterm -e" | ||
}; | ||
let mut targs: Vec<&str> = terminal.split_whitespace().collect(); | ||
let mut kubectl_args = vec![ | ||
"kubectl", | ||
"--namespace", | ||
ns, | ||
"--context", | ||
&kluster.name, | ||
"run", | ||
"one-off-shell", | ||
"--restart=Never", | ||
"-i", | ||
"--rm", | ||
"--tty", | ||
"--image", | ||
container_image.as_str(), | ||
]; | ||
targs.append(&mut kubectl_args); | ||
targs.push(cmd); | ||
clickwrite!(writer, "Starting in terminal\n"); | ||
if let Err(e) = duct::cmd(targs[0], &targs[1..]).start() { | ||
clickwrite!( | ||
writer, | ||
"Could not launch in terminal: {}\n", | ||
e.description() | ||
); | ||
} | ||
} else { | ||
clickwrite!(writer, "Starting one-off container with image {:?}\n", container_image); | ||
let status = Command::new("kubectl") | ||
.arg("--namespace") | ||
.arg(ns) | ||
.arg("--context") | ||
.arg(&kluster.name) | ||
.arg("run") | ||
.arg("one-off-shell") | ||
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. Could be named more specific eg. |
||
.arg("--restart=Never") | ||
.arg("-i") | ||
.arg("--rm") | ||
.arg("--tty") | ||
.arg("--image") | ||
.arg(container_image.as_str()) | ||
.arg(cmd) | ||
.status() | ||
.expect("failed to execute kubectl"); | ||
if !status.success() { | ||
clickwrite!(writer, "kubectl exited abnormally\n"); | ||
} | ||
} | ||
} else { | ||
clickwrite!(writer, "Need an active pod in order to run one-off command.\n"); | ||
} | ||
} | ||
); | ||
|
||
command!( | ||
Describe, | ||
"describe", | ||
|
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.
OneOff
should be probablyRun
to mimic thekubectl
command.