Skip to content

Commit

Permalink
Merge pull request kubernetes#3572 from jayantjain93/NoNodePoolIssue
Browse files Browse the repository at this point in the history
added a new NodeGroupDoesNotExistError in errors.go
  • Loading branch information
k8s-ci-robot authored Oct 2, 2020
2 parents 09c597e + d987394 commit 59ba79b
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 10 deletions.
12 changes: 12 additions & 0 deletions cluster-autoscaler/cloudprovider/gce/autoscaling_gce_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ import (
"strings"
"time"

"google.golang.org/api/googleapi"
"k8s.io/autoscaler/cluster-autoscaler/cloudprovider"
"k8s.io/autoscaler/cluster-autoscaler/utils/errors"
"k8s.io/autoscaler/cluster-autoscaler/utils/klogx"

gce "google.golang.org/api/compute/v1"
Expand Down Expand Up @@ -152,6 +154,11 @@ func (client *autoscalingGceClientV1) FetchMigTargetSize(migRef GceRef) (int64,
registerRequest("instance_group_managers", "get")
igm, err := client.gceService.InstanceGroupManagers.Get(migRef.Project, migRef.Zone, migRef.Name).Do()
if err != nil {
if err, ok := err.(*googleapi.Error); ok {
if err.Code == http.StatusNotFound {
return 0, errors.NewAutoscalerError(errors.NodeGroupDoesNotExistError, "%s", err.Error())
}
}
return 0, err
}
return igm.TargetSize, nil
Expand Down Expand Up @@ -345,6 +352,11 @@ func (client *autoscalingGceClientV1) FetchMigTemplate(migRef GceRef) (*gce.Inst
registerRequest("instance_group_managers", "get")
igm, err := client.gceService.InstanceGroupManagers.Get(migRef.Project, migRef.Zone, migRef.Name).Do()
if err != nil {
if err, ok := err.(*googleapi.Error); ok {
if err.Code == http.StatusNotFound {
return nil, errors.NewAutoscalerError(errors.NodeGroupDoesNotExistError, "%s", err.Error())
}
}
return nil, err
}
templateUrl, err := url.Parse(igm.InstanceTemplate)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ limitations under the License.
package gce

import (
"fmt"
klog "k8s.io/klog/v2"
"sync"

klog "k8s.io/klog/v2"
)

// MigTargetSizesProvider allows obtaining target sizes of MIGs
Expand Down Expand Up @@ -55,7 +55,9 @@ func (c *cachingMigTargetSizesProvider) GetMigTargetSize(migRef GceRef) (int64,
}

newTargetSizes, err := c.fillInMigTargetSizeCache()
if err != nil {

size, found := newTargetSizes[migRef]
if err != nil || !found {
// fallback to querying for single mig
targetSize, err = c.gceClient.FetchMigTargetSize(migRef)
if err != nil {
Expand All @@ -65,12 +67,6 @@ func (c *cachingMigTargetSizesProvider) GetMigTargetSize(migRef GceRef) (int64,
return targetSize, nil
}

// if we still do not have value here return an error
size, found := newTargetSizes[migRef]
if !found {
return 0, fmt.Errorf("Could not get target size for mig %v", migRef.String())
}

// we are good
return size, nil
}
Expand Down
3 changes: 3 additions & 0 deletions cluster-autoscaler/utils/errors/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ const (
// ConfigurationError is an error related to bad configuration provided
// by a user.
ConfigurationError AutoscalerErrorType = "configurationError"
// NodeGroupDoesNotExistError signifies that a NodeGroup
// does not exist.
NodeGroupDoesNotExistError AutoscalerErrorType = "nodeGroupDoesNotExistError"
)

// NewAutoscalerError returns new autoscaler error with a message constructed from format string
Expand Down
23 changes: 22 additions & 1 deletion cluster-autoscaler/utils/test/test_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ func NewHttpServerMock() *HttpServerMock {
mux.HandleFunc("/",
func(w http.ResponseWriter, req *http.Request) {
result := httpServerMock.handle(req.URL.Path)
w.Write([]byte(result))
_, _ = w.Write([]byte(result))
})

server := httptest.NewServer(mux)
Expand All @@ -237,3 +237,24 @@ func (l *HttpServerMock) handle(url string) string {
args := l.Called(url)
return args.String(0)
}

// NewHttpServerMockWithStatusCode creates new HttpServerMock.
func NewHttpServerMockWithStatusCode() *HttpServerMock {
httpServerMock := &HttpServerMock{}
mux := http.NewServeMux()
mux.HandleFunc("/",
func(w http.ResponseWriter, req *http.Request) {
code, result := httpServerMock.handleWithStatusCode(req.URL.Path)
w.WriteHeader(code)
_, _ = w.Write([]byte(result))
})

server := httptest.NewServer(mux)
httpServerMock.Server = server
return httpServerMock
}

func (l *HttpServerMock) handleWithStatusCode(url string) (int, string) {
args := l.Called(url)
return args.Int(0), args.String(1)
}

0 comments on commit 59ba79b

Please sign in to comment.