Skip to content

Commit

Permalink
init: overwrite kubeconfig address
Browse files Browse the repository at this point in the history
  • Loading branch information
3u13r committed Sep 28, 2023
1 parent 4a66899 commit 10bb339
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 6 deletions.
3 changes: 3 additions & 0 deletions cli/internal/cmd/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ go_library(
"@io_k8s_apiextensions_apiserver//pkg/apis/apiextensions/v1:apiextensions",
"@io_k8s_apimachinery//pkg/runtime",
"@io_k8s_client_go//tools/clientcmd",
"@io_k8s_client_go//tools/clientcmd/api",
"@io_k8s_client_go//tools/clientcmd/api/latest",
"@io_k8s_sigs_yaml//:yaml",
"@org_golang_google_grpc//:go_default_library",
Expand Down Expand Up @@ -172,6 +173,8 @@ go_test(
"@io_k8s_api//core/v1:core",
"@io_k8s_apiextensions_apiserver//pkg/apis/apiextensions/v1:apiextensions",
"@io_k8s_apimachinery//pkg/apis/meta/v1:meta",
"@io_k8s_client_go//tools/clientcmd/api",
"@io_k8s_sigs_yaml//:yaml",
"@org_golang_google_grpc//:go_default_library",
"@org_golang_google_grpc//codes",
"@org_golang_google_grpc//status",
Expand Down
18 changes: 17 additions & 1 deletion cli/internal/cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -472,7 +472,23 @@ func (i *initCmd) writeOutput(
tw.Flush()
fmt.Fprintln(wr)

if err := i.fileHandler.Write(constants.AdminConfFilename, initResp.GetKubeconfig(), file.OptNone); err != nil {
i.log.Debugf("Rewriting cluster server address in kubeconfig to %s", idFile.IP)
kubeconfig, err := clientcmd.Load(initResp.GetKubeconfig())
if err != nil {
return fmt.Errorf("loading kubeconfig: %w", err)
}
if len(kubeconfig.Clusters) != 1 {
return fmt.Errorf("expected exactly one cluster in kubeconfig, got %d", len(kubeconfig.Clusters))
}
for _, cluster := range kubeconfig.Clusters {
cluster.Server = "https://" + net.JoinHostPort(idFile.IP, strconv.Itoa(constants.KubernetesPort))
}
kubeconfigBytes, err := clientcmd.Write(*kubeconfig)
if err != nil {
return fmt.Errorf("marshaling kubeconfig: %w", err)
}

if err := i.fileHandler.Write(constants.AdminConfFilename, kubeconfigBytes, file.OptNone); err != nil {
return fmt.Errorf("writing kubeconfig: %w", err)
}
i.log.Debugf("Kubeconfig written to %s", i.pf.PrefixPrintablePath(constants.AdminConfFilename))
Expand Down
35 changes: 30 additions & 5 deletions cli/internal/cmd/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"google.golang.org/grpc"
"k8s.io/client-go/tools/clientcmd"
k8sclientapi "k8s.io/client-go/tools/clientcmd/api"
)

func TestInitArgumentValidation(t *testing.T) {
Expand Down Expand Up @@ -339,12 +341,34 @@ func TestWriteOutput(t *testing.T) {
assert := assert.New(t)
require := require.New(t)

clusterEndpoint := "cluster-endpoint"

expectedKubeconfig := k8sclientapi.Config{
Clusters: map[string]*k8sclientapi.Cluster{
"cluster": {
Server: "https://" + clusterEndpoint + ":6443",
},
},
}
expectedKubeconfigBytes, err := clientcmd.Write(expectedKubeconfig)
require.NoError(err)

respKubeconfig := k8sclientapi.Config{
Clusters: map[string]*k8sclientapi.Cluster{
"cluster": {
Server: "https://192.0.2.1:6443",
},
},
}
respKubeconfigBytes, err := clientcmd.Write(respKubeconfig)
require.NoError(err)

resp := &initproto.InitResponse{
Kind: &initproto.InitResponse_InitSuccess{
InitSuccess: &initproto.InitSuccessResponse{
OwnerId: []byte("ownerID"),
ClusterId: []byte("clusterID"),
Kubeconfig: []byte("kubeconfig"),
Kubeconfig: respKubeconfigBytes,
},
},
}
Expand All @@ -355,7 +379,7 @@ func TestWriteOutput(t *testing.T) {
expectedIDFile := clusterid.File{
ClusterID: clusterID,
OwnerID: ownerID,
IP: "cluster-ip",
IP: clusterEndpoint,
UID: "test-uid",
}

Expand All @@ -365,10 +389,10 @@ func TestWriteOutput(t *testing.T) {

idFile := clusterid.File{
UID: "test-uid",
IP: "cluster-ip",
IP: clusterEndpoint,
}
i := newInitCmd(nil, fileHandler, &nopSpinner{}, &stubMerger{}, logger.NewTest(t))
err := i.writeOutput(idFile, resp.GetInitSuccess(), false, &out)
err = i.writeOutput(idFile, resp.GetInitSuccess(), false, &out)
require.NoError(err)
// assert.Contains(out.String(), ownerID)
assert.Contains(out.String(), clusterID)
Expand All @@ -377,7 +401,8 @@ func TestWriteOutput(t *testing.T) {
afs := afero.Afero{Fs: testFs}
adminConf, err := afs.ReadFile(constants.AdminConfFilename)
assert.NoError(err)
assert.Equal(string(resp.GetInitSuccess().GetKubeconfig()), string(adminConf))
assert.Contains(string(adminConf), clusterEndpoint)
assert.Equal(string(expectedKubeconfigBytes), string(adminConf))

idsFile, err := afs.ReadFile(constants.ClusterIDsFilename)
assert.NoError(err)
Expand Down

0 comments on commit 10bb339

Please sign in to comment.