Skip to content

Commit e233f46

Browse files
committed
fix: some state handling wrong and also improve logic to set default values
1 parent 8dd8b21 commit e233f46

File tree

17 files changed

+227
-176
lines changed

17 files changed

+227
-176
lines changed

crates/kftray-commons/src/models/config_model.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@ pub struct Config {
1313
#[serde(default)]
1414
pub namespace: String,
1515
#[serde(default)]
16-
pub local_port: u16,
16+
#[serde(skip_serializing_if = "Option::is_none")]
17+
pub local_port: Option<u16>,
1718
#[serde(default)]
18-
pub remote_port: u16,
19+
#[serde(skip_serializing_if = "Option::is_none")]
20+
pub remote_port: Option<u16>,
1921
#[serde(default)]
2022
pub context: String,
2123
#[serde(default)]
@@ -42,8 +44,8 @@ impl Default for Config {
4244
id: None,
4345
service: Some("default-service".to_string()),
4446
namespace: "default-namespace".to_string(),
45-
local_port: 0,
46-
remote_port: 0,
47+
local_port: Some(0),
48+
remote_port: Some(0),
4749
context: "default-context".to_string(),
4850
workload_type: "default-workload".to_string(),
4951
protocol: "protocol".to_string(),

crates/kftray-commons/src/utils/config.rs

+7-8
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ pub async fn update_config(config: Config) -> Result<(), String> {
168168

169169
sqlx::query("UPDATE configs SET data = ?1 WHERE id = ?2")
170170
.bind(data)
171-
.bind(config.unwrap().id.unwrap())
171+
.bind(config.id.unwrap())
172172
.execute(&mut *conn)
173173
.await
174174
.map_err(|e| e.to_string())?;
@@ -261,25 +261,24 @@ fn remove_blank_or_default_fields(value: &mut JsonValue, default_config: &JsonVa
261261
}
262262
}
263263

264-
fn prepare_config(mut config: Config) -> Result<Config, String> {
264+
fn prepare_config(mut config: Config) -> Config {
265265
if let Some(ref mut alias) = config.alias {
266266
*alias = alias.trim().to_string();
267267
}
268268
if let Some(ref mut kubeconfig) = config.kubeconfig {
269269
*kubeconfig = kubeconfig.trim().to_string();
270270
}
271271

272-
if config.local_port == 0 {
273-
if config.remote_port == 0 {
274-
return Err("Both local_port and remote_port cannot be zero".to_string());
275-
}
272+
if config.local_port == Some(0) || config.local_port.is_none() {
276273
config.local_port = config.remote_port;
277274
}
278275

279276
if config.alias.as_deref() == Some("") || config.alias.is_none() {
280277
let alias = format!(
281278
"{}-{}-{}",
282-
config.workload_type, config.protocol, config.local_port
279+
config.workload_type,
280+
config.protocol,
281+
config.local_port.unwrap_or_default()
283282
);
284283
config.alias = Some(alias);
285284
}
@@ -288,5 +287,5 @@ fn prepare_config(mut config: Config) -> Result<Config, String> {
288287
config.kubeconfig = Some("default".to_string());
289288
}
290289

291-
Ok(config)
290+
config
292291
}

crates/kftray-portforward/src/core.rs

+12-12
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,14 @@ pub async fn start_port_forward(
6464
_ => TargetSelector::ServiceName(config.service.clone().unwrap_or_default()),
6565
};
6666

67-
let remote_port = Port::from(config.remote_port as i32);
67+
let remote_port = Port::from(config.remote_port.unwrap_or_default() as i32);
6868
let context_name = Some(config.context.clone());
6969
let kubeconfig = Some(config.kubeconfig.clone());
7070
let namespace = config.namespace.clone();
7171
let target = Target::new(selector, remote_port, namespace.clone());
7272

73-
log::debug!("Remote Port: {}", config.remote_port);
74-
log::debug!("Local Port: {}", config.local_port);
73+
log::debug!("Remote Port: {:?}", config.remote_port);
74+
log::debug!("Local Port: {:?}", config.local_port);
7575
if config.workload_type.as_str() == "pod" {
7676
log::info!("Attempting to forward to pod label: {:?}", &config.target);
7777
} else {
@@ -194,14 +194,14 @@ pub async fn start_port_forward(
194194
service: config.service.clone().unwrap(),
195195
namespace: namespace.clone(),
196196
local_port: actual_local_port,
197-
remote_port: config.remote_port,
197+
remote_port: config.remote_port.unwrap_or_default(),
198198
context: config.context.clone(),
199199
protocol: config.protocol.clone(),
200200
stdout: format!(
201-
"{} forwarding from 127.0.0.1:{} -> {}:{}",
201+
"{} forwarding from 127.0.0.1:{} -> {:?}:{}",
202202
protocol.to_uppercase(),
203203
actual_local_port,
204-
config.remote_port,
204+
config.remote_port.unwrap_or_default(),
205205
config.service.clone().unwrap()
206206
),
207207
stderr: String::new(),
@@ -632,8 +632,8 @@ pub async fn deploy_and_forward_pod(
632632
"remote_address",
633633
config.remote_address.as_ref().unwrap().clone(),
634634
);
635-
values.insert("remote_port", config.remote_port.to_string());
636-
values.insert("local_port", config.remote_port.to_string());
635+
values.insert("remote_port", config.remote_port.expect("None").to_string());
636+
values.insert("local_port", config.remote_port.expect("None").to_string());
637637
values.insert("protocol", protocol.clone());
638638

639639
let manifest_path = get_pod_manifest_path().map_err(|e| e.to_string())?;
@@ -856,8 +856,8 @@ fn parse_configs(
856856
namespace: namespace.to_string(),
857857
service: Some(service_name.to_string()),
858858
alias: Some(alias),
859-
local_port,
860-
remote_port: target_port as u16,
859+
local_port: Some(local_port),
860+
remote_port: Some(target_port as u16),
861861
protocol: "tcp".to_string(),
862862
workload_type: "service".to_string(),
863863
..Default::default()
@@ -878,8 +878,8 @@ fn create_default_configs(
878878
namespace: namespace.to_string(),
879879
service: Some(service_name.to_string()),
880880
alias: Some(service_name.to_string()),
881-
local_port: port as u16,
882-
remote_port: port as u16,
881+
local_port: Some(port as u16),
882+
remote_port: Some(port as u16),
883883
protocol: "tcp".to_string(),
884884
workload_type: "service".to_string(),
885885
..Default::default()

crates/kftui/src/tui/ui/table.rs

+15-3
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,11 @@ pub fn draw_configs_table(
7777
Row::new(vec![
7878
Cell::from(config.alias.clone().unwrap_or_default()),
7979
Cell::from(config.workload_type.clone()),
80-
Cell::from(config.local_port.to_string()),
80+
Cell::from(
81+
config
82+
.local_port
83+
.map_or_else(|| "".to_string(), |port| port.to_string()),
84+
),
8185
Cell::from(config.context.clone()),
8286
])
8387
.style(row_style)
@@ -191,7 +195,11 @@ pub fn render_details(
191195
"Local Port: ",
192196
Style::default().add_modifier(Modifier::BOLD),
193197
),
194-
Span::raw(config.local_port.to_string()),
198+
Span::raw(
199+
config
200+
.local_port
201+
.map_or_else(|| "".to_string(), |port| port.to_string()),
202+
),
195203
]),
196204
Line::from(vec![
197205
Span::styled(
@@ -205,7 +213,11 @@ pub fn render_details(
205213
"Remote Port: ",
206214
Style::default().add_modifier(Modifier::BOLD),
207215
),
208-
Span::raw(config.remote_port.to_string()),
216+
Span::raw(
217+
config
218+
.remote_port
219+
.map_or_else(|| "".to_string(), |port| port.to_string()),
220+
),
209221
]),
210222
Line::from(vec![
211223
Span::styled("Context: ", Style::default().add_modifier(Modifier::BOLD)),

docs/kftray/USAGE.md

+51-24
Original file line numberDiff line numberDiff line change
@@ -47,20 +47,52 @@ Example Json configuration File:
4747
4848
```json
4949
[
50-
{
51-
"service": "argocd-server",
52-
"namespace": "argocd",
53-
"local_port": 8888,
54-
"remote_port": 8080,
55-
"context": "test-cluster",
56-
"workload_type": "service",
57-
"protocol": "tcp",
58-
"remote_address": "",
59-
"local_address": "127.0.0.1",
60-
"alias": "argocd",
61-
"domain_enabled": true
62-
}
63-
]
50+
{
51+
"alias": "service-tcp-8080",
52+
"context": "kind",
53+
"kubeconfig": "/Users/henrique/.kube/config.bkp",
54+
"local_port": 8080,
55+
"namespace": "argocd",
56+
"protocol": "tcp",
57+
"remote_port": 8080,
58+
"service": "argocd-server",
59+
"workload_type": "service"
60+
},
61+
{
62+
"alias": "pod-tcp-8083",
63+
"context": "kind",
64+
"kubeconfig": "/Users/henrique/.kube/config.bkp",
65+
"local_port": 8083,
66+
"namespace": "argocd",
67+
"protocol": "tcp",
68+
"remote_port": 8083,
69+
"target": "app.kubernetes.io/component=server",
70+
"workload_type": "pod"
71+
},
72+
{
73+
"alias": "proxy-udp-5353",
74+
"context": "kind",
75+
"kubeconfig": "/Users/henrique/.kube/config.bkp",
76+
"local_port": 5353,
77+
"namespace": "argocd",
78+
"protocol": "udp",
79+
"remote_address": "coredns.cluster.local.internal",
80+
"remote_port": 5353,
81+
"workload_type": "proxy"
82+
},
83+
{
84+
"alias": "proxy-tcp-6443",
85+
"context": "kind",
86+
"kubeconfig": "/Users/henrique/.kube/config.bkp",
87+
"local_port": 8777,
88+
"namespace": "argocd",
89+
"protocol": "tcp",
90+
"remote_address": "test.homelab.cluster.internal",
91+
"remote_port": 80,
92+
"workload_type": "proxy"
93+
}
94+
]
95+
6496
```
6597
6698
## Sharing the configurations through Git
@@ -69,17 +101,12 @@ now, with the local json saved, you can share your configurations with your team
69101

70102
To import and sync your GitHub configs in kftray:
71103

72-
73-
1. Open the application's main menu
74-
2. Select the button with GitHub icon in the footer menu
75-
4. Enter the URL of your Git repository and path containing the JSON file
76-
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.
77-
6. Select the polling time for when Kftray will synchronize configurations and retrieve them from GitHub.
78-
104+
1. Open the application's main menu
105+
2. Select the button with GitHub icon in the footer menu
106+
3. Enter the URL of your Git repository and path containing the JSON file
107+
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.
108+
5. Select the polling time for when Kftray will synchronize configurations and retrieve them from GitHub.
79109

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

82112
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.
83-
84-
85-

examples/configs.json

+45-41
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,46 @@
11
[
2-
{
3-
"target": "app.kubernetes.io/component=server",
4-
"namespace": "default",
5-
"local_port": 8181,
6-
"remote_port": 8080,
7-
"context": "kind1",
8-
"workload_type": "pod",
9-
"protocol": "tcp",
10-
"alias": "argocd"
11-
},
12-
{
13-
"service": "httpbin",
14-
"namespace": "default",
15-
"local_port": 8081,
16-
"remote_port": 8080,
17-
"context": "kind1",
18-
"workload_type": "service",
19-
"protocol": "tcp",
20-
"alias": "httpbin"
21-
},
22-
{
23-
"target": "app.kubernetes.io/instance=echoserver",
24-
"namespace": "echoserver",
25-
"local_port": 8989,
26-
"remote_port": 80,
27-
"context": "kind-kind2",
28-
"workload_type": "pod",
29-
"protocol": "tcp",
30-
"alias": "echoserver"
31-
},
32-
{
33-
"alias": "ifconfig",
34-
"namespace": "default",
35-
"local_port": 8443,
36-
"remote_port": 443,
37-
"context": "kind-kind2",
38-
"workload_type": "proxy",
39-
"protocol": "tcp",
40-
"remote_address": "ifconfig.me"
41-
}
42-
]
2+
{
3+
"alias": "service-tcp-8080",
4+
"context": "kind",
5+
"kubeconfig": "/Users/henrique/.kube/config.bkp",
6+
"local_port": 8080,
7+
"namespace": "argocd",
8+
"protocol": "tcp",
9+
"remote_port": 8080,
10+
"service": "argocd-server",
11+
"workload_type": "service"
12+
},
13+
{
14+
"alias": "pod-tcp-8083",
15+
"context": "kind",
16+
"kubeconfig": "/Users/henrique/.kube/config.bkp",
17+
"local_port": 8083,
18+
"namespace": "argocd",
19+
"protocol": "tcp",
20+
"remote_port": 8083,
21+
"target": "app.kubernetes.io/component=server",
22+
"workload_type": "pod"
23+
},
24+
{
25+
"alias": "proxy-udp-5353",
26+
"context": "kind",
27+
"kubeconfig": "/Users/henrique/.kube/config.bkp",
28+
"local_port": 5353,
29+
"namespace": "argocd",
30+
"protocol": "udp",
31+
"remote_address": "coredns.cluster.local.internal",
32+
"remote_port": 5353,
33+
"workload_type": "proxy"
34+
},
35+
{
36+
"alias": "proxy-tcp-6443",
37+
"context": "kind",
38+
"kubeconfig": "/Users/henrique/.kube/config.bkp",
39+
"local_port": 8777,
40+
"namespace": "argocd",
41+
"protocol": "tcp",
42+
"remote_address": "test.homelab.cluster.internal",
43+
"remote_port": 80,
44+
"workload_type": "proxy"
45+
}
46+
]

frontend/package.json

-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
"dependencies": {
1717
"@chakra-ui/icons": "^2.1.1",
1818
"@chakra-ui/react": "^2.8.2",
19-
"@emotion/react": "^11.13.0",
2019
"@emotion/styled": "^11.13.0",
2120
"@fortawesome/fontawesome-svg-core": "^6.6.0",
2221
"@fortawesome/free-solid-svg-icons": "^6.6.0",

0 commit comments

Comments
 (0)