Skip to content

Commit 1101c21

Browse files
feat(userprefs): update documentation and list extensions endpoint
Signed-off-by: Laurentiu Niculae <niculae.laurentiu1@gmail.com>
1 parent 9ca85e0 commit 1101c21

11 files changed

Lines changed: 182 additions & 56 deletions

pkg/api/controller_test.go

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7248,11 +7248,12 @@ func TestDistSpecExtensions(t *testing.T) {
72487248
err = json.Unmarshal(resp.Body(), &extensionList)
72497249
So(err, ShouldBeNil)
72507250
So(len(extensionList.Extensions), ShouldEqual, 1)
7251-
So(len(extensionList.Extensions[0].Endpoints), ShouldEqual, 1)
7251+
So(len(extensionList.Extensions[0].Endpoints), ShouldEqual, 2)
72527252
So(extensionList.Extensions[0].Name, ShouldEqual, "_zot")
72537253
So(extensionList.Extensions[0].URL, ShouldContainSubstring, "_zot.md")
72547254
So(extensionList.Extensions[0].Description, ShouldNotBeEmpty)
7255-
So(extensionList.Extensions[0].Endpoints[0], ShouldEqual, constants.FullSearchPrefix)
7255+
So(extensionList.Extensions[0].Endpoints, ShouldContain, constants.FullSearchPrefix)
7256+
So(extensionList.Extensions[0].Endpoints, ShouldContain, constants.FullUserPreferencesPrefix)
72567257
})
72577258

72587259
Convey("start zot server with search and mgmt extensions", t, func(c C) {
@@ -7296,17 +7297,14 @@ func TestDistSpecExtensions(t *testing.T) {
72967297
So(resp.StatusCode(), ShouldEqual, 200)
72977298
err = json.Unmarshal(resp.Body(), &extensionList)
72987299
So(err, ShouldBeNil)
7299-
So(len(extensionList.Extensions), ShouldEqual, 2)
7300-
So(len(extensionList.Extensions[0].Endpoints), ShouldEqual, 1)
7301-
So(len(extensionList.Extensions[1].Endpoints), ShouldEqual, 1)
7300+
So(len(extensionList.Extensions), ShouldEqual, 1)
7301+
So(len(extensionList.Extensions[0].Endpoints), ShouldEqual, 3)
73027302
So(extensionList.Extensions[0].Name, ShouldEqual, "_zot")
73037303
So(extensionList.Extensions[0].URL, ShouldContainSubstring, "_zot.md")
73047304
So(extensionList.Extensions[0].Description, ShouldNotBeEmpty)
7305-
So(extensionList.Extensions[0].Endpoints[0], ShouldEqual, constants.FullSearchPrefix)
7306-
So(extensionList.Extensions[1].Name, ShouldEqual, "_zot")
7307-
So(extensionList.Extensions[1].URL, ShouldContainSubstring, "_zot.md")
7308-
So(extensionList.Extensions[1].Description, ShouldNotBeEmpty)
7309-
So(extensionList.Extensions[1].Endpoints[0], ShouldEqual, constants.FullMgmtPrefix)
7305+
So(extensionList.Extensions[0].Endpoints, ShouldContain, constants.FullSearchPrefix)
7306+
So(extensionList.Extensions[0].Endpoints, ShouldContain, constants.FullUserPreferencesPrefix)
7307+
So(extensionList.Extensions[0].Endpoints, ShouldContain, constants.FullMgmtPrefix)
73107308
})
73117309

73127310
Convey("start minimal zot server", t, func(c C) {

pkg/extensions/_zot.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ Component | Endpoint | Description
77
--- | --- | ---
88
[`search`](search/search.md) | `/v2/_zot/ext/search` | efficient and enhanced registry search capabilities using graphQL backend
99
[`mgmt`](mgmt.md) | `/v2/_zot/ext/mgmt` | config management
10+
[`userPrefs`](userprefs.md) | `/v2/_zot/ext/userprefs` | change user preferences
1011

1112

1213
# References

pkg/extensions/extension_mgmt.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ type StrippedConfig struct {
4040
} `json:"http" mapstructure:"http"`
4141
}
4242

43+
func IsBuiltWithMGMTExtension() bool {
44+
return true
45+
}
46+
4347
func (auth Auth) MarshalJSON() ([]byte, error) {
4448
type localAuth Auth
4549

pkg/extensions/extension_mgmt_disabled.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ import (
1010
"zotregistry.io/zot/pkg/log"
1111
)
1212

13+
func IsBuiltWithMGMTExtension() bool {
14+
return false
15+
}
16+
1317
func SetupMgmtRoutes(config *config.Config, router *mux.Router, log log.Logger) {
1418
log.Warn().Msg("skipping setting up mgmt routes because given zot binary doesn't include this feature," +
1519
"please build a binary that does so")

pkg/extensions/extension_search.go

Lines changed: 4 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010

1111
gqlHandler "github.com/99designs/gqlgen/graphql/handler"
1212
"github.com/gorilla/mux"
13-
distext "github.com/opencontainers/distribution-spec/specs-go/v1/extensions"
1413

1514
"zotregistry.io/zot/pkg/api/config"
1615
"zotregistry.io/zot/pkg/api/constants"
@@ -34,6 +33,10 @@ const (
3433
done
3534
)
3635

36+
func IsBuiltWithSearchExtension() bool {
37+
return true
38+
}
39+
3740
func GetCVEInfo(config *config.Config, storeController storage.StoreController,
3841
repoDB repodb.RepoDB, log log.Logger,
3942
) CveInfo {
@@ -199,42 +202,3 @@ func SearchACHeadersHandler() mux.MiddlewareFunc {
199202
})
200203
}
201204
}
202-
203-
func getExtension(name, url, description string, endpoints []string) distext.Extension {
204-
return distext.Extension{
205-
Name: name,
206-
URL: url,
207-
Description: description,
208-
Endpoints: endpoints,
209-
}
210-
}
211-
212-
func GetExtensions(config *config.Config) distext.ExtensionList {
213-
extensionList := distext.ExtensionList{}
214-
215-
extensions := make([]distext.Extension, 0)
216-
217-
if config.Extensions != nil && config.Extensions.Search != nil {
218-
endpoints := []string{constants.FullSearchPrefix}
219-
searchExt := getExtension("_zot",
220-
"https://github.com/project-zot/zot/blob/"+config.ReleaseTag+"/pkg/extensions/_zot.md",
221-
"zot registry extensions",
222-
endpoints)
223-
224-
extensions = append(extensions, searchExt)
225-
}
226-
227-
if config.Extensions != nil && config.Extensions.Mgmt != nil {
228-
endpoints := []string{constants.FullMgmtPrefix}
229-
mgmtExt := getExtension("_zot",
230-
"https://github.com/project-zot/zot/blob/"+config.ReleaseTag+"/pkg/extensions/_zot.md",
231-
"zot registry extensions",
232-
endpoints)
233-
234-
extensions = append(extensions, mgmtExt)
235-
}
236-
237-
extensionList.Extensions = extensions
238-
239-
return extensionList
240-
}

pkg/extensions/extension_search_disabled.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ package extensions
55

66
import (
77
"github.com/gorilla/mux"
8-
distext "github.com/opencontainers/distribution-spec/specs-go/v1/extensions"
98

109
"zotregistry.io/zot/pkg/api/config"
1110
"zotregistry.io/zot/pkg/log"
@@ -22,6 +21,10 @@ func GetCVEInfo(config *config.Config, storeController storage.StoreController,
2221
return nil
2322
}
2423

24+
func IsBuiltWithSearchExtension() bool {
25+
return false
26+
}
27+
2528
// EnableSearchExtension ...
2629
func EnableSearchExtension(config *config.Config, storeController storage.StoreController,
2730
repoDB repodb.RepoDB, scheduler *scheduler.Scheduler, cveInfo CveInfo, log log.Logger,
@@ -37,8 +40,3 @@ func SetupSearchRoutes(config *config.Config, router *mux.Router, storeControlle
3740
log.Warn().Msg("skipping setting up search routes because given zot binary doesn't include this feature," +
3841
"please build a binary that does so")
3942
}
40-
41-
// GetExtensions...
42-
func GetExtensions(config *config.Config) distext.ExtensionList {
43-
return distext.ExtensionList{}
44-
}

pkg/extensions/extension_userprefs.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ const (
2424
ToggleRepoStarAction = "toggleStar"
2525
)
2626

27+
func IsBuiltWithUserPrefsExtension() bool {
28+
return true
29+
}
30+
2731
func SetupUserPreferencesRoutes(config *config.Config, router *mux.Router, storeController storage.StoreController,
2832
repoDB repodb.RepoDB, cveInfo CveInfo, log log.Logger,
2933
) {

pkg/extensions/extension_userprefs_disable.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ import (
1212
"zotregistry.io/zot/pkg/storage"
1313
)
1414

15+
func IsBuiltWithUserPrefsExtension() bool {
16+
return false
17+
}
18+
1519
func SetupUserPreferencesRoutes(config *config.Config, router *mux.Router, storeController storage.StoreController,
1620
repoDB repodb.RepoDB, cveInfo CveInfo, log log.Logger,
1721
) {

pkg/extensions/get_extensions.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package extensions
2+
3+
import (
4+
distext "github.com/opencontainers/distribution-spec/specs-go/v1/extensions"
5+
6+
"zotregistry.io/zot/pkg/api/config"
7+
"zotregistry.io/zot/pkg/api/constants"
8+
)
9+
10+
func GetExtensions(config *config.Config) distext.ExtensionList {
11+
extensionList := distext.ExtensionList{}
12+
13+
endpoints := []string{}
14+
extensions := []distext.Extension{}
15+
16+
if config.Extensions != nil && config.Extensions.Search != nil {
17+
if IsBuiltWithSearchExtension() {
18+
endpoints = append(endpoints, constants.FullSearchPrefix)
19+
}
20+
21+
if IsBuiltWithUserPrefsExtension() {
22+
endpoints = append(endpoints, constants.FullUserPreferencesPrefix)
23+
}
24+
}
25+
26+
if IsBuiltWithMGMTExtension() && config.Extensions != nil && config.Extensions.Mgmt != nil {
27+
endpoints = append(endpoints, constants.FullMgmtPrefix)
28+
}
29+
30+
if len(endpoints) > 0 {
31+
extensions = append(extensions, distext.Extension{
32+
Name: "_zot",
33+
URL: "https://github.com/project-zot/zot/blob/" + config.ReleaseTag + "/pkg/extensions/_zot.md",
34+
Description: "zot registry extensions",
35+
Endpoints: endpoints,
36+
})
37+
}
38+
39+
extensionList.Extensions = extensions
40+
41+
return extensionList
42+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
//go:build !search && !mgmt && !userprefs
2+
3+
package extensions_test
4+
5+
import (
6+
"encoding/json"
7+
"os"
8+
"testing"
9+
10+
distext "github.com/opencontainers/distribution-spec/specs-go/v1/extensions"
11+
. "github.com/smartystreets/goconvey/convey"
12+
"gopkg.in/resty.v1"
13+
14+
"zotregistry.io/zot/pkg/api"
15+
"zotregistry.io/zot/pkg/api/config"
16+
"zotregistry.io/zot/pkg/api/constants"
17+
extconf "zotregistry.io/zot/pkg/extensions/config"
18+
"zotregistry.io/zot/pkg/test"
19+
)
20+
21+
func TestGetExensionsDisabled(t *testing.T) {
22+
Convey("start zot server with extensions but no extensions built", t, func(c C) {
23+
conf := config.New()
24+
port := test.GetFreePort()
25+
baseURL := test.GetBaseURL(port)
26+
27+
conf.HTTP.Port = port
28+
29+
defaultVal := true
30+
31+
searchConfig := &extconf.SearchConfig{
32+
BaseConfig: extconf.BaseConfig{Enable: &defaultVal},
33+
}
34+
35+
mgmtConfg := &extconf.MgmtConfig{
36+
BaseConfig: extconf.BaseConfig{Enable: &defaultVal},
37+
}
38+
39+
conf.Extensions = &extconf.ExtensionConfig{
40+
Search: searchConfig,
41+
Mgmt: mgmtConfg,
42+
}
43+
44+
logFile, err := os.CreateTemp("", "zot-log*.txt")
45+
So(err, ShouldBeNil)
46+
conf.Log.Output = logFile.Name()
47+
defer os.Remove(logFile.Name()) // clean up
48+
49+
ctlr := makeController(conf, t.TempDir(), "")
50+
51+
cm := test.NewControllerManager(ctlr)
52+
cm.StartAndWait(port)
53+
defer cm.StopServer()
54+
55+
var extensionList distext.ExtensionList
56+
57+
resp, err := resty.R().Get(baseURL + constants.RoutePrefix + constants.ExtOciDiscoverPrefix)
58+
So(err, ShouldBeNil)
59+
So(resp, ShouldNotBeNil)
60+
So(resp.StatusCode(), ShouldEqual, 200)
61+
err = json.Unmarshal(resp.Body(), &extensionList)
62+
So(err, ShouldBeNil)
63+
So(len(extensionList.Extensions), ShouldEqual, 0)
64+
})
65+
}
66+
67+
func makeController(conf *config.Config, dir string, copyTestDataDest string) *api.Controller {
68+
ctlr := api.NewController(conf)
69+
70+
if copyTestDataDest != "" {
71+
test.CopyTestFiles(copyTestDataDest, dir)
72+
}
73+
ctlr.Config.Storage.RootDirectory = dir
74+
75+
return ctlr
76+
}

0 commit comments

Comments
 (0)