From c551c291dea83790cf52c4a08a4bbec1ba407c34 Mon Sep 17 00:00:00 2001 From: Bence Csati Date: Wed, 4 Sep 2024 10:29:18 +0200 Subject: [PATCH 1/3] chore: fix some nitpicks Signed-off-by: Bence Csati --- e2e/kpa_test.go | 3 +-- internal/handlers/handlers.go | 4 ++-- internal/server/server.go | 14 ++++---------- .../autocomplete/model/{Field.go => field.go} | 0 pkg/log/logger.go | 3 +-- 5 files changed, 8 insertions(+), 16 deletions(-) rename internal/services/autocomplete/model/{Field.go => field.go} (100%) diff --git a/e2e/kpa_test.go b/e2e/kpa_test.go index b26d6cd..287a42d 100644 --- a/e2e/kpa_test.go +++ b/e2e/kpa_test.go @@ -62,8 +62,7 @@ func TestKPAEndpoints(t *testing.T) { require.Equal(t, http.StatusOK, resp.StatusCode) - autocompleteUrl := fmt.Sprintf(autocompleteURL, "localhost", 8080, "pods") - resp, err = http.Get(autocompleteUrl) + resp, err = http.Get(fmt.Sprintf(autocompleteURL, "localhost", 8080, "pods")) require.NoError(t, err) defer resp.Body.Close() diff --git a/internal/handlers/handlers.go b/internal/handlers/handlers.go index aa2d432..b62b7e8 100644 --- a/internal/handlers/handlers.go +++ b/internal/handlers/handlers.go @@ -2,8 +2,8 @@ package handlers import "github.com/gin-gonic/gin" -// SetupRouter initializes the router and registers all the handlers -func SetupRouter(router *gin.Engine) { +// SetupRoutes registers all the handlers on the router +func SetupRoutes(router *gin.Engine) { router.GET("/search/autocomplete/:resource", AutocompleteHandler) router.GET("/health", HealthHandler) } diff --git a/internal/server/server.go b/internal/server/server.go index 6f6d5bb..0a87ad4 100644 --- a/internal/server/server.go +++ b/internal/server/server.go @@ -21,6 +21,7 @@ func New(config *config.Config) (*Server, error) { // NOTE: Add Auth middleware if required router := gin.New() router.Use(gin.Recovery(), cors.Default()) + gin.SetMode(config.Mode) if config.LogServerAddress != "" { writer, err := net.Dial("udp", config.LogServerAddress) @@ -33,24 +34,17 @@ func New(config *config.Config) (*Server, error) { router.Use(gin.Logger()) } - // Set the mode of the gin router, default is debug - if config.Mode == gin.ReleaseMode { - gin.SetMode(gin.ReleaseMode) - } - // Add trusted proxies e.g. when running KPA behind a reverse proxy or a load balancer if err := router.SetTrustedProxies(config.TrustedProxies); err != nil { return nil, fmt.Errorf("failed to set trusted proxies: %w", err) } - handlers.SetupRouter(router) + handlers.SetupRoutes(router) - server := &Server{ + return &Server{ router: router, config: config, - } - - return server, nil + }, nil } func (s *Server) Run() error { diff --git a/internal/services/autocomplete/model/Field.go b/internal/services/autocomplete/model/field.go similarity index 100% rename from internal/services/autocomplete/model/Field.go rename to internal/services/autocomplete/model/field.go diff --git a/pkg/log/logger.go b/pkg/log/logger.go index 3d7dbed..e12ee08 100644 --- a/pkg/log/logger.go +++ b/pkg/log/logger.go @@ -65,8 +65,7 @@ func InitLogger(config *config.Config) { router = router.Add(slogsyslog.Option{Level: slog.LevelInfo, Writer: writer}.NewSyslogHandler()) } - logger := slog.New(router.Handler()) - logger = logger.With(slog.String("app", "kube-pod-autocomplete")) + logger := slog.New(router.Handler()).With(slog.String("app", "kube-pod-autocomplete")) // Set the default logger to the configured logger, // enabling direct usage of the slog package for logging. From 14d9922846438887bd2191eb4fd83224930b42f7 Mon Sep 17 00:00:00 2001 From: Bence Csati Date: Wed, 4 Sep 2024 10:53:45 +0200 Subject: [PATCH 2/3] docs: nits Signed-off-by: Bence Csati --- README.md | 4 ++-- docs/docs.md | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 35bec5d..8704d1c 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ ## Getting started -- Kube Pod Autocomplete is designed to be used in Kubernetes environment. +- Kube Pod Autocomplete is designed to be used in Kubernetes environments. - Take a look at the [documentation](./docs/docs.md). ## Development @@ -49,7 +49,7 @@ make fmt Build artifacts locally: ```shell -make container-image +make artifacts ``` Once you are done, you can tear down the development environment: diff --git a/docs/docs.md b/docs/docs.md index 13ac611..db6dc4e 100644 --- a/docs/docs.md +++ b/docs/docs.md @@ -1,6 +1,6 @@ # Kube Pod Autocomplete documentation -Kube Pod Autocomplete is a service designed to enhance the user experience when navigating resource lists in Kubernetes clusters. It retrieves specific data based on the requested resource type and supported filters. +This service retrieves specific data based on the requested resource type and supported filters. ## Supported resources From 9c6358bc84e322d0dec9af3ec254d67be5e3a312 Mon Sep 17 00:00:00 2001 From: Bence Csati Date: Wed, 4 Sep 2024 11:54:47 +0200 Subject: [PATCH 3/3] chore: fix log messages Signed-off-by: Bence Csati chore: fix log messages Signed-off-by: Bence Csati --- .golangci.yaml | 2 +- internal/handlers/autocomplete.go | 6 +++--- internal/k8s/client.go | 5 +++-- internal/services/autocomplete/autocomplete.go | 2 +- 4 files changed, 8 insertions(+), 7 deletions(-) diff --git a/.golangci.yaml b/.golangci.yaml index bb03ddc..dcd5034 100644 --- a/.golangci.yaml +++ b/.golangci.yaml @@ -7,7 +7,7 @@ linters-settings: - default - prefix(github.com/csatib02/kube-pod-autocomplete) goimports: - local-prefixes: github.com/bank-vaults/kube-pod-autocomplete + local-prefixes: github.com/csatib02/kube-pod-autocomplete misspell: locale: US nolintlint: diff --git a/internal/handlers/autocomplete.go b/internal/handlers/autocomplete.go index a213081..c203afd 100644 --- a/internal/handlers/autocomplete.go +++ b/internal/handlers/autocomplete.go @@ -24,7 +24,7 @@ func AutocompleteHandler(c *gin.Context) { // var req model.AutoCompleteRequest // if err := c.ShouldBindJSON(&req); err != nil { // slog.Error(fmt.Errorf("failed to bind request: %w", err).Error()) - // http.HandleHTTPError(c, errors.New("failed to bind request")) + // http.HandleHTTPError(c, http.StatusBadRequest, errors.New("failed to bind request")) // return // } @@ -59,14 +59,14 @@ func AutocompleteHandler(c *gin.Context) { service, err := autocomplete.NewAutoCompleteService() if err != nil { slog.Error(fmt.Errorf("failed to create autocomplete service: %w", err).Error()) - httperror.HandleHTTPError(c, http.StatusBadRequest, err) + httperror.HandleHTTPError(c, http.StatusInternalServerError, err) return } suggestions, err := service.GetAutocompleteSuggestions(c, req) if err != nil { slog.Error(fmt.Errorf("failed to get autocomplete suggestions: %w", err).Error()) - httperror.HandleHTTPError(c, http.StatusBadRequest, err) + httperror.HandleHTTPError(c, http.StatusInternalServerError, err) return } diff --git a/internal/k8s/client.go b/internal/k8s/client.go index 860b770..ca29ae1 100644 --- a/internal/k8s/client.go +++ b/internal/k8s/client.go @@ -2,6 +2,7 @@ package k8s import ( "context" + "errors" "fmt" v1 "k8s.io/api/core/v1" @@ -35,7 +36,7 @@ func (c *Client) ListResource(ctx context.Context, resource common.Resources) (c case common.ResourceType: return c.listPods(ctx) default: - return nil, fmt.Errorf("unsupported resource type") + return nil, fmt.Errorf("unsupported resource type: %T", resource) } } @@ -47,7 +48,7 @@ func (c *Client) listPods(ctx context.Context) (*v1.PodList, error) { // Validate whether there are any pods in the cluster if pods == nil { - return nil, fmt.Errorf("failed to list pods: no pods found") + return nil, errors.New("no pods found in the cluster") } return pods, nil diff --git a/internal/services/autocomplete/autocomplete.go b/internal/services/autocomplete/autocomplete.go index d31980e..f13e224 100644 --- a/internal/services/autocomplete/autocomplete.go +++ b/internal/services/autocomplete/autocomplete.go @@ -35,7 +35,7 @@ func (s *Service) GetAutocompleteSuggestions(ctx context.Context, req model.Auto resources, err := s.k8sClient.ListResource(ctx, req.ResourceType) if err != nil { - return nil, fmt.Errorf("failed to list pods: %w", err) + return nil, fmt.Errorf("failed to list resources: %w", err) } return s.extractSuggestions(resources, filters)