Skip to content

Commit

Permalink
Merge pull request #5426 from twz123/admin-kubeconfig-localhost
Browse files Browse the repository at this point in the history
Use localhost in admin kubeconfig again, if possible
  • Loading branch information
twz123 authored Jan 14, 2025
2 parents b1497f5 + 93b9f4c commit d71ff7b
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 6 deletions.
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)
}

return &url.URL{Scheme: "https", Host: host}
}

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

0 comments on commit d71ff7b

Please sign in to comment.