Skip to content

Commit

Permalink
Fix LP: 1930761.
Browse files Browse the repository at this point in the history
Templates created before juju 2.9 do not have arch tags.  Prefer to
match the arch, but do not fail if a template exists without an arch
tag.
  • Loading branch information
hmlanigan committed Jun 15, 2021
1 parent 6affbae commit b84c376
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 8 deletions.
16 changes: 9 additions & 7 deletions provider/vsphere/vm_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ func (v *vmTemplateManager) getVMArch(ctx context.Context, vmObj *object.Virtual
}

func (v *vmTemplateManager) getImportedTemplate(ctx context.Context, series string, arches []string) (*object.VirtualMachine, string, error) {
logger.Tracef("getImportedTemplate for series %q, arches %q", strings.Join(arches, ", "))
seriesTemplatesFolder := v.seriesTemplateFolder(series)
seriesTemplates, err := v.client.ListVMTemplates(ctx, path.Join(seriesTemplatesFolder, "*"))
if err != nil {
Expand All @@ -138,7 +139,7 @@ func (v *vmTemplateManager) getImportedTemplate(ctx context.Context, series stri
}
logger.Tracef("Series templates: %v", seriesTemplates)
if len(seriesTemplates) == 0 {
return nil, "", errors.NotFoundf("valid template")
return nil, "", errors.NotFoundf("%s templates", series)
}
var vmTpl *object.VirtualMachine
var arch string
Expand All @@ -147,20 +148,21 @@ func (v *vmTemplateManager) getImportedTemplate(ctx context.Context, series stri
arch, err = v.getVMArch(ctx, item)
if err != nil {
if errors.IsNotFound(err) {
continue
logger.Debugf("failed find arch for template %q: %s", item.InventoryPath, err)
} else {
logger.Infof("failed to get arch for template %q: %s", item.InventoryPath, err)
}
continue
}
if !set.NewStrings(arches...).Contains(arch) {
continue
}

vmTpl = item
break
}
if vmTpl == nil {
return nil, "", errors.NotFoundf("valid template")
}
} else {
}
if vmTpl == nil {
// Templates created by juju before 2.9, do not have an arch tag.
vmTpl = seriesTemplates[0]
}

Expand Down
30 changes: 29 additions & 1 deletion provider/vsphere/vm_template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,18 @@ func (v *vmTemplateSuite) addMockLocalTemplateToClient() {
}

func (v *vmTemplateSuite) addMockDownloadedTemplateToClient() {
v.mockDownloadedTemplateToClient("amd64")
}

func (v *vmTemplateSuite) addMockDownloadedTemplateToClientNoArch() {
v.mockDownloadedTemplateToClient("")
}

func (v *vmTemplateSuite) mockDownloadedTemplateToClient(arch string) {
args := vsphereclient.ImportOVAParameters{
OVASHA256: ovatest.FakeOVASHA256(),
Series: "trusty",
Arch: "amd64",
Arch: arch,
TemplateName: "juju-template-" + ovatest.FakeOVASHA256(),
DestinationFolder: object.NewFolder(nil, types.ManagedObjectReference{
Type: "Folder",
Expand Down Expand Up @@ -233,3 +241,23 @@ func (v *vmTemplateSuite) TestEnsureTemplateImageCachedImage(c *gc.C) {
c.Assert(arch, gc.Equals, "amd64")
v.client.CheckCallNames(c, "ListVMTemplates", "VirtualMachineObjectToManagedObject")
}

func (v *vmTemplateSuite) TestEnsureTemplateImageCachedImageNoArch(c *gc.C) {
arches := []string{
"amd64",
}

v.addMockDownloadedTemplateToClientNoArch()
resPool := v.client.resourcePools["/DC/host/z1/..."][0]
tplMgr := vsphere.NewVMTemplateManager(
nil, v.env, v.client, resPool.Reference(),
v.datastore, v.statusUpdateParams, "",
coretesting.FakeControllerConfig().ControllerUUID(),
)

tpl, arch, err := tplMgr.EnsureTemplate(context.Background(), "trusty", arches)
c.Assert(err, jc.ErrorIsNil)
c.Assert(tpl, gc.NotNil)
c.Assert(arch, gc.Equals, "")
v.client.CheckCallNames(c, "ListVMTemplates", "VirtualMachineObjectToManagedObject")
}

0 comments on commit b84c376

Please sign in to comment.