Skip to content

Commit f0d96b4

Browse files
authored
fix: sync custom registry configuration (#1314)
1 parent 82bda81 commit f0d96b4

File tree

6 files changed

+289
-23
lines changed

6 files changed

+289
-23
lines changed

docs/server/docs.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/server/swagger.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/server/swagger.yaml

Lines changed: 24 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/api/server.go

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ import (
3232
"github.com/stacklok/toolhive/pkg/container"
3333
"github.com/stacklok/toolhive/pkg/groups"
3434
"github.com/stacklok/toolhive/pkg/logger"
35-
"github.com/stacklok/toolhive/pkg/registry"
3635
"github.com/stacklok/toolhive/pkg/updates"
3736
"github.com/stacklok/toolhive/pkg/workloads"
3837
)
@@ -167,12 +166,6 @@ func Serve(
167166
return fmt.Errorf("failed to create container runtime: %v", err)
168167
}
169168

170-
// Create registry provider
171-
registryProvider, err := registry.GetDefaultProvider()
172-
if err != nil {
173-
return fmt.Errorf("failed to create registry provider: %v", err)
174-
}
175-
176169
clientManager, err := client.NewManager(ctx)
177170
if err != nil {
178171
return fmt.Errorf("failed to create client manager: %v", err)
@@ -190,7 +183,7 @@ func Serve(
190183
"/health": v1.HealthcheckRouter(containerRuntime),
191184
"/api/v1beta/version": v1.VersionRouter(),
192185
"/api/v1beta/workloads": v1.WorkloadRouter(workloadManager, containerRuntime, groupManager, debugMode),
193-
"/api/v1beta/registry": v1.RegistryRouter(registryProvider),
186+
"/api/v1beta/registry": v1.RegistryRouter(),
194187
"/api/v1beta/discovery": v1.DiscoveryRouter(),
195188
"/api/v1beta/clients": v1.ClientRouter(clientManager, workloadManager, groupManager),
196189
"/api/v1beta/secrets": v1.SecretsRouter(),

pkg/api/v1/registry.go

Lines changed: 85 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,50 @@ const (
1717
defaultRegistryName = "default"
1818
)
1919

20-
// RegistryRoutes defines the routes for the registry API.
21-
type RegistryRoutes struct {
22-
provider registry.Provider
20+
// RegistryType represents the type of registry
21+
type RegistryType string
22+
23+
const (
24+
// RegistryTypeFile represents a local file registry
25+
RegistryTypeFile RegistryType = "file"
26+
// RegistryTypeURL represents a remote URL registry
27+
RegistryTypeURL RegistryType = "url"
28+
// RegistryTypeDefault represents a built-in registry
29+
RegistryTypeDefault RegistryType = "default"
30+
)
31+
32+
// getRegistryInfo returns the registry type and the source
33+
func getRegistryInfo() (RegistryType, string) {
34+
url, localPath, _, registryType := config.GetRegistryConfig()
35+
36+
switch registryType {
37+
case "url":
38+
return RegistryTypeURL, url
39+
case "file":
40+
return RegistryTypeFile, localPath
41+
default:
42+
// Default built-in registry
43+
return RegistryTypeDefault, ""
44+
}
2345
}
2446

47+
// getCurrentProvider returns the current registry provider
48+
func getCurrentProvider(w http.ResponseWriter) (registry.Provider, bool) {
49+
provider, err := registry.GetDefaultProvider()
50+
if err != nil {
51+
http.Error(w, "Failed to get registry provider", http.StatusInternalServerError)
52+
logger.Errorf("Failed to get registry provider: %v", err)
53+
return nil, false
54+
}
55+
return provider, true
56+
}
57+
58+
// RegistryRoutes defines the routes for the registry API.
59+
type RegistryRoutes struct{}
60+
2561
// RegistryRouter creates a new router for the registry API.
26-
func RegistryRouter(provider registry.Provider) http.Handler {
27-
routes := RegistryRoutes{provider: provider}
62+
func RegistryRouter() http.Handler {
63+
routes := RegistryRoutes{}
2864

2965
r := chi.NewRouter()
3066
r.Get("/", routes.listRegistries)
@@ -49,19 +85,28 @@ func RegistryRouter(provider registry.Provider) http.Handler {
4985
// @Produce json
5086
// @Success 200 {object} registryListResponse
5187
// @Router /api/v1beta/registry [get]
52-
func (routes *RegistryRoutes) listRegistries(w http.ResponseWriter, _ *http.Request) {
53-
reg, err := routes.provider.GetRegistry()
88+
func (*RegistryRoutes) listRegistries(w http.ResponseWriter, _ *http.Request) {
89+
provider, ok := getCurrentProvider(w)
90+
if !ok {
91+
return
92+
}
93+
94+
reg, err := provider.GetRegistry()
5495
if err != nil {
5596
http.Error(w, "Failed to get registry", http.StatusInternalServerError)
5697
return
5798
}
5899

100+
registryType, source := getRegistryInfo()
101+
59102
registries := []registryInfo{
60103
{
61104
Name: defaultRegistryName,
62105
Version: reg.Version,
63106
LastUpdated: reg.LastUpdated,
64107
ServerCount: len(reg.Servers),
108+
Type: registryType,
109+
Source: source,
65110
},
66111
}
67112

@@ -98,7 +143,7 @@ func (*RegistryRoutes) addRegistry(w http.ResponseWriter, _ *http.Request) {
98143
// @Success 200 {object} getRegistryResponse
99144
// @Failure 404 {string} string "Not Found"
100145
// @Router /api/v1beta/registry/{name} [get]
101-
func (routes *RegistryRoutes) getRegistry(w http.ResponseWriter, r *http.Request) {
146+
func (*RegistryRoutes) getRegistry(w http.ResponseWriter, r *http.Request) {
102147
name := chi.URLParam(r, "name")
103148

104149
// Only "default" registry is supported currently
@@ -107,17 +152,26 @@ func (routes *RegistryRoutes) getRegistry(w http.ResponseWriter, r *http.Request
107152
return
108153
}
109154

110-
reg, err := routes.provider.GetRegistry()
155+
provider, ok := getCurrentProvider(w)
156+
if !ok {
157+
return
158+
}
159+
160+
reg, err := provider.GetRegistry()
111161
if err != nil {
112162
http.Error(w, "Failed to get registry", http.StatusInternalServerError)
113163
return
114164
}
115165

166+
registryType, source := getRegistryInfo()
167+
116168
response := getRegistryResponse{
117169
Name: defaultRegistryName,
118170
Version: reg.Version,
119171
LastUpdated: reg.LastUpdated,
120172
ServerCount: len(reg.Servers),
173+
Type: registryType,
174+
Source: source,
121175
Registry: reg,
122176
}
123177

@@ -249,7 +303,7 @@ func (*RegistryRoutes) removeRegistry(w http.ResponseWriter, r *http.Request) {
249303
// @Success 200 {object} listServersResponse
250304
// @Failure 404 {string} string "Not Found"
251305
// @Router /api/v1beta/registry/{name}/servers [get]
252-
func (routes *RegistryRoutes) listServers(w http.ResponseWriter, r *http.Request) {
306+
func (*RegistryRoutes) listServers(w http.ResponseWriter, r *http.Request) {
253307
registryName := chi.URLParam(r, "name")
254308

255309
// Only "default" registry is supported currently
@@ -258,7 +312,12 @@ func (routes *RegistryRoutes) listServers(w http.ResponseWriter, r *http.Request
258312
return
259313
}
260314

261-
servers, err := routes.provider.ListServers()
315+
provider, ok := getCurrentProvider(w)
316+
if !ok {
317+
return
318+
}
319+
320+
servers, err := provider.ListServers()
262321
if err != nil {
263322
logger.Errorf("Failed to list servers: %v", err)
264323
http.Error(w, "Failed to list servers", http.StatusInternalServerError)
@@ -285,7 +344,7 @@ func (routes *RegistryRoutes) listServers(w http.ResponseWriter, r *http.Request
285344
// @Success 200 {object} getServerResponse
286345
// @Failure 404 {string} string "Not Found"
287346
// @Router /api/v1beta/registry/{name}/servers/{serverName} [get]
288-
func (routes *RegistryRoutes) getServer(w http.ResponseWriter, r *http.Request) {
347+
func (*RegistryRoutes) getServer(w http.ResponseWriter, r *http.Request) {
289348
registryName := chi.URLParam(r, "name")
290349
serverName := chi.URLParam(r, "serverName")
291350

@@ -295,7 +354,12 @@ func (routes *RegistryRoutes) getServer(w http.ResponseWriter, r *http.Request)
295354
return
296355
}
297356

298-
server, err := routes.provider.GetServer(serverName)
357+
provider, ok := getCurrentProvider(w)
358+
if !ok {
359+
return
360+
}
361+
362+
server, err := provider.GetServer(serverName)
299363
if err != nil {
300364
logger.Errorf("Failed to get server '%s': %v", serverName, err)
301365
http.Error(w, "ImageMetadata not found", http.StatusNotFound)
@@ -325,6 +389,10 @@ type registryInfo struct {
325389
LastUpdated string `json:"last_updated"`
326390
// Number of servers in the registry
327391
ServerCount int `json:"server_count"`
392+
// Type of registry (file, url, or default)
393+
Type RegistryType `json:"type"`
394+
// Source of the registry (URL, file path, or empty string for built-in)
395+
Source string `json:"source"`
328396
}
329397

330398
// registryListResponse represents the response for listing registries
@@ -347,6 +415,10 @@ type getRegistryResponse struct {
347415
LastUpdated string `json:"last_updated"`
348416
// Number of servers in the registry
349417
ServerCount int `json:"server_count"`
418+
// Type of registry (file, url, or default)
419+
Type RegistryType `json:"type"`
420+
// Source of the registry (URL, file path, or empty string for built-in)
421+
Source string `json:"source"`
350422
// Full registry data
351423
Registry *registry.Registry `json:"registry"`
352424
}

0 commit comments

Comments
 (0)