diff --git a/README.md b/README.md index d8c3e415..7e6ff3ea 100644 --- a/README.md +++ b/README.md @@ -8,12 +8,12 @@ It can help users quickly complete the personalized configuration of the K3s clu ## Design Ideas This tool uses the cloud provider's SDK to create and manage hosts, and then uses SSH to install the K3s cluster to the remote host. -You can also use it to join hosts as masters/agents to the K3s cluster. It will automatically merge and store the `kubeconfig` in `/var/lib/rancher/autok3s/.kube/config` which necessary for user to access the cluster. +You can also use it to join hosts as masters/agents to the K3s cluster. It will automatically merge and store the `kubeconfig` in `$HOME/.autok3s/.kube/config` which necessary for user to access the cluster. Then user can use `autok3s kubectl` command quickly access the cluster. -Use [viper](https://github.com/spf13/viper) to bind flags and configuration file. `autok3s` will generate a configuration file to store cloud-providers' access information at the specified location(`/var/lib/rancher/autok3s/config.yaml`) to reduce the number of flags to be passed for multiple runs. +Use [viper](https://github.com/spf13/viper) to bind flags and configuration file. `autok3s` will generate a configuration file to store cloud-providers' access information at the specified location(`$HOME/.autok3s/config.yaml`) to reduce the number of flags to be passed for multiple runs. -It's also generated a state file `/var/lib/rancher/autok3s/.state` to record the clusters' information created on this host. +It's also generated a state file `$HOME/.autok3s/.state` to record the clusters' information created on this host. ## Providers - alibaba @@ -22,9 +22,9 @@ It's also generated a state file `/var/lib/rancher/autok3s/.state` to record the User can get the commands available for different providers according to the `--provider --help`. ### Setup K3s Cluster -If already have access information in `/var/lib/rancher/autok3s/config.yaml` you can use the simplified command. +If already have access information in `$HOME/.autok3s/config.yaml` you can use the simplified command. ```bash -sudo autok3s create \ +autok3s create \ --provider alibaba \ --region \ --name \ @@ -37,7 +37,7 @@ sudo autok3s create \ Generic commands can be used anywhere. ```bash -sudo autok3s create \ +autok3s create \ --provider alibaba \ --region \ --name \ @@ -52,13 +52,13 @@ sudo autok3s create \ HA(embedded etcd: >= 1.19.1-k3s1) mode need `--master` at least 3 master nodes, e.g. ```bash -sudo autok3s ... \ +autok3s ... \ --master 3 ``` HA(external database) mode need `--master` greater than 1 node, also need to specify `--datastore`, e.g. ```bash -sudo autok3s ... \ +autok3s ... \ --master 2 \ --datastore "mysql://:@tcp(:)/" ``` @@ -66,7 +66,7 @@ sudo autok3s ... \ ### Join K3s Nodes If you have ever created a cluster using `autok3s` on your current machine, you can use the simplified command. ```bash -sudo autok3s join \ +autok3s join \ --provider alibaba \ --region \ --name \ @@ -76,7 +76,7 @@ sudo autok3s join \ Generic commands can be used anywhere. ```bash -sudo autok3s join \ +autok3s join \ --provider alibaba \ --region \ --name \ @@ -92,13 +92,13 @@ sudo autok3s join \ Join master nodes to (embedded etcd: >= 1.19.1-k3s1) HA cluster e.g. ```bash -sudo autok3s ... \ +autok3s ... \ --master 2 ``` Join master nodes to (external database) HA cluster, also need to specify `--datastore`, e.g. ```bash -sudo autok3s ... \ +autok3s ... \ --master 2 \ --datastore "mysql://:@tcp(:)/" ``` @@ -106,7 +106,7 @@ sudo autok3s ... \ ### Start K3s Cluster If you have ever created a cluster using `autok3s` on your current machine, you can use the simplified command. ```bash -sudo autok3s start \ +autok3s start \ --provider alibaba \ --region \ --name @@ -114,7 +114,7 @@ sudo autok3s start \ Generic commands can be used anywhere. ```bash -sudo autok3s start \ +autok3s start \ --provider alibaba \ --region \ --name \ @@ -125,7 +125,7 @@ sudo autok3s start \ ### Stop K3s Cluster If you have ever created a cluster using `autok3s` on your current machine, you can use the simplified command. ```bash -sudo autok3s stop \ +autok3s stop \ --provider alibaba \ --region \ --name @@ -133,7 +133,7 @@ sudo autok3s stop \ Generic commands can be used anywhere. ```bash -sudo autok3s stop \ +autok3s stop \ --provider alibaba \ --region \ --name \ @@ -144,7 +144,7 @@ sudo autok3s stop \ ### Delete K3s Cluster If you have ever created a cluster using `autok3s` on your current machine, you can use the simplified command. ```bash -sudo autok3s delete \ +autok3s delete \ --provider alibaba \ --region \ --name @@ -152,7 +152,7 @@ sudo autok3s delete \ Generic commands can be used anywhere. ```bash -sudo autok3s delete \ +autok3s delete \ --provider alibaba \ --region \ --name \ @@ -163,25 +163,25 @@ sudo autok3s delete \ ### List K3s Clusters This command will list the clusters that you have created on this machine. ```bash -sudo autok3s list +autok3s list ``` ### Access K3s Cluster After the cluster created, `autok3s` will automatically merge the `kubeconfig` which necessary for us to access the cluster. ```bash -sudo autok3s kubectl +autok3s kubectl ``` In the scenario of multiple clusters, the access to different clusters can be completed by switching context. ```bash -sudo autok3s kubectl config get-contexts -sudo autok3s kubectl config use-context +autok3s kubectl config get-contexts +autok3s kubectl config use-context ``` ### SSH K3s Cluster's Node If you have ever created a cluster using `autok3s` on your current machine, you can use the simplified command. ```bash -sudo autok3s ssh \ +autok3s ssh \ --provider alibaba \ --region \ --name @@ -189,7 +189,7 @@ sudo autok3s ssh \ Generic commands can be used anywhere. ```bash -sudo autok3s ssh \ +autok3s ssh \ --provider alibaba \ --region \ --name \ diff --git a/cmd/cmd.go b/cmd/cmd.go index 3ff20f48..cb7e7765 100644 --- a/cmd/cmd.go +++ b/cmd/cmd.go @@ -41,13 +41,6 @@ func init() { } func Command() *cobra.Command { - cmd.PreRun = func(cmd *cobra.Command, args []string) { - if os.Getuid() != 0 { - logrus.Errorf("%s: need to be root", os.Args[0]) - os.Exit(1) - } - } - cmd.Run = func(cmd *cobra.Command, args []string) { printASCII() if err := cmd.Help(); err != nil { @@ -59,11 +52,6 @@ func Command() *cobra.Command { } func initCfg() { - if os.Getuid() != 0 { - logrus.Errorf("%s: need to be root", os.Args[0]) - os.Exit(1) - } - viper.SetConfigType("yaml") viper.SetConfigFile(fmt.Sprintf("%s/%s", common.CfgPath, common.ConfigFile)) viper.AutomaticEnv() diff --git a/pkg/common/common.go b/pkg/common/common.go index f4901784..8e14ec45 100644 --- a/pkg/common/common.go +++ b/pkg/common/common.go @@ -3,6 +3,8 @@ package common import ( "time" + "github.com/cnrancher/autok3s/pkg/utils" + "k8s.io/apimachinery/pkg/util/wait" ) @@ -20,7 +22,7 @@ const ( var ( Debug = false - CfgPath = "/var/lib/rancher/autok3s" + CfgPath = utils.UserHome() + "/.autok3s" Backoff = wait.Backoff{ Duration: 30 * time.Second, Factor: 1, diff --git a/pkg/providers/alibaba/alibaba.go b/pkg/providers/alibaba/alibaba.go index f8436d58..9e732ae6 100644 --- a/pkg/providers/alibaba/alibaba.go +++ b/pkg/providers/alibaba/alibaba.go @@ -44,8 +44,8 @@ const ( eipStatusAvailable = "Available" eipStatusInUse = "InUse" usageInfo = `=========================== Prompt Info =========================== -Use 'sudo autok3s kubectl config use-context %s' -Use 'sudo autok3s kubectl get pods -A' get POD status` +Use 'autok3s kubectl config use-context %s' +Use 'autok3s kubectl get pods -A' get POD status` ) // ProviderName is the name of this provider.