From 1d4b706386b492967acd7b65f1aa40f6cd9d4e7c Mon Sep 17 00:00:00 2001 From: hys Date: Thu, 28 Nov 2024 11:35:01 +0800 Subject: [PATCH] fix: loss config when upgrade --- .../middleware-request/controller_config.go | 17 +++++++---------- pkg/workload/nats/conf.go | 14 ++------------ 2 files changed, 9 insertions(+), 22 deletions(-) diff --git a/cmd/middleware/operator/middleware-request/controller_config.go b/cmd/middleware/operator/middleware-request/controller_config.go index a4fa322..44ae4ac 100644 --- a/cmd/middleware/operator/middleware-request/controller_config.go +++ b/cmd/middleware/operator/middleware-request/controller_config.go @@ -6,6 +6,7 @@ import ( "fmt" "io/ioutil" "os" + "path/filepath" "time" workload_nats "bytetrade.io/web3os/tapr/pkg/workload/nats" @@ -80,15 +81,7 @@ func (c *configmapController) handleAddObject(obj interface{}) { } func (c *configmapController) handleUpdateObject(obj interface{}) { - cm, ok := obj.(*corev1.ConfigMap) - if !ok { - klog.Infof("Not a ConfigMap") - return - } - if cm.Name != "nats-config" { - return - } - c.enqueue(enqueueObj{UPDATE, obj}) + return } func (c *configmapController) Run(workers int) error { @@ -157,12 +150,16 @@ func (c *configmapController) handler(action Action, obj interface{}) error { return errors.New("invalid configmap object") } + if _, err := os.Stat(workload_nats.ConfPath); err == nil { + return nil + } + natsConf, exists := cm.Data["nats.conf"] if !exists { klog.Infof("nats.conf not found in configmap data") return errors.New("nats.conf not found in configmap data") } - err := os.MkdirAll("/dbdata/nats_data/config", 0755) + err := os.MkdirAll(filepath.Dir(workload_nats.ConfPath), 0755) if err != nil { klog.Infof("mkdirall err=%v", err) return err diff --git a/pkg/workload/nats/conf.go b/pkg/workload/nats/conf.go index 744006e..9986666 100644 --- a/pkg/workload/nats/conf.go +++ b/pkg/workload/nats/conf.go @@ -2,13 +2,12 @@ package nats import ( "bytes" - "context" + "io/ioutil" "strings" "text/template" "github.com/mitchellh/mapstructure" load "github.com/nats-io/nats-server/conf" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/klog/v2" ) @@ -79,16 +78,7 @@ func RenderConfigFile(config *Config) error { if err != nil { return err } - clientSet, err := newClientSet() - if err != nil { - return err - } - cm, err := clientSet.CoreV1().ConfigMaps("os-system").Get(context.TODO(), "nats-config", metav1.GetOptions{}) - if err != nil { - return err - } - cm.Data["nats.conf"] = string(data) - _, err = clientSet.CoreV1().ConfigMaps("os-system").Update(context.TODO(), cm, metav1.UpdateOptions{}) + err = ioutil.WriteFile(ConfPath, data, 0644) if err != nil { return err }