1
0
mirror of https://github.com/rancher/os.git synced 2025-09-09 02:31:36 +00:00

Make datasource.AvailabilityChanges() able to be dynamic so fail out for configdrive mount and url 404's faster

Signed-off-by: Sven Dowideit <SvenDowideit@home.org.au>
This commit is contained in:
Sven Dowideit
2017-03-02 11:52:29 +10:00
parent 78051c2814
commit 10a4c59385
5 changed files with 96 additions and 97 deletions

View File

@@ -37,13 +37,14 @@ const (
)
type ConfigDrive struct {
root string
readFile func(filename string) ([]byte, error)
lastError error
root string
readFile func(filename string) ([]byte, error)
lastError error
availabilityChanges bool
}
func NewDatasource(root string) *ConfigDrive {
return &ConfigDrive{root, ioutil.ReadFile, nil}
return &ConfigDrive{root, ioutil.ReadFile, nil, true}
}
func (cd *ConfigDrive) IsAvailable() bool {
@@ -51,6 +52,8 @@ func (cd *ConfigDrive) IsAvailable() bool {
cd.lastError = MountConfigDrive()
if cd.lastError != nil {
log.Error(cd.lastError)
// Don't keep retrying if we can't mount
cd.availabilityChanges = false
return false
}
defer cd.Finish()
@@ -58,6 +61,7 @@ func (cd *ConfigDrive) IsAvailable() bool {
_, cd.lastError = os.Stat(cd.root)
return !os.IsNotExist(cd.lastError)
// TODO: consider changing IsNotExists to not-available _and_ does not change
}
func (cd *ConfigDrive) Finish() error {
@@ -72,7 +76,7 @@ func (cd *ConfigDrive) String() string {
}
func (cd *ConfigDrive) AvailabilityChanges() bool {
return true
return cd.availabilityChanges
}
func (cd *ConfigDrive) ConfigRoot() string {
@@ -130,15 +134,13 @@ func (cd *ConfigDrive) tryReadFile(filename string) ([]byte, error) {
}
defer cd.Finish()
}
log.Printf("Attempting to read from %q\n", filename)
log.Debugf("Attempting to read from %q\n", filename)
data, err := cd.readFile(filename)
if os.IsNotExist(err) {
err = nil
}
if err != nil {
log.Errorf("ERROR read cloud-config file(%s) - err: %q", filename, err)
} else {
log.Debugf("SUCCESS read cloud-config file(%s) - date: %s", filename, string(data))
}
return data, err
}

View File

@@ -57,7 +57,7 @@ func TestFetchMetadata(t *testing.T) {
},
},
} {
cd := ConfigDrive{tt.root, tt.files.ReadFile, nil}
cd := ConfigDrive{tt.root, tt.files.ReadFile, nil, true}
metadata, err := cd.FetchMetadata()
if err != nil {
t.Fatalf("bad error for %+v: want %v, got %q", tt, nil, err)
@@ -91,7 +91,7 @@ func TestFetchUserdata(t *testing.T) {
"userdata",
},
} {
cd := ConfigDrive{tt.root, tt.files.ReadFile, nil}
cd := ConfigDrive{tt.root, tt.files.ReadFile, nil, true}
userdata, err := cd.FetchUserdata()
if err != nil {
t.Fatalf("bad error for %+v: want %v, got %q", tt, nil, err)
@@ -116,7 +116,7 @@ func TestConfigRoot(t *testing.T) {
"/media/configdrive/openstack",
},
} {
cd := ConfigDrive{tt.root, nil, nil}
cd := ConfigDrive{tt.root, nil, nil, true}
if configRoot := cd.ConfigRoot(); configRoot != tt.configRoot {
t.Fatalf("bad config root for %q: want %q, got %q", tt, tt.configRoot, configRoot)
}

View File

@@ -46,7 +46,9 @@ func (f *RemoteFile) String() string {
func (f *RemoteFile) AvailabilityChanges() bool {
if f.lastError != nil {
if _, ok := f.lastError.(pkg.ErrNotFound); ok {
// if we have a Network error, then we should retry.
// otherwise, we've made a request to the server, and its said nope.
if _, ok := f.lastError.(pkg.ErrNetwork); !ok {
return false
}
}