Skip to content

Commit

Permalink
fix: some state handling wrong and also improve logic to set default …
Browse files Browse the repository at this point in the history
…values
  • Loading branch information
hcavarsan committed Sep 21, 2024
1 parent 8dd8b21 commit e233f46
Show file tree
Hide file tree
Showing 17 changed files with 227 additions and 176 deletions.
10 changes: 6 additions & 4 deletions crates/kftray-commons/src/models/config_model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ pub struct Config {
#[serde(default)]
pub namespace: String,
#[serde(default)]
pub local_port: u16,
#[serde(skip_serializing_if = "Option::is_none")]
pub local_port: Option<u16>,
#[serde(default)]
pub remote_port: u16,
#[serde(skip_serializing_if = "Option::is_none")]
pub remote_port: Option<u16>,
#[serde(default)]
pub context: String,
#[serde(default)]
Expand All @@ -42,8 +44,8 @@ impl Default for Config {
id: None,
service: Some("default-service".to_string()),
namespace: "default-namespace".to_string(),
local_port: 0,
remote_port: 0,
local_port: Some(0),
remote_port: Some(0),
context: "default-context".to_string(),
workload_type: "default-workload".to_string(),
protocol: "protocol".to_string(),
Expand Down
15 changes: 7 additions & 8 deletions crates/kftray-commons/src/utils/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ pub async fn update_config(config: Config) -> Result<(), String> {

sqlx::query("UPDATE configs SET data = ?1 WHERE id = ?2")
.bind(data)
.bind(config.unwrap().id.unwrap())
.bind(config.id.unwrap())
.execute(&mut *conn)
.await
.map_err(|e| e.to_string())?;
Expand Down Expand Up @@ -261,25 +261,24 @@ fn remove_blank_or_default_fields(value: &mut JsonValue, default_config: &JsonVa
}
}

fn prepare_config(mut config: Config) -> Result<Config, String> {
fn prepare_config(mut config: Config) -> Config {
if let Some(ref mut alias) = config.alias {
*alias = alias.trim().to_string();
}
if let Some(ref mut kubeconfig) = config.kubeconfig {
*kubeconfig = kubeconfig.trim().to_string();
}

if config.local_port == 0 {
if config.remote_port == 0 {
return Err("Both local_port and remote_port cannot be zero".to_string());
}
if config.local_port == Some(0) || config.local_port.is_none() {
config.local_port = config.remote_port;
}

if config.alias.as_deref() == Some("") || config.alias.is_none() {
let alias = format!(
"{}-{}-{}",
config.workload_type, config.protocol, config.local_port
config.workload_type,
config.protocol,
config.local_port.unwrap_or_default()
);
config.alias = Some(alias);
}
Expand All @@ -288,5 +287,5 @@ fn prepare_config(mut config: Config) -> Result<Config, String> {
config.kubeconfig = Some("default".to_string());
}

Ok(config)
config
}
24 changes: 12 additions & 12 deletions crates/kftray-portforward/src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,14 @@ pub async fn start_port_forward(
_ => TargetSelector::ServiceName(config.service.clone().unwrap_or_default()),
};

let remote_port = Port::from(config.remote_port as i32);
let remote_port = Port::from(config.remote_port.unwrap_or_default() as i32);
let context_name = Some(config.context.clone());
let kubeconfig = Some(config.kubeconfig.clone());
let namespace = config.namespace.clone();
let target = Target::new(selector, remote_port, namespace.clone());

log::debug!("Remote Port: {}", config.remote_port);
log::debug!("Local Port: {}", config.local_port);
log::debug!("Remote Port: {:?}", config.remote_port);
log::debug!("Local Port: {:?}", config.local_port);
if config.workload_type.as_str() == "pod" {
log::info!("Attempting to forward to pod label: {:?}", &config.target);
} else {
Expand Down Expand Up @@ -194,14 +194,14 @@ pub async fn start_port_forward(
service: config.service.clone().unwrap(),
namespace: namespace.clone(),
local_port: actual_local_port,
remote_port: config.remote_port,
remote_port: config.remote_port.unwrap_or_default(),
context: config.context.clone(),
protocol: config.protocol.clone(),
stdout: format!(
"{} forwarding from 127.0.0.1:{} -> {}:{}",
"{} forwarding from 127.0.0.1:{} -> {:?}:{}",
protocol.to_uppercase(),
actual_local_port,
config.remote_port,
config.remote_port.unwrap_or_default(),
config.service.clone().unwrap()
),
stderr: String::new(),
Expand Down Expand Up @@ -632,8 +632,8 @@ pub async fn deploy_and_forward_pod(
"remote_address",
config.remote_address.as_ref().unwrap().clone(),
);
values.insert("remote_port", config.remote_port.to_string());
values.insert("local_port", config.remote_port.to_string());
values.insert("remote_port", config.remote_port.expect("None").to_string());
values.insert("local_port", config.remote_port.expect("None").to_string());
values.insert("protocol", protocol.clone());

let manifest_path = get_pod_manifest_path().map_err(|e| e.to_string())?;
Expand Down Expand Up @@ -856,8 +856,8 @@ fn parse_configs(
namespace: namespace.to_string(),
service: Some(service_name.to_string()),
alias: Some(alias),
local_port,
remote_port: target_port as u16,
local_port: Some(local_port),
remote_port: Some(target_port as u16),
protocol: "tcp".to_string(),
workload_type: "service".to_string(),
..Default::default()
Expand All @@ -878,8 +878,8 @@ fn create_default_configs(
namespace: namespace.to_string(),
service: Some(service_name.to_string()),
alias: Some(service_name.to_string()),
local_port: port as u16,
remote_port: port as u16,
local_port: Some(port as u16),
remote_port: Some(port as u16),
protocol: "tcp".to_string(),
workload_type: "service".to_string(),
..Default::default()
Expand Down
18 changes: 15 additions & 3 deletions crates/kftui/src/tui/ui/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,11 @@ pub fn draw_configs_table(
Row::new(vec![
Cell::from(config.alias.clone().unwrap_or_default()),
Cell::from(config.workload_type.clone()),
Cell::from(config.local_port.to_string()),
Cell::from(
config
.local_port
.map_or_else(|| "".to_string(), |port| port.to_string()),
),
Cell::from(config.context.clone()),
])
.style(row_style)
Expand Down Expand Up @@ -191,7 +195,11 @@ pub fn render_details(
"Local Port: ",
Style::default().add_modifier(Modifier::BOLD),
),
Span::raw(config.local_port.to_string()),
Span::raw(
config
.local_port
.map_or_else(|| "".to_string(), |port| port.to_string()),
),
]),
Line::from(vec![
Span::styled(
Expand All @@ -205,7 +213,11 @@ pub fn render_details(
"Remote Port: ",
Style::default().add_modifier(Modifier::BOLD),
),
Span::raw(config.remote_port.to_string()),
Span::raw(
config
.remote_port
.map_or_else(|| "".to_string(), |port| port.to_string()),
),
]),
Line::from(vec![
Span::styled("Context: ", Style::default().add_modifier(Modifier::BOLD)),
Expand Down
75 changes: 51 additions & 24 deletions docs/kftray/USAGE.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,20 +47,52 @@ Example Json configuration File:
```json
[
{
"service": "argocd-server",
"namespace": "argocd",
"local_port": 8888,
"remote_port": 8080,
"context": "test-cluster",
"workload_type": "service",
"protocol": "tcp",
"remote_address": "",
"local_address": "127.0.0.1",
"alias": "argocd",
"domain_enabled": true
}
]
{
"alias": "service-tcp-8080",
"context": "kind",
"kubeconfig": "/Users/henrique/.kube/config.bkp",
"local_port": 8080,
"namespace": "argocd",
"protocol": "tcp",
"remote_port": 8080,
"service": "argocd-server",
"workload_type": "service"
},
{
"alias": "pod-tcp-8083",
"context": "kind",
"kubeconfig": "/Users/henrique/.kube/config.bkp",
"local_port": 8083,
"namespace": "argocd",
"protocol": "tcp",
"remote_port": 8083,
"target": "app.kubernetes.io/component=server",
"workload_type": "pod"
},
{
"alias": "proxy-udp-5353",
"context": "kind",
"kubeconfig": "/Users/henrique/.kube/config.bkp",
"local_port": 5353,
"namespace": "argocd",
"protocol": "udp",
"remote_address": "coredns.cluster.local.internal",
"remote_port": 5353,
"workload_type": "proxy"
},
{
"alias": "proxy-tcp-6443",
"context": "kind",
"kubeconfig": "/Users/henrique/.kube/config.bkp",
"local_port": 8777,
"namespace": "argocd",
"protocol": "tcp",
"remote_address": "test.homelab.cluster.internal",
"remote_port": 80,
"workload_type": "proxy"
}
]
```
## Sharing the configurations through Git
Expand All @@ -69,17 +101,12 @@ now, with the local json saved, you can share your configurations with your team

To import and sync your GitHub configs in kftray:


1. Open the application's main menu
2. Select the button with GitHub icon in the footer menu
4. Enter the URL of your Git repository and path containing the JSON file
5. If your GitHub repository is private, you will need to enter the private token. Credentials are securely saved in the SO keyring (Keychain on macOS). Kftray does not store or save credentials in any local file; they are only stored in the local keyring.
6. Select the polling time for when Kftray will synchronize configurations and retrieve them from GitHub.

1. Open the application's main menu
2. Select the button with GitHub icon in the footer menu
3. Enter the URL of your Git repository and path containing the JSON file
4. If your GitHub repository is private, you will need to enter the private token. Credentials are securely saved in the SO keyring (Keychain on macOS). Kftray does not store or save credentials in any local file; they are only stored in the local keyring.
5. Select the polling time for when Kftray will synchronize configurations and retrieve them from GitHub.

6. KFtray will now sync with the Git repository to automatically import any new configurations or changes committed to the JSON file.

This allows you to quickly deploy any port forward changes to all team members. And if someone on your team adds a new configuration, it will be automatically synced to everyone else's KFtray.



86 changes: 45 additions & 41 deletions examples/configs.json
Original file line number Diff line number Diff line change
@@ -1,42 +1,46 @@
[
{
"target": "app.kubernetes.io/component=server",
"namespace": "default",
"local_port": 8181,
"remote_port": 8080,
"context": "kind1",
"workload_type": "pod",
"protocol": "tcp",
"alias": "argocd"
},
{
"service": "httpbin",
"namespace": "default",
"local_port": 8081,
"remote_port": 8080,
"context": "kind1",
"workload_type": "service",
"protocol": "tcp",
"alias": "httpbin"
},
{
"target": "app.kubernetes.io/instance=echoserver",
"namespace": "echoserver",
"local_port": 8989,
"remote_port": 80,
"context": "kind-kind2",
"workload_type": "pod",
"protocol": "tcp",
"alias": "echoserver"
},
{
"alias": "ifconfig",
"namespace": "default",
"local_port": 8443,
"remote_port": 443,
"context": "kind-kind2",
"workload_type": "proxy",
"protocol": "tcp",
"remote_address": "ifconfig.me"
}
]
{
"alias": "service-tcp-8080",
"context": "kind",
"kubeconfig": "/Users/henrique/.kube/config.bkp",
"local_port": 8080,
"namespace": "argocd",
"protocol": "tcp",
"remote_port": 8080,
"service": "argocd-server",
"workload_type": "service"
},
{
"alias": "pod-tcp-8083",
"context": "kind",
"kubeconfig": "/Users/henrique/.kube/config.bkp",
"local_port": 8083,
"namespace": "argocd",
"protocol": "tcp",
"remote_port": 8083,
"target": "app.kubernetes.io/component=server",
"workload_type": "pod"
},
{
"alias": "proxy-udp-5353",
"context": "kind",
"kubeconfig": "/Users/henrique/.kube/config.bkp",
"local_port": 5353,
"namespace": "argocd",
"protocol": "udp",
"remote_address": "coredns.cluster.local.internal",
"remote_port": 5353,
"workload_type": "proxy"
},
{
"alias": "proxy-tcp-6443",
"context": "kind",
"kubeconfig": "/Users/henrique/.kube/config.bkp",
"local_port": 8777,
"namespace": "argocd",
"protocol": "tcp",
"remote_address": "test.homelab.cluster.internal",
"remote_port": 80,
"workload_type": "proxy"
}
]
1 change: 0 additions & 1 deletion frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
"dependencies": {
"@chakra-ui/icons": "^2.1.1",
"@chakra-ui/react": "^2.8.2",
"@emotion/react": "^11.13.0",
"@emotion/styled": "^11.13.0",
"@fortawesome/fontawesome-svg-core": "^6.6.0",
"@fortawesome/free-solid-svg-icons": "^6.6.0",
Expand Down
Loading

0 comments on commit e233f46

Please sign in to comment.