Skip to content

Commit 5df9dfb

Browse files
committed
improved source filter
Signed-off-by: Frank Jogeleit <[email protected]>
1 parent 289dc9e commit 5df9dfb

File tree

5 files changed

+43
-5
lines changed

5 files changed

+43
-5
lines changed

backend/pkg/server/api/handler.go

+13-1
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,18 @@ func (h *Handler) ListPolicySources(ctx *gin.Context) {
5151
ctx.JSON(http.StatusOK, details)
5252
}
5353

54+
func (h *Handler) ListNamespaces(ctx *gin.Context) {
55+
namespaces, err := h.service.Namespaces(ctx, ctx.Param("cluster"), ctx.Request.URL.Query())
56+
if err != nil {
57+
zap.L().Error("failed to list namespaces", zap.String("cluster", ctx.Param("cluster")), zap.Error(err))
58+
59+
ctx.AbortWithStatus(http.StatusInternalServerError)
60+
return
61+
}
62+
63+
ctx.JSON(http.StatusOK, namespaces)
64+
}
65+
5466
func (h *Handler) CreateException(ctx *gin.Context) {
5567
req := service.ExceptionRequest{
5668
Cluster: ctx.Param("cluster"),
@@ -224,7 +236,7 @@ func (h *Handler) Dashboard(ctx *gin.Context) {
224236
var namespaces []string
225237
g.Go(func() error {
226238
var err error
227-
namespaces, err = endpoints.Core.ListNamespaces(ctx, url.Values{
239+
namespaces, err = h.service.Namespaces(ctx, ctx.Param("cluster"), url.Values{
228240
"sources": query["sources"],
229241
"kinds": query["kinds"],
230242
"categories": query["categories"],

backend/pkg/server/server.go

+1
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ func (s *Server) RegisterAPI(c *api.Config, customBoards map[string]api.CustomBo
6969
s.api.GET("config/:cluster/resource/:id", handler.GetResourceDetails)
7070
s.api.POST("config/:cluster/resource/:id/exception", handler.CreateException)
7171
s.api.GET("config/:cluster/policy-sources", handler.ListPolicySources)
72+
s.api.GET("config/:cluster/namespaces", handler.ListNamespaces)
7273
s.api.GET("config/:cluster/:source/policy/details", handler.GetPolicyDetails)
7374
s.api.GET("config/:cluster/:source/policies", handler.Policies)
7475
s.api.GET("config/:cluster/:source/policy-report", handler.PolicyReport)

backend/pkg/service/service.go

+28-1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,21 @@ func (s *Service) plugin(cluster, p string) (*plugin.Client, bool) {
4747
return c, ok
4848
}
4949

50+
func (s *Service) Namespaces(ctx context.Context, cluster string, query url.Values) ([]string, error) {
51+
client, err := s.core(cluster)
52+
if err != nil {
53+
return nil, err
54+
}
55+
56+
if len(query["sources"]) == 1 {
57+
config := s.configs[query["sources"][0]]
58+
59+
query["status"] = config.EnabledResults()
60+
}
61+
62+
return client.ListNamespaces(ctx, query)
63+
}
64+
5065
func (s *Service) PolicyDetails(ctx context.Context, cluster, source, policy string, query url.Values) (any, error) {
5166
client, err := s.core(cluster)
5267
if err != nil {
@@ -56,7 +71,10 @@ func (s *Service) PolicyDetails(ctx context.Context, cluster, source, policy str
5671
query.Set("sources", source)
5772
query.Set("policies", policy)
5873

59-
config := s.configs[source]
74+
config, ok := s.configs[source]
75+
if ok {
76+
query["status"] = config.EnabledResults()
77+
}
6078

6179
g := &errgroup.Group{}
6280

@@ -355,6 +373,15 @@ func (s *Service) Dashboard(ctx context.Context, cluster string, sources []strin
355373
combinedFilter, namespaceFilter, clusterFilter := BuildFilters(query)
356374
combinedFilter.Set("namespaced", strconv.FormatBool(!clusterScope))
357375

376+
if len(sources) == 1 {
377+
config, ok := s.configs[sources[0]]
378+
if ok {
379+
combinedFilter["status"] = config.EnabledResults()
380+
namespaceFilter["status"] = config.EnabledResults()
381+
clusterFilter["status"] = config.EnabledResults()
382+
}
383+
}
384+
358385
namespaceResults := make(map[string]core.NamespaceStatusCounts, len(sources))
359386
clusterResults := make(map[string]map[string]int, len(sources))
360387
showResults := make([]string, 0, len(sources))

frontend/modules/core/api.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ export class CoreAPI {
9393
}
9494

9595
namespaces (filter?: Filter) {
96-
return exec<string[]>(`/proxy/${this.cluster}/core/v1/namespaces`, { baseURL: this.baseURL, params: { ...filter } })
96+
return exec<string[]>(`/api/config/${this.cluster}/namespaces`, { baseURL: this.baseURL, params: { ...filter } })
9797
}
9898

9999
namespacedKinds (sources?: string[]) {

frontend/modules/core/composables/api.ts

-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import { CoreAPI } from "../api";
22

3-
type CancelFunc = (reason?: string) => void
4-
53
type Callback<T> = (api: CoreAPI) => Promise<T>
64

75
export type APIResult<T> = {

0 commit comments

Comments
 (0)