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 23c66fe6 [operator] Add sdk creation logic (#535)
23c66fe6 is described below

commit 23c66fe6fc6e8958b8a9a56e365e0a646718f53b
Author: Jian Zhong <[email protected]>
AuthorDate: Tue Jan 14 13:43:04 2025 +0800

    [operator] Add sdk creation logic (#535)
---
 dubboctl/pkg/cli/context.go                   |   2 +-
 dubboctl/pkg/cli/options.go                   |   2 +-
 dubboctl/pkg/sdk/client.go                    | 108 ++++++++++++++++++++++++++
 operator/cmd/cluster/create.go                | 107 +++++++++++++++++++++++--
 operator/cmd/cluster/manifest.go              |  24 +++---
 operator/pkg/schema/{schema.go => impl.go}    |   4 +-
 operator/pkg/uninstall/uninstaller.go         |   2 +-
 {pkg => operator/pkg/util}/pointer/pointer.go |   0
 operator/pkg/values/map.go                    |   2 +-
 pkg/art/dubbo-ascii.txt                       |   6 +-
 pkg/kube/collections/collection.gen.go        |   2 +-
 11 files changed, 229 insertions(+), 30 deletions(-)

diff --git a/dubboctl/pkg/cli/context.go b/dubboctl/pkg/cli/context.go
index f28a59d8..d647af3d 100644
--- a/dubboctl/pkg/cli/context.go
+++ b/dubboctl/pkg/cli/context.go
@@ -1,8 +1,8 @@
 package cli
 
 import (
+       "github.com/apache/dubbo-kubernetes/operator/pkg/util/pointer"
        "github.com/apache/dubbo-kubernetes/pkg/kube"
-       "github.com/apache/dubbo-kubernetes/pkg/pointer"
        "k8s.io/client-go/rest"
 )
 
diff --git a/dubboctl/pkg/cli/options.go b/dubboctl/pkg/cli/options.go
index c3e5ba4a..0934a02a 100644
--- a/dubboctl/pkg/cli/options.go
+++ b/dubboctl/pkg/cli/options.go
@@ -1,7 +1,7 @@
 package cli
 
 import (
-       "github.com/apache/dubbo-kubernetes/pkg/pointer"
+       "github.com/apache/dubbo-kubernetes/operator/pkg/util/pointer"
        "github.com/ory/viper"
        "github.com/spf13/pflag"
        v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
diff --git a/dubboctl/pkg/sdk/client.go b/dubboctl/pkg/sdk/client.go
new file mode 100644
index 00000000..09323de3
--- /dev/null
+++ b/dubboctl/pkg/sdk/client.go
@@ -0,0 +1,108 @@
+package sdk
+
+import (
+       "fmt"
+       "github.com/spf13/cobra"
+       "os"
+       "path/filepath"
+)
+
+type Client struct {
+}
+
+func (c *Client) Create(cfg *Dubbo, init bool, cmd *cobra.Command) (*Dubbo, 
error) {
+       // convert Root path to absolute
+       var err error
+       oldRoot := cfg.Root
+       cfg.Root, err = filepath.Abs(cfg.Root)
+       if err != nil {
+               return cfg, err
+       }
+
+       // Create project root directory, if it doesn't already exist
+       if err = os.MkdirAll(cfg.Root, 0o755); err != nil {
+               return cfg, err
+       }
+
+       // Create should never clobber a pre-existing function
+       hasApp, err := hasInitializedApplication(cfg.Root)
+       if err != nil {
+               return cfg, err
+       }
+       if hasApp {
+               return cfg, fmt.Errorf("application at '%v' already 
initialized", cfg.Root)
+       }
+
+       // Path is defaulted to the current working directory
+       if cfg.Root == "" {
+               if cfg.Root, err = os.Getwd(); err != nil {
+                       return cfg, err
+               }
+       }
+
+       // Name is defaulted to the directory of the given path.
+       if cfg.Name == "" {
+               cfg.Name = nameFromPath(cfg.Root)
+       }
+
+       if !init {
+               // The path for the new function should not have any 
contentious files
+               // (hidden files OK, unless it's one used by dubbo)
+               if err := assertEmptyRoot(cfg.Root); err != nil {
+                       return cfg, err
+               }
+       }
+
+       // Create a new application (in memory)
+       f := NewDubboWith(cfg, init)
+
+       // Create a .dubbo directory which is also added to a .gitignore
+       if err = EnsureRunDataDir(f.Root); err != nil {
+               return f, err
+       }
+
+       if !init {
+               // Write out the new function's Template files.
+               // Templates contain values which may result in the function 
being mutated
+               // (default builders, etc), so a new (potentially mutated) 
function is
+               // returned from Templates.Write
+               err = c.Templates().Write(f)
+               if err != nil {
+                       return f, err
+               }
+       }
+       f.Created = time.Now()
+       err = f.Write()
+       if err != nil {
+               return f, err
+       }
+       err = f.EnsureDockerfile(cmd)
+       if err != nil {
+               return f, err
+       }
+
+       // Load the now-initialized application.
+       return NewDubbo(oldRoot)
+}
+
+func hasInitializedApplication(path string) (bool, error) {
+       var err error
+       filename := filepath.Join(path, DubboFile)
+
+       if _, err = os.Stat(filename); err != nil {
+               if os.IsNotExist(err) {
+                       return false, nil
+               }
+               return false, err // invalid path or access error
+       }
+       bb, err := os.ReadFile(filename)
+       if err != nil {
+               return false, err
+       }
+       f := Dubbo{}
+       if err = yaml.Unmarshal(bb, &f); err != nil {
+               return false, err
+       }
+
+       return f.Initialized(), nil
+}
diff --git a/operator/cmd/cluster/create.go b/operator/cmd/cluster/create.go
index d3841db5..c1c57411 100644
--- a/operator/cmd/cluster/create.go
+++ b/operator/cmd/cluster/create.go
@@ -1,27 +1,118 @@
 package cluster
 
 import (
+       "fmt"
        "github.com/apache/dubbo-kubernetes/dubboctl/pkg/cli"
+       "github.com/apache/dubbo-kubernetes/operator/pkg/util/clog"
+       "github.com/apache/dubbo-kubernetes/pkg/kube"
        "github.com/spf13/cobra"
+       "os"
+       "path/filepath"
+       "strings"
 )
 
-type createArgs struct {
-       skipConfirmation bool
+type templateArgs struct {
+       template string
 }
 
-func addCreateFlags(cmd *cobra.Command, args *uninstallArgs) {
-       cmd.PersistentFlags().BoolVarP(&args.skipConfirmation, 
"skip-confirmation", "y", false, `The skipConfirmation determines whether the 
user is prompted for confirmation.`)
+func addTemplateFlags(cmd *cobra.Command, args *templateArgs) {
+       cmd.PersistentFlags().StringVarP(&args.template, "template", "t", "", 
"java or go sdk template")
 }
 
 func CreateCmd(ctx cli.Context) *cobra.Command {
+       rootArgs := &RootArgs{}
+       tArgs := &templateArgs{}
+       sc := sdkCmd(ctx, rootArgs, tArgs)
        cc := &cobra.Command{
-               Use:     "create",
-               Short:   "",
-               Long:    "",
-               Example: "",
+               Use:   "create",
+               Short: "Create a custom sdk",
                RunE: func(cmd *cobra.Command, args []string) error {
                        return nil
                },
        }
+       addFlags(cc, rootArgs)
+       addFlags(sc, rootArgs)
+       addTemplateFlags(cc, tArgs)
+       cc.AddCommand(sc)
        return cc
 }
+
+func sdkCmd(ctx cli.Context, _ *RootArgs, tArgs *templateArgs) *cobra.Command {
+       return &cobra.Command{
+               Use:   "sdk",
+               Short: "Generates dubbo sdk language templates",
+               Long:  "",
+               Example: `
+    Create a java common in the directory 'mydubbo'.
+    dubboctl create sdk java -t common mydubbo
+
+       Create a go common in the directory ./mydubbo.
+       dubboctl create sdk go -t common mydubbogo
+`,
+               Args: func(cmd *cobra.Command, args []string) error {
+                       if len(args) > 0 {
+                               if args[0] == "java" {
+                                       // TODO
+                                       fmt.Println("This is java sdk.")
+                               }
+                               if args[0] == "go" {
+                                       // TODO
+                                       fmt.Println("This is go sdk.")
+                               }
+                       }
+                       return nil
+               },
+               RunE: func(cmd *cobra.Command, args []string) error {
+                       return nil
+               },
+       }
+}
+
+type createArgs struct {
+       dirname string
+       path    string
+       create  string
+}
+
+func create(kc kube.CLIClient, tArgs *templateArgs, cl clog.Logger) {
+       return
+}
+
+func newCreate(kc kube.CLIClient, tArgs *templateArgs, cl clog.Logger) 
(createArgs, error) {
+       var (
+               path         string
+               dirName      string
+               absolutePath string
+       )
+       dirName, absolutePath = deriveNameAndAbsolutePathFromPath(path)
+
+       _ = createArgs{
+               dirname: dirName,
+               path:    absolutePath,
+       }
+       return createArgs{}, nil
+}
+
+func cwd() (cwd string) {
+       cwd, err := os.Getwd()
+       if err != nil {
+               panic(fmt.Sprintf("Unable to determine current working 
directory: %v", err))
+       }
+       return cwd
+}
+
+func deriveNameAndAbsolutePathFromPath(path string) (string, string) {
+       var absPath string
+
+       if path == "" {
+               path = cwd()
+       }
+
+       absPath, err := filepath.Abs(path)
+       if err != nil {
+               return "", ""
+       }
+
+       pathParts := strings.Split(strings.TrimRight(path, 
string(os.PathSeparator)), string(os.PathSeparator))
+       return pathParts[len(pathParts)-1], absPath
+}
diff --git a/operator/cmd/cluster/manifest.go b/operator/cmd/cluster/manifest.go
index 75656bcc..5e501c43 100644
--- a/operator/cmd/cluster/manifest.go
+++ b/operator/cmd/cluster/manifest.go
@@ -14,13 +14,13 @@ import (
        "strings"
 )
 
-type ManifestGenerateArgs struct {
+type manifestGenerateArgs struct {
        files        []string
        sets         []string
        manifestPath string
 }
 
-func (a *ManifestGenerateArgs) String() string {
+func (a *manifestGenerateArgs) String() string {
        var b strings.Builder
        b.WriteString("files:   " + fmt.Sprint(a.files) + "\n")
        b.WriteString("sets:           " + fmt.Sprint(a.sets) + "\n")
@@ -28,7 +28,7 @@ func (a *ManifestGenerateArgs) String() string {
        return b.String()
 }
 
-func addManifestGenerateFlags(cmd *cobra.Command, args *ManifestGenerateArgs) {
+func addManifestGenerateFlags(cmd *cobra.Command, args *manifestGenerateArgs) {
        cmd.PersistentFlags().StringSliceVarP(&args.files, "filename", "f", 
nil, ``)
        cmd.PersistentFlags().StringArrayVarP(&args.sets, "set", "s", nil, ``)
        cmd.PersistentFlags().StringVarP(&args.manifestPath, "manifests", "d", 
"", ``)
@@ -36,12 +36,12 @@ func addManifestGenerateFlags(cmd *cobra.Command, args 
*ManifestGenerateArgs) {
 
 func ManifestCmd(ctx cli.Context) *cobra.Command {
        rootArgs := &RootArgs{}
-       mgcArgs := &ManifestGenerateArgs{}
-       mgc := ManifestGenerateCmd(ctx, rootArgs, mgcArgs)
+       mgcArgs := &manifestGenerateArgs{}
+       mgc := manifestGenerateCmd(ctx, rootArgs, mgcArgs)
        mc := &cobra.Command{
                Use:   "manifest",
                Short: "dubbo manifest related commands",
-               Long:  "The manifest command will generates and diffs dubbo 
manifests.",
+               Long:  "The manifest command will generates dubbo manifests.",
        }
        addFlags(mc, rootArgs)
        addFlags(mgc, rootArgs)
@@ -52,16 +52,16 @@ func ManifestCmd(ctx cli.Context) *cobra.Command {
 
 var kubeClientFunc func() (kube.CLIClient, error)
 
-func ManifestGenerateCmd(ctx cli.Context, _ *RootArgs, mgArgs 
*ManifestGenerateArgs) *cobra.Command {
+func manifestGenerateCmd(ctx cli.Context, _ *RootArgs, mgArgs 
*manifestGenerateArgs) *cobra.Command {
        return &cobra.Command{
                Use:   "generate",
                Short: "Generates an Dubbo install manifest",
                Long:  "The generate subcommand generates an Dubbo install 
manifest and outputs to the console by default.",
-               Example: `  # Generate a default Istio installation
-  istioctl manifest generate
+               Example: `  # Generate a default Dubbo installation
+  dubboctl manifest generate
   
   # Generate the demo profile
-  istioctl manifest generate --set profile=demo
+  dubboctl manifest generate --set profile=demo
 `,
                Args: func(cmd *cobra.Command, args []string) error {
                        if len(args) != 0 {
@@ -81,7 +81,7 @@ func ManifestGenerateCmd(ctx cli.Context, _ *RootArgs, mgArgs 
*ManifestGenerateA
                        kubeClient = kc
 
                        cl := clog.NewConsoleLogger(cmd.OutOrStdout(), 
cmd.ErrOrStderr(), installerScope)
-                       return ManifestGenerate(kubeClient, mgArgs, cl)
+                       return manifestGenerate(kubeClient, mgArgs, cl)
                },
        }
 }
@@ -90,7 +90,7 @@ const (
        YAMLSeparator = "\n---\n"
 )
 
-func ManifestGenerate(kc kube.CLIClient, mgArgs *ManifestGenerateArgs, cl 
clog.Logger) error {
+func manifestGenerate(kc kube.CLIClient, mgArgs *manifestGenerateArgs, cl 
clog.Logger) error {
        setFlags := applyFlagAliases(mgArgs.sets, mgArgs.manifestPath)
        manifests, _, err := render.GenerateManifest(mgArgs.files, setFlags, 
cl, kc)
        if err != nil {
diff --git a/operator/pkg/schema/schema.go b/operator/pkg/schema/impl.go
similarity index 98%
rename from operator/pkg/schema/schema.go
rename to operator/pkg/schema/impl.go
index cbd758cc..7e1c3aff 100644
--- a/operator/pkg/schema/schema.go
+++ b/operator/pkg/schema/impl.go
@@ -83,7 +83,7 @@ type Builder struct {
        Identifier    string
        Plural        string
        ClusterScoped bool
-       ProtoPkg      string
+       ProtoPackage  string
        Proto         string
        Kind          string
        Group         string
@@ -103,7 +103,7 @@ func (b Builder) BuildNoValidate() Schema {
                },
                plural:        b.Plural,
                clusterScoped: b.ClusterScoped,
-               goPkg:         b.ProtoPkg,
+               goPkg:         b.ProtoPackage,
                proto:         b.Proto,
                apiVersion:    b.Group + "/" + b.Version,
                reflectType:   b.ReflectType,
diff --git a/operator/pkg/uninstall/uninstaller.go 
b/operator/pkg/uninstall/uninstaller.go
index f393669a..abf88469 100644
--- a/operator/pkg/uninstall/uninstaller.go
+++ b/operator/pkg/uninstall/uninstaller.go
@@ -6,9 +6,9 @@ import (
        "github.com/apache/dubbo-kubernetes/operator/pkg/manifest"
        "github.com/apache/dubbo-kubernetes/operator/pkg/util"
        "github.com/apache/dubbo-kubernetes/operator/pkg/util/clog"
+       "github.com/apache/dubbo-kubernetes/operator/pkg/util/pointer"
        "github.com/apache/dubbo-kubernetes/pkg/config/schema/gvk"
        "github.com/apache/dubbo-kubernetes/pkg/kube"
-       "github.com/apache/dubbo-kubernetes/pkg/pointer"
        kerrors "k8s.io/apimachinery/pkg/api/errors"
        metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
        "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
diff --git a/pkg/pointer/pointer.go b/operator/pkg/util/pointer/pointer.go
similarity index 100%
rename from pkg/pointer/pointer.go
rename to operator/pkg/util/pointer/pointer.go
diff --git a/operator/pkg/values/map.go b/operator/pkg/values/map.go
index 4abcac7e..00e5565d 100644
--- a/operator/pkg/values/map.go
+++ b/operator/pkg/values/map.go
@@ -3,7 +3,7 @@ package values
 import (
        "encoding/json"
        "fmt"
-       "github.com/apache/dubbo-kubernetes/pkg/pointer"
+       "github.com/apache/dubbo-kubernetes/operator/pkg/util/pointer"
        "path/filepath"
        "reflect"
        "sigs.k8s.io/yaml"
diff --git a/pkg/art/dubbo-ascii.txt b/pkg/art/dubbo-ascii.txt
index cc81a2c4..bccba1a4 100644
--- a/pkg/art/dubbo-ascii.txt
+++ b/pkg/art/dubbo-ascii.txt
@@ -1,5 +1,5 @@
- ____   _   _  _      _      _____
-|  _ \ | | | || |__  | |__  |  _  |
-| | | || | | || |_ \ | |_ \ | | | |
+.____  ._. ._.._.    ._.    ._____.
+|  _ \ | | | || |__  | |__  | ._. |
+| | | || | | || |_.\ | |_.\ | | | |
 | |_| || |_| || |_| || |_| || |_| |
 |____/ |_____||____/ |____/ |_____|
diff --git a/pkg/kube/collections/collection.gen.go 
b/pkg/kube/collections/collection.gen.go
index d7a4b7ac..c81bfd18 100644
--- a/pkg/kube/collections/collection.gen.go
+++ b/pkg/kube/collections/collection.gen.go
@@ -16,7 +16,7 @@ var (
                Version:       "v1",
                Proto:         
"k8s.io.apiextensions_apiserver.pkg.apis.apiextensions.v1.CustomResourceDefinition",
                ReflectType:   
reflect.TypeOf(&k8sioapiextensionsapiserverpkgapisapiextensionsv1.CustomResourceDefinition{}).Elem(),
-               ProtoPkg:      
"k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1",
+               ProtoPackage:  
"k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1",
                ClusterScoped: true,
                Synthetic:     false,
                Builtin:       true,

Reply via email to