mirror of
https://github.com/kubesphere/kubekey.git
synced 2025-12-26 01:22:51 +00:00
feat: add create config command (#2527)
Signed-off-by: joyceliu <joyceliu@yunify.com> Co-authored-by: joyceliu <joyceliu@yunify.com>
This commit is contained in:
parent
2c19021fb2
commit
6612e69054
|
|
@ -33,6 +33,7 @@ func NewCreateCommand() *cobra.Command {
|
|||
Short: "Create a cluster or a cluster configuration file",
|
||||
}
|
||||
cmd.AddCommand(newCreateClusterCommand())
|
||||
cmd.AddCommand(newCreateConfigCommand())
|
||||
|
||||
return cmd
|
||||
}
|
||||
|
|
@ -59,3 +60,21 @@ func newCreateClusterCommand() *cobra.Command {
|
|||
|
||||
return cmd
|
||||
}
|
||||
|
||||
func newCreateConfigCommand() *cobra.Command {
|
||||
o := builtin.NewCreateConfigOptions()
|
||||
|
||||
cmd := &cobra.Command{
|
||||
Use: "config",
|
||||
Short: "Create a Kubernetes or KubeSphere cluster",
|
||||
RunE: func(cmd *cobra.Command, _ []string) error {
|
||||
return o.Run()
|
||||
},
|
||||
}
|
||||
flags := cmd.Flags()
|
||||
for _, f := range o.Flags().FlagSets {
|
||||
flags.AddFlagSet(f)
|
||||
}
|
||||
|
||||
return cmd
|
||||
}
|
||||
|
|
|
|||
|
|
@ -21,6 +21,8 @@ package builtin
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/cockroachdb/errors"
|
||||
kkcorev1 "github.com/kubesphere/kubekey/api/core/v1"
|
||||
|
|
@ -29,6 +31,7 @@ import (
|
|||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
||||
cliflag "k8s.io/component-base/cli/flag"
|
||||
|
||||
"github.com/kubesphere/kubekey/v4/builtin/core"
|
||||
"github.com/kubesphere/kubekey/v4/cmd/kk/app/options"
|
||||
)
|
||||
|
||||
|
|
@ -37,6 +40,10 @@ const (
|
|||
defaultContainerManager = "docker"
|
||||
)
|
||||
|
||||
// ======================================================================================
|
||||
// create cluster
|
||||
// ======================================================================================
|
||||
|
||||
// NewCreateClusterOptions for newCreateClusterCommand
|
||||
func NewCreateClusterOptions() *CreateClusterOptions {
|
||||
// set default value
|
||||
|
|
@ -116,3 +123,58 @@ func (o *CreateClusterOptions) completeConfig() error {
|
|||
|
||||
return nil
|
||||
}
|
||||
|
||||
// ======================================================================================
|
||||
// create config
|
||||
// ======================================================================================
|
||||
|
||||
// NewCreateConfigOptions for newCreateConfigCommand
|
||||
func NewCreateConfigOptions() *CreateConfigOptions {
|
||||
// set default value
|
||||
return &CreateConfigOptions{
|
||||
Kubernetes: defaultKubeVersion,
|
||||
}
|
||||
}
|
||||
|
||||
// CreateConfigOptions for NewCreateConfigOptions
|
||||
type CreateConfigOptions struct {
|
||||
// kubernetes version which the config will install.
|
||||
Kubernetes string
|
||||
// OutputDir for config file. if set will generate file in this dir
|
||||
OutputDir string
|
||||
}
|
||||
|
||||
// Flags add to newCreateConfigCommand
|
||||
func (o *CreateConfigOptions) Flags() cliflag.NamedFlagSets {
|
||||
fss := cliflag.NamedFlagSets{}
|
||||
kfs := fss.FlagSet("config")
|
||||
kfs.StringVar(&o.Kubernetes, "with-kubernetes", o.Kubernetes, fmt.Sprintf("Specify a supported version of kubernetes. default is %s", o.Kubernetes))
|
||||
kfs.StringVarP(&o.OutputDir, "output", "o", o.OutputDir, "Output dir for config. if not set will output to stdout")
|
||||
|
||||
return fss
|
||||
}
|
||||
|
||||
// Run executes the create config operation. It reads the default config file for the specified
|
||||
// Kubernetes version and either writes it to the specified output directory or prints it to stdout.
|
||||
// If an output directory is specified, it creates a config file named "config-<kubernetes-version>.yaml".
|
||||
func (o *CreateConfigOptions) Run() error {
|
||||
// Read the default config file for the specified Kubernetes version
|
||||
data, err := core.Defaults.ReadFile(fmt.Sprintf("defaults/config/%s.yaml", o.Kubernetes))
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "failed to get local configFile for kube_version: %q.")
|
||||
}
|
||||
|
||||
if o.OutputDir != "" {
|
||||
// Write config to file if output directory is specified
|
||||
filename := filepath.Join(o.OutputDir, fmt.Sprintf("config-%s.yaml", o.Kubernetes))
|
||||
if err := os.WriteFile(filename, data, os.ModePerm); err != nil {
|
||||
return errors.Wrapf(err, "failed to write config file to %s", filename)
|
||||
}
|
||||
fmt.Printf("write config file to %s success.\n", filename)
|
||||
} else {
|
||||
// Print config to stdout if no output directory specified
|
||||
fmt.Println(string(data))
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue