Skip to content

Commit

Permalink
Metadata package: remove OVF-provider from default list; move retry()…
Browse files Browse the repository at this point in the history
… to Azure-OVF provider

Signed-off-by: Michael Schnerring <3743342+schnerring@users.noreply.github.com>
  • Loading branch information
schnerring committed May 24, 2021
1 parent f9ea9d5 commit cbd0e5e
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 38 deletions.
34 changes: 2 additions & 32 deletions pkg/metadata/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,12 @@ package main
import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"net/http"
"os"
"path"
"strconv"
"strings"
"syscall"
"time"

log "github.com/sirupsen/logrus"
)
Expand Down Expand Up @@ -92,8 +89,7 @@ func main() {
"packet",
"metaldata",
"cdrom",
"azure-imds",
"azure-ovf",
"azure",
}
args := flag.Args()
if len(args) > 0 {
Expand All @@ -119,14 +115,8 @@ func main() {
netProviders = append(netProviders, NewDigitalOcean())
case p == "metaldata":
netProviders = append(netProviders, NewMetalData())
case p == "azure-imds":
case p == "azure":
netProviders = append(netProviders, NewAzureIMDS())
case p == "azure-ovf":
// TODO not every provider should create a separate http client
client := &http.Client{
Timeout: time.Second * 2,
}
netProviders = append(netProviders, NewAzureOVF(client))
case p == "cdrom":
cdromProviders = ListCDROMs()
case strings.HasPrefix(p, "file="):
Expand Down Expand Up @@ -297,23 +287,3 @@ type Entry struct {
Content *string `json:"content,omitempty"`
Entries map[string]Entry `json:"entries,omitempty"`
}

// https://stackoverflow.com/questions/47606761/repeat-code-if-an-error-occured/47606858#47606858
// https://blog.abourget.net/en/2016/01/04/my-favorite-golang-retry-function/
func retry(attempts int, sleep time.Duration, f func() error) (err error) {
for i := 0; ; i++ {
err = f()
if err == nil {
return
}

if i >= (attempts - 1) {
break
}

time.Sleep(sleep)

log.Debugf("Retrying after error: %s", err)
}
return fmt.Errorf("After %d attempts, last error: %s", attempts, err)
}
8 changes: 2 additions & 6 deletions pkg/metadata/provider_azure_imds.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,7 @@ func (p *ProviderAzureIMDS) String() string {
func (p *ProviderAzureIMDS) Probe() bool {
// "Poll" VM Unique ID
// See: https://azure.microsoft.com/en-us/blog/accessing-and-using-azure-vm-unique-id/
pollVMID := func() error {
_, err := p.imdsGet("compute/vmId")
return err
}
if err := retry(6, 5*time.Second, pollVMID); err != nil {
if _, err := p.imdsGet("compute/vmId"); err != nil {
log.Debugf("%s: Probe failed: %s", p.String(), err)
return false
}
Expand Down Expand Up @@ -173,7 +169,7 @@ func (p *ProviderAzureIMDS) getUserData() ([]byte, error) {
if len(userData) > 0 { // Always false
log.Warnf("%s: Unexpectedly received user data: \n%s", p.String(), string(userData))
// TODO
// Getting user data via IMDS is disabled. See upstream issue:
// Getting user data via IMDS is disabled. See blocking upstream issue:
// * https://github.com/MicrosoftDocs/azure-docs/issues/64154
// * https://github.com/MicrosoftDocs/azure-docs/issues/30370 (OP)
// return userData, nil
Expand Down
20 changes: 20 additions & 0 deletions pkg/metadata/provider_azure_ovf.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,3 +169,23 @@ func (p *ProviderAzureOVF) getUserData(ovf *OVF) ([]byte, error) {
log.Debugf("%s: Raw user data: \n%s", p.String(), string(userData))
return userData, nil
}

// https://stackoverflow.com/questions/47606761/repeat-code-if-an-error-occured/47606858#47606858
// https://blog.abourget.net/en/2016/01/04/my-favorite-golang-retry-function/
func retry(attempts int, sleep time.Duration, f func() error) (err error) {
for i := 0; ; i++ {
err = f()
if err == nil {
return
}

if i >= (attempts - 1) {
break
}

time.Sleep(sleep)

log.Debugf("Retrying after error: %s", err)
}
return fmt.Errorf("After %d attempts, last error: %s", attempts, err)
}

0 comments on commit cbd0e5e

Please sign in to comment.