kubewharf / kubeadmiral

Multi-Cluster Kubernetes Orchestration
Apache License 2.0
658 stars 89 forks source link

feat: add command-line tool admiralctl join&unjoin #303

Open miraclejzd opened 6 months ago

miraclejzd commented 6 months ago

Summary

Currently, users need to manually create Secret and FederatedCluster resources through YAML files to add member clusters, and this file-based approach is not very convenient. Thanks to the work in #202, the command-line tool admiralctl has taken shape. In this proposal, I am building upon this work by introducing a new subcommand, admiralctl join, corresponding to #260. With this command, users can add member clusters via the command line.

Motivation

With the command-line tool, users can conveniently add member clusters without the need to manually create Secret and FederatedCluster resources.

Proposal

Command line tool framework selection

Similar to #202, utilizing cobra as the framework for the command-line tool.

admiralctl join subcommand

  1. Retrieve the kubeconfig file of the member cluster and extract information such as server endpoint, CA, cert, key, and bearer token.
  2. Validate the legitimacy of the member cluster:
    • Check if the control-plane nodes of the member cluster are Ready.
    • Verify if the member cluster has already joined Kubeadmiral (by checking the existence of the kube-admiral-system namespace and the annotation of it).
  3. Create Secret and FederatedCluster resources through the Clientset.

Command design

./admiralctl join --help
Join a member cluster to Kubeadmiral control plane.

 If the control plane of the member cluster is not ready or has already joined, this command will do nothing.

Examples:
  # Join cluster1 to Kubeadmiral by kubeconfig
  admiralctl join cluster1 --cluster-kubeconfig=<CLUSTER_KUBECONFIG_PATH>

  # Support to use '--api-endpoint' to overwrite the member cluster's api-endpoint
  admiralctl join cluster1 --cluster-kubeconfig=<CLUSTER_KUBECONFIG_PATH> --api-endpoint=<CLUSTER_API_ENDPOINT>

  # Support to use '--use-service-account' to determine whether create a new ServiceAccount when join
  admiralctl join cluster1 --cluster-kubeconfig=<CLUSTER_KUBECONFIG_PATH> --use-service-account=false

  # Support to use '--cluster-context' to specify the context of member cluster
  admiralctl join cluster1 --cluster-kubeconfig=<CLUSTER_KUBECONFIG_PATH> --cluster-context=<CLUSTER_CONTEXT>

Options:
    --api-endpoint='':
        api-endpoint of the member cluster.

    --cluster-context='':
        Context name of member cluster in kubeconfig.

    --cluster-kubeconfig='':
        Path of the member cluster kubeconfig.

    --use-service-account=true:
        Whether creates a new ServiceAccount when join, corresponding to FederatedCluster.spec.useServiceAccount.
        If you set 'false', BearerToken should be in the kubeconfig.

Usage:
  admiralctl join <FCLUSTER_NAME> --cluster-kubeconfig <CLUSTER_KUBECONFIG_PATH> [options]

admiralctl unjoin subcommand

  1. Validate the legitimacy of the member cluster:
    • Verify if the cluster has already joined Kubeadmiral
    • Check if the secret of the FederatedCluster is existed.
  2. Delete the FederatedCluster resource from Kubeadmiral.

Command design

./admiralctl unjoin --help
Unjoin a FederatedCluster from Kubeadmiral federation.

 If the the federated cluster has not joined the federation, this command will do nothing.

Examples:
  # Unjoin cluster1 from Kubeadmiral federation
  admiralctl unjoin cluster1

Usage:
  admiralctl unjoin <FCLUSTER_NAME> [options]