-
Notifications
You must be signed in to change notification settings - Fork 604
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
feat: recognize Kubernetes NodePorts and LoadBalancer #1355
Conversation
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.
I will fill up unit test if this implementation looks reasonable.
"/etc/rancher/k3s/k3s.yaml", | ||
"/root/.kube/config", |
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.
A more reliable way is to add kubernetes related config into lima YAML, but I feel it a bit exaggerated.
type Entry struct { | ||
Protocol Protocol | ||
IP net.IP | ||
Port uint16 | ||
} |
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.
I feel the need of unifying port detecting packages(prochttp, iptables, kubernetesservice) if this PR is merged. These packages should use the same Entry
struct for instance.
I will squash commits after all reviews are done, for reviewers' convenience. |
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.
Thanks, could you squash the commits?
Signed-off-by: pengyu <[email protected]>
eeff040
to
e1ed721
Compare
I think this is fine as-is, but I would like to review this on Monday together with @Nino-K to make sure any remaining additional code we have in the Rancher Desktop version is only required for WSL support on Windows, and not relevant to Lima. |
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.
Just an optional comment, otherwise LGTM
_, err := os.Stat(kubeconfig) | ||
if err != nil { | ||
if os.IsNotExist(err) { | ||
continue | ||
} | ||
|
||
return nil, fmt.Errorf("stat kubeconfig %s failed: %w", kubeconfig, err) | ||
} | ||
|
||
restConfig, err := clientcmd.BuildConfigFromFlags("", kubeconfig) | ||
if err != nil { | ||
return nil, fmt.Errorf("build kubeconfig from %s failed: %w", kubeconfig, err) | ||
} | ||
u, err := url.Parse(restConfig.Host) | ||
if err != nil { | ||
return nil, fmt.Errorf("parse kubeconfig host %s failed: %w", restConfig.Host, err) | ||
} | ||
if u.Hostname() != "127.0.0.1" { // might need to support IPv6 | ||
// ensures the kubeconfig points to local k8s | ||
continue | ||
} | ||
|
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.
What you have here seems fine, but, want to let you know that you can also leverage the library to do more work for you, something like:
func getClientConfig(configPath string) (*restclient.Config, error) {
loadingRules := clientcmd.ClientConfigLoadingRules{
ExplicitPath: configPath,
}
clientConfig := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(&loadingRules, nil)
config, err := clientConfig.ClientConfig()
if err != nil {
return nil, fmt.Errorf("could not load Kubernetes client config from %s: %w", configPath, err)
}
return config, nil
}
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.
Nice one, I wasn't aware of this approach. Will update the code later today.
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.
Hmmm, after some trying I would like to stay at current implementation. Because
using Precedence
makes error message kind of confusing
loadingRules := clientcmd.ClientConfigLoadingRules{
Precedence: []string{
"/etc/rancher/k3s/k3s.yaml",
"/root/.kube/config",
},
}
using ExplicitPath
needs the same amount of code, and it would rely on client-go to return os.ErrNotExist if configPath
is missing.
loadingRules := clientcmd.ClientConfigLoadingRules{
ExplicitPath: configPath,
}
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.
Sounds good 👍
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.
Thanks
This PR closes #1021, recognizes kubernetes NodePorts and LoadBalancer for port-forwarding. Based on the idea of #1021 (comment)