Skip to content

Commit 775095b

Browse files
committed
fix: pass backend image fallback tags via system state instead of env vars
Instead of setting environment variables in run.go (which InstallBackends cannot access), pass the configuration through the SystemState struct. Changes: - Added BackendImagesReleaseTag, BackendImagesBranchTag, and BackendDevSuffix fields to the Backend struct in pkg/system/state.go - Added WithBackendImagesReleaseTag, WithBackendImagesBranchTag, and WithBackendDevSuffix SystemStateOptions functions - Modified run.go to pass these options via systemStateOpts slice - Updated getFallbackTagValues in gallery/backends.go to accept systemState parameter and read values from it first, falling back to environment variables This follows the reviewer's request to pass-by configuration to InstallBackends instead of setting environment variables.
1 parent f222a29 commit 775095b

3 files changed

Lines changed: 58 additions & 27 deletions

File tree

core/cli/run.go

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,14 @@ func (r *RunCMD) Run(ctx *cliContext.Context) error {
103103
os.MkdirAll(r.BackendsPath, 0750)
104104
os.MkdirAll(r.ModelsPath, 0750)
105105

106+
systemStateOpts := []system.SystemStateOptions{
107+
}
108+
106109
systemState, err := system.GetSystemState(
107110
system.WithBackendSystemPath(r.BackendsSystemPath),
108111
system.WithModelPath(r.ModelsPath),
109112
system.WithBackendPath(r.BackendsPath),
113+
systemStateOpts...,
110114
)
111115
if err != nil {
112116
return err
@@ -272,31 +276,18 @@ func (r *RunCMD) Run(ctx *cliContext.Context) error {
272276
opts = append(opts, config.WithOpenResponsesStoreTTL(dur))
273277
}
274278

275-
// Configure backend image fallback tags
276-
if r.BackendImagesReleaseTag != "" {
277-
opts = append(opts, config.WithBackendImagesReleaseTag(r.BackendImagesReleaseTag))
278-
}
279-
if r.BackendImagesBranchTag != "" {
280-
opts = append(opts, config.WithBackendImagesBranchTag(r.BackendImagesBranchTag))
281-
}
282-
if r.BackendDevSuffix != "" {
283-
opts = append(opts, config.WithBackendDevSuffix(r.BackendDevSuffix))
284-
}
285279

286-
// Set environment variables for backend image fallback tags
287-
// These are read by gallery.InstallBackend when installing backends
280+
// Pass backend image fallback tags via system state
288281
if r.BackendImagesReleaseTag != "" {
289-
os.Setenv("LOCALAI_BACKEND_IMAGES_RELEASE_TAG", r.BackendImagesReleaseTag)
282+
systemStateOpts = append(systemStateOpts, system.WithBackendImagesReleaseTag(r.BackendImagesReleaseTag))
290283
}
291284
if r.BackendImagesBranchTag != "" {
292-
os.Setenv("LOCALAI_BACKEND_IMAGES_BRANCH_TAG", r.BackendImagesBranchTag)
285+
systemStateOpts = append(systemStateOpts, system.WithBackendImagesBranchTag(r.BackendImagesBranchTag))
293286
}
294287
if r.BackendDevSuffix != "" {
295-
os.Setenv("LOCALAI_BACKEND_DEV_SUFFIX", r.BackendDevSuffix)
288+
systemStateOpts = append(systemStateOpts, system.WithBackendDevSuffix(r.BackendDevSuffix))
296289
}
297290

298-
299-
300291
// split ":" to get backend name and the uri
301292
for _, v := range r.ExternalGRPCBackends {
302293
backend := v[:strings.IndexByte(v, ':')]

core/gallery/backends.go

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,30 @@ const (
3838
envDevSuffix = "LOCALAI_BACKEND_DEV_SUFFIX"
3939
)
4040

41-
// getFallbackTagValues returns the configurable fallback tag values from environment variables
42-
func getFallbackTagValues() (latestTag, masterTag, devSuffix string) {
43-
latestTag = os.Getenv(envLatestTag)
44-
masterTag = os.Getenv(envMasterTag)
45-
devSuffix = os.Getenv(envDevSuffix)
4641

47-
// Use defaults if environment variables are not set
42+
// getFallbackTagValues returns the configurable fallback tag values from system state, falling back to environment variables
43+
func getFallbackTagValues(systemState *system.SystemState) (latestTag, masterTag, devSuffix string) {
44+
// First try to get values from system state
45+
if systemState != nil && systemState.Backend.BackendImagesReleaseTag != "" {
46+
latestTag = systemState.Backend.BackendImagesReleaseTag
47+
} else {
48+
// Fallback to environment variables
49+
latestTag = os.Getenv(envLatestTag)
50+
}
51+
52+
if systemState != nil && systemState.Backend.BackendImagesBranchTag != "" {
53+
masterTag = systemState.Backend.BackendImagesBranchTag
54+
} else {
55+
masterTag = os.Getenv(envMasterTag)
56+
}
57+
58+
if systemState != nil && systemState.Backend.BackendDevSuffix != "" {
59+
devSuffix = systemState.Backend.BackendDevSuffix
60+
} else {
61+
devSuffix = os.Getenv(envDevSuffix)
62+
}
63+
64+
// Use defaults if values are not set
4865
if latestTag == "" {
4966
latestTag = defaultLatestTag
5067
}
@@ -58,6 +75,8 @@ func getFallbackTagValues() (latestTag, masterTag, devSuffix string) {
5875
return latestTag, masterTag, devSuffix
5976
}
6077

78+
}
79+
6180
// backendCandidate represents an installed concrete backend option for a given alias
6281
type backendCandidate struct {
6382
name string
@@ -172,8 +191,8 @@ func InstallBackendFromGallery(ctx context.Context, galleries []config.Gallery,
172191
}
173192

174193
func InstallBackend(ctx context.Context, systemState *system.SystemState, modelLoader *model.ModelLoader, config *GalleryBackend, downloadStatus func(string, string, string, float64)) error {
175-
// Get configurable fallback tag values from environment variables
176-
latestTag, masterTag, devSuffix := getFallbackTagValues()
194+
// Get configurable fallback tag values from system state, falling back to environment variables
195+
latestTag, masterTag, devSuffix := getFallbackTagValues(systemState)
177196

178197
// Create base path if it doesn't exist
179198
err := os.MkdirAll(systemState.Backend.BackendsPath, 0750)

pkg/system/state.go

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,11 @@ import (
66
)
77

88
type Backend struct {
9-
BackendsPath string
10-
BackendsSystemPath string
9+
BackendsPath string
10+
BackendsSystemPath string
11+
BackendImagesReleaseTag string // Release tag for backend images
12+
BackendImagesBranchTag string // Branch tag for backend images
13+
BackendDevSuffix string // Development suffix for backend images
1114
}
1215

1316
type Model struct {
@@ -43,6 +46,24 @@ func WithModelPath(path string) SystemStateOptions {
4346
}
4447
}
4548

49+
func WithBackendImagesReleaseTag(tag string) SystemStateOptions {
50+
return func(s *SystemState) {
51+
s.Backend.BackendImagesReleaseTag = tag
52+
}
53+
}
54+
55+
func WithBackendImagesBranchTag(tag string) SystemStateOptions {
56+
return func(s *SystemState) {
57+
s.Backend.BackendImagesBranchTag = tag
58+
}
59+
}
60+
61+
func WithBackendDevSuffix(suffix string) SystemStateOptions {
62+
return func(s *SystemState) {
63+
s.Backend.BackendDevSuffix = suffix
64+
}
65+
}
66+
4667
func GetSystemState(opts ...SystemStateOptions) (*SystemState, error) {
4768
state := &SystemState{}
4869
for _, opt := range opts {

0 commit comments

Comments
 (0)