Skip to content

Commit

Permalink
Merge pull request #31 from beclab/fix/nats
Browse files Browse the repository at this point in the history
fix: loss nats config when upgrade
  • Loading branch information
hysyeah authored Nov 28, 2024
2 parents ac1d3ac + 1d4b706 commit e165839
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 22 deletions.
17 changes: 7 additions & 10 deletions cmd/middleware/operator/middleware-request/controller_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"time"

workload_nats "bytetrade.io/web3os/tapr/pkg/workload/nats"
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
14 changes: 2 additions & 12 deletions pkg/workload/nats/conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit e165839

Please sign in to comment.