forked from kubernetes/kube-state-metrics
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix multiListerWatcher to proceed on individual errors
The multiListerWatcher is a composite object encapsulating multiple ListerWatchers and implements the ListerWatcher interface. When calling the List method on the multiListerWatcher, if an individual Lister call fails, the outcome is treated as an error and the entire call fails. This leads to KSM not exporting any metrics when it does not have the necessary permissions for resources in one more more namespaces. This commit modifies the multiListerWatcher List function to log errors from individual ListerWatchers and continue with execution. As a result, when KSM does not have permissions to list resources from a namespace, it will still export metrics from namespaces it has permissions to. Fixes kubernetes#1413 Signed-off-by: fpetkovski <filip.petkovsky@gmail.com>
- Loading branch information
1 parent
cbfc906
commit 50b9477
Showing
2 changed files
with
104 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
/* | ||
Copyright 2019 The Kubernetes Authors All rights reserved. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package listwatch_test | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
v1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
"k8s.io/apimachinery/pkg/watch" | ||
"k8s.io/client-go/tools/cache" | ||
"k8s.io/kube-state-metrics/v2/pkg/listwatch" | ||
) | ||
|
||
var errList = fmt.Errorf("error listing resources") | ||
|
||
type listWatcherStub struct { | ||
err error | ||
} | ||
|
||
func (l listWatcherStub) List(_ metav1.ListOptions) (runtime.Object, error) { | ||
if l.err != nil { | ||
return nil, l.err | ||
} | ||
|
||
return makePodList(), nil | ||
} | ||
|
||
func (l listWatcherStub) Watch(_ metav1.ListOptions) (watch.Interface, error) { | ||
return nil, nil | ||
} | ||
|
||
func TestMultiNamespaceListerWatcher(t *testing.T) { | ||
namespaces := []string{"ns-succeed", "ns-fail", "ns-succeed"} | ||
mlw := listwatch.MultiNamespaceListerWatcher(namespaces, []string{}, func(s string) cache.ListerWatcher { | ||
if s == "ns-fail" { | ||
return listWatcherStub{err: errList} | ||
} else { | ||
return listWatcherStub{} | ||
} | ||
}) | ||
|
||
result, err := mlw.List(metav1.ListOptions{}) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
list, ok := result.(*metav1.List) | ||
if !ok { | ||
t.Fatal("invalid result type: want meta.List") | ||
} | ||
if len(list.Items) != 2 { | ||
t.Fatalf("invalid item count: got %d, want %d", len(list.Items), 1) | ||
} | ||
if list.ResourceVersion != "1//1" { | ||
t.Fatalf("invalid resource version: got %s, want %s", list.ResourceVersion, "/1") | ||
} | ||
} | ||
|
||
func makePodList() *v1.PodList { | ||
return &v1.PodList{ | ||
TypeMeta: metav1.TypeMeta{ | ||
Kind: "pods", | ||
APIVersion: "", | ||
}, | ||
ListMeta: metav1.ListMeta{ | ||
ResourceVersion: "1", | ||
}, | ||
Items: []v1.Pod{ | ||
{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "test-pod", | ||
}, | ||
}, | ||
}, | ||
} | ||
} |