sigs.k8s.io/cluster-api@v1.6.3/cmd/clusterctl/client/cluster/workload_cluster.go (about) 1 /* 2 Copyright 2020 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package cluster 18 19 import ( 20 "context" 21 22 "github.com/pkg/errors" 23 "sigs.k8s.io/controller-runtime/pkg/client" 24 25 utilkubeconfig "sigs.k8s.io/cluster-api/util/kubeconfig" 26 ) 27 28 // WorkloadCluster has methods for fetching kubeconfig of workload cluster from management cluster. 29 type WorkloadCluster interface { 30 // GetKubeconfig returns the kubeconfig of the workload cluster. 31 GetKubeconfig(ctx context.Context, workloadClusterName string, namespace string) (string, error) 32 } 33 34 // workloadCluster implements WorkloadCluster. 35 type workloadCluster struct { 36 proxy Proxy 37 } 38 39 // newWorkloadCluster returns a workloadCluster. 40 func newWorkloadCluster(proxy Proxy) *workloadCluster { 41 return &workloadCluster{ 42 proxy: proxy, 43 } 44 } 45 46 func (p *workloadCluster) GetKubeconfig(ctx context.Context, workloadClusterName string, namespace string) (string, error) { 47 cs, err := p.proxy.NewClient() 48 if err != nil { 49 return "", err 50 } 51 52 obj := client.ObjectKey{ 53 Namespace: namespace, 54 Name: workloadClusterName, 55 } 56 dataBytes, err := utilkubeconfig.FromSecret(ctx, cs, obj) 57 if err != nil { 58 return "", errors.Wrapf(err, "\"%s-kubeconfig\" not found in namespace %q", workloadClusterName, namespace) 59 } 60 return string(dataBytes), nil 61 }