From d5607fdf0988b9773b130ddc6d4be6bc2d8c7469 Mon Sep 17 00:00:00 2001 From: jyjiangkai Date: Mon, 3 Apr 2023 17:05:21 +0800 Subject: [PATCH 1/2] fix: modify vanus operator default endpoint Signed-off-by: jyjiangkai --- vsctl/command/cluster.go | 55 ++++++++++++++++---------------------- vsctl/command/connector.go | 28 ++++++------------- vsctl/command/global.go | 2 +- vsctl/command/util.go | 7 +++-- vsctl/main.go | 6 ----- 5 files changed, 37 insertions(+), 61 deletions(-) diff --git a/vsctl/command/cluster.go b/vsctl/command/cluster.go index d1484a0a4..d11fa80fe 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,13 @@ 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) + } + + if storeReplicas == 0 { + cmdFailedf(cmd, "the --replicas flag MUST be set") } client := &http.Client{} @@ -651,7 +644,7 @@ func scaleStoreReplicas() *cobra.Command { } }, } - cmd.Flags().Int32Var(&storeReplicas, "replicas", 3, "replicas") + cmd.Flags().Int32Var(&storeReplicas, "replicas", 0, "the replicas of store") return cmd } @@ -660,12 +653,13 @@ 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) + } + + if triggerReplicas == 0 { + cmdFailedf(cmd, "the --replicas flag MUST be set") } client := &http.Client{} @@ -723,7 +717,7 @@ func scaleTriggerReplicas() *cobra.Command { } }, } - cmd.Flags().Int32Var(&triggerReplicas, "replicas", 3, "replicas") + cmd.Flags().Int32Var(&triggerReplicas, "replicas", 0, "the replicas of trigger") return cmd } @@ -732,12 +726,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..bd56b68c4 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,12 +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) - } + cmdFailedf(cmd, "get operator endpoint failed: %s", err) } if name == "" { @@ -324,12 +318,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,12 +392,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) - } + cmdFailedf(cmd, "get operator endpoint failed: %s", err) } if name == "" { diff --git a/vsctl/command/global.go b/vsctl/command/global.go index 5d21d4ec7..0579c3428 100644 --- a/vsctl/command/global.go +++ b/vsctl/command/global.go @@ -40,7 +40,7 @@ const ( const ( RespCodeOK int32 = 200 - DefaultOperatorPort = 30009 + DefaultOperatorPort = 8089 HttpPrefix = "http://" BaseUrl = "/api/v1" ) diff --git a/vsctl/command/util.go b/vsctl/command/util.go index 2e0a6ffb2..e4999fc04 100644 --- a/vsctl/command/util.go +++ b/vsctl/command/util.go @@ -94,11 +94,14 @@ func operatorIsDeployed(cmd *cobra.Command, endpoint string) bool { } 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..c0b79ce88 100644 --- a/vsctl/main.go +++ b/vsctl/main.go @@ -43,8 +43,6 @@ func init() { 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, @@ -62,10 +60,6 @@ func init() { globalFlags.Endpoint = os.Getenv("VANUS_GATEWAY") } - if os.Getenv("VANUS_OPERATOR") != "" { - globalFlags.OperatorEndpoint = os.Getenv("VANUS_OPERATOR") - } - rootCmd.AddCommand( command.NewEventCommand(), command.NewEventbusCommand(), From 3efd1f2ef4447fe37bf6fd2756329546e002909b Mon Sep 17 00:00:00 2001 From: jyjiangkai Date: Tue, 4 Apr 2023 19:53:21 +0800 Subject: [PATCH 2/2] modify vanus gateway endpoint Signed-off-by: jyjiangkai --- vsctl/command/cluster.go | 10 ++-------- vsctl/command/connector.go | 10 ++-------- vsctl/command/global.go | 19 +++++++------------ vsctl/command/util.go | 11 +++++++++++ vsctl/main.go | 6 ------ 5 files changed, 22 insertions(+), 34 deletions(-) diff --git a/vsctl/command/cluster.go b/vsctl/command/cluster.go index d11fa80fe..fd15c55ff 100644 --- a/vsctl/command/cluster.go +++ b/vsctl/command/cluster.go @@ -585,10 +585,6 @@ func scaleStoreReplicas() *cobra.Command { cmdFailedf(cmd, "get operator endpoint failed: %s", err) } - if storeReplicas == 0 { - cmdFailedf(cmd, "the --replicas flag MUST be set") - } - client := &http.Client{} url := fmt.Sprintf("%s%s%s/cluster", HttpPrefix, operatorEndpoint, BaseUrl) annotations := make(map[string]string) @@ -645,6 +641,7 @@ func scaleStoreReplicas() *cobra.Command { }, } cmd.Flags().Int32Var(&storeReplicas, "replicas", 0, "the replicas of store") + cobra.MarkFlagRequired(cmd.Flags(), "replicas") return cmd } @@ -658,10 +655,6 @@ func scaleTriggerReplicas() *cobra.Command { cmdFailedf(cmd, "get operator endpoint failed: %s", err) } - if triggerReplicas == 0 { - cmdFailedf(cmd, "the --replicas flag MUST be set") - } - client := &http.Client{} url := fmt.Sprintf("%s%s%s/cluster", HttpPrefix, operatorEndpoint, BaseUrl) annotations := make(map[string]string) @@ -718,6 +711,7 @@ func scaleTriggerReplicas() *cobra.Command { }, } cmd.Flags().Int32Var(&triggerReplicas, "replicas", 0, "the replicas of trigger") + cobra.MarkFlagRequired(cmd.Flags(), "replicas") return cmd } diff --git a/vsctl/command/connector.go b/vsctl/command/connector.go index bd56b68c4..98fc5b1a5 100644 --- a/vsctl/command/connector.go +++ b/vsctl/command/connector.go @@ -257,10 +257,6 @@ func uninstallConnectorCommand() *cobra.Command { cmdFailedf(cmd, "get operator endpoint failed: %s", err) } - if name == "" { - cmdFailedf(cmd, "the --name flag MUST be set") - } - client := &http.Client{} url := fmt.Sprintf("%s%s%s/connectors/%s", HttpPrefix, operatorEndpoint, BaseUrl, name) req, err := http.NewRequest("DELETE", url, &bytes.Reader{}) @@ -310,6 +306,7 @@ func uninstallConnectorCommand() *cobra.Command { }, } cmd.Flags().StringVar(&name, "name", "", "connector name") + cobra.MarkFlagRequired(cmd.Flags(), "name") return cmd } @@ -397,10 +394,6 @@ func getConnectorCommand() *cobra.Command { cmdFailedf(cmd, "get operator endpoint failed: %s", err) } - if name == "" { - cmdFailedf(cmd, "the --name flag MUST be set") - } - client := &http.Client{} url := fmt.Sprintf("%s%s%s/connectors/%s", HttpPrefix, operatorEndpoint, BaseUrl, name) req, err := http.NewRequest("GET", url, &bytes.Reader{}) @@ -459,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 0579c3428..ae929f7fd 100644 --- a/vsctl/command/global.go +++ b/vsctl/command/global.go @@ -40,6 +40,7 @@ const ( const ( RespCodeOK int32 = 200 + 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 e4999fc04..c3c36833e 100644 --- a/vsctl/command/util.go +++ b/vsctl/command/util.go @@ -93,6 +93,17 @@ 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) { if os.Getenv("VANUS_OPERATOR") != "" { return os.Getenv("VANUS_OPERATOR"), nil diff --git a/vsctl/main.go b/vsctl/main.go index c0b79ce88..b9a46ded9 100644 --- a/vsctl/main.go +++ b/vsctl/main.go @@ -41,8 +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().StringVarP(&globalFlags.ConfigFile, "config", "C", "~/.vanus/vanus.yml", "the config file of vsctl") rootCmd.PersistentFlags().BoolVarP(&globalFlags.Debug, "debug", "D", false, @@ -56,10 +54,6 @@ func init() { globalFlags.Token = os.Getenv("VANUS_TOKEN") } - if os.Getenv("VANUS_GATEWAY") != "" { - globalFlags.Endpoint = os.Getenv("VANUS_GATEWAY") - } - rootCmd.AddCommand( command.NewEventCommand(), command.NewEventbusCommand(),