From a9c78e77d548a267813f4ced3aa0c8c0b37d032c Mon Sep 17 00:00:00 2001 From: Julian Vassev Date: Sat, 5 Oct 2024 18:25:04 +0300 Subject: [PATCH] Only pull metadata when necessary In the spirit of 9f1dd7e56d16c0e879a56cf98b04442fd5263857. Make it possible to run controller outside of DO --- driver/driver.go | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/driver/driver.go b/driver/driver.go index 5bc1b342..5b79dd9b 100644 --- a/driver/driver.go +++ b/driver/driver.go @@ -122,20 +122,23 @@ func NewDriver(p NewDriverParams) (*Driver, error) { }) oauthClient := oauth2.NewClient(context.Background(), tokenSource) - mdClient := metadata.NewClient() - var region string - if p.Region == "" { + region := p.Region + hostID := "external" + + if region == "" { + mdClient := metadata.NewClient() var err error region, err = mdClient.Region() if err != nil { return nil, fmt.Errorf("couldn't get region from metadata: %s (are you running outside of a DigitalOcean droplet and possibly forgot to specify the 'region' flag?)", err) } + + hostIDInt, err := mdClient.DropletID() + if err != nil { + return nil, fmt.Errorf("couldn't get droplet ID from metadata: %s (are you running outside of a DigitalOcean droplet?)", err) + } + hostID = strconv.Itoa(hostIDInt) } - hostIDInt, err := mdClient.DropletID() - if err != nil { - return nil, fmt.Errorf("couldn't get droplet ID from metadata: %s (are you running outside of a DigitalOcean droplet?)", err) - } - hostID := strconv.Itoa(hostIDInt) var opts []godo.ClientOpt opts = append(opts, godo.SetBaseURL(p.URL))