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 56708865 [dubboctl] Remove image build and optimization (#577)
56708865 is described below

commit 56708865f560e6ba6207a47c32a491c256f3d5df
Author: Jian Zhong <[email protected]>
AuthorDate: Tue Feb 4 00:32:54 2025 +0800

    [dubboctl] Remove image build and optimization (#577)
---
 dubboctl/cmd/create.go           |  2 +-
 dubboctl/cmd/image.go            | 59 +++++++++------------------
 dubboctl/pkg/sdk/client.go       | 17 +++++++-
 dubboctl/pkg/sdk/dubbo/config.go | 87 +++++++++++++++++++++++++++++++++++-----
 4 files changed, 111 insertions(+), 54 deletions(-)

diff --git a/dubboctl/cmd/create.go b/dubboctl/cmd/create.go
index b9fefb98..b3b60e2d 100644
--- a/dubboctl/cmd/create.go
+++ b/dubboctl/cmd/create.go
@@ -121,7 +121,7 @@ func runCreate(cmd *cobra.Command, args []string, 
clientFactory ClientFactory) e
        if err != nil {
                return err
        }
-       fmt.Printf("Custom Dubbo %v SDK was successfully created.", 
dcfg.Runtime)
+       fmt.Printf("🎉 Custom Dubbo %v sdk was successfully created.\n", 
dcfg.Runtime)
        return nil
 }
 
diff --git a/dubboctl/cmd/image.go b/dubboctl/cmd/image.go
index 395bbde5..a19770c3 100644
--- a/dubboctl/cmd/image.go
+++ b/dubboctl/cmd/image.go
@@ -1,7 +1,6 @@
 package cmd
 
 import (
-       "fmt"
        "github.com/apache/dubbo-kubernetes/dubboctl/pkg/cli"
        "github.com/apache/dubbo-kubernetes/dubboctl/pkg/sdk"
        "github.com/apache/dubbo-kubernetes/dubboctl/pkg/sdk/dubbo"
@@ -16,6 +15,7 @@ import (
 type buildConfig struct {
        Build bool
        Path  string
+       Push  bool
 }
 
 func (c buildConfig) buildclientOptions() ([]sdk.Option, error) {
@@ -58,39 +58,37 @@ func newApplyConfig(cmd *cobra.Command) *applyConfig {
 }
 
 func ImageCmd(ctx cli.Context, cmd *cobra.Command, clientFactory 
ClientFactory) *cobra.Command {
-       ibc := imageBuildCmd(cmd, clientFactory)
        ipc := imagePushCmd(cmd, clientFactory)
        iac := imageApplyCmd(cmd, clientFactory)
 
        ic := &cobra.Command{
                Use:   "image",
-               Short: "Used to build images, push images, apply to cluster",
+               Short: "Used to build and push images, apply to cluster",
        }
-       ic.AddCommand(ibc)
        ic.AddCommand(ipc)
        ic.AddCommand(iac)
        return ic
 }
 
-func imageBuildCmd(cmd *cobra.Command, clientFactory ClientFactory) 
*cobra.Command {
-       bc := &cobra.Command{
-               Use:     "build",
-               Short:   "build to images",
-               Long:    "The build subcommand used to build images",
+func imagePushCmd(cmd *cobra.Command, clientFactory ClientFactory) 
*cobra.Command {
+       pc := &cobra.Command{
+               Use:     "push",
+               Short:   "Build and push to images",
+               Long:    "The push subcommand used to push images",
                Example: "",
                RunE: func(cmd *cobra.Command, args []string) error {
-                       return runBuild(cmd, args, clientFactory)
+                       return runPush(cmd, args, clientFactory)
                },
        }
-       return bc
+       return pc
 }
 
-func runBuild(cmd *cobra.Command, args []string, clientFactory ClientFactory) 
error {
+func runPush(cmd *cobra.Command, args []string, clientFactory ClientFactory) 
error {
        if err := util.GetCreatePath(); err != nil {
                return err
        }
-
        config := newBuildConfig(cmd)
+
        fp, err := dubbo.NewDubboConfig(config.Path)
        if err != nil {
                return err
@@ -108,44 +106,24 @@ func runBuild(cmd *cobra.Command, args []string, 
clientFactory ClientFactory) er
        if fp, err = client.Build(cmd.Context(), fp); err != nil {
                return err
        }
-
-       return fmt.Errorf("TODO")
-}
-
-func imagePushCmd(cmd *cobra.Command, clientFactory ClientFactory) 
*cobra.Command {
-       pc := &cobra.Command{
-               Use:     "push",
-               Short:   "push to images",
-               Long:    "The push subcommand used to push images",
-               Example: "",
-               RunE: func(cmd *cobra.Command, args []string) error {
-                       return runPush(cmd, args, clientFactory)
-               },
-       }
-       return pc
-}
-
-func runPush(cmd *cobra.Command, args []string, clientFactory ClientFactory) 
error {
-       if err := util.GetCreatePath(); err != nil {
-               return err
+       if config.Push {
+               if fp, err = client.Push(cmd.Context(), fp); err != nil {
+                       return err
+               }
        }
-       config := newPushConfig(cmd)
 
-       fp, err := dubbo.NewDubboConfig(config.Path)
+       err = fp.WriteYamlFile()
        if err != nil {
                return err
        }
 
-       if !fp.Initialized() {
-       }
-
-       return fmt.Errorf("TODO")
+       return nil
 }
 
 func imageApplyCmd(cmd *cobra.Command, clientFactory ClientFactory) 
*cobra.Command {
        ac := &cobra.Command{
                Use:     "apply",
-               Short:   "apply to images",
+               Short:   "Deploy the images to the cluster",
                Long:    "The apply subcommand used to apply images",
                Example: "",
                RunE: func(cmd *cobra.Command, args []string) error {
@@ -169,7 +147,6 @@ func runApply(cmd *cobra.Command, args []string, 
clientFactory ClientFactory) er
 
        if !fp.Initialized() {
        }
-
        if err := applyToCluster(cmd, fp); err != nil {
                return err
        }
diff --git a/dubboctl/pkg/sdk/client.go b/dubboctl/pkg/sdk/client.go
index ca3171ec..5bf0e1c6 100644
--- a/dubboctl/pkg/sdk/client.go
+++ b/dubboctl/pkg/sdk/client.go
@@ -20,6 +20,7 @@ type Client struct {
        repositories     *Repositories
        repositoriesPath string
        builder          Builder
+       pusher           Pusher
 }
 
 type Builder interface {
@@ -157,9 +158,21 @@ func (c *Client) Build(ctx context.Context, dcfg 
*dubbo.DubboConfig, options ...
        return dcfg, nil
 }
 
+func (c *Client) Push(ctx context.Context, dc *dubbo.DubboConfig) 
(*dubbo.DubboConfig, 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
+       }
+
+       return dc, nil
+}
+
 func hasInitialized(path string) (bool, error) {
        var err error
-       filename := filepath.Join(path, dubbo.DubboYamlFile)
+       filename := filepath.Join(path, dubbo.DubboLogFile)
 
        if _, err = os.Stat(filename); err != nil {
                if os.IsNotExist(err) {
@@ -202,7 +215,7 @@ func assertEmptyRoot(path string) (err error) {
 }
 
 var contentiousFiles = []string{
-       dubbo.DubboYamlFile,
+       dubbo.DubboLogFile,
        ".gitignore",
 }
 
diff --git a/dubboctl/pkg/sdk/dubbo/config.go b/dubboctl/pkg/sdk/dubbo/config.go
index d8ac7874..446e251f 100644
--- a/dubboctl/pkg/sdk/dubbo/config.go
+++ b/dubboctl/pkg/sdk/dubbo/config.go
@@ -1,10 +1,13 @@
 package dubbo
 
 import (
+       "bytes"
+       "crypto/sha256"
        "errors"
        "fmt"
        "github.com/spf13/cobra"
        "gopkg.in/yaml.v3"
+       "io/fs"
        "os"
        "path/filepath"
        "strings"
@@ -12,19 +15,20 @@ import (
 )
 
 const (
-       DubboYamlFile   = "dubbo.yaml"
+       DubboLogFile    = ".dubbo/dubbo.log"
        Dockerfile      = "Dockerfile"
        DataDir         = ".dubbo"
        DefaultTemplate = "common"
 )
 
 type DubboConfig struct {
-       Root     string    `yaml:"-"`
-       Name     string    `yaml:"name,omitempty" 
jsonschema:"pattern=^[a-z0-9]([-a-z0-9]*[a-z0-9])?$"`
-       Image    string    `yaml:"image,omitempty"`
-       Runtime  string    `yaml:"runtime,omitempty"`
-       Template string    `yaml:"template,omitempty"`
-       Created  time.Time `yaml:"created,omitempty"`
+       Root        string    `yaml:"-"`
+       Name        string    `yaml:"name,omitempty" 
jsonschema:"pattern=^[a-z0-9]([-a-z0-9]*[a-z0-9])?$"`
+       Image       string    `yaml:"image,omitempty"`
+       ImageDigest string    `yaml:"-"`
+       Runtime     string    `yaml:"runtime,omitempty"`
+       Template    string    `yaml:"template,omitempty"`
+       Created     time.Time `yaml:"created,omitempty"`
 }
 
 func NewDubboConfig(path string) (*DubboConfig, error) {
@@ -45,7 +49,7 @@ func NewDubboConfig(path string) (*DubboConfig, error) {
                return nil, fmt.Errorf("function path must be a directory")
        }
 
-       filename := filepath.Join(path, DubboYamlFile)
+       filename := filepath.Join(path, DubboLogFile)
        if _, err = os.Stat(filename); err != nil {
                if os.IsNotExist(err) {
                        err = nil
@@ -78,7 +82,7 @@ func NewDubboConfigWithTemplate(dc *DubboConfig, initialized 
bool) *DubboConfig
 }
 
 func (dc *DubboConfig) WriteYamlFile() (err error) {
-       file := filepath.Join(dc.Root, DubboYamlFile)
+       file := filepath.Join(dc.Root, DubboLogFile)
        var bytes []byte
        if bytes, err = yaml.Marshal(dc); err != nil {
                return
@@ -113,7 +117,7 @@ func (dc *DubboConfig) Validate() error {
        }
 
        var b strings.Builder
-       b.WriteString(fmt.Sprintf("'%v' contains errors:", DubboYamlFile))
+       b.WriteString(fmt.Sprintf("'%v' contains errors:", DubboLogFile))
 
        for _, ee := range errs {
                if len(ee) > 0 {
@@ -132,6 +136,69 @@ func (dc *DubboConfig) Validate() error {
        return errors.New(b.String())
 }
 
+func (dc *DubboConfig) BuildStamp() string {
+       path := filepath.Join(dc.Root, DataDir, "built")
+       if _, err := os.Stat(path); err != nil {
+               return ""
+       }
+       b, err := os.ReadFile(path)
+       if err != nil {
+               return ""
+       }
+       return string(b)
+}
+
+func (dc *DubboConfig) Built() bool {
+       // If there is no build stamp, it is not built.
+       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 Fingerprint(dc *DubboConfig) (hash, log string, err error) {
+       h := sha256.New()   // Hash builder
+       l := bytes.Buffer{} // Log buffer
+
+       root := dc.Root
+       abs, err := filepath.Abs(root)
+       if err != nil {
+               return "", "", err
+       }
+       // TODO
+       output := ""
+
+       err = filepath.Walk(abs, func(path string, info fs.FileInfo, err error) 
error {
+               if err != nil {
+                       return err
+               }
+               if path == root {
+                       return nil
+               }
+               if info.IsDir() && (info.Name() == DataDir || info.Name() == 
".git" || info.Name() == ".idea") {
+                       return filepath.SkipDir
+               }
+               if info.Name() == DubboLogFile || info.Name() == Dockerfile || 
info.Name() == output {
+                       return nil
+               }
+               fmt.Fprintf(h, "%v:%v:", path, info.ModTime().UnixNano())   // 
Write to the Hashed
+               fmt.Fprintf(&l, "%v:%v\n", path, info.ModTime().UnixNano()) // 
Write to the Log
+               return nil
+       })
+       return fmt.Sprintf("%x", h.Sum(nil)), l.String(), err
+}
+
 func validateOptions() []string {
        return nil
 }

Reply via email to