Skip to content
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

Merged
merged 1 commit into from
Feb 15, 2023

Conversation

lobshunter
Copy link
Contributor

@lobshunter lobshunter commented Feb 9, 2023

This PR closes #1021, recognizes kubernetes NodePorts and LoadBalancer for port-forwarding. Based on the idea of #1021 (comment)

Copy link
Contributor Author

@lobshunter lobshunter left a 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.

Comment on lines +64 to +79
"/etc/rancher/k3s/k3s.yaml",
"/root/.kube/config",
Copy link
Contributor Author

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.

Comment on lines +28 to +33
type Entry struct {
Protocol Protocol
IP net.IP
Port uint16
}
Copy link
Contributor Author

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.

@lobshunter
Copy link
Contributor Author

I will squash commits after all reviews are done, for reviewers' convenience.

@lobshunter lobshunter marked this pull request as ready for review February 10, 2023 16:00
@lobshunter lobshunter requested review from AkihiroSuda and Nino-K and removed request for AkihiroSuda and Nino-K February 10, 2023 16:01
AkihiroSuda
AkihiroSuda previously approved these changes Feb 11, 2023
Copy link
Member

@AkihiroSuda AkihiroSuda left a 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?

@AkihiroSuda AkihiroSuda requested review from afbjorklund and jandubois and removed request for Nino-K February 11, 2023 12:25
@AkihiroSuda AkihiroSuda added this to the v0.15 (tentative) milestone Feb 11, 2023
@jandubois
Copy link
Member

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.

Copy link
Contributor

@Nino-K Nino-K left a 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

Comment on lines +83 to +104
_, 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
}

Copy link
Contributor

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
}

Copy link
Contributor Author

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.

Copy link
Contributor Author

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,
	}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good 👍

Copy link
Member

@AkihiroSuda AkihiroSuda left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

pkg/guestagent/iptables: recognize Kubernetes NodePorts
4 participants