Skip to content

Commit

Permalink
Merge pull request #7 from csatib02/chore/fix-nitpicks
Browse files Browse the repository at this point in the history
chore: fix nits
  • Loading branch information
csatib02 authored Sep 8, 2024
2 parents fb65c55 + 9c6358b commit 61f7a55
Show file tree
Hide file tree
Showing 11 changed files with 19 additions and 26 deletions.
2 changes: 1 addition & 1 deletion .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion docs/docs.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down
3 changes: 1 addition & 2 deletions e2e/kpa_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
6 changes: 3 additions & 3 deletions internal/handlers/autocomplete.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
// }

Expand Down Expand Up @@ -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
}

Expand Down
4 changes: 2 additions & 2 deletions internal/handlers/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
5 changes: 3 additions & 2 deletions internal/k8s/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package k8s

import (
"context"
"errors"
"fmt"

v1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -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)
}
}

Expand All @@ -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
Expand Down
14 changes: 4 additions & 10 deletions internal/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion internal/services/autocomplete/autocomplete.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 1 addition & 2 deletions pkg/log/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 61f7a55

Please sign in to comment.