Skip to content

Commit 45cff6b

Browse files
committed
feat: Add Overlaybd API server configuration and notification handling
1 parent ed37a6a commit 45cff6b

7 files changed

Lines changed: 158 additions & 4 deletions

File tree

docs/BUILDING.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,12 @@ sudo mkdir /etc/overlaybd-snapshotter
3737
sudo cat <<-EOF | sudo tee /etc/overlaybd-snapshotter/config.json
3838
{
3939
"root": "/var/lib/overlaybd/",
40-
"address": "/run/overlaybd-snapshotter/overlaybd.sock"
40+
"address": "/run/overlaybd-snapshotter/overlaybd.sock",
41+
"experimental": {
42+
"enabled": false,
43+
"preAuth": true,
44+
"overlaybdApiServer": "http://127.0.0.1:9862"
45+
}
4146
}
4247
EOF
4348
```

docs/DOCKER.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,12 @@ Edit `/etc/overlaybd-snapshotter/config.json`:
3131
"verbose": "info",
3232
"logReportCaller": false,
3333
"autoRemoveDev": true,
34-
"mirrorRegistry": []
34+
"mirrorRegistry": [],
35+
"experimental": {
36+
"enabled": false,
37+
"preAuth": true,
38+
"overlaybdApiServer": "http://127.0.0.1:9862"
39+
}
3540
}
3641
```
3742

docs/QUICKSTART.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,12 @@ The config file is `/etc/overlaybd-snapshotter/config.json`. Please create the f
7878
"host": "registry-1.docker.io",
7979
"insecure": false
8080
}
81-
]
81+
],
82+
"experimental": {
83+
"enabled": false,
84+
"preAuth": true,
85+
"overlaybdApiServer": "http://127.0.0.1:9862"
86+
}
8287
}
8388
```
8489
| Field | Description |
@@ -93,6 +98,9 @@ The config file is `/etc/overlaybd-snapshotter/config.json`. Please create the f
9398
| `exporterConfig.enable` | whether or not create a server to show Prometheus metrics |
9499
| `exporterConfig.uriPrefix` | URI prefix for export metrics, default `/metrics` |
95100
| `exporterConfig.port` | port for http server to show metrics, default `9863` |
101+
| `experimental.enabled` | enable experimental features |
102+
| `experimental.preAuth` | enable overlaybd API server pre-auth notification |
103+
| `experimental.overlaybdApiServer` | overlaybd API server address, default `http://127.0.0.1:9862` |
96104
| `mirrorRegistry` | an arrary of mirror registries |
97105
| `mirrorRegistry.host` | host address, eg. `registry-1.docker.io`` |
98106
| `mirrorRegistry.insecure` | `true` or `false` |

pkg/snapshot/docker.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ func (o *snapshotter) prepareDockerInitLayer(ctx context.Context, s storage.Snap
8585
}
8686
}
8787

88+
o.notifyOverlaybdAPIServer(o.overlaybdConfPath(parentID))
89+
8890
// Attach and mount overlaybd device at parent's mountpoint (readonly)
8991
if err := o.attachAndMountBlockDevice(ctx, parentID, RoDir, fsType, false); err != nil {
9092
return nil, fmt.Errorf("failed to attach overlaybd for docker init layer: %w", err)

pkg/snapshot/overlay.go

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ import (
2222
"encoding/binary"
2323
"errors"
2424
"fmt"
25+
"io"
26+
"net/http"
27+
"net/url"
2528
"os"
2629
"os/exec"
2730
"path"
@@ -86,6 +89,12 @@ type Registry struct {
8689
Insecure bool `json:"insecure"`
8790
}
8891

92+
type ExperimentalConfig struct {
93+
Enabled bool `json:"enabled"`
94+
PreAuth bool `json:"preAuth"`
95+
OverlaybdApiServer string `json:"overlaybdApiServer"`
96+
}
97+
8998
type BootConfig struct {
9099
AsyncRemove bool `json:"asyncRemove"`
91100
Address string `json:"address"`
@@ -102,6 +111,7 @@ type BootConfig struct {
102111
Tenant int `json:"tenant"` // do not set this if only a single snapshotter service in the host
103112
TurboFsType []string `json:"turboFsType"`
104113
RuntimeType string `json:"runtimeType"` // "containerd" (default) or "docker"
114+
Experimental ExperimentalConfig `json:"experimental"`
105115
}
106116

107117
func DefaultBootConfig() *BootConfig {
@@ -126,6 +136,11 @@ func DefaultBootConfig() *BootConfig {
126136
"ext4",
127137
},
128138
RuntimeType: "containerd",
139+
Experimental: ExperimentalConfig{
140+
Enabled: false,
141+
PreAuth: true,
142+
OverlaybdApiServer: "http://127.0.0.1:9862",
143+
},
129144
}
130145
}
131146

@@ -206,6 +221,7 @@ type snapshotter struct {
206221
turboFsType []string
207222
asyncRemove bool
208223
runtimeType string
224+
experimental ExperimentalConfig
209225

210226
quotaDriver *diskquota.PrjQuotaDriver
211227
quotaSize string
@@ -270,6 +286,7 @@ func NewSnapshotter(bootConfig *BootConfig, opts ...Opt) (snapshots.Snapshotter,
270286
turboFsType: bootConfig.TurboFsType,
271287
tenant: bootConfig.Tenant,
272288
quotaSize: bootConfig.RootfsQuota,
289+
experimental: bootConfig.Experimental,
273290
quotaDriver: &diskquota.PrjQuotaDriver{
274291
QuotaIDs: make(map[uint32]struct{}),
275292
},
@@ -434,6 +451,61 @@ func (o *snapshotter) lockSnapshotID(id string) func() {
434451
}
435452
}
436453

454+
func (o *snapshotter) preAuthEnabled() bool {
455+
return o.experimental.Enabled && o.experimental.PreAuth
456+
}
457+
458+
func (o *snapshotter) notifyOverlaybdAPIServer(configPath string) {
459+
if !o.preAuthEnabled() || configPath == "" {
460+
return
461+
}
462+
463+
go func() {
464+
address := strings.TrimSpace(o.experimental.OverlaybdApiServer)
465+
if address == "" {
466+
log.L.Warn("overlaybd api server address is empty")
467+
return
468+
}
469+
470+
authBase, err := url.Parse(address)
471+
if err != nil {
472+
log.L.WithError(err).Warnf("failed to parse overlaybd api server address %q", address)
473+
return
474+
}
475+
476+
authURL := authBase.ResolveReference(&url.URL{Path: "/auth"})
477+
query := authURL.Query()
478+
query.Set("config", configPath)
479+
authURL.RawQuery = query.Encode()
480+
481+
log.L.Infof("overlaybd api request: %s", authURL.String())
482+
483+
reqCtx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
484+
defer cancel()
485+
486+
req, err := http.NewRequestWithContext(reqCtx, http.MethodGet, authURL.String(), nil)
487+
if err != nil {
488+
log.L.WithError(err).Warnf("failed to create overlaybd api request for %s", authURL.String())
489+
return
490+
}
491+
492+
resp, err := http.DefaultClient.Do(req)
493+
if err != nil {
494+
log.L.WithError(err).Warnf("overlaybd api request failed: %s", authURL.String())
495+
return
496+
}
497+
defer resp.Body.Close()
498+
499+
body, err := io.ReadAll(resp.Body)
500+
if err != nil {
501+
log.L.WithError(err).Warnf("failed to read overlaybd api response from %s", authURL.String())
502+
return
503+
}
504+
505+
log.L.Infof("overlaybd api response: url=%s status=%s body=%s", authURL.String(), resp.Status, strings.TrimSpace(string(body)))
506+
}()
507+
}
508+
437509
func (o *snapshotter) cleanupFailedPrepare(ctx context.Context, key, id string) {
438510
// TODO: recover half-done prepare state after process crash. This path only
439511
// cleans up failures observed in-process after metadata has been committed.
@@ -757,6 +829,7 @@ func (o *snapshotter) createMountPoint(ctx context.Context, kind snapshots.Kind,
757829
// concurrent Prepare/Mounts/Remove operating on the same parent snapshot.
758830
unlockObd = o.lockSnapshotID(obdID)
759831
}
832+
o.notifyOverlaybdAPIServer(o.overlaybdConfPath(obdID))
760833
if err = o.attachAndMountBlockDevice(ctx, obdID, writeType, fsType, parent == ""); err != nil {
761834
unlockObd()
762835
log.G(ctx).Errorf("%v", err)

pkg/snapshot/overlay_test.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,62 @@ func newSnapshotterWithOpts(opts ...Opt) testsuite.SnapshotterFunc {
3838
}
3939
}
4040

41+
func TestDefaultBootConfigExperimental(t *testing.T) {
42+
cfg := DefaultBootConfig()
43+
44+
if cfg.Experimental.Enabled {
45+
t.Fatal("expected experimental to be disabled by default")
46+
}
47+
if !cfg.Experimental.PreAuth {
48+
t.Fatal("expected preAuth to be enabled by default")
49+
}
50+
if cfg.Experimental.OverlaybdApiServer != "http://127.0.0.1:9862" {
51+
t.Fatalf("unexpected overlaybd api server address %q", cfg.Experimental.OverlaybdApiServer)
52+
}
53+
}
54+
55+
func TestPreAuthEnabled(t *testing.T) {
56+
tests := []struct {
57+
name string
58+
experimental ExperimentalConfig
59+
want bool
60+
}{
61+
{
62+
name: "experimental disabled",
63+
experimental: ExperimentalConfig{
64+
Enabled: false,
65+
PreAuth: true,
66+
},
67+
want: false,
68+
},
69+
{
70+
name: "preAuth disabled",
71+
experimental: ExperimentalConfig{
72+
Enabled: true,
73+
PreAuth: false,
74+
},
75+
want: false,
76+
},
77+
{
78+
name: "experimental and preAuth enabled",
79+
experimental: ExperimentalConfig{
80+
Enabled: true,
81+
PreAuth: true,
82+
},
83+
want: true,
84+
},
85+
}
86+
87+
for _, tt := range tests {
88+
t.Run(tt.name, func(t *testing.T) {
89+
sn := &snapshotter{experimental: tt.experimental}
90+
if got := sn.preAuthEnabled(); got != tt.want {
91+
t.Fatalf("preAuthEnabled() = %v, want %v", got, tt.want)
92+
}
93+
})
94+
}
95+
}
96+
4197
func TestBasicSnapshotterOnOverlayFS(t *testing.T) {
4298
testutil.RequiresRoot(t)
4399
testsuite.SnapshotterSuite(t, "overlaybd-on-overlayFS", newSnapshotterWithOpts())

script/config.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,10 @@
66
"runtimeType": "containerd",
77
"logReportCaller": false,
88
"autoRemoveDev": false,
9-
"mirrorRegistry": []
9+
"mirrorRegistry": [],
10+
"experimental": {
11+
"enabled": false,
12+
"preAuth": true,
13+
"overlaybdApiServer": "http://127.0.0.1:9862"
14+
}
1015
}

0 commit comments

Comments
 (0)