Agent not work yet. Need to build Part B first

This commit is contained in:
2026-03-30 22:30:42 +08:00
parent bdbc29649c
commit 3bbd0a00a8
4 changed files with 80 additions and 56 deletions

View File

@@ -41,7 +41,6 @@ start() {
--pidfile "$pidfile" \ --pidfile "$pidfile" \
--stdout "$output_log" \ --stdout "$output_log" \
--stderr "$error_log" \ --stderr "$error_log" \
--respawn \
--respawn-delay 5 \ --respawn-delay 5 \
--respawn-max 0 \ --respawn-max 0 \
-- \ -- \

View File

@@ -10,21 +10,38 @@ import (
"k8s.io/apimachinery/pkg/runtime/schema" "k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/cli-runtime/pkg/genericclioptions" "k8s.io/cli-runtime/pkg/genericclioptions"
"k8s.io/klog/v2" "k8s.io/klog/v2"
monov1alpha1 "undecided.project/monok8s/pkg/apis/monok8s/v1alpha1"
types "undecided.project/monok8s/pkg/apis/monok8s/v1alpha1"
mkscmd "undecided.project/monok8s/pkg/cmd"
"undecided.project/monok8s/pkg/kube" "undecided.project/monok8s/pkg/kube"
"undecided.project/monok8s/pkg/templates"
) )
func NewCmdAgent(flags *genericclioptions.ConfigFlags) *cobra.Command { func NewCmdAgent(flags *genericclioptions.ConfigFlags) *cobra.Command {
var namespace string var namespace string
var envFile string
cmd := &cobra.Command{ cmd := &cobra.Command{
Use: "agent --env-file path", Use: "agent --env-file path",
Short: "Watch OSUpgrade resources and do nothing for now", Short: "Watch OSUpgrade resources and do nothing for now",
RunE: func(cmd *cobra.Command, _ []string) error { RunE: func(cmd *cobra.Command, _ []string) error {
var cfg *types.MonoKSConfig // or value, depending on your API
if err := mkscmd.LoadEnvFile(envFile); err != nil {
return fmt.Errorf("load env file %q: %w", envFile, err)
}
vals := templates.LoadTemplateValuesFromEnv()
rendered := templates.DefaultMonoKSConfig(vals)
cfg = &rendered
klog.InfoS("starting init", "node", cfg.Spec.NodeName, "envFile", envFile)
clients, err := kube.NewClients(flags) clients, err := kube.NewClients(flags)
if err != nil { if err != nil {
return err return err
} }
gvr := schema.GroupVersionResource{Group: monov1alpha1.Group, Version: monov1alpha1.Version, Resource: "osupgrades"} gvr := schema.GroupVersionResource{Group: types.Group, Version: types.Version, Resource: "osupgrades"}
ctx := cmd.Context() ctx := cmd.Context()
for { for {
list, err := clients.Dynamic.Resource(gvr).Namespace(namespace).List(ctx, metav1.ListOptions{}) list, err := clients.Dynamic.Resource(gvr).Namespace(namespace).List(ctx, metav1.ListOptions{})
@@ -44,6 +61,7 @@ func NewCmdAgent(flags *genericclioptions.ConfigFlags) *cobra.Command {
}, },
} }
cmd.Flags().StringVar(&namespace, "namespace", "kube-system", "namespace to watch") cmd.Flags().StringVar(&namespace, "namespace", "kube-system", "namespace to watch")
cmd.Flags().StringVar(&envFile, "env-file", "", "path to env file containing MKS_* variables")
return cmd return cmd
} }

View File

@@ -0,0 +1,57 @@
package cmd
import (
"bufio"
"fmt"
"os"
"strings"
)
func LoadEnvFile(path string) error {
f, err := os.Open(path)
if err != nil {
return err
}
defer f.Close()
scanner := bufio.NewScanner(f)
lineNum := 0
for scanner.Scan() {
lineNum++
line := strings.TrimSpace(scanner.Text())
if line == "" || strings.HasPrefix(line, "#") {
continue
}
key, val, ok := strings.Cut(line, "=")
if !ok {
return fmt.Errorf("line %d: expected KEY=VALUE", lineNum)
}
key = strings.TrimSpace(key)
val = strings.TrimSpace(val)
if key == "" {
return fmt.Errorf("line %d: empty variable name", lineNum)
}
// Remove matching single or double quotes around the whole value.
if len(val) >= 2 {
if (val[0] == '"' && val[len(val)-1] == '"') || (val[0] == '\'' && val[len(val)-1] == '\'') {
val = val[1 : len(val)-1]
}
}
if err := os.Setenv(key, val); err != nil {
return fmt.Errorf("line %d: set %q: %w", lineNum, key, err)
}
}
if err := scanner.Err(); err != nil {
return err
}
return nil
}

View File

@@ -1,9 +1,7 @@
package initcmd package initcmd
import ( import (
"bufio"
"fmt" "fmt"
"os"
"sort" "sort"
"strconv" "strconv"
"strings" "strings"
@@ -16,6 +14,7 @@ import (
"undecided.project/monok8s/pkg/config" "undecided.project/monok8s/pkg/config"
types "undecided.project/monok8s/pkg/apis/monok8s/v1alpha1" types "undecided.project/monok8s/pkg/apis/monok8s/v1alpha1"
mkscmd "undecided.project/monok8s/pkg/cmd"
"undecided.project/monok8s/pkg/templates" "undecided.project/monok8s/pkg/templates"
) )
@@ -65,7 +64,7 @@ Supported formats:
} }
if strings.TrimSpace(envFile) != "" { if strings.TrimSpace(envFile) != "" {
if err := loadEnvFile(envFile); err != nil { if err := mkscmd.LoadEnvFile(envFile); err != nil {
return fmt.Errorf("load env file %q: %w", envFile, err) return fmt.Errorf("load env file %q: %w", envFile, err)
} }
} }
@@ -74,7 +73,7 @@ Supported formats:
switch { switch {
case strings.TrimSpace(envFile) != "": case strings.TrimSpace(envFile) != "":
if err := loadEnvFile(envFile); err != nil { if err := mkscmd.LoadEnvFile(envFile); err != nil {
return fmt.Errorf("load env file %q: %w", envFile, err) return fmt.Errorf("load env file %q: %w", envFile, err)
} }
vals := templates.LoadTemplateValuesFromEnv() vals := templates.LoadTemplateValuesFromEnv()
@@ -133,55 +132,6 @@ Supported formats:
return cmd return cmd
} }
func loadEnvFile(path string) error {
f, err := os.Open(path)
if err != nil {
return err
}
defer f.Close()
scanner := bufio.NewScanner(f)
lineNum := 0
for scanner.Scan() {
lineNum++
line := strings.TrimSpace(scanner.Text())
if line == "" || strings.HasPrefix(line, "#") {
continue
}
key, val, ok := strings.Cut(line, "=")
if !ok {
return fmt.Errorf("line %d: expected KEY=VALUE", lineNum)
}
key = strings.TrimSpace(key)
val = strings.TrimSpace(val)
if key == "" {
return fmt.Errorf("line %d: empty variable name", lineNum)
}
// Remove matching single or double quotes around the whole value.
if len(val) >= 2 {
if (val[0] == '"' && val[len(val)-1] == '"') || (val[0] == '\'' && val[len(val)-1] == '\'') {
val = val[1 : len(val)-1]
}
}
if err := os.Setenv(key, val); err != nil {
return fmt.Errorf("line %d: set %q: %w", lineNum, key, err)
}
}
if err := scanner.Err(); err != nil {
return err
}
return nil
}
func parseStepSelection(raw string, max int) (bootstrap.StepSelection, error) { func parseStepSelection(raw string, max int) (bootstrap.StepSelection, error) {
raw = strings.TrimSpace(raw) raw = strings.TrimSpace(raw)
if raw == "" { if raw == "" {