Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix tool downloads for IAAS hosted model on CAAS #13393

Merged
merged 3 commits into from Oct 7, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
39 changes: 30 additions & 9 deletions apiserver/tools.go
Expand Up @@ -229,7 +229,7 @@ func (h *toolsDownloadHandler) getToolsForRequest(r *http.Request, st *state.Sta
if osTypeName != "" {
storageVers.Release = osTypeName
}
err = h.fetchAndCacheTools(vers, storageVers)
err = h.fetchAndCacheTools(vers, storageVers, st, storage)
if err != nil {
err = errors.Annotate(err, "error fetching agent binaries")
} else {
Expand All @@ -249,15 +249,42 @@ func (h *toolsDownloadHandler) getToolsForRequest(r *http.Request, st *state.Sta
func (h *toolsDownloadHandler) fetchAndCacheTools(
v version.Binary,
storageVers version.Binary,
st *state.State,
modelStorage binarystorage.Storage,
) error {
systemState := h.ctxt.statePool().SystemState()

controllerModel, err := systemState.Model()
if err != nil {
return err
}

var model *state.Model
var storage binarystorage.Storage
switch controllerModel.Type() {
case state.ModelTypeCAAS:
// TODO(caas): unify tool fetching
// Cache the tools against the model when the controller is CAAS.
model, err = st.Model()
if err != nil {
return err
}
storage = modelStorage
case state.ModelTypeIAAS:
// Cache the tools against the controller when the controller is IAAS.
model = controllerModel
controllerStorage, err := systemState.ToolsStorage()
if err != nil {
return err
}
defer controllerStorage.Close()
storage = controllerStorage
default:
return errors.NotValidf("model type %q", controllerModel.Type())
}

newEnviron := stateenvirons.GetNewEnvironFunc(environs.New)
env, err := newEnviron(controllerModel)
env, err := newEnviron(model)
if err != nil {
return err
}
Expand All @@ -284,12 +311,6 @@ func (h *toolsDownloadHandler) fetchAndCacheTools(
return errors.New(msg)
}

controllerStorage, err := systemState.ToolsStorage()
if err != nil {
return err
}
defer controllerStorage.Close()

data, respSha256, size, err := tmpCacheAndHash(resp.Body)
if err != nil {
return err
Expand All @@ -307,7 +328,7 @@ func (h *toolsDownloadHandler) fetchAndCacheTools(
Size: exactTools.Size,
SHA256: exactTools.SHA256,
}
if err := controllerStorage.Add(data, md); err != nil {
if err := storage.Add(data, md); err != nil {
return errors.Annotate(err, "error caching agent binaries")
}

Expand Down
1 change: 1 addition & 0 deletions provider/lxd/credentials.go
Expand Up @@ -677,6 +677,7 @@ func configDirs() []string {
dirs = append(dirs, filepath.Join(utils.Home(), ".config", "lxc"))
if runtime.GOOS == "linux" {
dirs = append(dirs, filepath.Join(utils.Home(), "snap", "lxd", "current", ".config", "lxc"))
dirs = append(dirs, filepath.Join(utils.Home(), "snap", "lxd", "common", "config"))
}
return dirs
}
14 changes: 11 additions & 3 deletions provider/lxd/credentials_test.go
Expand Up @@ -122,6 +122,7 @@ func (s *credentialsSuite) TestDetectCredentialsGeneratesCertFailsToWriteOnError
deps.certReadWriter.EXPECT().Read(path).Return(nil, nil, os.ErrNotExist)
deps.certReadWriter.EXPECT().Read(filepath.Join(utils.Home(), ".config", "lxc")).Return(nil, nil, os.ErrNotExist)
deps.certReadWriter.EXPECT().Read("snap/lxd/current/.config/lxc").Return(nil, nil, os.ErrNotExist)
deps.certReadWriter.EXPECT().Read("snap/lxd/common/config").Return(nil, nil, os.ErrNotExist)
deps.certGenerator.EXPECT().Generate(true, true).Return(nil, nil, errors.Errorf("bad"))

_, err := deps.provider.DetectCredentials("")
Expand All @@ -138,6 +139,7 @@ func (s *credentialsSuite) TestDetectCredentialsGeneratesCertFailsToGetCertifica
deps.certReadWriter.EXPECT().Read(path).Return(nil, nil, os.ErrNotExist)
deps.certReadWriter.EXPECT().Read(filepath.Join(utils.Home(), ".config", "lxc")).Return(nil, nil, os.ErrNotExist)
deps.certReadWriter.EXPECT().Read("snap/lxd/current/.config/lxc").Return(nil, nil, os.ErrNotExist)
deps.certReadWriter.EXPECT().Read("snap/lxd/common/config").Return(nil, nil, os.ErrNotExist)
deps.certGenerator.EXPECT().Generate(true, true).Return([]byte(coretesting.CACert), []byte(coretesting.CAKey), nil)
deps.certReadWriter.EXPECT().Write(path, []byte(coretesting.CACert), []byte(coretesting.CAKey)).Return(errors.Errorf("bad"))

Expand Down Expand Up @@ -171,6 +173,7 @@ func (s *credentialsSuite) TestRemoteDetectCredentials(c *gc.C) {
},
},
}, nil)
deps.configReader.EXPECT().ReadConfig(path.Join(utils.Home(), "snap/lxd/common/config/config.yml")).Return(lxd.LXCConfig{}, nil)
deps.certReadWriter.EXPECT().Read("snap/lxd/current/.config/lxc").Return([]byte(coretesting.CACert), []byte(coretesting.CAKey), nil)
deps.configReader.EXPECT().ReadCert("snap/lxd/current/.config/lxc/servercerts/nuc1.crt").Return([]byte(coretesting.ServerCert), nil)
deps.server.EXPECT().GetCertificate(s.clientCertFingerprint(c)).Return(nil, "", nil)
Expand Down Expand Up @@ -216,7 +219,8 @@ func (s *credentialsSuite) TestRemoteDetectCredentialsNoRemoteCert(c *gc.C) {

deps.configReader.EXPECT().ReadConfig(path.Join(osenv.JujuXDGDataHomePath("lxd"), "config.yml")).Return(lxd.LXCConfig{}, nil)
deps.configReader.EXPECT().ReadConfig(path.Join(utils.Home(), ".config/lxc/config.yml")).Return(lxd.LXCConfig{}, nil)
deps.configReader.EXPECT().ReadConfig(path.Join(utils.Home(), "snap/lxd/current/.config/lxc/config.yml")).Return(lxd.LXCConfig{
deps.configReader.EXPECT().ReadConfig(path.Join(utils.Home(), "snap/lxd/current/.config/lxc/config.yml")).Return(lxd.LXCConfig{}, nil)
deps.configReader.EXPECT().ReadConfig(path.Join(utils.Home(), "snap/lxd/common/config/config.yml")).Return(lxd.LXCConfig{
DefaultRemote: "localhost",
Remotes: map[string]lxd.LXCRemoteConfig{
"nuc1": {
Expand All @@ -227,8 +231,8 @@ func (s *credentialsSuite) TestRemoteDetectCredentialsNoRemoteCert(c *gc.C) {
},
},
}, nil)
deps.certReadWriter.EXPECT().Read("snap/lxd/current/.config/lxc").Return([]byte(coretesting.CACert), []byte(coretesting.CAKey), nil)
deps.configReader.EXPECT().ReadCert("snap/lxd/current/.config/lxc/servercerts/nuc1.crt").Return(nil, os.ErrNotExist)
deps.certReadWriter.EXPECT().Read("snap/lxd/common/config").Return([]byte(coretesting.CACert), []byte(coretesting.CAKey), nil)
deps.configReader.EXPECT().ReadCert("snap/lxd/common/config/servercerts/nuc1.crt").Return(nil, os.ErrNotExist)
deps.server.EXPECT().GetCertificate(s.clientCertFingerprint(c)).Return(nil, "", nil)
deps.server.EXPECT().ServerCertificate().Return(coretesting.ServerCert)

Expand Down Expand Up @@ -273,6 +277,7 @@ func (s *credentialsSuite) TestRemoteDetectCredentialsWithConfigFailure(c *gc.C)
deps.configReader.EXPECT().ReadConfig(path.Join(osenv.JujuXDGDataHomePath("lxd"), "config.yml")).Return(lxd.LXCConfig{}, nil)
deps.configReader.EXPECT().ReadConfig(path.Join(utils.Home(), ".config/lxc/config.yml")).Return(lxd.LXCConfig{}, nil)
deps.configReader.EXPECT().ReadConfig(path.Join(utils.Home(), "snap/lxd/current/.config/lxc/config.yml")).Return(lxd.LXCConfig{}, nil)
deps.configReader.EXPECT().ReadConfig(path.Join(utils.Home(), "snap/lxd/common/config/config.yml")).Return(lxd.LXCConfig{}, nil)

deps.server.EXPECT().GetCertificate(s.clientCertFingerprint(c)).Return(nil, "", errors.New("bad"))

Expand Down Expand Up @@ -303,6 +308,7 @@ func (s *credentialsSuite) TestRemoteDetectCredentialsWithCertFailure(c *gc.C) {
},
},
}, nil)
deps.configReader.EXPECT().ReadConfig(path.Join(utils.Home(), "snap/lxd/common/config/config.yml")).Return(lxd.LXCConfig{}, nil)
deps.certReadWriter.EXPECT().Read(path.Join(utils.Home(), "snap/lxd/current/.config/lxc")).Return([]byte(coretesting.CACert), []byte(coretesting.CAKey), nil)
deps.configReader.EXPECT().ReadCert("snap/lxd/current/.config/lxc/servercerts/nuc1.crt").Return(nil, errors.New("bad"))
deps.server.EXPECT().GetCertificate(s.clientCertFingerprint(c)).Return(nil, "", errors.New("bad"))
Expand All @@ -324,6 +330,7 @@ func (s *credentialsSuite) TestRegisterCredentials(c *gc.C) {
deps.certReadWriter.EXPECT().Read(path).Return(nil, nil, os.ErrNotExist)
deps.certReadWriter.EXPECT().Read(filepath.Join(utils.Home(), ".config", "lxc")).Return(nil, nil, os.ErrNotExist)
deps.certReadWriter.EXPECT().Read("snap/lxd/current/.config/lxc").Return(nil, nil, os.ErrNotExist)
deps.certReadWriter.EXPECT().Read("snap/lxd/common/config").Return(nil, nil, os.ErrNotExist)
deps.certGenerator.EXPECT().Generate(true, true).Return([]byte(coretesting.CACert), []byte(coretesting.CAKey), nil)
deps.certReadWriter.EXPECT().Write(path, []byte(coretesting.CACert), []byte(coretesting.CAKey)).Return(nil)

Expand Down Expand Up @@ -365,6 +372,7 @@ func (s *credentialsSuite) TestRegisterCredentialsWithAlternativeCloudName(c *gc
deps.certReadWriter.EXPECT().Read(path).Return(nil, nil, os.ErrNotExist)
deps.certReadWriter.EXPECT().Read(filepath.Join(utils.Home(), ".config", "lxc")).Return(nil, nil, os.ErrNotExist)
deps.certReadWriter.EXPECT().Read("snap/lxd/current/.config/lxc").Return(nil, nil, os.ErrNotExist)
deps.certReadWriter.EXPECT().Read("snap/lxd/common/config").Return(nil, nil, os.ErrNotExist)
deps.certGenerator.EXPECT().Generate(true, true).Return([]byte(coretesting.CACert), []byte(coretesting.CAKey), nil)
deps.certReadWriter.EXPECT().Write(path, []byte(coretesting.CACert), []byte(coretesting.CAKey)).Return(nil)
deps.server.EXPECT().GetCertificate(s.clientCertFingerprint(c)).Return(nil, "", nil)
Expand Down