diff --git a/pkg/k8s/kubeconfig/builder.go b/pkg/k8s/kubeconfig/builder.go index 2dc76b1a6..f75318f6a 100644 --- a/pkg/k8s/kubeconfig/builder.go +++ b/pkg/k8s/kubeconfig/builder.go @@ -97,8 +97,29 @@ func (b *Builder) Build() Interface { } func (b *Builder) build() ([]byte, error) { + // Set current context to clustername if it was provided + var result *api.Config + var err error + var kubeContext string + if b.clusterName != "" { + kubeContext, result, err = b.buildOne(b.clusterName) + if err != nil { + return nil, err + } + } else { + result, err = b.builtSiteKubeconf() + if err != nil { + return nil, err + } + } + b.siteKubeconf.CurrentContext = kubeContext + return clientcmd.Write(*result) +} + +func (b *Builder) builtSiteKubeconf() (*api.Config, error) { + log.Debugf("Getting site kubeconfig") for _, clusterID := range b.clusterMap.AllClusters() { - log.Printf("Getting kubeconfig for cluster '%s'", clusterID) + log.Debugf("Getting kubeconfig for cluster '%s' to build site kubeconfig", clusterID) // buildOne merges context into site kubeconfig _, _, err := b.buildOne(clusterID) if IsErrAllSourcesFailedErr(err) { @@ -109,15 +130,7 @@ func (b *Builder) build() ([]byte, error) { return nil, err } } - // Set current context to clustername if it was provided - if b.clusterName != "" { - kubeContext, err := b.clusterMap.ClusterKubeconfigContext(b.clusterName) - if err != nil { - return nil, err - } - b.siteKubeconf.CurrentContext = kubeContext - } - return clientcmd.Write(*b.siteKubeconf) + return b.siteKubeconf, nil } func (b *Builder) buildOne(clusterID string) (string, *api.Config, error) { diff --git a/pkg/k8s/kubeconfig/builder_test.go b/pkg/k8s/kubeconfig/builder_test.go index a28f10c7f..125bf0913 100644 --- a/pkg/k8s/kubeconfig/builder_test.go +++ b/pkg/k8s/kubeconfig/builder_test.go @@ -100,11 +100,10 @@ func TestBuilderClusterctl(t *testing.T) { fs fs.FileSystem }{ { - name: "success cluster-api not reachable", - requestedClusterName: childClusterID, - expectedContexts: []string{parentClusterID}, - expectedClusters: []string{parentParentCluster}, - expectedAuthInfos: []string{parentParentUser}, + name: "success cluster-api not reachable", + expectedContexts: []string{parentClusterID}, + expectedClusters: []string{parentParentCluster}, + expectedAuthInfos: []string{parentParentUser}, clusterMap: clustermap.NewClusterMap(&v1alpha1.ClusterMap{ Map: map[string]*v1alpha1.Cluster{ childClusterID: { diff --git a/pkg/phase/client.go b/pkg/phase/client.go index a5bbc3295..9063235b2 100644 --- a/pkg/phase/client.go +++ b/pkg/phase/client.go @@ -102,7 +102,6 @@ func (p *phase) Executor() (ifc.Executor, error) { kubeconf := kubeconfig.NewBuilder(). WithBundle(p.helper.PhaseBundleRoot()). WithClusterMap(cMap). - WithClusterName(p.apiObj.ClusterName). WithTempRoot(wd). WithClusterctClient(cctlClient). Build()