Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fixes an edge case where a container might get stuck in created state #3338

Merged
merged 4 commits into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion internal/docker/container_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func (s *ContainerStore) checkConnectivity() error {
}

running := lo.Filter(containers, func(item Container, index int) bool {
return item.State == "running"
return item.State != "exited"
})

sem := semaphore.NewWeighted(maxFetchParallelism)
Expand Down
2 changes: 1 addition & 1 deletion internal/web/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func Test_createRoutes_version(t *testing.T) {
fs := afero.NewMemMapFs()
require.NoError(t, afero.WriteFile(fs, "index.html", []byte("index page"), 0644), "WriteFile should have no error.")
handler := createHandler(nil, afero.NewIOFS(fs), Config{Base: "/", Version: "dev", Authorization: Authorization{Provider: NONE}})
req, err := http.NewRequest("GET", "/version", nil)
req, err := http.NewRequest("GET", "/api/version", nil)
require.NoError(t, err, "NewRequest should not return an error.")
rr := httptest.NewRecorder()

Expand Down
18 changes: 18 additions & 0 deletions internal/web/debug.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package web

import (
"encoding/json"
"net/http"
)

func (h *handler) debugStore(w http.ResponseWriter, r *http.Request) {
respone := make(map[string]interface{})
respone["hosts"] = h.multiHostService.Hosts()
containers, errors := h.multiHostService.ListAllContainers()
respone["containers"] = containers
respone["errors"] = errors

w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(respone)
}
52 changes: 28 additions & 24 deletions internal/web/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ func CreateServer(multiHostService *MultiHostService, content fs.FS, config Conf
var fileServer http.Handler

func createRouter(h *handler) *chi.Mux {
fileServer = http.FileServer(http.FS(h.content))
base := h.config.Base
r := chi.NewRouter()

Expand All @@ -82,41 +83,46 @@ func createRouter(h *handler) *chi.Mux {
if h.config.Authorization.Provider != NONE {
r.Use(h.config.Authorization.Authorizer.AuthMiddleware)
}
r.Group(func(r chi.Router) {

r.Route("/api", func(r chi.Router) {
// Authenticated routes
r.Group(func(r chi.Router) {
if h.config.Authorization.Provider != NONE {
r.Use(auth.RequireAuthentication)
}
r.Get("/api/hosts/{host}/containers/{id}/logs/stream", h.streamContainerLogs)
r.Get("/api/hosts/{host}/containers/{id}/logs/download", h.downloadLogs)
r.Get("/api/hosts/{host}/containers/{id}/logs", h.fetchLogsBetweenDates)
r.Get("/api/hosts/{host}/logs/mergedStream/{ids}", h.streamLogsMerged)
r.Get("/api/stacks/{stack}/logs/stream", h.streamStackLogs)
r.Get("/api/services/{service}/logs/stream", h.streamServiceLogs)
r.Get("/api/groups/{group}/logs/stream", h.streamGroupedLogs)
r.Get("/api/events/stream", h.streamEvents)
r.Get("/hosts/{host}/containers/{id}/logs/stream", h.streamContainerLogs)
r.Get("/hosts/{host}/containers/{id}/logs/download", h.downloadLogs)
r.Get("/hosts/{host}/containers/{id}/logs", h.fetchLogsBetweenDates)
r.Get("/hosts/{host}/logs/mergedStream/{ids}", h.streamLogsMerged)
r.Get("/stacks/{stack}/logs/stream", h.streamStackLogs)
r.Get("/services/{service}/logs/stream", h.streamServiceLogs)
r.Get("/groups/{group}/logs/stream", h.streamGroupedLogs)
r.Get("/events/stream", h.streamEvents)
if h.config.EnableActions {
r.Post("/api/hosts/{host}/containers/{id}/actions/{action}", h.containerActions)
r.Post("/hosts/{host}/containers/{id}/actions/{action}", h.containerActions)
}
r.Get("/api/releases", h.releases)
r.Get("/api/profile/avatar", h.avatar)
r.Patch("/api/profile", h.updateProfile)
r.Get("/releases", h.releases)
r.Get("/profile/avatar", h.avatar)
r.Patch("/profile", h.updateProfile)
r.Get("/version", h.version)
if log.Debug().Enabled() {
r.Get("/debug/store", h.debugStore)
}
})

defaultHandler := http.StripPrefix(strings.Replace(base+"/", "//", "/", 1), http.HandlerFunc(h.index))
r.Get("/*", func(w http.ResponseWriter, req *http.Request) {
defaultHandler.ServeHTTP(w, req)
})
// Public API routes
if h.config.Authorization.Provider == SIMPLE {
r.Post("/token", h.createToken)
r.Delete("/token", h.deleteToken)
}
})

if h.config.Authorization.Provider == SIMPLE {
r.Post("/api/token", h.createToken)
r.Delete("/api/token", h.deleteToken)
}

r.Get("/healthcheck", h.healthcheck)

defaultHandler := http.StripPrefix(strings.Replace(base+"/", "//", "/", 1), http.HandlerFunc(h.index))
r.Get("/*", func(w http.ResponseWriter, req *http.Request) {
defaultHandler.ServeHTTP(w, req)
})
})

if base != "/" {
Expand All @@ -125,8 +131,6 @@ func createRouter(h *handler) *chi.Mux {
})
}

fileServer = http.FileServer(http.FS(h.content))

// r.Mount("/debug", middleware.Profiler())

return r
Expand Down
Loading