sigs.k8s.io/cluster-api-provider-aws@v1.5.5/cmd/clusterawsadm/controller/helper.go (about)

     1  /*
     2  Copyright 2021 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 controller
    18  
    19  import (
    20  	"encoding/base64"
    21  	"fmt"
    22  	"strings"
    23  
    24  	"github.com/pkg/errors"
    25  	corev1 "k8s.io/api/core/v1"
    26  	"k8s.io/client-go/kubernetes"
    27  	"k8s.io/client-go/tools/clientcmd"
    28  
    29  	"sigs.k8s.io/cluster-api-provider-aws/version"
    30  )
    31  
    32  // BootstrapCredsSecret defines the tag for capa manager bootstrap credentials.
    33  const BootstrapCredsSecret = "capa-manager-bootstrap-credentials"
    34  
    35  // GetClient creates the config for a kubernetes client and returns a client-go client for the cluster.
    36  func GetClient(kubeconfigPath string, kubeconfigContext string) (*kubernetes.Clientset, error) {
    37  	// If a kubeconfig file isn't provided, find one in the standard locations.
    38  	rules := clientcmd.NewDefaultClientConfigLoadingRules()
    39  	if kubeconfigPath != "" {
    40  		rules.ExplicitPath = kubeconfigPath
    41  	}
    42  
    43  	config, err := rules.Load()
    44  	if err != nil {
    45  		return nil, errors.Wrap(err, "failed to load Kubeconfig")
    46  	}
    47  
    48  	configOverrides := &clientcmd.ConfigOverrides{}
    49  	if kubeconfigContext != "" {
    50  		configOverrides.CurrentContext = kubeconfigContext
    51  	}
    52  
    53  	restConfig, err := clientcmd.NewDefaultClientConfig(*config, configOverrides).ClientConfig()
    54  	if err != nil {
    55  		if strings.HasPrefix(err.Error(), "invalid configuration:") {
    56  			return nil, errors.New(strings.Replace(err.Error(), "invalid configuration:", "invalid kubeconfig file; clusterawsadm requires a valid kubeconfig file to connect to the management cluster:", 1))
    57  		}
    58  		return nil, err
    59  	}
    60  	restConfig.UserAgent = fmt.Sprintf("clusterawsadm/%s (%s)", version.Get().GitVersion, version.Get().Platform)
    61  
    62  	// Get a client-go client for the cluster
    63  	cs, err := kubernetes.NewForConfig(restConfig)
    64  	return cs, err
    65  }
    66  
    67  // PrintBootstrapCredentials will print the bootstrap credentials.
    68  func PrintBootstrapCredentials(secret *corev1.Secret) {
    69  	if creds, ok := secret.Data["credentials"]; ok {
    70  		if base64.StdEncoding.EncodeToString(creds) == "Cg==" {
    71  			fmt.Println("Credentials are zeroed")
    72  		} else {
    73  			fmt.Print(string(creds))
    74  		}
    75  	}
    76  }