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 c2f5ddde [dubboctl] add docker client logic (#585)
c2f5ddde is described below

commit c2f5dddeedbc509ab3f837327d0391c52a2af22e
Author: Jian Zhong <[email protected]>
AuthorDate: Fri Feb 7 17:02:15 2025 +0800

    [dubboctl] add docker client logic (#585)
---
 dubboctl/cmd/image.go            |  21 +++-----
 dubboctl/pkg/hub/client.go       |   4 --
 dubboctl/pkg/sdk/client.go       |  78 ++++++++++++++-------------
 dubboctl/pkg/sdk/dubbo/config.go | 110 +++++++++++++++++++++++++++++++++++----
 4 files changed, 147 insertions(+), 66 deletions(-)

diff --git a/dubboctl/cmd/image.go b/dubboctl/cmd/image.go
index 565cc48f..4220c380 100644
--- a/dubboctl/cmd/image.go
+++ b/dubboctl/cmd/image.go
@@ -37,9 +37,7 @@ func ImageCmd(ctx cli.Context, cmd *cobra.Command, 
clientFactory ClientFactory)
 }
 
 func newBuildConfig(cmd *cobra.Command) *buildConfig {
-       bc := &buildConfig{
-               BuilderImage: viper.GetString("builder-image"),
-       }
+       bc := &buildConfig{}
        return bc
 }
 
@@ -108,22 +106,13 @@ func runPush(cmd *cobra.Command, args []string, 
clientFactory ClientFactory) err
        if err != nil {
                return err
        }
-
        client, done := clientFactory(clientOptions...)
        defer done()
+
        if fp, err = client.Push(cmd.Context(), fp); err != nil {
                return err
        }
 
-       pushArgs := newPushConfig(cmd)
-
-       if pushArgs.Apply {
-               err := apply(cmd, fp)
-               if err != nil {
-                       return err
-               }
-       }
-
        err = fp.WriteFile()
        if err != nil {
                return err
@@ -165,6 +154,10 @@ func runBuild(cmd *cobra.Command, args []string, 
clientFactory ClientFactory) er
                return err
        }
 
+       if fp, err = client.Push(cmd.Context(), fp); err != nil {
+               return err
+       }
+
        err = fp.WriteFile()
        if err != nil {
                return err
@@ -173,7 +166,7 @@ func runBuild(cmd *cobra.Command, args []string, 
clientFactory ClientFactory) er
        return nil
 }
 
-func apply(cmd *cobra.Command, dc *dubbo.DubboConfig) error {
+func runApply(cmd *cobra.Command, dc *dubbo.DubboConfig) error {
        file := filepath.Join(dc.Root)
        ec := exec.CommandContext(cmd.Context(), "kubectl", "apply", "-f", file)
        ec.Stdout = os.Stdout
diff --git a/dubboctl/pkg/hub/client.go b/dubboctl/pkg/hub/client.go
index cbf4931f..381d15ae 100644
--- a/dubboctl/pkg/hub/client.go
+++ b/dubboctl/pkg/hub/client.go
@@ -91,8 +91,6 @@ func NewClient(defaultHost string) (dockerClient 
client.CommonAPIClient, dockerH
        }
 
        httpClient := &http.Client{
-               // No tls
-               // No proxy
                Transport: &http.Transport{
                        DialContext: contextDialer.DialContext,
                },
@@ -150,7 +148,6 @@ func newHttpClient() *http.Client {
                dockerCertPath = config.Dir()
        }
 
-       // Set root CA.
        caData, err := os.ReadFile(filepath.Join(dockerCertPath, "ca.pem"))
        if err == nil {
                certPool := x509.NewCertPool()
@@ -161,7 +158,6 @@ func newHttpClient() *http.Client {
                }
        }
 
-       // Set client certificate.
        certData, certErr := os.ReadFile(filepath.Join(dockerCertPath, 
"cert.pem"))
        keyData, keyErr := os.ReadFile(filepath.Join(dockerCertPath, "key.pem"))
        if certErr == nil && keyErr == nil {
diff --git a/dubboctl/pkg/sdk/client.go b/dubboctl/pkg/sdk/client.go
index 35ac1ec1..8a6aed8f 100644
--- a/dubboctl/pkg/sdk/client.go
+++ b/dubboctl/pkg/sdk/client.go
@@ -144,7 +144,7 @@ type BuildOptions struct{}
 
 type BuildOption func(c *BuildOptions)
 
-func (c *Client) Build(ctx context.Context, dcfg *dubbo.DubboConfig, options 
...BuildOption) (*dubbo.DubboConfig, error) {
+func (c *Client) Build(ctx context.Context, dc *dubbo.DubboConfig, options 
...BuildOption) (*dubbo.DubboConfig, error) {
        fmt.Fprintln(os.Stderr, "Building application image")
        ctx, cancel := context.WithCancel(ctx)
        defer cancel()
@@ -153,18 +153,22 @@ func (c *Client) Build(ctx context.Context, dcfg 
*dubbo.DubboConfig, options ...
        for _, o := range options {
                o(&bo)
        }
-       if err := c.builder.Build(ctx, dcfg); err != nil {
-               return dcfg, err
+
+       if err := c.builder.Build(ctx, dc); err != nil {
+               return dc, err
+       }
+       if err := dc.Stamp(); err != nil {
+               return dc, err
        }
-       fmt.Printf("Application built: %v\n", dcfg.Image)
-       return dcfg, nil
+       fmt.Printf("Application built: %v\n", dc.Image)
+       return dc, nil
 }
 
 func (c *Client) Push(ctx context.Context, dc *dubbo.DubboConfig) 
(*dubbo.DubboConfig, error) {
+       var err error
        if !dc.Built() {
                return dc, errors.New("not built")
        }
-       var err error
        if dc.ImageDigest, err = c.pusher.Push(ctx, dc); err != nil {
                return dc, err
        }
@@ -197,7 +201,7 @@ func (c *Client) Deploy(ctx context.Context, dc 
*dubbo.DubboConfig, opts ...Depl
 
        if result.Status == Deployed {
                // TODO
-               fmt.Fprintf(os.Stderr, "✅ Application deployed in namespace %q 
or manifest had been generated\n", result.Namespace)
+               fmt.Fprintf(os.Stderr, "Application deployed in namespace %q or 
manifest had been generated\n", result.Namespace)
        }
 
        return dc, nil
@@ -247,36 +251,6 @@ func assertEmptyRoot(path string) (err error) {
        return
 }
 
-var contentiousFiles = []string{
-       dubbo.DubboLogFile,
-       ".gitignore",
-}
-
-func contentiousFilesIn(dir string) (contentious []string, err error) {
-       files, err := os.ReadDir(dir)
-       for _, file := range files {
-               for _, name := range contentiousFiles {
-                       if file.Name() == name {
-                               contentious = append(contentious, name)
-                       }
-               }
-       }
-       return
-}
-
-func isEffectivelyEmpty(dir string) (bool, error) {
-       files, err := os.ReadDir(dir)
-       if err != nil {
-               return false, err
-       }
-       for _, file := range files {
-               if !strings.HasPrefix(file.Name(), ".") {
-                       return false, nil
-               }
-       }
-       return true, nil
-}
-
 func runDataDir(root string) error {
        if err := os.MkdirAll(filepath.Join(root, dubbo.DataDir), os.ModePerm); 
err != nil {
                return err
@@ -321,6 +295,36 @@ func runDataDir(root string) error {
        return nil
 }
 
+var contentiousFiles = []string{
+       dubbo.DubboLogFile,
+       ".gitignore",
+}
+
+func contentiousFilesIn(dir string) (contentious []string, err error) {
+       files, err := os.ReadDir(dir)
+       for _, file := range files {
+               for _, name := range contentiousFiles {
+                       if file.Name() == name {
+                               contentious = append(contentious, name)
+                       }
+               }
+       }
+       return
+}
+
+func isEffectivelyEmpty(dir string) (bool, error) {
+       files, err := os.ReadDir(dir)
+       if err != nil {
+               return false, err
+       }
+       for _, file := range files {
+               if !strings.HasPrefix(file.Name(), ".") {
+                       return false, nil
+               }
+       }
+       return true, nil
+}
+
 func WithRepositoriesPath(path string) Option {
        return func(c *Client) {
                c.repositoriesPath = path
diff --git a/dubboctl/pkg/sdk/dubbo/config.go b/dubboctl/pkg/sdk/dubbo/config.go
index 1176b1a7..7f7f1870 100644
--- a/dubboctl/pkg/sdk/dubbo/config.go
+++ b/dubboctl/pkg/sdk/dubbo/config.go
@@ -1,6 +1,7 @@
 package dubbo
 
 import (
+       "bufio"
        "bytes"
        "crypto/sha256"
        "errors"
@@ -157,7 +158,25 @@ func (dc *DubboConfig) Validate() error {
        return errors.New(b.String())
 }
 
-func (dc *DubboConfig) BuildStamp() string {
+func (dc *DubboConfig) Built() bool {
+       stamp := dc.buildStamp()
+       if stamp == "" {
+               return false
+       }
+
+       if dc.Image == "" {
+               return false
+       }
+
+       hash, _, err := Fingerprint(dc)
+       if err != nil {
+               fmt.Fprintf(os.Stderr, "error calculating function's 
fingerprint: %v\n", err)
+               return false
+       }
+       return stamp == hash
+}
+
+func (dc *DubboConfig) buildStamp() string {
        path := filepath.Join(dc.Root, DataDir, "built")
        if _, err := os.Stat(path); err != nil {
                return ""
@@ -169,22 +188,47 @@ func (dc *DubboConfig) BuildStamp() string {
        return string(b)
 }
 
-func (dc *DubboConfig) Built() bool {
-       stamp := dc.BuildStamp()
-       if stamp == "" {
-               return false
+type (
+       stampOptions struct {
+               log bool
        }
+       stampOption func(o *stampOptions)
+)
 
-       if dc.Image == "" {
-               return false
+func (dc *DubboConfig) Stamp(oo ...stampOption) (err error) {
+       options := &stampOptions{}
+       for _, o := range oo {
+               o(options)
+       }
+       if err = runDataDir(dc.Root); err != nil {
+               return
        }
 
-       hash, _, err := Fingerprint(dc)
+       var hash, log string
+       if hash, log, err = Fingerprint(dc); err != nil {
+               return
+       }
+
+       if err = os.WriteFile(filepath.Join(dc.Root, DataDir, "built"), 
[]byte(hash), os.ModePerm); err != nil {
+               return
+       }
+
+       blt := "built.log"
+       if options.log {
+               blt = timestamp(blt)
+       }
+       logfile, err := os.Create(filepath.Join(dc.Root, DataDir, blt))
        if err != nil {
-               fmt.Fprintf(os.Stderr, "error calculating function's 
fingerprint: %v\n", err)
-               return false
+               return
        }
-       return stamp == hash
+       defer logfile.Close()
+       _, err = fmt.Fprintln(logfile, log)
+       return
+}
+
+func timestamp(s string) string {
+       t := time.Now()
+       return fmt.Sprintf("%s.%09d.%s", t.Format("20060102150405"), 
t.Nanosecond(), s)
 }
 
 func (dc *DubboConfig) Initialized() bool {
@@ -223,6 +267,50 @@ func Fingerprint(dc *DubboConfig) (hash, log string, err 
error) {
        return fmt.Sprintf("%x", h.Sum(nil)), l.String(), err
 }
 
+func runDataDir(root string) error {
+       if err := os.MkdirAll(filepath.Join(root, DataDir), os.ModePerm); err 
!= nil {
+               return err
+       }
+       filePath := filepath.Join(root, ".gitignore")
+       roFile, err := os.Open(filePath)
+       if err != nil && !os.IsNotExist(err) {
+               return err
+       }
+       defer roFile.Close()
+       if !os.IsNotExist(err) {
+               s := bufio.NewScanner(roFile)
+               for s.Scan() {
+                       if strings.HasPrefix(s.Text(), "# /"+DataDir) { // if 
it was commented
+                               return nil // user wants it
+                       }
+                       if strings.HasPrefix(s.Text(), "#/"+DataDir) {
+                               return nil // user wants it
+                       }
+                       if strings.HasPrefix(s.Text(), "/"+DataDir) { // if it 
is there
+                               return nil // we're done
+                       }
+               }
+       }
+       roFile.Close()
+       rwFile, err := os.OpenFile(filePath, 
os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0o644)
+       if err != nil {
+               return err
+       }
+       defer rwFile.Close()
+       if _, err = rwFile.WriteString(`
+# Applications use the .dubbo directory for local runtime data which should
+# generally not be tracked in source control. To instruct the system to track
+# .dubbo in source control, comment the following line (prefix it with '# ').
+/.dubbo
+`); err != nil {
+               return err
+       }
+       if err = rwFile.Sync(); err != nil {
+               fmt.Fprintf(os.Stderr, "warning: error when syncing .gitignore. 
%s\n", err)
+       }
+       return nil
+}
+
 func validateOptions() []string {
        return nil
 }

Reply via email to