feat : add create inventory cmd (#2853)

feat : add create inventory cmd



feat: feat no root ssh

Signed-off-by: xuesongzuo@yunify.com <xuesongzuo@yunify.com>
This commit is contained in:
zuoxuesong-worker 2025-11-13 16:50:30 +08:00 committed by GitHub
parent dcce8df095
commit 64262bf6c3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 71 additions and 2 deletions

View File

@ -5,7 +5,7 @@ spec:
# if set as "cn", so that online downloads will try to use available domestic sources whenever possible.
zone: ""
kubernetes:
kube_version: { { .kube_version } }
kube_version: {{ .kube_version }}
# helm binary
helm_version: v3.18.5
etcd:
@ -42,7 +42,6 @@ spec:
# runc binary
runc_version: v1.1.7
cni:
type: cilium
ipv6_support: false
multus:
image:

View File

@ -34,6 +34,7 @@ func NewCreateCommand() *cobra.Command {
}
cmd.AddCommand(newCreateClusterCommand())
cmd.AddCommand(newCreateConfigCommand())
cmd.AddCommand(newCreateInventoryCommand())
return cmd
}
@ -78,3 +79,21 @@ func newCreateConfigCommand() *cobra.Command {
return cmd
}
func newCreateInventoryCommand() *cobra.Command {
o := builtin.NewCreateInventoryOptions()
cmd := &cobra.Command{
Use: "inventory",
Short: "Create a default inventory",
RunE: func(cmd *cobra.Command, _ []string) error {
return o.Run()
},
}
flags := cmd.Flags()
for _, f := range o.Flags().FlagSets {
flags.AddFlagSet(f)
}
return cmd
}

View File

@ -51,6 +51,10 @@ var getInventory options.InventoryFunc = func() (*kkcorev1.Inventory, error) {
return inventory, errors.Wrap(yaml.Unmarshal(data, inventory), "failed to unmarshal local inventory file: %q.")
}
func getInventoryData() ([]byte, error) {
return core.Defaults.ReadFile("defaults/inventory/localhost.yaml")
}
func getConfig(kubeVersion string) ([]byte, error) {
t, err := template.ParseFS(core.Defaults, fmt.Sprintf("defaults/config/%s.yaml", kubeVersion[:5]))
if err != nil {

View File

@ -157,3 +157,50 @@ func (o *CreateConfigOptions) Run() error {
return nil
}
// ======================================================================================
// create inventory
// ======================================================================================
// NewCreateInventoryOptions for newCreateInventoryCommand
func NewCreateInventoryOptions() *CreateInventoryOptions {
// set default value
return &CreateInventoryOptions{}
}
// CreateInventoryOptions for NewCreateInventoryOptions
type CreateInventoryOptions struct {
// OutputDir for inventory file. if set will generate file in this dir
OutputDir string
}
// Flags add to newCreateInventoryCommand
func (o *CreateInventoryOptions) Flags() cliflag.NamedFlagSets {
fss := cliflag.NamedFlagSets{}
kfs := fss.FlagSet("inventory")
kfs.StringVarP(&o.OutputDir, "output", "o", o.OutputDir, "Output dir for inventory. if not set will output to stdout")
return fss
}
func (o *CreateInventoryOptions) Run() error {
data, err := getInventoryData()
if err != nil {
return err
}
if o.OutputDir != "" {
// Write inventory to file if output directory is specified
filename := filepath.Join(o.OutputDir, fmt.Sprintf("inventory.yaml"))
if err := os.WriteFile(filename, data, os.ModePerm); err != nil {
return errors.Wrapf(err, "failed to write inventory file to %s", filename)
}
fmt.Printf("write inventory file to %s success.\n", filename)
} else {
// Print inventory to stdout if no output directory specified
fmt.Println(string(data))
}
return nil
}