Merge pull request #98346 from mortent/checkForScalePDBs
Check if resources implement scale in disruption controller
This commit is contained in:
@@ -40,6 +40,7 @@ import (
|
||||
"k8s.io/apimachinery/pkg/util/intstr"
|
||||
"k8s.io/apimachinery/pkg/util/uuid"
|
||||
"k8s.io/apimachinery/pkg/util/wait"
|
||||
discoveryfake "k8s.io/client-go/discovery/fake"
|
||||
"k8s.io/client-go/informers"
|
||||
"k8s.io/client-go/kubernetes/fake"
|
||||
scalefake "k8s.io/client-go/scale/fake"
|
||||
@@ -105,8 +106,9 @@ type disruptionController struct {
|
||||
dStore cache.Store
|
||||
ssStore cache.Store
|
||||
|
||||
coreClient *fake.Clientset
|
||||
scaleClient *scalefake.FakeScaleClient
|
||||
coreClient *fake.Clientset
|
||||
scaleClient *scalefake.FakeScaleClient
|
||||
discoveryClient *discoveryfake.FakeDiscovery
|
||||
}
|
||||
|
||||
var customGVK = schema.GroupVersionKind{
|
||||
@@ -124,6 +126,9 @@ func newFakeDisruptionController() (*disruptionController, *pdbStates) {
|
||||
scheme := runtime.NewScheme()
|
||||
scheme.AddKnownTypeWithName(customGVK, &v1.Service{})
|
||||
fakeScaleClient := &scalefake.FakeScaleClient{}
|
||||
fakeDiscovery := &discoveryfake.FakeDiscovery{
|
||||
Fake: &core.Fake{},
|
||||
}
|
||||
|
||||
dc := NewDisruptionController(
|
||||
informerFactory.Core().V1().Pods(),
|
||||
@@ -135,6 +140,7 @@ func newFakeDisruptionController() (*disruptionController, *pdbStates) {
|
||||
coreClient,
|
||||
testrestmapper.TestOnlyStaticRESTMapper(scheme),
|
||||
fakeScaleClient,
|
||||
fakeDiscovery,
|
||||
)
|
||||
dc.getUpdater = func() updater { return ps.Set }
|
||||
dc.podListerSynced = alwaysReady
|
||||
@@ -157,6 +163,7 @@ func newFakeDisruptionController() (*disruptionController, *pdbStates) {
|
||||
informerFactory.Apps().V1().StatefulSets().Informer().GetStore(),
|
||||
coreClient,
|
||||
fakeScaleClient,
|
||||
fakeDiscovery,
|
||||
}, ps
|
||||
}
|
||||
|
||||
@@ -571,6 +578,77 @@ func TestScaleResource(t *testing.T) {
|
||||
ps.VerifyPdbStatus(t, pdbName, disruptionsAllowed, pods, replicas-maxUnavailable, replicas, map[string]metav1.Time{})
|
||||
}
|
||||
|
||||
func TestScaleFinderNoResource(t *testing.T) {
|
||||
resourceName := "customresources"
|
||||
testCases := map[string]struct {
|
||||
apiResources []metav1.APIResource
|
||||
expectError bool
|
||||
}{
|
||||
"resource implements scale": {
|
||||
apiResources: []metav1.APIResource{
|
||||
{
|
||||
Kind: customGVK.Kind,
|
||||
Name: resourceName,
|
||||
},
|
||||
{
|
||||
Kind: customGVK.Kind,
|
||||
Name: resourceName + "/scale",
|
||||
},
|
||||
},
|
||||
expectError: false,
|
||||
},
|
||||
"resource does not implement scale": {
|
||||
apiResources: []metav1.APIResource{
|
||||
{
|
||||
Kind: customGVK.Kind,
|
||||
Name: resourceName,
|
||||
},
|
||||
},
|
||||
expectError: true,
|
||||
},
|
||||
}
|
||||
|
||||
for tn, tc := range testCases {
|
||||
t.Run(tn, func(t *testing.T) {
|
||||
customResourceUID := uuid.NewUUID()
|
||||
|
||||
dc, _ := newFakeDisruptionController()
|
||||
|
||||
dc.scaleClient.AddReactor("get", resourceName, func(action core.Action) (handled bool, ret runtime.Object, err error) {
|
||||
gr := schema.GroupResource{
|
||||
Group: customGVK.Group,
|
||||
Resource: resourceName,
|
||||
}
|
||||
return true, nil, errors.NewNotFound(gr, "name")
|
||||
})
|
||||
dc.discoveryClient.Resources = []*metav1.APIResourceList{
|
||||
{
|
||||
GroupVersion: customGVK.GroupVersion().String(),
|
||||
APIResources: tc.apiResources,
|
||||
},
|
||||
}
|
||||
|
||||
trueVal := true
|
||||
ownerRef := &metav1.OwnerReference{
|
||||
Kind: customGVK.Kind,
|
||||
APIVersion: customGVK.GroupVersion().String(),
|
||||
Controller: &trueVal,
|
||||
UID: customResourceUID,
|
||||
}
|
||||
|
||||
_, err := dc.getScaleController(ownerRef, "default")
|
||||
|
||||
if tc.expectError && err == nil {
|
||||
t.Error("expected error, but didn't get one")
|
||||
}
|
||||
|
||||
if !tc.expectError && err != nil {
|
||||
t.Errorf("did not expect error, but got %v", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// Verify that multiple controllers doesn't allow the PDB to be set true.
|
||||
func TestMultipleControllers(t *testing.T) {
|
||||
const podCount = 2
|
||||
|
Reference in New Issue
Block a user