Can ctl init with index

This commit is contained in:
2026-03-29 23:55:59 +08:00
parent 03a5e5bedb
commit 5fbc2846a1
5 changed files with 334 additions and 57 deletions

View File

@@ -1,21 +1,35 @@
package initcmd
import (
"context"
"fmt"
"sort"
"strconv"
"strings"
"undecided.project/monok8s/pkg/bootstrap"
"undecided.project/monok8s/pkg/config"
"github.com/spf13/cobra"
"k8s.io/cli-runtime/pkg/genericclioptions"
"k8s.io/klog/v2"
"undecided.project/monok8s/pkg/bootstrap"
"undecided.project/monok8s/pkg/config"
)
func NewCmdInit(_ *genericclioptions.ConfigFlags) *cobra.Command {
var configPath string
cmd := &cobra.Command{
Use: "init",
Short: "Equivalent of apply-node-config + bootstrap-cluster",
RunE: func(cmd *cobra.Command, _ []string) error {
Use: "init [list|STEPSEL]",
Short: "Start the bootstrap process for this node",
Example: strings.TrimSpace(`
ctl init
ctl init list
ctl init 1-3
ctl init -3
ctl init 3-
ctl init 1,3,5
`),
Args: cobra.MaximumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
path, err := (config.Loader{}).ResolvePath(configPath)
if err != nil {
return err
@@ -24,11 +38,135 @@ func NewCmdInit(_ *genericclioptions.ConfigFlags) *cobra.Command {
if err != nil {
return err
}
runner := bootstrap.NewRunner(cfg)
if len(args) == 1 && strings.EqualFold(strings.TrimSpace(args[0]), "list") {
steps := runner.InitSteps()
fmt.Fprintln(cmd.OutOrStdout(), "Showing current bootstrap sequence")
// width = number of digits of max step number
width := len(fmt.Sprintf("%d", len(steps)))
for i, s := range steps {
fmt.Fprintf(cmd.OutOrStdout(), "\n %*d. %s\n", width, i+1, s.Name)
fmt.Fprintf(cmd.OutOrStdout(), " %s\n", s.Desc)
}
return err
}
klog.InfoS("starting init", "config", path, "node", cfg.Spec.NodeName)
return bootstrap.NewRunner(cfg).Init(cmd.Context())
if len(args) == 0 {
return runner.Init(cmd.Context())
}
steps := runner.InitSteps()
sel, err := parseStepSelection(args[0], len(steps))
if err != nil {
return err
}
return runner.InitSelected(cmd.Context(), sel)
},
}
cmd.Flags().StringVarP(&configPath, "config", "c", "", "path to MonoKSConfig yaml")
_ = context.Background()
return cmd
}
func parseStepSelection(raw string, max int) (bootstrap.StepSelection, error) {
raw = strings.TrimSpace(raw)
if raw == "" {
return bootstrap.StepSelection{}, fmt.Errorf("empty step selection")
}
if max <= 0 {
return bootstrap.StepSelection{}, fmt.Errorf("no steps available")
}
selected := map[int]struct{}{}
parts := strings.Split(raw, ",")
for _, part := range parts {
part = strings.TrimSpace(part)
if part == "" {
return bootstrap.StepSelection{}, fmt.Errorf("invalid empty selector in %q", raw)
}
if strings.Contains(part, "-") {
if strings.Count(part, "-") != 1 {
return bootstrap.StepSelection{}, fmt.Errorf("invalid range %q", part)
}
bounds := strings.SplitN(part, "-", 2)
left := strings.TrimSpace(bounds[0])
right := strings.TrimSpace(bounds[1])
var start, end int
switch {
case left == "" && right == "":
return bootstrap.StepSelection{}, fmt.Errorf("invalid range %q", part)
case left == "":
n, err := parseStepNumber(right, max)
if err != nil {
return bootstrap.StepSelection{}, err
}
start, end = 1, n
case right == "":
n, err := parseStepNumber(left, max)
if err != nil {
return bootstrap.StepSelection{}, err
}
start, end = n, max
default:
a, err := parseStepNumber(left, max)
if err != nil {
return bootstrap.StepSelection{}, err
}
b, err := parseStepNumber(right, max)
if err != nil {
return bootstrap.StepSelection{}, err
}
start, end = a, b
}
if start > end {
return bootstrap.StepSelection{}, fmt.Errorf("invalid descending range %q", part)
}
for i := start; i <= end; i++ {
selected[i] = struct{}{}
}
continue
}
n, err := parseStepNumber(part, max)
if err != nil {
return bootstrap.StepSelection{}, err
}
selected[n] = struct{}{}
}
out := make([]int, 0, len(selected))
for n := range selected {
out = append(out, n)
}
sort.Ints(out)
return bootstrap.StepSelection{Indices: out}, nil
}
func parseStepNumber(raw string, max int) (int, error) {
n, err := strconv.Atoi(strings.TrimSpace(raw))
if err != nil {
return 0, fmt.Errorf("invalid step number %q", raw)
}
if n < 1 || n > max {
return 0, fmt.Errorf("step number %d out of range 1-%d", n, max)
}
return n, nil
}

View File

@@ -2,6 +2,7 @@ package root
import (
"flag"
"os"
"github.com/spf13/cobra"
"k8s.io/cli-runtime/pkg/genericclioptions"
@@ -15,6 +16,16 @@ import (
versioncmd "undecided.project/monok8s/pkg/cmd/version"
)
func init() {
klog.InitFlags(nil)
if os.Getenv("DEBUG") != "" {
_ = flag.Set("v", "4") // debug level
} else {
_ = flag.Set("v", "0")
}
}
func NewRootCmd() *cobra.Command {
flags := genericclioptions.NewConfigFlags(true)
@@ -24,7 +35,6 @@ func NewRootCmd() *cobra.Command {
SilenceUsage: true,
SilenceErrors: true,
PersistentPreRun: func(*cobra.Command, []string) {
klog.InitFlags(nil)
_ = flag.Set("logtostderr", "true")
},
}