Merge pull request #109957 from adammw/adammw/disruption-implements-scale

disruptioncontroller: check for scale subresource correctly
This commit is contained in:
Kubernetes Prow Robot 2022-06-21 08:00:26 -07:00 committed by GitHub
commit 375fd32b9f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 33 additions and 8 deletions

View File

@ -297,14 +297,13 @@ func (dc *DisruptionController) getScaleController(ctx context.Context, controll
return nil, err return nil, err
} }
gr := mapping.Resource.GroupResource() gr := mapping.Resource.GroupResource()
scale, err := dc.scaleNamespacer.Scales(namespace).Get(ctx, gr, controllerRef.Name, metav1.GetOptions{}) scale, err := dc.scaleNamespacer.Scales(namespace).Get(ctx, gr, controllerRef.Name, metav1.GetOptions{})
if err != nil { if err != nil {
if errors.IsNotFound(err) { if errors.IsNotFound(err) {
// The IsNotFound error can mean either that the resource does not exist, // The IsNotFound error can mean either that the resource does not exist,
// or it exist but doesn't implement the scale subresource. We check which // or it exist but doesn't implement the scale subresource. We check which
// situation we are facing so we can give an appropriate error message. // situation we are facing so we can give an appropriate error message.
isScale, err := dc.implementsScale(gv, controllerRef.Kind) isScale, err := dc.implementsScale(mapping.Resource)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -321,19 +320,26 @@ func (dc *DisruptionController) getScaleController(ctx context.Context, controll
return &controllerAndScale{scale.UID, scale.Spec.Replicas}, nil return &controllerAndScale{scale.UID, scale.Spec.Replicas}, nil
} }
func (dc *DisruptionController) implementsScale(gv schema.GroupVersion, kind string) (bool, error) { func (dc *DisruptionController) implementsScale(gvr schema.GroupVersionResource) (bool, error) {
resourceList, err := dc.discoveryClient.ServerResourcesForGroupVersion(gv.String()) resourceList, err := dc.discoveryClient.ServerResourcesForGroupVersion(gvr.GroupVersion().String())
if err != nil { if err != nil {
return false, err return false, err
} }
scaleSubresourceName := fmt.Sprintf("%s/scale", gvr.Resource)
for _, resource := range resourceList.APIResources { for _, resource := range resourceList.APIResources {
if resource.Kind != kind { if resource.Name != scaleSubresourceName {
continue continue
} }
if strings.HasSuffix(resource.Name, "/scale") {
for _, scaleGv := range scaleclient.NewScaleConverter().ScaleVersions() {
if resource.Group == scaleGv.Group &&
resource.Version == scaleGv.Version &&
resource.Kind == "Scale" {
return true, nil return true, nil
} }
} }
}
return false, nil return false, nil
} }

View File

@ -663,6 +663,25 @@ func TestScaleFinderNoResource(t *testing.T) {
expectError bool expectError bool
}{ }{
"resource implements scale": { "resource implements scale": {
apiResources: []metav1.APIResource{
{
Kind: customGVK.Kind,
Name: resourceName + "/status",
},
{
Kind: "Scale",
Group: autoscalingapi.GroupName,
Version: "v1",
Name: resourceName + "/scale",
},
{
Kind: customGVK.Kind,
Name: resourceName,
},
},
expectError: false,
},
"resource implements unsupported data format for scale subresource": {
apiResources: []metav1.APIResource{ apiResources: []metav1.APIResource{
{ {
Kind: customGVK.Kind, Kind: customGVK.Kind,
@ -673,7 +692,7 @@ func TestScaleFinderNoResource(t *testing.T) {
Name: resourceName + "/scale", Name: resourceName + "/scale",
}, },
}, },
expectError: false, expectError: true,
}, },
"resource does not implement scale": { "resource does not implement scale": {
apiResources: []metav1.APIResource{ apiResources: []metav1.APIResource{