sigs.k8s.io/cluster-api@v1.7.1/cmd/clusterctl/client/get_kubeconfig.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 client 18 19 import ( 20 "context" 21 22 "github.com/pkg/errors" 23 ) 24 25 // GetKubeconfigOptions carries all the options supported by GetKubeconfig. 26 type GetKubeconfigOptions struct { 27 // Kubeconfig defines the kubeconfig to use for accessing the management cluster. If empty, 28 // default rules for kubeconfig discovery will be used. 29 Kubeconfig Kubeconfig 30 31 // Namespace is the namespace in which secret is placed. 32 Namespace string 33 34 // WorkloadClusterName is the name of the workload cluster. 35 WorkloadClusterName string 36 } 37 38 func (c *clusterctlClient) GetKubeconfig(ctx context.Context, options GetKubeconfigOptions) (string, error) { 39 // gets access to the management cluster 40 clusterClient, err := c.clusterClientFactory(ClusterClientFactoryInput{Kubeconfig: options.Kubeconfig}) 41 if err != nil { 42 return "", err 43 } 44 45 // Ensure this command only runs against management clusters with the current Cluster API contract. 46 if err := clusterClient.ProviderInventory().CheckCAPIContract(ctx); err != nil { 47 return "", err 48 } 49 50 if options.Namespace == "" { 51 currentNamespace, err := clusterClient.Proxy().CurrentNamespace() 52 if err != nil { 53 return "", err 54 } 55 if currentNamespace == "" { 56 return "", errors.New("failed to identify the current namespace. Please specify the namespace where the workload cluster exists") 57 } 58 options.Namespace = currentNamespace 59 } 60 61 return clusterClient.WorkloadCluster().GetKubeconfig(ctx, options.WorkloadClusterName, options.Namespace) 62 }