...

Source file src/github.com/linkerd/linkerd2/multicluster/cmd/root.go

Documentation: github.com/linkerd/linkerd2/multicluster/cmd

     1  package cmd
     2  
     3  import (
     4  	"context"
     5  	"regexp"
     6  
     7  	"github.com/fatih/color"
     8  	"github.com/linkerd/linkerd2/pkg/charts/linkerd2"
     9  	pkgcmd "github.com/linkerd/linkerd2/pkg/cmd"
    10  	"github.com/linkerd/linkerd2/pkg/healthcheck"
    11  	"github.com/linkerd/linkerd2/pkg/k8s"
    12  	log "github.com/sirupsen/logrus"
    13  	"github.com/spf13/cobra"
    14  )
    15  
    16  const (
    17  	defaultLinkerdNamespace              = "linkerd"
    18  	defaultMulticlusterNamespace         = "linkerd-multicluster"
    19  	defaultGatewayName                   = "linkerd-gateway"
    20  	helmMulticlusterDefaultChartName     = "linkerd-multicluster"
    21  	helmMulticlusterLinkDefaultChartName = "linkerd-multicluster-link"
    22  	tokenKey                             = "token"
    23  
    24  	saNameAnnotationKey       = "kubernetes.io/service-account.name"
    25  	defaultServiceAccountName = "linkerd-service-mirror-remote-access-default"
    26  )
    27  
    28  var (
    29  	apiAddr               string // An empty value means "use the Kubernetes configuration"
    30  	controlPlaneNamespace string
    31  	kubeconfigPath        string
    32  	kubeContext           string
    33  	impersonate           string
    34  	impersonateGroup      []string
    35  	verbose               bool
    36  
    37  	// special handling for Windows, on all other platforms these resolve to
    38  	// os.Stdout and os.Stderr, thanks to https://github.com/mattn/go-colorable
    39  	stdout = color.Output
    40  	stderr = color.Error
    41  
    42  	// These regexs are not as strict as they could be, but are a quick and dirty
    43  	// sanity check against illegal characters.
    44  	alphaNumDashDot = regexp.MustCompile(`^[\.a-zA-Z0-9-]+$`)
    45  )
    46  
    47  // NewCmdMulticluster returns a new multicluster command
    48  func NewCmdMulticluster() *cobra.Command {
    49  
    50  	multiclusterCmd := &cobra.Command{
    51  		Use:     "multicluster [flags]",
    52  		Aliases: []string{"mc"},
    53  		Args:    cobra.NoArgs,
    54  		Short:   "Manages the multicluster setup for Linkerd",
    55  		Long: `Manages the multicluster setup for Linkerd.
    56  
    57  This command provides subcommands to manage the multicluster support
    58  functionality of Linkerd. You can use it to install the service mirror
    59  components on a cluster, manage credentials and link clusters together.`,
    60  		Example: `  # Install multicluster addons.
    61    linkerd --context=cluster-a multicluster install | kubectl --context=cluster-a apply -f -
    62  
    63    # Extract mirroring cluster credentials from cluster A and install them on cluster B
    64    linkerd --context=cluster-a multicluster link --cluster-name=target | kubectl apply --context=cluster-b -f -`,
    65  		PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
    66  			if verbose {
    67  				log.SetLevel(log.DebugLevel)
    68  			} else {
    69  				log.SetLevel(log.PanicLevel)
    70  			}
    71  			return nil
    72  		},
    73  	}
    74  
    75  	multiclusterCmd.PersistentFlags().StringVarP(&controlPlaneNamespace, "linkerd-namespace", "L", defaultLinkerdNamespace, "Namespace in which Linkerd is installed")
    76  	multiclusterCmd.PersistentFlags().StringVar(&kubeconfigPath, "kubeconfig", "", "Path to the kubeconfig file to use for CLI requests")
    77  	multiclusterCmd.PersistentFlags().StringVar(&kubeContext, "context", "", "Name of the kubeconfig context to use")
    78  	multiclusterCmd.PersistentFlags().StringVar(&impersonate, "as", "", "Username to impersonate for Kubernetes operations")
    79  	multiclusterCmd.PersistentFlags().StringArrayVar(&impersonateGroup, "as-group", []string{}, "Group to impersonate for Kubernetes operations")
    80  	multiclusterCmd.PersistentFlags().StringVar(&apiAddr, "api-addr", "", "Override kubeconfig and communicate directly with the control plane at host:port (mostly for testing)")
    81  	multiclusterCmd.PersistentFlags().BoolVar(&verbose, "verbose", false, "Turn on debug logging")
    82  	multiclusterCmd.AddCommand(newLinkCommand())
    83  	multiclusterCmd.AddCommand(newUnlinkCommand())
    84  	multiclusterCmd.AddCommand(newMulticlusterInstallCommand())
    85  	multiclusterCmd.AddCommand(NewCmdCheck())
    86  	multiclusterCmd.AddCommand(newMulticlusterUninstallCommand())
    87  	multiclusterCmd.AddCommand(newGatewaysCommand())
    88  	multiclusterCmd.AddCommand(newAllowCommand())
    89  
    90  	// resource-aware completion flag configurations
    91  	pkgcmd.ConfigureNamespaceFlagCompletion(
    92  		multiclusterCmd, []string{"linkerd-namespace"},
    93  		kubeconfigPath, impersonate, impersonateGroup, kubeContext)
    94  
    95  	pkgcmd.ConfigureKubeContextFlagCompletion(multiclusterCmd, kubeconfigPath)
    96  	return multiclusterCmd
    97  }
    98  
    99  func getLinkerdConfigMap(ctx context.Context) (*linkerd2.Values, error) {
   100  	kubeAPI, err := k8s.NewAPI(kubeconfigPath, kubeContext, impersonate, impersonateGroup, 0)
   101  	if err != nil {
   102  		return nil, err
   103  	}
   104  
   105  	_, values, err := healthcheck.FetchCurrentConfiguration(ctx, kubeAPI, controlPlaneNamespace)
   106  	if err != nil {
   107  		return nil, err
   108  	}
   109  
   110  	return values, nil
   111  }
   112  

View as plain text