You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@dubbo.apache.org by zh...@apache.org on 2025/02/07 17:02:21 UTC

(dubbo-kubernetes) branch master updated: [dubboctl] Fix image option bug (#587)

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 55e9da2b [dubboctl] Fix image option bug (#587)
55e9da2b is described below

commit 55e9da2bda02ac1fa1d1c5bb49268779cc97c008
Author: Jian Zhong <11...@qq.com>
AuthorDate: Sat Feb 8 01:02:15 2025 +0800

    [dubboctl] Fix image option bug (#587)
---
 dubboctl/cmd/create.go                       | 10 ++--
 dubboctl/cmd/image.go                        | 79 +++++++++++++++++++++-------
 dubboctl/pkg/hub/builder/dockerfile/build.go | 43 +++++++++++++++
 go.mod                                       |  6 ++-
 go.sum                                       |  4 ++
 5 files changed, 115 insertions(+), 27 deletions(-)

diff --git a/dubboctl/cmd/create.go b/dubboctl/cmd/create.go
index b3b60e2d..13e69b7f 100644
--- a/dubboctl/cmd/create.go
+++ b/dubboctl/cmd/create.go
@@ -19,7 +19,7 @@ type createArgs struct {
 func addCreateFlags(cmd *cobra.Command, tempArgs *createArgs) {
 	cmd.PersistentFlags().StringVarP(&tempArgs.language, "language", "l", "", "java or go language")
 	cmd.PersistentFlags().StringVarP(&tempArgs.template, "template", "t", "", "java or go sdk template")
-	cmd.PersistentFlags().StringVarP(&tempArgs.dirname, "dirname", "d", "", "java or go sdk template custom directory name")
+	cmd.PersistentFlags().StringVar(&tempArgs.dirname, "dirname", "", "java or go sdk template custom directory name")
 }
 
 type bindFunc func(*cobra.Command, []string) error
@@ -60,16 +60,16 @@ func sdkGenerateCmd(cmd *cobra.Command, clientFactory ClientFactory) *cobra.Comm
 		Example: `  # Create a java sample sdk.
   dubboctl create sdk --language java --template common --dirname mydubbo
 
-  dubboctl create sdk -l java -t common -d mydubbo
+  dubboctl create sdk -l java -t common --dirname mydubbo
   
-  dubboctl create sdk -l java -t common -d myrepo/mydubbo
+  dubboctl create sdk -l java -t common --dirname myrepo/mydubbo
 
   # Create a go sample sdk.
   dubboctl create sdk --language go --template common --dirname mydubbogo
 
-  dubboctl create sdk -l go -t common -d mydubbogo
+  dubboctl create sdk -l go -t common --dirname mydubbogo
 
-  dubboctl create sdk -l go -t common -d myrepo/mydubbogo
+  dubboctl create sdk -l go -t common --dirname myrepo/mydubbogo
 `,
 		PreRunE: bindEnv("language", "template", "dirname"),
 		RunE: func(cmd *cobra.Command, args []string) error {
diff --git a/dubboctl/cmd/image.go b/dubboctl/cmd/image.go
index 3a32802f..96019b06 100644
--- a/dubboctl/cmd/image.go
+++ b/dubboctl/cmd/image.go
@@ -1,19 +1,32 @@
 package cmd
 
 import (
+	"fmt"
 	"github.com/AlecAivazis/survey/v2"
 	"github.com/apache/dubbo-kubernetes/dubboctl/pkg/cli"
+	"github.com/apache/dubbo-kubernetes/dubboctl/pkg/hub/builder/dockerfile"
 	"github.com/apache/dubbo-kubernetes/dubboctl/pkg/hub/builder/pack"
 	"github.com/apache/dubbo-kubernetes/dubboctl/pkg/sdk"
 	"github.com/apache/dubbo-kubernetes/dubboctl/pkg/sdk/dubbo"
 	"github.com/apache/dubbo-kubernetes/dubboctl/pkg/util"
+	"github.com/ory/viper"
 	"github.com/spf13/cobra"
 	"os"
 	"os/exec"
 	"path/filepath"
+	"strings"
 )
 
+type imageArgs struct {
+	dockerfile bool
+}
+
+func addImageFlags(cmd *cobra.Command, iArgs *imageArgs) {
+	cmd.PersistentFlags().BoolVarP(&iArgs.dockerfile, "file", "f", false, "Specify the file as a dockerfile")
+}
+
 type hubConfig struct {
+	Dockerfile   bool
 	Image        string
 	BuilderImage string
 	Path         string
@@ -30,27 +43,53 @@ func ImageCmd(ctx cli.Context, cmd *cobra.Command, clientFactory ClientFactory)
 }
 
 func newHubConfig(cmd *cobra.Command) *hubConfig {
-	hc := &hubConfig{}
+	hc := &hubConfig{
+		Dockerfile: viper.GetBool("file"),
+	}
 	return hc
 }
 
-func (c hubConfig) imageClientOptions() ([]sdk.Option, error) {
+func (hc hubConfig) imageClientOptions() ([]sdk.Option, error) {
 	var do []sdk.Option
-	do = append(do, sdk.WithBuilder(pack.NewBuilder()))
+	if hc.Dockerfile {
+		do = append(do, sdk.WithBuilder(dockerfile.NewBuilder()))
+	} else {
+		do = append(do, sdk.WithBuilder(pack.NewBuilder()))
+	}
 	return do, nil
 }
 
 func imageHubCmd(cmd *cobra.Command, clientFactory ClientFactory) *cobra.Command {
-	bc := &cobra.Command{
+	iArgs := &imageArgs{}
+	hc := &cobra.Command{
 		Use:     "hub",
 		Short:   "Build and Push to images",
 		Long:    "The hub subcommand used to build and push images",
 		Example: "",
+		Args: func(cmd *cobra.Command, args []string) error {
+			if len(args) != 1 {
+				return fmt.Errorf("accepts %d arg(s), received %d", args, len(args))
+			}
+
+			if cmd.Flags().Changed("file") {
+				if len(args) != 1 {
+					return fmt.Errorf("you must provide exactly one argument when using the -f flag: the path to the Dockerfile")
+				}
+
+				df := args[0]
+				if !strings.HasSuffix(df, "Dockerfile") {
+					return fmt.Errorf("the provided file must be a Dockerfile when using the -f flag")
+				}
+			}
+			return nil
+		},
+		PreRunE: bindEnv("file"),
 		RunE: func(cmd *cobra.Command, args []string) error {
 			return runHub(cmd, args, clientFactory)
 		},
 	}
-	return bc
+	addImageFlags(hc, iArgs)
+	return hc
 }
 
 func runHub(cmd *cobra.Command, args []string, clientFactory ClientFactory) error {
@@ -113,44 +152,44 @@ func runApply(cmd *cobra.Command, dc *dubbo.DubboConfig) error {
 	return nil
 }
 
-func (c *hubConfig) configure(dc *dubbo.DubboConfig) {
-	if c.Path == "" {
+func (hc *hubConfig) configure(dc *dubbo.DubboConfig) {
+	if hc.Path == "" {
 		root, err := os.Getwd()
 		if err != nil {
 			return
 		}
 		dc.Root = root
 	} else {
-		dc.Root = c.Path
+		dc.Root = hc.Path
 	}
-	if c.BuilderImage != "" {
-		dc.Build.BuilderImages["pack"] = c.BuilderImage
+	if hc.BuilderImage != "" {
+		dc.Build.BuilderImages["pack"] = hc.BuilderImage
 	}
-	if c.Image != "" {
-		dc.Image = c.Image
+	if hc.Image != "" {
+		dc.Image = hc.Image
 	}
 }
 
-func (c *hubConfig) prompt(dc *dubbo.DubboConfig) (*hubConfig, error) {
+func (hc *hubConfig) prompt(dc *dubbo.DubboConfig) (*hubConfig, error) {
 	var err error
 	if !util.InteractiveTerminal() {
-		return c, nil
+		return hc, nil
 	}
 
-	if c.Image == "" && dc.Image == "" {
+	if hc.Image == "" && dc.Image == "" {
 		qs := []*survey.Question{
 			{
 				Name:     "image",
 				Validate: survey.Required,
 				Prompt: &survey.Input{
-					Message: "Please enter the image tag ([REGISTRY]/[USERNAME]/[IMAGENAME]:tag)\n Image: ",
-					Default: c.Image,
+					Message: "Please enter the image tag ([REGISTRY]/[USERNAME]/[IMAGENAME]:tag)\n  Image: ",
+					Default: hc.Image,
 				},
 			},
 		}
-		if err = survey.Ask(qs, c); err != nil {
-			return c, err
+		if err = survey.Ask(qs, hc); err != nil {
+			return hc, err
 		}
 	}
-	return c, err
+	return hc, err
 }
diff --git a/dubboctl/pkg/hub/builder/dockerfile/build.go b/dubboctl/pkg/hub/builder/dockerfile/build.go
new file mode 100644
index 00000000..0a4f7e23
--- /dev/null
+++ b/dubboctl/pkg/hub/builder/dockerfile/build.go
@@ -0,0 +1,43 @@
+package dockerfile
+
+import (
+	"context"
+	"github.com/apache/dubbo-kubernetes/dubboctl/pkg/hub"
+	"github.com/apache/dubbo-kubernetes/dubboctl/pkg/sdk/dubbo"
+	"github.com/containers/storage/pkg/archive"
+	"github.com/docker/docker/api/types"
+	"github.com/docker/docker/client"
+	"github.com/docker/docker/pkg/jsonmessage"
+	"github.com/moby/term"
+	"os"
+)
+
+type Builder struct{}
+
+func NewBuilder() *Builder {
+	return &Builder{}
+}
+
+func (b Builder) Build(ctx context.Context, dc *dubbo.DubboConfig) error {
+	cli, _, err := hub.NewClient(client.DefaultDockerHost)
+	if err != nil {
+		return err
+	}
+	buildOpts := types.ImageBuildOptions{
+		Dockerfile: "Dockerfile",
+		Tags:       []string{dc.Image},
+	}
+
+	buildCtx, _ := archive.TarWithOptions(dc.Root, &archive.TarOptions{})
+	resp, err := cli.ImageBuild(ctx, buildCtx, buildOpts)
+	if err != nil {
+		return err
+	}
+	defer resp.Body.Close()
+	termFd, isTerm := term.GetFdInfo(os.Stderr)
+	err = jsonmessage.DisplayJSONMessagesStream(resp.Body, os.Stderr, termFd, isTerm, nil)
+	if err != nil {
+		return err
+	}
+	return nil
+}
diff --git a/go.mod b/go.mod
index 5f9e080a..b16a8117 100644
--- a/go.mod
+++ b/go.mod
@@ -26,6 +26,7 @@ require (
 	github.com/buildpacks/pack v0.30.0
 	github.com/cheggaaa/pb/v3 v3.1.5
 	github.com/containers/image/v5 v5.34.0
+	github.com/containers/storage v1.57.1
 	github.com/docker/cli v27.5.1+incompatible
 	github.com/docker/docker v27.5.1+incompatible
 	github.com/docker/docker-credential-helpers v0.8.2
@@ -59,6 +60,7 @@ require (
 	github.com/jhump/protoreflect v1.16.0
 	github.com/kelseyhightower/envconfig v1.4.0
 	github.com/mitchellh/mapstructure v1.5.0
+	github.com/moby/term v0.5.0
 	github.com/onsi/ginkgo/v2 v2.22.1
 	github.com/onsi/gomega v1.36.2
 	github.com/ory/viper v1.7.5
@@ -169,7 +171,6 @@ require (
 	github.com/containerd/containerd v1.7.2 // indirect
 	github.com/containerd/stargz-snapshotter/estargz v0.16.3 // indirect
 	github.com/containerd/typeurl v1.0.2 // indirect
-	github.com/containers/storage v1.57.1 // indirect
 	github.com/coreos/go-semver v0.3.1 // indirect
 	github.com/coreos/go-systemd/v22 v22.5.0 // indirect
 	github.com/creasty/defaults v1.5.2 // indirect
@@ -240,6 +241,7 @@ require (
 	github.com/kevinburke/ssh_config v1.2.0 // indirect
 	github.com/klauspost/compress v1.17.11 // indirect
 	github.com/klauspost/cpuid/v2 v2.2.4 // indirect
+	github.com/klauspost/pgzip v1.2.6 // indirect
 	github.com/knadh/koanf v1.5.0 // indirect
 	github.com/leodido/go-urn v1.2.4 // indirect
 	github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
@@ -261,7 +263,6 @@ require (
 	github.com/moby/sys/mountinfo v0.7.2 // indirect
 	github.com/moby/sys/sequential v0.5.0 // indirect
 	github.com/moby/sys/user v0.3.0 // indirect
-	github.com/moby/term v0.5.0 // indirect
 	github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
 	github.com/modern-go/reflect2 v1.0.2 // indirect
 	github.com/monochromegane/go-gitignore v0.0.0-20200626010858-205db1a8cc00 // indirect
@@ -310,6 +311,7 @@ require (
 	github.com/uber/jaeger-client-go v2.29.1+incompatible // indirect
 	github.com/uber/jaeger-lib v2.4.1+incompatible // indirect
 	github.com/ugorji/go/codec v1.2.11 // indirect
+	github.com/ulikunitz/xz v0.5.12 // indirect
 	github.com/vbatts/tar-split v0.11.7 // indirect
 	github.com/x448/float16 v0.8.4 // indirect
 	github.com/xanzy/ssh-agent v0.3.3 // indirect
diff --git a/go.sum b/go.sum
index d12ce416..84e9dd44 100644
--- a/go.sum
+++ b/go.sum
@@ -807,6 +807,8 @@ github.com/klauspost/compress v1.17.11/go.mod h1:pMDklpSncoRMuLFrf1W9Ss9KT+0rH90
 github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
 github.com/klauspost/cpuid/v2 v2.2.4 h1:acbojRNwl3o09bUq+yDCtZFc1aiwaAAxtcn8YkZXnvk=
 github.com/klauspost/cpuid/v2 v2.2.4/go.mod h1:RVVoqg1df56z8g3pUjL/3lE5UfnlrJX8tyFgg4nqhuY=
+github.com/klauspost/pgzip v1.2.6 h1:8RXeL5crjEUFnR2/Sn6GJNWtSQ3Dk8pq4CL3jvdDyjU=
+github.com/klauspost/pgzip v1.2.6/go.mod h1:Ch1tH69qFZu15pkjo5kYi6mth2Zzwzt50oCQKQE9RUs=
 github.com/knadh/koanf v1.5.0 h1:q2TSd/3Pyc/5yP9ldIrSdIz26MCcyNQzW0pEAugLPNs=
 github.com/knadh/koanf v1.5.0/go.mod h1:Hgyjp4y8v44hpZtPzs7JZfRAW5AhN7KfZcwv1RYggDs=
 github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
@@ -1223,6 +1225,8 @@ github.com/ugorji/go v1.2.6/go.mod h1:anCg0y61KIhDlPZmnH+so+RQbysYVyDko0IMgJv0Nn
 github.com/ugorji/go/codec v1.2.6/go.mod h1:V6TCNZ4PHqoHGFZuSG1W8nrCzzdgA2DozYxWFFpvxTw=
 github.com/ugorji/go/codec v1.2.11 h1:BMaWp1Bb6fHwEtbplGBGJ498wD+LKlNSl25MjdZY4dU=
 github.com/ugorji/go/codec v1.2.11/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZgYf6w6lg=
+github.com/ulikunitz/xz v0.5.12 h1:37Nm15o69RwBkXM0J6A5OlE67RZTfzUxTj8fB3dfcsc=
+github.com/ulikunitz/xz v0.5.12/go.mod h1:nbz6k7qbPmH4IRqmfOplQw/tblSgqTqBwxkY0oWt/14=
 github.com/urfave/cli v1.20.0/go.mod h1:70zkFmudgCuE/ngEzBv17Jvp/497gISqfk5gWijbERA=
 github.com/urfave/cli v1.22.1/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0=
 github.com/urfave/cli/v2 v2.3.0/go.mod h1:LJmUH05zAU44vOAcrfzZQKsZbVcdbOG8rtL3/XcUArI=