Skip to content

Commit 1907fd8

Browse files
committed
Fix image lookup with remote registry
1 parent 8c8e35c commit 1907fd8

File tree

7 files changed

+33
-22
lines changed

7 files changed

+33
-22
lines changed

internal/app/container_handler.go

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"fmt"
1212
"html/template"
1313
"io/fs"
14+
"math"
1415
"net/http"
1516
"net/url"
1617
"os"
@@ -294,6 +295,8 @@ func (h *ContainerHandler) idleAppShutdown(ctx context.Context) {
294295
}
295296

296297
func (h *ContainerHandler) healthChecker(ctx context.Context) {
298+
time.Sleep(60 * time.Second) // wait for 1 minute to let the app start up
299+
h.Debug().Msgf("Health checker started for app %s", h.app.Id)
297300
for range h.healthCheckTicker.C {
298301
err := h.WaitForHealth(h.containerConfig.StatusHealthAttempts)
299302
if err == nil {
@@ -707,21 +710,18 @@ func (h *ContainerHandler) WaitForHealth(attempts int) error {
707710

708711
var err error
709712
var resp *http.Response
710-
proxyUrl, err := url.Parse(h.GetProxyUrl())
711-
if err != nil {
712-
return err
713-
}
714-
if !h.stripAppPath {
715-
// Apps like Streamlit require the app path to be present
716-
proxyUrl = proxyUrl.JoinPath(h.app.Path)
717-
}
713+
for attempt := 1; attempt <= attempts; attempt++ {
714+
proxyUrl, err := url.Parse(h.GetProxyUrl())
715+
if err != nil {
716+
return err
717+
}
718+
if !h.stripAppPath {
719+
// Apps like Streamlit require the app path to be present
720+
proxyUrl = proxyUrl.JoinPath(h.app.Path)
721+
}
718722

719-
proxyUrl = proxyUrl.JoinPath(h.health)
720-
if err != nil {
721-
return err
722-
}
723+
proxyUrl = proxyUrl.JoinPath(h.health)
723724

724-
for attempt := 1; attempt <= attempts; attempt++ {
725725
resp, err = client.Get(proxyUrl.String())
726726
statusCode := "N/A"
727727
if err == nil {
@@ -736,7 +736,8 @@ func (h *ContainerHandler) WaitForHealth(attempts int) error {
736736
}
737737

738738
h.Debug().Msgf("Attempt %d failed on %s : status %s err %s", attempt, proxyUrl, statusCode, err)
739-
time.Sleep(1 * time.Second)
739+
sleepSecs := math.Min(float64(attempt), 5)
740+
time.Sleep(time.Duration(sleepSecs) * time.Second)
740741
}
741742
return err
742743
}

internal/container/command.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ func (c *ContainerCommand) RemoveContainer(ctx context.Context, name ContainerNa
147147

148148
// GetContainerState returns the host:port of the running container, "" if not running. running is true if the container is running.
149149
func (c *ContainerCommand) GetContainerState(ctx context.Context, name ContainerName) (string, bool, error) {
150-
containers, err := c.getContainers(ctx, name, false)
150+
containers, err := c.getContainers(ctx, name, true)
151151
if err != nil {
152152
return "", false, fmt.Errorf("error getting containers: %w", err)
153153
}
@@ -345,6 +345,10 @@ func (c *ContainerCommand) RunContainer(ctx context.Context, appEntry *types.App
345345
}
346346

347347
func (c *ContainerCommand) ImageExists(ctx context.Context, name ImageName) (bool, error) {
348+
if c.config.Registry.URL != "" {
349+
return ImageExists(ctx, c.Logger, string(name), &c.config.Registry)
350+
}
351+
348352
c.Debug().Msgf("Getting images with name %s", name)
349353
args := []string{"images", string(name)}
350354
cmd := exec.CommandContext(ctx, c.config.System.ContainerCommand, args...)

internal/container/delegate.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,8 @@ func delegateBuild(ctx context.Context, logger *types.Logger, config *types.Serv
276276
return fmt.Errorf("read image from docker daemon: %w", err)
277277
}
278278

279-
remoteRef, remoteOpts, err := GetDockerConfig(ctx, remoteTag, data.RegistryConfig)
279+
logger.Debug().Msgf("Getting remote registry config for %s", data.ImageTag)
280+
remoteRef, remoteOpts, err := GetDockerConfig(ctx, data.ImageTag, data.RegistryConfig)
280281
if err != nil {
281282
return fmt.Errorf("get remote registry config: %w", err)
282283
}

internal/container/kubernetes.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,16 @@ func loadConfig() (*rest.Config, error) {
5252
}
5353

5454
func (k *KubernetesContainerManager) ImageExists(ctx context.Context, name ImageName) (bool, error) {
55+
if k.config.Registry.URL == "" {
56+
return false, fmt.Errorf("registry url is required for kubernetes container manager")
57+
}
5558
return ImageExists(ctx, k.Logger, string(name), &k.config.Registry)
5659
}
5760

5861
func (k *KubernetesContainerManager) BuildImage(ctx context.Context, imgName ImageName, sourceUrl, containerFile string, containerArgs map[string]string) error {
5962
targetUrl, found := strings.CutPrefix(k.config.Builder.Mode, "delegate:")
6063
if found {
64+
// delegated build
6165
err := sendDelegateBuild(targetUrl, DelegateRequest{
6266
ImageTag: string(imgName),
6367
ContainerFile: containerFile,

internal/container/registry.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,7 @@ func GetDockerConfig(ctx context.Context, imageRef string, registryConfig *types
214214
parseOpts = append(parseOpts, name.Insecure)
215215
}
216216

217+
imageRef = registryConfig.URL + "/" + imageRef
217218
ref, err := name.ParseReference(imageRef, parseOpts...)
218219
if err != nil {
219220
return nil, nil, fmt.Errorf("parse ref: %w", err)

internal/system/config_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,12 @@ func TestServerConfig(t *testing.T) {
7070

7171
// Container Settings
7272
testutil.AssertEqualsString(t, "health", "/", c.AppConfig.Container.HealthUrl)
73-
testutil.AssertEqualsInt(t, "attempts", 30, c.AppConfig.Container.HealthAttemptsAfterStartup)
73+
testutil.AssertEqualsInt(t, "attempts", 10, c.AppConfig.Container.HealthAttemptsAfterStartup)
7474
testutil.AssertEqualsInt(t, "timeout", 5, c.AppConfig.Container.HealthTimeoutSecs)
7575
testutil.AssertEqualsInt(t, "idle", 180, c.AppConfig.Container.IdleShutdownSecs)
7676
testutil.AssertEqualsInt(t, "idle bytes high watermark", 1500, c.AppConfig.Container.IdleBytesHighWatermark)
77-
testutil.AssertEqualsInt(t, "status interval", 5, c.AppConfig.Container.StatusCheckIntervalSecs)
78-
testutil.AssertEqualsInt(t, "status attempts", 3, c.AppConfig.Container.StatusHealthAttempts)
77+
testutil.AssertEqualsInt(t, "status interval", 20, c.AppConfig.Container.StatusCheckIntervalSecs)
78+
testutil.AssertEqualsInt(t, "status attempts", 10, c.AppConfig.Container.StatusHealthAttempts)
7979

8080
testutil.AssertEqualsInt(t, "proxy max idle", 250, c.AppConfig.Proxy.MaxIdleConns)
8181
testutil.AssertEqualsInt(t, "proxy idle timeout", 15, c.AppConfig.Proxy.IdleConnTimeoutSecs)

internal/system/openrun.default.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ cors.max_age = "2678400"
108108

109109
# Health check Config
110110
container.health_url = "/"
111-
container.health_attempts_after_startup = 30
111+
container.health_attempts_after_startup = 10
112112
container.health_timeout_secs = 5
113113

114114
# Idle Shutdown Config
@@ -118,8 +118,8 @@ container.idle_bytes_high_watermark = 1500 # bytes high watermark for idle shutd
118118
# (1500 bytes sent and recv over 180 seconds)
119119

120120
# Status check Config
121-
container.status_check_interval_secs = 5
122-
container.status_health_attempts = 3
121+
container.status_check_interval_secs = 20
122+
container.status_health_attempts = 10
123123

124124
# Proxy related settings
125125
proxy.max_idle_conns = 250

0 commit comments

Comments
 (0)