commit 3bbd0a00a80960cb3f767bfa8ebb522a55288bed56b7fbef61d57022140228eb
| author | 斟酌 鵬兄 <tgckpg@gmail.com> |
| date | 2026-03-30T14:30:42Z |
| subject | Agent not work yet. Need to build Part B first |
commit 3bbd0a00a80960cb3f767bfa8ebb522a55288bed56b7fbef61d57022140228eb
Author: 斟酌 鵬兄 <tgckpg@gmail.com>
Date: 2026-03-30T14:30:42Z
Agent not work yet. Need to build Part B first
---
alpine/rootfs-extra/etc/init.d/control-agent | 1 -
clitools/pkg/cmd/agent/agent.go | 22 ++++++++++-
clitools/pkg/cmd/helpers.go | 57 ++++++++++++++++++++++++++++
clitools/pkg/cmd/initcmd/init.go | 56 ++-------------------------
4 files changed, 80 insertions(+), 56 deletions(-)
diff --git a/alpine/rootfs-extra/etc/init.d/control-agent b/alpine/rootfs-extra/etc/init.d/control-agent
index 2f10193..1dc276b 100755
--- a/alpine/rootfs-extra/etc/init.d/control-agent
+++ b/alpine/rootfs-extra/etc/init.d/control-agent
@@ -41,7 +41,6 @@ start() {
--pidfile "$pidfile" \
--stdout "$output_log" \
--stderr "$error_log" \
- --respawn \
--respawn-delay 5 \
--respawn-max 0 \
-- \
diff --git a/clitools/pkg/cmd/agent/agent.go b/clitools/pkg/cmd/agent/agent.go
index b79f91c..068cbbd 100644
--- a/clitools/pkg/cmd/agent/agent.go
+++ b/clitools/pkg/cmd/agent/agent.go
@@ -10,21 +10,38 @@ import (
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/cli-runtime/pkg/genericclioptions"
"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/templates"
)
func NewCmdAgent(flags *genericclioptions.ConfigFlags) *cobra.Command {
var namespace string
+ var envFile string
+
cmd := &cobra.Command{
Use: "agent --env-file path",
Short: "Watch OSUpgrade resources and do nothing for now",
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)
if err != nil {
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()
for {
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(&envFile, "env-file", "", "path to env file containing MKS_* variables")
return cmd
}
diff --git a/clitools/pkg/cmd/helpers.go b/clitools/pkg/cmd/helpers.go
new file mode 100644
index 0000000..07108d5
--- /dev/null
+++ b/clitools/pkg/cmd/helpers.go
@@ -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
+}
diff --git a/clitools/pkg/cmd/initcmd/init.go b/clitools/pkg/cmd/initcmd/init.go
index 03fdaf6..5a1ffe8 100644
--- a/clitools/pkg/cmd/initcmd/init.go
+++ b/clitools/pkg/cmd/initcmd/init.go
@@ -1,9 +1,7 @@
package initcmd
import (
- "bufio"
"fmt"
- "os"
"sort"
"strconv"
"strings"
@@ -16,6 +14,7 @@ import (
"undecided.project/monok8s/pkg/config"
types "undecided.project/monok8s/pkg/apis/monok8s/v1alpha1"
+ mkscmd "undecided.project/monok8s/pkg/cmd"
"undecided.project/monok8s/pkg/templates"
)
@@ -65,7 +64,7 @@ Supported formats:
}
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)
}
}
@@ -74,7 +73,7 @@ Supported formats:
switch {
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)
}
vals := templates.LoadTemplateValuesFromEnv()
@@ -133,55 +132,6 @@ Supported formats:
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) {
raw = strings.TrimSpace(raw)
if raw == "" {