This is an automated email from the ASF dual-hosted git repository.

zhongxjian pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/dubbo-kubernetes.git


The following commit(s) were added to refs/heads/master by this push:
     new eea76c39 [navi] add naviagent wait and proxy command (#759)
eea76c39 is described below

commit eea76c396a52175bad739b9908d7507f13063173
Author: Jian Zhong <[email protected]>
AuthorDate: Wed Jul 30 23:42:36 2025 +0800

    [navi] add naviagent wait and proxy command (#759)
---
 navigator/cmd/navigator-agent/app/cmd.go           | 38 +++++++++++++
 navigator/cmd/navigator-agent/app/wait.go          | 64 ++++++++++++++++++++++
 .../cmd/navigator-agent/options/agent_proxy.go     | 13 +++++
 pkg/model/proxy.go                                 | 22 ++++++++
 pkg/navi-agent/agent.go                            |  8 +++
 5 files changed, 145 insertions(+)

diff --git a/navigator/cmd/navigator-agent/app/cmd.go 
b/navigator/cmd/navigator-agent/app/cmd.go
index a1f91952..bb133333 100644
--- a/navigator/cmd/navigator-agent/app/cmd.go
+++ b/navigator/cmd/navigator-agent/app/cmd.go
@@ -1,16 +1,34 @@
 package app
 
 import (
+       "fmt"
+       
"github.com/apache/dubbo-kubernetes/navigator/cmd/navigator-agent/options"
        "github.com/apache/dubbo-kubernetes/navigator/pkg/cmd"
+       "github.com/apache/dubbo-kubernetes/pkg/model"
        "github.com/spf13/cobra"
 )
 
+var (
+       proxyArgs options.ProxyArgs
+)
+
 func NewRootCommand() *cobra.Command {
        rootCmd := &cobra.Command{
                Use:          "navi-agent",
+               Short:        "Dubbo Navi agent.",
+               Long:         "Dubbo Navi agent runs in the sidecar or gateway 
container and bootstraps Envoy.",
                SilenceUsage: true,
+               FParseErrWhitelist: cobra.FParseErrWhitelist{
+                       // Allow unknown flags for backward-compatibility.
+                       UnknownFlags: true,
+               },
        }
        cmd.AddFlags(rootCmd)
+       proxyCmd := newProxyCommand()
+       addFlags(proxyCmd)
+       rootCmd.AddCommand(proxyCmd)
+       rootCmd.AddCommand(waitCmd)
+
        return rootCmd
 }
 
@@ -19,10 +37,30 @@ func newProxyCommand() *cobra.Command {
                Use:   "proxy",
                Short: "XDS proxy agent",
                FParseErrWhitelist: cobra.FParseErrWhitelist{
+                       // Allow unknown flags for backward-compatibility.
                        UnknownFlags: true,
                },
                RunE: func(c *cobra.Command, args []string) error {
+                       err := initProxy(args)
+                       if err != nil {
+                               return err
+                       }
                        return nil
                },
        }
 }
+
+func initProxy(args []string) error {
+       proxyArgs.Type = model.SidecarProxy
+       if len(args) > 0 {
+               proxyArgs.Type = model.NodeType(args[0])
+               if !model.IsApplicationNodeType(proxyArgs.Type) {
+                       return fmt.Errorf("invalid proxy Type: %s", 
string(proxyArgs.Type))
+               }
+       }
+       return nil
+}
+
+func addFlags(proxyCmd *cobra.Command) {
+       proxyArgs = options.NewProxyArgs()
+}
diff --git a/navigator/cmd/navigator-agent/app/wait.go 
b/navigator/cmd/navigator-agent/app/wait.go
new file mode 100644
index 00000000..367208fe
--- /dev/null
+++ b/navigator/cmd/navigator-agent/app/wait.go
@@ -0,0 +1,64 @@
+package app
+
+import (
+       "fmt"
+       "github.com/spf13/cobra"
+       "io"
+       "net/http"
+       "time"
+)
+
+var (
+       timeoutSeconds       int
+       requestTimeoutMillis int
+       periodMillis         int
+       url                  string
+
+       waitCmd = &cobra.Command{
+               Use:   "wait",
+               Short: "Waits until the Envoy proxy is ready",
+               RunE: func(c *cobra.Command, args []string) error {
+                       client := &http.Client{
+                               Timeout: time.Duration(requestTimeoutMillis) * 
time.Millisecond,
+                       }
+                       fmt.Printf("Waiting for Envoy proxy to be ready 
(timeout: %d seconds)...", timeoutSeconds)
+
+                       var err error
+                       timeout := time.After(time.Duration(timeoutSeconds) * 
time.Second)
+
+                       for {
+                               select {
+                               case <-timeout:
+                                       return fmt.Errorf("timeout waiting for 
Envoy proxy to become ready. Last error: %v", err)
+                               case <-time.After(time.Duration(periodMillis) * 
time.Millisecond):
+                                       err = checkIfReady(client, url)
+                                       if err == nil {
+                                               fmt.Println("Envoy is ready!")
+                                               return nil
+                                       }
+                                       fmt.Printf("Not ready yet: %v", err)
+                               }
+                       }
+               },
+       }
+)
+
+func checkIfReady(client *http.Client, url string) error {
+       req, err := http.NewRequest(http.MethodGet, url, nil)
+       if err != nil {
+               return err
+       }
+       resp, err := client.Do(req)
+       if err != nil {
+               return err
+       }
+       defer resp.Body.Close()
+       _, err = io.ReadAll(resp.Body)
+       if err != nil {
+               return err
+       }
+       if resp.StatusCode != http.StatusOK {
+               return fmt.Errorf("HTTP status code %v", resp.StatusCode)
+       }
+       return nil
+}
diff --git a/navigator/cmd/navigator-agent/options/agent_proxy.go 
b/navigator/cmd/navigator-agent/options/agent_proxy.go
new file mode 100644
index 00000000..a52abd7c
--- /dev/null
+++ b/navigator/cmd/navigator-agent/options/agent_proxy.go
@@ -0,0 +1,13 @@
+package options
+
+import naviagent "github.com/apache/dubbo-kubernetes/pkg/navi-agent"
+
+// ProxyArgs provides all of the configuration parameters for the Navi proxy.
+type ProxyArgs struct {
+       naviagent.Proxy
+}
+
+func NewProxyArgs() ProxyArgs {
+       p := ProxyArgs{}
+       return p
+}
diff --git a/pkg/model/proxy.go b/pkg/model/proxy.go
new file mode 100644
index 00000000..2aab6b74
--- /dev/null
+++ b/pkg/model/proxy.go
@@ -0,0 +1,22 @@
+package model
+
+// NodeType decides the responsibility of the proxy serves in the mesh
+type NodeType string
+
+const (
+       // SidecarProxy type is used for sidecar proxies in the application 
containers
+       SidecarProxy NodeType = "sidecar"
+
+       // Router type is used for standalone proxies acting as L7/L4 routers
+       Router NodeType = "router"
+)
+
+// IsApplicationNodeType verifies that the NodeType is one of the declared 
constants in the model
+func IsApplicationNodeType(nType NodeType) bool {
+       switch nType {
+       case SidecarProxy, Router:
+               return true
+       default:
+               return false
+       }
+}
diff --git a/pkg/navi-agent/agent.go b/pkg/navi-agent/agent.go
new file mode 100644
index 00000000..8c65019a
--- /dev/null
+++ b/pkg/navi-agent/agent.go
@@ -0,0 +1,8 @@
+package naviagent
+
+import "github.com/apache/dubbo-kubernetes/pkg/model"
+
+// Shared properties with Pilot Proxy struct.
+type Proxy struct {
+       Type model.NodeType
+}

Reply via email to