Skip to content

Commit 5bd6a23

Browse files
committed
Minimize API requests by skipping to check a given image ID
Save on API requests and stop querying for the image ID from the DO API if the numeric ID is already given: a failure to reference the image would show up during the droplet create request and lead to the same backoff that we run into today. Also refactor the code and improve error messages slightly as a drive-by change.
1 parent 8756303 commit 5bd6a23

File tree

2 files changed

+15
-12
lines changed

2 files changed

+15
-12
lines changed

cloud/services/computes/droplets.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ func (s *Service) CreateDroplet(scope *scope.MachineScope) (*godo.Droplet, error
6666
clusterName := infrav1.DOSafeName(s.scope.Name())
6767
instanceName := infrav1.DOSafeName(scope.Name())
6868

69-
image, err := s.GetImage(scope.DOMachine.Spec.Image)
69+
imageID, err := s.GetImageID(scope.DOMachine.Spec.Image)
7070
if err != nil {
7171
return nil, errors.Wrap(err, "failed getting image")
7272
}
@@ -102,7 +102,7 @@ func (s *Service) CreateDroplet(scope *scope.MachineScope) (*godo.Droplet, error
102102
Size: scope.DOMachine.Spec.Size,
103103
SSHKeys: sshkeys,
104104
Image: godo.DropletCreateImage{
105-
ID: image.ID,
105+
ID: imageID,
106106
},
107107
UserData: bootstrapData,
108108
PrivateNetworking: true,

cloud/services/computes/images.go

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,27 +17,30 @@ limitations under the License.
1717
package computes
1818

1919
import (
20+
"fmt"
21+
2022
"github.com/digitalocean/godo"
2123
"github.com/pkg/errors"
2224

2325
"k8s.io/apimachinery/pkg/util/intstr"
2426
)
2527

26-
func (s *Service) GetImage(imageSpec intstr.IntOrString) (*godo.Image, error) {
28+
func (s *Service) GetImageID(imageSpec intstr.IntOrString) (int, error) {
2729
var image *godo.Image
28-
var reterr error
2930

3031
if imageSpec.IntValue() != 0 { // nolint
31-
image, _, reterr = s.scope.Images.GetByID(s.ctx, imageSpec.IntValue())
32-
} else if imageSpec.String() != "" && imageSpec.String() != "0" {
33-
image, _, reterr = s.scope.Images.GetBySlug(s.ctx, imageSpec.String())
34-
} else {
35-
reterr = errors.New("Unable to get image")
32+
return imageSpec.IntValue(), nil
33+
}
34+
35+
imageSpecStr := imageSpec.String()
36+
if imageSpecStr == "" || imageSpecStr == "0" {
37+
return 0, fmt.Errorf("invalid image spec string %q", imageSpecStr)
3638
}
3739

38-
if reterr != nil {
39-
return nil, errors.Wrap(reterr, "Unable to get image")
40+
image, _, err := s.scope.Images.GetBySlug(s.ctx, imageSpecStr)
41+
if err != nil {
42+
return 0, errors.Wrap(err, "Unable to get image")
4043
}
4144

42-
return image, nil
45+
return image.ID, nil
4346
}

0 commit comments

Comments
 (0)