@@ -4,8 +4,10 @@ import (
44 "net/url"
55 "os"
66 "path/filepath"
7+ "sync/atomic"
78
89 "github.com/dustin/go-humanize"
10+ "github.com/modelpack/model-csi-driver/pkg/logger"
911 "github.com/pkg/errors"
1012 "gopkg.in/yaml.v2"
1113)
@@ -28,7 +30,7 @@ func (s *HumanizeSize) UnmarshalYAML(unmarshal func(interface{}) error) error {
2830 return nil
2931}
3032
31- type Config struct {
33+ type RawConfig struct {
3234 // Pattern:
3335 // static: /var/lib/dragonfly/model-csi/volumes/$volumeName/model
3436 // dynamic: /var/lib/dragonfly/model-csi/volumes/$volumeName/models
@@ -40,7 +42,7 @@ type Config struct {
4042 DynamicCSIEndpoint string `yaml:"dynamic_csi_endpoint"`
4143 CSIEndpoint string `yaml:"csi_endpoint"`
4244 MetricsAddr string `yaml:"metrics_addr"`
43- TraceEndpooint string `yaml:"trace_endpoint"`
45+ TraceEndpoint string `yaml:"trace_endpoint"`
4446 PprofAddr string `yaml:"pprof_addr"`
4547 PullConfig PullConfig `yaml:"pull_config"`
4648 Features Features `yaml:"features"`
@@ -61,89 +63,89 @@ type PullConfig struct {
6163 PullLayerTimeoutInSeconds uint `yaml:"pull_layer_timeout_in_seconds"`
6264}
6365
64- func (cfg * Config ) ParameterKeyType () string {
66+ func (cfg * RawConfig ) ParameterKeyType () string {
6567 return cfg .ServiceName + "/type"
6668}
6769
68- func (cfg * Config ) ParameterKeyReference () string {
70+ func (cfg * RawConfig ) ParameterKeyReference () string {
6971 return cfg .ServiceName + "/reference"
7072}
7173
72- func (cfg * Config ) ParameterKeyMountID () string {
74+ func (cfg * RawConfig ) ParameterKeyMountID () string {
7375 return cfg .ServiceName + "/mount-id"
7476}
7577
76- func (cfg * Config ) ParameterKeyStatusState () string {
78+ func (cfg * RawConfig ) ParameterKeyStatusState () string {
7779 return cfg .ServiceName + "/status/state"
7880}
7981
80- func (cfg * Config ) ParameterKeyStatusProgress () string {
82+ func (cfg * RawConfig ) ParameterKeyStatusProgress () string {
8183 return cfg .ServiceName + "/status/progress"
8284}
8385
84- func (cfg * Config ) ParameterVolumeContextNodeIP () string {
86+ func (cfg * RawConfig ) ParameterVolumeContextNodeIP () string {
8587 return cfg .ServiceName + "/node-ip"
8688}
8789
88- func (cfg * Config ) ParameterKeyCheckDiskQuota () string {
90+ func (cfg * RawConfig ) ParameterKeyCheckDiskQuota () string {
8991 return cfg .ServiceName + "/check-disk-quota"
9092}
9193
9294// /var/lib/dragonfly/model-csi/volumes
93- func (cfg * Config ) GetVolumesDir () string {
95+ func (cfg * RawConfig ) GetVolumesDir () string {
9496 return filepath .Join (cfg .RootDir , "volumes" )
9597}
9698
9799// /var/lib/dragonfly/model-csi/volumes/$volumeName
98- func (cfg * Config ) GetVolumeDir (volumeName string ) string {
100+ func (cfg * RawConfig ) GetVolumeDir (volumeName string ) string {
99101 return filepath .Join (cfg .GetVolumesDir (), volumeName )
100102}
101103
102104// /var/lib/dragonfly/model-csi/volumes/$volumeName/model
103- func (cfg * Config ) GetModelDir (volumeName string ) string {
105+ func (cfg * RawConfig ) GetModelDir (volumeName string ) string {
104106 return filepath .Join (cfg .GetVolumesDir (), volumeName , "model" )
105107}
106108
107109// /var/lib/dragonfly/model-csi/volumes/$volumeName
108- func (cfg * Config ) GetVolumeDirForDynamic (volumeName string ) string {
110+ func (cfg * RawConfig ) GetVolumeDirForDynamic (volumeName string ) string {
109111 return filepath .Join (cfg .GetVolumesDir (), volumeName )
110112}
111113
112114// /var/lib/dragonfly/model-csi/volumes/$volumeName/models
113- func (cfg * Config ) GetModelsDirForDynamic (volumeName string ) string {
115+ func (cfg * RawConfig ) GetModelsDirForDynamic (volumeName string ) string {
114116 return filepath .Join (cfg .GetVolumeDirForDynamic (volumeName ), "models" )
115117}
116118
117119// /var/lib/dragonfly/model-csi/volumes/$volumeName/models/$mountID
118- func (cfg * Config ) GetMountIDDirForDynamic (volumeName , mountID string ) string {
120+ func (cfg * RawConfig ) GetMountIDDirForDynamic (volumeName , mountID string ) string {
119121 return filepath .Join (cfg .GetVolumeDirForDynamic (volumeName ), "models" , mountID )
120122}
121123
122124// /var/lib/dragonfly/model-csi/volumes/$volumeName/models/$mountID/model
123- func (cfg * Config ) GetModelDirForDynamic (volumeName , mountID string ) string {
125+ func (cfg * RawConfig ) GetModelDirForDynamic (volumeName , mountID string ) string {
124126 return filepath .Join (cfg .GetVolumeDirForDynamic (volumeName ), "models" , mountID , "model" )
125127}
126128
127129// /var/lib/dragonfly/model-csi/volumes/$volumeName/csi
128- func (cfg * Config ) GetCSISockDirForDynamic (volumeName string ) string {
130+ func (cfg * RawConfig ) GetCSISockDirForDynamic (volumeName string ) string {
129131 return filepath .Join (cfg .GetVolumeDirForDynamic (volumeName ), "csi" )
130132}
131133
132- func (cfg * Config ) IsControllerMode () bool {
134+ func (cfg * RawConfig ) IsControllerMode () bool {
133135 return cfg .Mode == "controller"
134136}
135137
136- func (cfg * Config ) IsNodeMode () bool {
138+ func (cfg * RawConfig ) IsNodeMode () bool {
137139 return cfg .Mode == "node"
138140}
139141
140- func parse (path string ) (* Config , error ) {
142+ func parse (path string ) (* RawConfig , error ) {
141143 data , err := os .ReadFile (path )
142144 if err != nil {
143145 return nil , errors .Wrap (err , "read config file" )
144146 }
145147
146- var cfg Config
148+ var cfg RawConfig
147149 if err := yaml .Unmarshal (data , & cfg ); err != nil {
148150 return nil , errors .Wrap (err , "unmarshal config file" )
149151 }
@@ -206,13 +208,46 @@ func parse(path string) (*Config, error) {
206208 return & cfg , nil
207209}
208210
211+ type Config struct {
212+ atomic.Value
213+ }
214+
209215func New (path string ) (* Config , error ) {
210216 cfg , err := parse (path )
211217 if err != nil {
212218 return nil , err
213219 }
214220
215- go cfg .watch (path )
221+ atomicCfg := NewWithRaw (cfg )
222+
223+ go atomicCfg .watch (path )
224+
225+ return atomicCfg , nil
226+ }
227+
228+ func NewWithRaw (cfg * RawConfig ) * Config {
229+ atomicCfg := & Config {
230+ Value : atomic.Value {},
231+ }
232+ atomicCfg .Store (cfg )
233+ return atomicCfg
234+ }
235+
236+ func (cfg * Config ) Get () * RawConfig {
237+ return cfg .Load ().(* RawConfig )
238+ }
239+
240+ func (cfg * Config ) reload (path string ) {
241+ newCfg , err := parse (path )
242+ if err != nil {
243+ logger .Logger ().WithError (err ).Error ("failed to parse config file" )
244+ return
245+ }
246+
247+ mutex .Lock ()
248+ defer mutex .Unlock ()
249+
250+ cfg .Store (newCfg )
216251
217- return cfg , nil
252+ logger . Logger (). Infof ( "config reloaded: %s" , path )
218253}
0 commit comments