Skip to content

Commit aec799e

Browse files
authored
Merge pull request #625 from imeoer/revert-config
Revert "feat: Remove DumpFile operations"
2 parents 021c505 + 9ec5a73 commit aec799e

File tree

12 files changed

+91
-178
lines changed

12 files changed

+91
-178
lines changed

config/daemonconfig/daemonconfig.go

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ package daemonconfig
99

1010
import (
1111
"encoding/json"
12+
"os"
1213
"reflect"
1314
"strings"
14-
"sync"
1515

1616
"github.com/pkg/errors"
1717

@@ -36,6 +36,7 @@ type DaemonConfig interface {
3636
StorageBackend() (StorageBackendType, *BackendConfig)
3737
UpdateMirrors(mirrorsConfigDir, registryHost string) error
3838
DumpString() (string, error)
39+
DumpFile(path string) error
3940
}
4041

4142
// Daemon configurations factory
@@ -121,14 +122,19 @@ type DeviceConfig struct {
121122
} `json:"cache"`
122123
}
123124

124-
var configRWMutex sync.RWMutex
125+
// For nydusd as FUSE daemon. Serialize Daemon info and persist to a json file
126+
// We don't have to persist configuration file for fscache since its configuration
127+
// is passed through HTTP API.
128+
func DumpConfigFile(c interface{}, path string) error {
129+
if config.IsBackendSourceEnabled() {
130+
c = serializeWithSecretFilter(c)
131+
}
132+
b, err := json.Marshal(c)
133+
if err != nil {
134+
return errors.Wrapf(err, "marshal config")
135+
}
125136

126-
type SupplementInfoInterface interface {
127-
GetImageID() string
128-
GetSnapshotID() string
129-
IsVPCRegistry() bool
130-
GetLabels() map[string]string
131-
GetParams() map[string]string
137+
return os.WriteFile(path, b, 0600)
132138
}
133139

134140
func DumpConfigString(c interface{}) (string, error) {
@@ -137,22 +143,20 @@ func DumpConfigString(c interface{}) (string, error) {
137143
}
138144

139145
// Achieve a daemon configuration from template or snapshotter's configuration
140-
func SupplementDaemonConfig(c DaemonConfig, info SupplementInfoInterface) error {
141-
142-
configRWMutex.Lock()
143-
defer configRWMutex.Unlock()
146+
func SupplementDaemonConfig(c DaemonConfig, imageID, snapshotID string,
147+
vpcRegistry bool, labels map[string]string, params map[string]string) error {
144148

145-
image, err := registry.ParseImage(info.GetImageID())
149+
image, err := registry.ParseImage(imageID)
146150
if err != nil {
147-
return errors.Wrapf(err, "parse image %s", info.GetImageID())
151+
return errors.Wrapf(err, "parse image %s", imageID)
148152
}
149153

150154
backendType, _ := c.StorageBackend()
151155

152156
switch backendType {
153157
case backendTypeRegistry:
154158
registryHost := image.Host
155-
if info.IsVPCRegistry() {
159+
if vpcRegistry {
156160
registryHost = registry.ConvertToVPCHost(registryHost)
157161
} else if registryHost == "docker.io" {
158162
// For docker.io images, we should use index.docker.io
@@ -166,8 +170,8 @@ func SupplementDaemonConfig(c DaemonConfig, info SupplementInfoInterface) error
166170
// If no auth is provided, don't touch auth from provided nydusd configuration file.
167171
// We don't validate the original nydusd auth from configuration file since it can be empty
168172
// when repository is public.
169-
keyChain := auth.GetRegistryKeyChain(registryHost, info.GetImageID(), info.GetLabels())
170-
c.Supplement(registryHost, image.Repo, info.GetSnapshotID(), info.GetParams())
173+
keyChain := auth.GetRegistryKeyChain(registryHost, imageID, labels)
174+
c.Supplement(registryHost, image.Repo, snapshotID, params)
171175
c.FillAuth(keyChain)
172176

173177
// Localfs and OSS backends don't need any update,

config/daemonconfig/fscache.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ package daemonconfig
99
import (
1010
"encoding/json"
1111
"os"
12+
"path"
1213

1314
"github.com/containerd/log"
1415
"github.com/containerd/nydus-snapshotter/pkg/auth"
@@ -120,3 +121,11 @@ func (c *FscacheDaemonConfig) FillAuth(kc *auth.PassKeyChain) {
120121
func (c *FscacheDaemonConfig) DumpString() (string, error) {
121122
return DumpConfigString(c)
122123
}
124+
125+
func (c *FscacheDaemonConfig) DumpFile(f string) error {
126+
if err := os.MkdirAll(path.Dir(f), 0755); err != nil {
127+
return err
128+
}
129+
130+
return DumpConfigFile(c, f)
131+
}

config/daemonconfig/fuse.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ package daemonconfig
99
import (
1010
"encoding/json"
1111
"os"
12+
"path"
1213

1314
"github.com/pkg/errors"
1415

@@ -91,7 +92,12 @@ func (c *FuseDaemonConfig) StorageBackend() (string, *BackendConfig) {
9192
}
9293

9394
func (c *FuseDaemonConfig) DumpString() (string, error) {
94-
configRWMutex.Lock()
95-
defer configRWMutex.Unlock()
9695
return DumpConfigString(c)
9796
}
97+
98+
func (c *FuseDaemonConfig) DumpFile(f string) error {
99+
if err := os.MkdirAll(path.Dir(f), 0755); err != nil {
100+
return err
101+
}
102+
return DumpConfigFile(c, f)
103+
}

docs/optimize_nydus_image.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ Nydus provides a [nydusify](https://github.com/dragonflyoss/nydus/blob/master/do
108108
We can install the `nydusify` cli tool from the nydus package.
109109

110110
```console
111-
VERSION=v2.1.5
111+
VERSION=v2.3.0
112112

113113
wget https://github.com/dragonflyoss/nydus/releases/download/$VERSION/nydus-static-$VERSION-linux-amd64.tgz
114114
tar -zxvf nydus-static-$VERSION-linux-amd64.tgz

integration/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ ARG CONTAINERD_PROJECT=/containerd
33
ARG RUNC_VER=1.1.4
44
ARG NYDUS_SNAPSHOTTER_PROJECT=/nydus-snapshotter
55
ARG DOWNLOADS_MIRROR="https://github.com"
6-
ARG NYDUS_VER=2.2.5
6+
ARG NYDUS_VER=2.3.0
77
ARG NERDCTL_VER=1.7.6
88
ARG DELVE_VER=1.23.0
99
ARG GO_VER=1.22.5-bookworm

pkg/daemon/daemon.go

Lines changed: 12 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -87,21 +87,6 @@ type Daemon struct {
8787
state types.DaemonState
8888
}
8989

90-
type NydusdSupplementInfo struct {
91-
DaemonState ConfigState
92-
ImageID string
93-
SnapshotID string
94-
Vpc bool
95-
Labels map[string]string
96-
Params map[string]string
97-
}
98-
99-
func (s *NydusdSupplementInfo) GetImageID() string { return s.ImageID }
100-
func (s *NydusdSupplementInfo) GetSnapshotID() string { return s.SnapshotID }
101-
func (s *NydusdSupplementInfo) IsVPCRegistry() bool { return s.Vpc }
102-
func (s *NydusdSupplementInfo) GetLabels() map[string]string { return s.Labels }
103-
func (s *NydusdSupplementInfo) GetParams() map[string]string { return s.Params }
104-
10590
func (d *Daemon) Lock() {
10691
d.mu.Lock()
10792
}
@@ -265,7 +250,12 @@ func (d *Daemon) sharedFusedevMount(rafs *rafs.Rafs) error {
265250
return err
266251
}
267252

268-
c := d.Config
253+
c, err := daemonconfig.NewDaemonConfig(d.States.FsDriver, d.ConfigFile(rafs.SnapshotID))
254+
if err != nil {
255+
return errors.Wrapf(err, "Failed to reload instance configuration %s",
256+
d.ConfigFile(rafs.SnapshotID))
257+
}
258+
269259
cfg, err := c.DumpString()
270260
if err != nil {
271261
return errors.Wrap(err, "dump instance configuration")
@@ -290,7 +280,12 @@ func (d *Daemon) sharedErofsMount(ra *rafs.Rafs) error {
290280
return errors.Wrapf(err, "failed to create fscache work dir %s", ra.FscacheWorkDir())
291281
}
292282

293-
c := d.Config
283+
c, err := daemonconfig.NewDaemonConfig(d.States.FsDriver, d.ConfigFile(ra.SnapshotID))
284+
if err != nil {
285+
log.L.Errorf("Failed to reload daemon configuration %s, %s", d.ConfigFile(ra.SnapshotID), err)
286+
return err
287+
}
288+
294289
cfgStr, err := c.DumpString()
295290
if err != nil {
296291
return err
@@ -655,29 +650,3 @@ func NewDaemon(opt ...NewDaemonOpt) (*Daemon, error) {
655650

656651
return d, nil
657652
}
658-
659-
func (d *Daemon) MountByAPI() error {
660-
rafs := d.RafsCache.Head()
661-
if rafs == nil {
662-
return errors.Wrapf(errdefs.ErrNotFound, "daemon %s no rafs instance associated", d.ID())
663-
}
664-
client, err := d.GetClient()
665-
if err != nil {
666-
return errors.Wrapf(err, "mount instance %s", rafs.SnapshotID)
667-
}
668-
bootstrap, err := rafs.BootstrapFile()
669-
if err != nil {
670-
return err
671-
}
672-
c := d.Config
673-
cfg, err := c.DumpString()
674-
if err != nil {
675-
return errors.Wrap(err, "dump instance configuration")
676-
}
677-
err = client.Mount("/", bootstrap, cfg)
678-
if err != nil {
679-
return errors.Wrapf(err, "mount rafs instance MountByAPI()")
680-
}
681-
return nil
682-
683-
}

pkg/filesystem/fs.go

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -130,19 +130,6 @@ func NewFileSystem(ctx context.Context, opt ...NewFSOpt) (*Filesystem, error) {
130130
if err != nil {
131131
return errors.Wrapf(err, "get filesystem manager for daemon %s", d.States.ID)
132132
}
133-
134-
supplementInfo, err := fsManager.GetInfo(d.ID())
135-
if err != nil {
136-
return errors.Wrap(err, "GetInfo failed")
137-
}
138-
139-
cfg := d.Config
140-
err = daemonconfig.SupplementDaemonConfig(cfg, supplementInfo)
141-
if err != nil {
142-
return errors.Wrap(err, "supplement configuration")
143-
}
144-
d.Config = cfg
145-
146133
if err := fsManager.StartDaemon(d); err != nil {
147134
return errors.Wrapf(err, "start daemon %s", d.ID())
148135
}
@@ -245,6 +232,7 @@ func (fs *Filesystem) Mount(ctx context.Context, snapshotID string, labels map[s
245232
// Instance already exists, how could this happen? Can containerd handle this case?
246233
return nil
247234
}
235+
248236
fsDriver := config.GetFsDriver()
249237
if label.IsTarfsDataLayer(labels) {
250238
fsDriver = config.FsDriverBlockdev
@@ -314,25 +302,34 @@ func (fs *Filesystem) Mount(ctx context.Context, snapshotID string, labels map[s
314302
daemonconfig.WorkDir: workDir,
315303
daemonconfig.CacheDir: cacheDir,
316304
}
317-
supplementInfo := &daemon.NydusdSupplementInfo{
318-
DaemonState: d.States,
319-
ImageID: imageID,
320-
SnapshotID: snapshotID,
321-
Vpc: false,
322-
Labels: labels,
323-
Params: params,
324-
}
325305
cfg := deepcopy.Copy(*fsManager.DaemonConfig).(daemonconfig.DaemonConfig)
326-
err = daemonconfig.SupplementDaemonConfig(cfg, supplementInfo)
306+
err = daemonconfig.SupplementDaemonConfig(cfg, imageID, snapshotID, false, labels, params)
327307
if err != nil {
328308
return errors.Wrap(err, "supplement configuration")
329309
}
330-
if errs := fsManager.AddSupplementInfo(supplementInfo); errs != nil {
331-
return errors.Wrapf(err, "AddSupplementInfo failed %s", d.States.ID)
332-
}
310+
333311
// TODO: How to manage rafs configurations on-disk? separated json config file or DB record?
334312
// In order to recover erofs mount, the configuration file has to be persisted.
335-
d.Config = cfg
313+
var configSubDir string
314+
if useSharedDaemon {
315+
configSubDir = snapshotID
316+
} else {
317+
// Associate daemon config object when creating a new daemon object to avoid
318+
// reading disk file again and again.
319+
// For shared daemon, each rafs instance has its own configuration, so we don't
320+
// attach a config interface to daemon in this case.
321+
d.Config = cfg
322+
}
323+
324+
err = cfg.DumpFile(d.ConfigFile(configSubDir))
325+
if err != nil {
326+
if errors.Is(err, errdefs.ErrAlreadyExists) {
327+
log.L.Debugf("Configuration file %s already exits", d.ConfigFile(configSubDir))
328+
} else {
329+
return errors.Wrap(err, "dump daemon configuration file")
330+
}
331+
}
332+
336333
d.AddRafsInstance(rafs)
337334

338335
// if publicKey is not empty we should verify bootstrap file of image
@@ -599,6 +596,10 @@ func (fs *Filesystem) initSharedDaemon(fsManager *manager.Manager) (err error) {
599596
// it is loaded when requesting mount api
600597
// Dump the configuration file since it is reloaded when recovering the nydusd
601598
d.Config = *fsManager.DaemonConfig
599+
err = d.Config.DumpFile(d.ConfigFile(""))
600+
if err != nil && !errors.Is(err, errdefs.ErrAlreadyExists) {
601+
return errors.Wrapf(err, "dump configuration file %s", d.ConfigFile(""))
602+
}
602603

603604
if err := fsManager.StartDaemon(d); err != nil {
604605
return errors.Wrap(err, "start shared daemon")

pkg/manager/daemon_adaptor.go

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,16 +44,6 @@ func (m *Manager) StartDaemon(d *daemon.Daemon) error {
4444
if err := cmd.Start(); err != nil {
4545
return err
4646
}
47-
fsDriver := config.GetFsDriver()
48-
isSharedFusedev := fsDriver == config.FsDriverFusedev && config.GetDaemonMode() == config.DaemonModeShared
49-
useSharedDaemon := fsDriver == config.FsDriverFscache || isSharedFusedev
50-
51-
if !useSharedDaemon {
52-
errs := d.MountByAPI()
53-
if errs != nil {
54-
return errors.Wrapf(err, "failed to mount")
55-
}
56-
}
5747

5848
d.Lock()
5949
defer d.Unlock()
@@ -165,6 +155,10 @@ func (m *Manager) BuildDaemonCommand(d *daemon.Daemon, bin string, upgrade bool)
165155
return nil, errors.Wrapf(err, "locate bootstrap %s", bootstrap)
166156
}
167157

158+
cmdOpts = append(cmdOpts,
159+
command.WithConfig(d.ConfigFile("")),
160+
command.WithBootstrap(bootstrap),
161+
)
168162
if config.IsBackendSourceEnabled() {
169163
configAPIPath := fmt.Sprintf(endpointGetBackend, d.States.ID)
170164
cmdOpts = append(cmdOpts,

pkg/manager/manager.go

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -197,23 +197,6 @@ func (m *Manager) AddDaemon(daemon *daemon.Daemon) error {
197197
return nil
198198
}
199199

200-
func (m *Manager) AddSupplementInfo(supplementInfo *daemon.NydusdSupplementInfo) error {
201-
m.mu.Lock()
202-
defer m.mu.Unlock()
203-
if err := m.store.AddInfo(supplementInfo); err != nil {
204-
return errors.Wrapf(err, "add supplementInfo %s", supplementInfo.DaemonState.ID)
205-
}
206-
return nil
207-
}
208-
209-
func (m *Manager) GetInfo(daemonID string) (*daemon.NydusdSupplementInfo, error) {
210-
info, err := m.store.GetInfo(daemonID)
211-
if err != nil {
212-
return nil, errors.Wrapf(err, "add supplementInfo %s", daemonID)
213-
}
214-
return info, nil
215-
}
216-
217200
func (m *Manager) UpdateDaemon(daemon *daemon.Daemon) error {
218201
m.mu.Lock()
219202
defer m.mu.Unlock()
@@ -339,7 +322,13 @@ func (m *Manager) recoverDaemons(ctx context.Context,
339322
}
340323

341324
if d.States.FsDriver == config.FsDriverFusedev {
342-
d.Config = *m.DaemonConfig
325+
cfg, err := daemonconfig.NewDaemonConfig(d.States.FsDriver, d.ConfigFile(""))
326+
if err != nil {
327+
log.L.Errorf("Failed to reload daemon configuration %s, %s", d.ConfigFile(""), err)
328+
return err
329+
}
330+
331+
d.Config = cfg
343332
}
344333

345334
state, err := d.GetState()

pkg/manager/store.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,6 @@ type Store interface {
2929
WalkRafsInstances(ctx context.Context, cb func(*rafs.Rafs) error) error
3030

3131
NextInstanceSeq() (uint64, error)
32-
33-
AddInfo(supplementInfo *daemon.NydusdSupplementInfo) error
34-
GetInfo(daemonID string) (*daemon.NydusdSupplementInfo, error)
3532
}
3633

3734
var _ Store = &store.DaemonRafsStore{}

0 commit comments

Comments
 (0)