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

Use localhost in admin kubeconfig again, if possible #5426

Merged
merged 1 commit into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions cmd/controller/certificates.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
"net/url"
"os"
"path/filepath"
"strconv"

"github.com/k0sproject/k0s/internal/pkg/file"
"github.com/k0sproject/k0s/internal/pkg/users"
Expand Down Expand Up @@ -67,8 +66,7 @@ func (c *Certificates) Init(ctx context.Context) error {
}
c.CACert = string(cert)
// Changing the URL here also requires changes in the "k0s kubeconfig admin" subcommand.
apiAddress := net.JoinHostPort(c.ClusterSpec.API.Address, strconv.Itoa(c.ClusterSpec.API.Port))
kubeConfigAPIUrl := (&url.URL{Scheme: "https", Host: apiAddress}).String()
kubeConfigAPIUrl := c.ClusterSpec.API.LocalURL()

apiServerUID, err := users.LookupUID(constant.ApiserverUser)
if err != nil {
Expand Down Expand Up @@ -289,7 +287,7 @@ func detectLocalIPs(ctx context.Context) ([]string, error) {
return localIPs, nil
}

func kubeConfig(dest, url, caCert, clientCert, clientKey string, ownerID int) error {
func kubeConfig(dest string, url *url.URL, caCert, clientCert, clientKey string, ownerID int) error {
// We always overwrite the kubeconfigs as the certs might be regenerated at startup
const (
clusterName = "local"
Expand All @@ -300,7 +298,7 @@ func kubeConfig(dest, url, caCert, clientCert, clientKey string, ownerID int) er
kubeconfig, err := clientcmd.Write(clientcmdapi.Config{
Clusters: map[string]*clientcmdapi.Cluster{clusterName: {
// The server URL is replaced in the "k0s kubeconfig admin" subcommand.
Server: url,
Server: url.String(),
CertificateAuthorityData: []byte(caCert),
}},
Contexts: map[string]*clientcmdapi.Context{contextName: {
Expand Down
2 changes: 1 addition & 1 deletion cmd/kubeconfig/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func kubeConfigAdminCmd() *cobra.Command {
if err != nil {
return err
}
internalURL := fmt.Sprintf("https://localhost:%d", nodeConfig.Spec.API.Port)
internalURL := nodeConfig.Spec.API.LocalURL().String()
externalURL := nodeConfig.Spec.API.APIAddressURL()
for _, c := range adminConfig.Clusters {
if c.Server == internalURL {
Expand Down
12 changes: 12 additions & 0 deletions pkg/apis/k0s/v1beta1/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package v1beta1

import (
"encoding/json"
"fmt"
"net"
"net/url"
"strconv"
Expand Down Expand Up @@ -72,6 +73,17 @@ func DefaultAPISpec() *APISpec {
return a
}

func (a *APISpec) LocalURL() *url.URL {
var host string
if a.OnlyBindToAddress {
host = net.JoinHostPort(a.Address, strconv.Itoa(a.Port))
} else {
host = fmt.Sprintf("localhost:%d", a.Port)
Copy link
Member

Choose a reason for hiding this comment

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

Why not use net.JoinHostPort in this case too?

Copy link
Member Author

Choose a reason for hiding this comment

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

We could, but we don't need to. JoinHostPort is only necessary if the host may be an IPv6 address, which, in this case, it isn't. Would you prefer to use it anyways?

}

return &url.URL{Scheme: "https", Host: host}
Comment on lines +77 to +84
Copy link
Collaborator

Choose a reason for hiding this comment

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

It doesn't matter, but....

Suggested change
var host string
if a.OnlyBindToAddress {
host = net.JoinHostPort(a.Address, strconv.Itoa(a.Port))
} else {
host = fmt.Sprintf("localhost:%d", a.Port)
}
return &url.URL{Scheme: "https", Host: host}
host := "localhost"
if a.OnlyBindToAddress {
host = a.Address
}
return &url.URL{Scheme: "https", Host: net.JoinHostPort(host, strconv.Itoa(a.Port))}

}

// APIAddress ...
func (a *APISpec) APIAddress() string {
if a.ExternalAddress != "" {
Expand Down
Loading