diff --git a/vsctl/command/cluster.go b/vsctl/command/cluster.go index d1484a0a4..fd15c55ff 100644 --- a/vsctl/command/cluster.go +++ b/vsctl/command/cluster.go @@ -170,12 +170,9 @@ func createClusterCommand() *cobra.Command { Use: "create", Short: "create a cluster", Run: func(cmd *cobra.Command, args []string) { - operatorEndpoint, err := cmd.Flags().GetString("operator-endpoint") + operatorEndpoint, err := getOperatorEndpoint() if err != nil { - operatorEndpoint, err = getOperatorEndpoint() - if err != nil { - cmdFailedf(cmd, "get operator endpoint failed: %s", err) - } + cmdFailedf(cmd, "get operator endpoint failed: %s", err) } if showInstallableList { @@ -251,6 +248,7 @@ func createClusterCommand() *cobra.Command { } clusterspec := table.NewWriter() + clusterspec.SetCaption("The Endpoints Of Vanus Operator: %s\n", operatorEndpoint) clusterspec.AppendHeader(table.Row{"Cluster", "Version", "Component", "Replicas", "StorageSize", "StorageClass"}) if c.Etcd.StorageClass == nil { clusterspec.AppendRow(table.Row{"vanus", *c.Version, "etcd", *c.Etcd.Replicas, *c.Etcd.StorageSize, "-"}) @@ -351,12 +349,9 @@ func deleteClusterCommand() *cobra.Command { Use: "delete", Short: "delete a cluster", Run: func(cmd *cobra.Command, args []string) { - operatorEndpoint, err := cmd.Flags().GetString("operator-endpoint") + operatorEndpoint, err := getOperatorEndpoint() if err != nil { - operatorEndpoint, err = getOperatorEndpoint() - if err != nil { - cmdFailedf(cmd, "get operator endpoint failed: %s", err) - } + cmdFailedf(cmd, "get operator endpoint failed: %s", err) } fmt.Print("Deleting a cluster will lose all cluster data and can't be recovered, do you still want to delete the vanus cluster(y/n):") @@ -465,12 +460,9 @@ func upgradeClusterCommand() *cobra.Command { Use: "upgrade", Short: "upgrade cluster", Run: func(cmd *cobra.Command, args []string) { - operatorEndpoint, err := cmd.Flags().GetString("operator-endpoint") + operatorEndpoint, err := getOperatorEndpoint() if err != nil { - operatorEndpoint, err = getOperatorEndpoint() - if err != nil { - cmdFailedf(cmd, "get operator endpoint failed: %s", err) - } + cmdFailedf(cmd, "get operator endpoint failed: %s", err) } if showUpgradeableList { @@ -588,12 +580,9 @@ func scaleStoreReplicas() *cobra.Command { Use: "store", Short: "scale store replicas", Run: func(cmd *cobra.Command, args []string) { - operatorEndpoint, err := cmd.Flags().GetString("operator-endpoint") + operatorEndpoint, err := getOperatorEndpoint() if err != nil { - operatorEndpoint, err = getOperatorEndpoint() - if err != nil { - cmdFailedf(cmd, "get operator endpoint failed: %s", err) - } + cmdFailedf(cmd, "get operator endpoint failed: %s", err) } client := &http.Client{} @@ -651,7 +640,8 @@ func scaleStoreReplicas() *cobra.Command { } }, } - cmd.Flags().Int32Var(&storeReplicas, "replicas", 3, "replicas") + cmd.Flags().Int32Var(&storeReplicas, "replicas", 0, "the replicas of store") + cobra.MarkFlagRequired(cmd.Flags(), "replicas") return cmd } @@ -660,12 +650,9 @@ func scaleTriggerReplicas() *cobra.Command { Use: "trigger", Short: "scale trigger replicas", Run: func(cmd *cobra.Command, args []string) { - operatorEndpoint, err := cmd.Flags().GetString("operator-endpoint") + operatorEndpoint, err := getOperatorEndpoint() if err != nil { - operatorEndpoint, err = getOperatorEndpoint() - if err != nil { - cmdFailedf(cmd, "get operator endpoint failed: %s", err) - } + cmdFailedf(cmd, "get operator endpoint failed: %s", err) } client := &http.Client{} @@ -723,7 +710,8 @@ func scaleTriggerReplicas() *cobra.Command { } }, } - cmd.Flags().Int32Var(&triggerReplicas, "replicas", 3, "replicas") + cmd.Flags().Int32Var(&triggerReplicas, "replicas", 0, "the replicas of trigger") + cobra.MarkFlagRequired(cmd.Flags(), "replicas") return cmd } @@ -732,12 +720,9 @@ func getClusterCommand() *cobra.Command { Use: "status", Short: "get cluster status", Run: func(cmd *cobra.Command, args []string) { - operatorEndpoint, err := cmd.Flags().GetString("operator-endpoint") + operatorEndpoint, err := getOperatorEndpoint() if err != nil { - operatorEndpoint, err = getOperatorEndpoint() - if err != nil { - cmdFailedf(cmd, "get operator endpoint failed: %s", err) - } + cmdFailedf(cmd, "get operator endpoint failed: %s", err) } client := &http.Client{} diff --git a/vsctl/command/connector.go b/vsctl/command/connector.go index 0b36457e3..98fc5b1a5 100644 --- a/vsctl/command/connector.go +++ b/vsctl/command/connector.go @@ -102,12 +102,9 @@ func installConnectorCommand() *cobra.Command { Use: "install", Short: "install a connector", Run: func(cmd *cobra.Command, args []string) { - operatorEndpoint, err := cmd.Flags().GetString("operator-endpoint") + operatorEndpoint, err := getOperatorEndpoint() if err != nil { - operatorEndpoint, err = getOperatorEndpoint() - if err != nil { - cmdFailedf(cmd, "get operator endpoint failed: %s", err) - } + cmdFailedf(cmd, "get operator endpoint failed: %s", err) } if showConnectors { @@ -255,16 +252,9 @@ func uninstallConnectorCommand() *cobra.Command { Use: "uninstall", Short: "uninstall a connector", Run: func(cmd *cobra.Command, args []string) { - operatorEndpoint, err := cmd.Flags().GetString("operator-endpoint") + operatorEndpoint, err := getOperatorEndpoint() if err != nil { - operatorEndpoint, err = getOperatorEndpoint() - if err != nil { - cmdFailedf(cmd, "get operator endpoint failed: %s", err) - } - } - - if name == "" { - cmdFailedf(cmd, "the --name flag MUST be set") + cmdFailedf(cmd, "get operator endpoint failed: %s", err) } client := &http.Client{} @@ -316,6 +306,7 @@ func uninstallConnectorCommand() *cobra.Command { }, } cmd.Flags().StringVar(&name, "name", "", "connector name") + cobra.MarkFlagRequired(cmd.Flags(), "name") return cmd } @@ -324,12 +315,9 @@ func listConnectorCommand() *cobra.Command { Use: "list", Short: "list connectors", Run: func(cmd *cobra.Command, args []string) { - operatorEndpoint, err := cmd.Flags().GetString("operator-endpoint") + operatorEndpoint, err := getOperatorEndpoint() if err != nil { - operatorEndpoint, err = getOperatorEndpoint() - if err != nil { - cmdFailedf(cmd, "get operator endpoint failed: %s", err) - } + cmdFailedf(cmd, "get operator endpoint failed: %s", err) } if !operatorIsDeployed(cmd, operatorEndpoint) { @@ -401,16 +389,9 @@ func getConnectorCommand() *cobra.Command { Use: "info", Short: "get connector info", Run: func(cmd *cobra.Command, args []string) { - operatorEndpoint, err := cmd.Flags().GetString("operator-endpoint") + operatorEndpoint, err := getOperatorEndpoint() if err != nil { - operatorEndpoint, err = getOperatorEndpoint() - if err != nil { - cmdFailedf(cmd, "get operator endpoint failed: %s", err) - } - } - - if name == "" { - cmdFailedf(cmd, "the --name flag MUST be set") + cmdFailedf(cmd, "get operator endpoint failed: %s", err) } client := &http.Client{} @@ -471,6 +452,7 @@ func getConnectorCommand() *cobra.Command { }, } cmd.Flags().StringVar(&name, "name", "", "connector name") + cobra.MarkFlagRequired(cmd.Flags(), "name") return cmd } diff --git a/vsctl/command/global.go b/vsctl/command/global.go index 5d21d4ec7..ae929f7fd 100644 --- a/vsctl/command/global.go +++ b/vsctl/command/global.go @@ -40,7 +40,8 @@ const ( const ( RespCodeOK int32 = 200 - DefaultOperatorPort = 30009 + DefaultGatewayPort = 8080 + DefaultOperatorPort = 8089 HttpPrefix = "http://" BaseUrl = "/api/v1" ) @@ -102,12 +103,10 @@ const ( var retryTime = 30 type GlobalFlags struct { - Endpoint string - OperatorEndpoint string - Debug bool - ConfigFile string - Format string - Token string + Debug bool + ConfigFile string + Format string + Token string } var ( @@ -116,7 +115,7 @@ var ( ) func InitGatewayClient(cmd *cobra.Command) { - endpoint, err := cmd.Flags().GetString("endpoint") + endpoint, err := getGatewayEndpoint() if err != nil { cmdFailedf(cmd, "get gateway endpoint failed: %s", err) } @@ -148,17 +147,13 @@ func DestroyGatewayClient() { } func mustGetGatewayCloudEventsEndpoint(cmd *cobra.Command) string { - //res, err := client.ClusterInfo(context.Background(), &emptypb.Empty{}) - //if err != nil { - // cmdFailedf(cmd, "get cloudevents endpoint failed: %s", err) - //} sp := strings.Split(mustGetGatewayEndpoint(cmd), ":") v, _ := strconv.ParseInt(sp[1], 10, 64) return fmt.Sprintf("%s:%d", sp[0], v+1) } func mustGetGatewayEndpoint(cmd *cobra.Command) string { - endpoint, err := cmd.Flags().GetString("endpoint") + endpoint, err := getGatewayEndpoint() if err != nil { cmdFailedf(cmd, "get gateway endpoint failed: %s", err) } diff --git a/vsctl/command/util.go b/vsctl/command/util.go index 2e0a6ffb2..c3c36833e 100644 --- a/vsctl/command/util.go +++ b/vsctl/command/util.go @@ -93,12 +93,26 @@ func operatorIsDeployed(cmd *cobra.Command, endpoint string) bool { return true } +func getGatewayEndpoint() (string, error) { + if os.Getenv("VANUS_GATEWAY") != "" { + return os.Getenv("VANUS_GATEWAY"), nil + } + hostname, err := exec.Command("bash", "-c", "kubectl -n vanus get svc vanus-gateway -o jsonpath='{.status.loadBalancer.ingress[*].hostname}'").Output() + if err != nil { + return "", err + } + return fmt.Sprintf("%s:%d", strings.Trim(string(hostname), "\n"), DefaultGatewayPort), nil +} + func getOperatorEndpoint() (string, error) { - nodeip, err := exec.Command("bash", "-c", "kubectl get no --no-headers -o wide | awk '{print $6}' | head -n 1").Output() + if os.Getenv("VANUS_OPERATOR") != "" { + return os.Getenv("VANUS_OPERATOR"), nil + } + hostname, err := exec.Command("bash", "-c", "kubectl -n vanus get svc vanus-operator -o jsonpath='{.status.loadBalancer.ingress[*].hostname}'").Output() if err != nil { return "", err } - return fmt.Sprintf("%s:%d", strings.Trim(string(nodeip), "\n"), DefaultOperatorPort), nil + return fmt.Sprintf("%s:%d", strings.Trim(string(hostname), "\n"), DefaultOperatorPort), nil } func LoadConfig(filename string, config interface{}) error { diff --git a/vsctl/main.go b/vsctl/main.go index 8c264db66..b9a46ded9 100644 --- a/vsctl/main.go +++ b/vsctl/main.go @@ -41,10 +41,6 @@ var ( func init() { cobra.EnablePrefixMatching = true cobra.EnableCommandSorting = false - rootCmd.PersistentFlags().StringVar(&globalFlags.Endpoint, "endpoint", - "127.0.0.1:8080", "the endpoints of vanus controller") - rootCmd.PersistentFlags().StringVar(&globalFlags.OperatorEndpoint, "operator-endpoint", - "127.0.0.1:8080", "the endpoints of vanus operator") rootCmd.PersistentFlags().StringVarP(&globalFlags.ConfigFile, "config", "C", "~/.vanus/vanus.yml", "the config file of vsctl") rootCmd.PersistentFlags().BoolVarP(&globalFlags.Debug, "debug", "D", false, @@ -58,14 +54,6 @@ func init() { globalFlags.Token = os.Getenv("VANUS_TOKEN") } - if os.Getenv("VANUS_GATEWAY") != "" { - globalFlags.Endpoint = os.Getenv("VANUS_GATEWAY") - } - - if os.Getenv("VANUS_OPERATOR") != "" { - globalFlags.OperatorEndpoint = os.Getenv("VANUS_OPERATOR") - } - rootCmd.AddCommand( command.NewEventCommand(), command.NewEventbusCommand(),