Skip to content

Commit

Permalink
refactor: use cache correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
FabianKramm committed Nov 15, 2023
1 parent 438d8ac commit 720cbd5
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 21 deletions.
17 changes: 1 addition & 16 deletions pkg/server/filters/metrics_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -516,28 +516,13 @@ func (p *MetricsServerProxy) rewritePodMetricsListData(data []byte) ([]byte, err
// returns the types.NamespacedName list of pods for the given namespace
func getVirtualPodObjectsInNamespace(ctx context.Context, vClient client.Client, namespace string) ([]corev1.Pod, error) {
podList := &corev1.PodList{}

// This is to counter an issue which occurred after the latest update
// to 0.28 client libraries where the cache api was changed and broke
// our cachedClients resulting in errors of the form
// "unable to list: kube-system because of unknown namespace for the cache"
err := vClient.List(ctx, podList, &client.ListOptions{
// Namespace: namespace,
Namespace: namespace,
})
if err != nil {
return nil, err
}

// manually filter until the above issue with cachedClient is resolved
filteredPodList := &corev1.PodList{}
for _, pod := range podList.Items {
if pod.Namespace == namespace {
filteredPodList.Items = append(filteredPodList.Items, pod)
}
}

podList.Items = filteredPodList.Items

return podList.Items, nil
}

Expand Down
15 changes: 10 additions & 5 deletions pkg/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,12 +296,17 @@ func (s *Server) ServeOnListenerTLS(address string, port int, stopChan <-chan st
}

func createCachedClient(ctx context.Context, config *rest.Config, namespace string, restMapper meta.RESTMapper, scheme *runtime.Scheme, registerIndices func(cache cache.Cache) error) (client.Client, error) {
// create cache options
cacheOptions := cache.Options{
Scheme: scheme,
Mapper: restMapper,
}
if namespace != "" {
cacheOptions.DefaultNamespaces = map[string]cache.Config{namespace: {}}
}

// create the new cache
clientCache, err := cache.New(config, cache.Options{
Scheme: scheme,
Mapper: restMapper,
DefaultNamespaces: map[string]cache.Config{namespace: {}},
})
clientCache, err := cache.New(config, cacheOptions)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 720cbd5

Please sign in to comment.