Skip to content

Commit

Permalink
Specify the network when deploying the VM from content library
Browse files Browse the repository at this point in the history
  • Loading branch information
abhinavmpandey08 committed Jun 14, 2022
1 parent 7ef2ca8 commit be81fa7
Show file tree
Hide file tree
Showing 10 changed files with 40 additions and 27 deletions.
8 changes: 7 additions & 1 deletion pkg/executables/config/deploy-opts.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
{
"DiskProvisioning": "thin"
"DiskProvisioning": "thin",
"NetworkMapping": [
{
"Name": "nic0",
"Network": "%s"
}
]
}
10 changes: 5 additions & 5 deletions pkg/executables/govc.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ const (
var requiredEnvs = []string{govcUsernameKey, govcPasswordKey, govcURLKey, govcInsecure}

//go:embed config/deploy-opts.json
var deployOpts []byte
var deployOpts string

type FolderType string

Expand Down Expand Up @@ -269,9 +269,9 @@ func (g *Govc) CreateLibrary(ctx context.Context, datastore, library string) err
return nil
}

func (g *Govc) DeployTemplateFromLibrary(ctx context.Context, templateDir, templateName, library, datacenter, datastore, resourcePool string, resizeBRDisk bool) error {
func (g *Govc) DeployTemplateFromLibrary(ctx context.Context, templateDir, templateName, library, datacenter, datastore, network, resourcePool string, resizeBRDisk bool) error {
logger.V(4).Info("Deploying template", "dir", templateDir, "templateName", templateName)
if err := g.deployTemplate(ctx, library, templateName, templateDir, datacenter, datastore, resourcePool); err != nil {
if err := g.deployTemplate(ctx, library, templateName, templateDir, datacenter, datastore, network, resourcePool); err != nil {
return err
}

Expand Down Expand Up @@ -343,7 +343,7 @@ func (g *Govc) ImportTemplate(ctx context.Context, library, ovaURL, name string)
return nil
}

func (g *Govc) deployTemplate(ctx context.Context, library, templateName, deployFolder, datacenter, datastore, resourcePool string) error {
func (g *Govc) deployTemplate(ctx context.Context, library, templateName, deployFolder, datacenter, datastore, network, resourcePool string) error {
envMap, err := g.validateAndSetupCreds()
if err != nil {
return fmt.Errorf("failed govc validations: %v", err)
Expand All @@ -354,7 +354,7 @@ func (g *Govc) deployTemplate(ctx context.Context, library, templateName, deploy
templateInLibraryPath = fmt.Sprintf("/%s", templateInLibraryPath)
}

deployOptsPath, err := g.writer.Write(deployOptsFile, deployOpts, filewriter.PersistentFile)
deployOptsPath, err := g.writer.Write(deployOptsFile, []byte(fmt.Sprintf(deployOpts, network)), filewriter.PersistentFile)
if err != nil {
return fmt.Errorf("failed writing deploy options file to disk: %v", err)
}
Expand Down
4 changes: 3 additions & 1 deletion pkg/executables/govc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ type deployTemplateTest struct {
env map[string]string
datacenter string
datastore string
network string
resourcePool string
templatePath string
ovaURL string
Expand All @@ -124,6 +125,7 @@ func newDeployTemplateTest(t *testing.T) *deployTemplateTest {
env: env,
datacenter: "SDDC-Datacenter",
datastore: "/SDDC-Datacenter/datastore/WorkloadDatastore",
network: "/SDDC-Datacenter/network/sddc-cgw-network-1",
resourcePool: "*/Resources/Compute-ResourcePool",
templatePath: "/SDDC-Datacenter/vm/Templates/ubuntu-2004-kube-v1.19.6",
ovaURL: "https://aws.com/ova",
Expand Down Expand Up @@ -181,7 +183,7 @@ func (dt *deployTemplateTest) expectMarkAsTemplateToReturn(err error) {

func (dt *deployTemplateTest) DeployTemplateFromLibrary() error {
gomock.InOrder(dt.expectations...)
return dt.govc.DeployTemplateFromLibrary(dt.ctx, dt.deployFolder, dt.templateName, templateLibrary, dt.datacenter, dt.datastore, dt.resourcePool, dt.resizeDisk2)
return dt.govc.DeployTemplateFromLibrary(dt.ctx, dt.deployFolder, dt.templateName, templateLibrary, dt.datacenter, dt.datastore, dt.network, dt.resourcePool, dt.resizeDisk2)
}

func (dt *deployTemplateTest) assertDeployTemplateSuccess(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/providers/vsphere/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ func (d *Defaulter) setupDefaultTemplate(ctx context.Context, spec *Spec, machin
tags := requiredTemplateTagsByCategory(spec.Spec, machineConfig)

// TODO: figure out if it's worth refactoring the factory to be able to reuse across machine configs.
templateFactory := templates.NewFactory(d.govc, spec.datacenterConfig.Spec.Datacenter, machineConfig.Spec.Datastore, machineConfig.Spec.ResourcePool, defaultTemplateLibrary)
templateFactory := templates.NewFactory(d.govc, spec.datacenterConfig.Spec.Datacenter, machineConfig.Spec.Datastore, spec.datacenterConfig.Spec.Network, machineConfig.Spec.ResourcePool, defaultTemplateLibrary)

// TODO: remove the factory's dependency on a machineConfig
if err := templateFactory.CreateIfMissing(ctx, spec.datacenterConfig.Spec.Datacenter, machineConfig, ova.URI, tags); err != nil {
Expand Down
8 changes: 5 additions & 3 deletions pkg/providers/vsphere/internal/templates/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,15 @@ type Factory struct {
client GovcClient
datacenter string
datastore string
network string
resourcePool string
templateLibrary string
tagsFactory *tags.Factory
}

type GovcClient interface {
CreateLibrary(ctx context.Context, datastore, library string) error
DeployTemplateFromLibrary(ctx context.Context, templateDir, templateName, library, datacenter, datastore, resourcePool string, resizeBRDisk bool) error
DeployTemplateFromLibrary(ctx context.Context, templateDir, templateName, library, datacenter, datastore, network, resourcePool string, resizeBRDisk bool) error
SearchTemplate(ctx context.Context, datacenter string, machineConfig *v1alpha1.VSphereMachineConfig) (string, error)
ImportTemplate(ctx context.Context, library, ovaURL, name string) error
LibraryElementExists(ctx context.Context, library string) (bool, error)
Expand All @@ -40,11 +41,12 @@ type GovcClient interface {
CreateCategoryForVM(ctx context.Context, name string) error
}

func NewFactory(client GovcClient, datacenter, datastore, resourcePool, templateLibrary string) *Factory {
func NewFactory(client GovcClient, datacenter, datastore, network, resourcePool, templateLibrary string) *Factory {
return &Factory{
client: client,
datacenter: datacenter,
datastore: datastore,
network: network,
resourcePool: resourcePool,
templateLibrary: templateLibrary,
tagsFactory: tags.NewFactory(client),
Expand Down Expand Up @@ -92,7 +94,7 @@ func (f *Factory) createTemplate(ctx context.Context, templatePath, ovaURL, osFa
if strings.EqualFold(osFamily, string(v1alpha1.Bottlerocket)) {
resizeBRDisk = true
}
if err := f.client.DeployTemplateFromLibrary(ctx, templateDir, templateName, f.templateLibrary, f.datacenter, f.datastore, f.resourcePool, resizeBRDisk); err != nil {
if err := f.client.DeployTemplateFromLibrary(ctx, templateDir, templateName, f.templateLibrary, f.datacenter, f.datastore, f.network, f.resourcePool, resizeBRDisk); err != nil {
return fmt.Errorf("failed deploying template: %v", err)
}

Expand Down
15 changes: 9 additions & 6 deletions pkg/providers/vsphere/internal/templates/factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type test struct {
t *testing.T
datacenter string
datastore string
network string
resourcePool string
templateLibrary string
resizeDisk2 bool
Expand Down Expand Up @@ -47,6 +48,7 @@ func newTest(t *testing.T) *test {
t: t,
datacenter: "SDDC-Datacenter",
datastore: "datastore",
network: "sddc-cgw-network-1",
resourcePool: "*/pool/",
templateLibrary: "library",
resizeDisk2: false,
Expand All @@ -61,6 +63,7 @@ func newTest(t *testing.T) *test {
test.govc,
test.datacenter,
test.datastore,
test.network,
test.resourcePool,
test.templateLibrary,
)
Expand Down Expand Up @@ -173,7 +176,7 @@ func TestFactoryCreateIfMissingErrorDeploy(t *testing.T) {
ct.govc.EXPECT().GetLibraryElementContentVersion(ct.ctx, ct.templateInLibrary).Return(ct.libraryContentDoesNotExist, nil)
ct.govc.EXPECT().ImportTemplate(ct.ctx, ct.templateLibrary, ct.ovaURL, ct.templateName).Return(nil)
ct.govc.EXPECT().DeployTemplateFromLibrary(
ct.ctx, ct.templateDir, ct.templateName, ct.templateLibrary, ct.datacenter, ct.datastore, ct.resourcePool, ct.resizeDisk2,
ct.ctx, ct.templateDir, ct.templateName, ct.templateLibrary, ct.datacenter, ct.datastore, ct.network, ct.resourcePool, ct.resizeDisk2,
).Return(ct.dummyError)

ct.assertErrorFromCreateIfMissing()
Expand All @@ -187,7 +190,7 @@ func TestFactoryCreateIfMissingErrorFromTagFactory(t *testing.T) {
ct.govc.EXPECT().GetLibraryElementContentVersion(ct.ctx, ct.templateInLibrary).Return(ct.libraryContentDoesNotExist, nil)
ct.govc.EXPECT().ImportTemplate(ct.ctx, ct.templateLibrary, ct.ovaURL, ct.templateName).Return(nil)
ct.govc.EXPECT().DeployTemplateFromLibrary(
ct.ctx, ct.templateDir, ct.templateName, ct.templateLibrary, ct.datacenter, ct.datastore, ct.resourcePool, ct.resizeDisk2,
ct.ctx, ct.templateDir, ct.templateName, ct.templateLibrary, ct.datacenter, ct.datastore, ct.network, ct.resourcePool, ct.resizeDisk2,
).Return(nil)

// expects for tagging
Expand All @@ -204,7 +207,7 @@ func TestFactoryCreateIfMissingSuccessLibraryDoesNotExist(t *testing.T) {
ct.govc.EXPECT().GetLibraryElementContentVersion(ct.ctx, ct.templateInLibrary).Return(ct.libraryContentDoesNotExist, nil)
ct.govc.EXPECT().ImportTemplate(ct.ctx, ct.templateLibrary, ct.ovaURL, ct.templateName).Return(nil)
ct.govc.EXPECT().DeployTemplateFromLibrary(
ct.ctx, ct.templateDir, ct.templateName, ct.templateLibrary, ct.datacenter, ct.datastore, ct.resourcePool, ct.resizeDisk2,
ct.ctx, ct.templateDir, ct.templateName, ct.templateLibrary, ct.datacenter, ct.datastore, ct.network, ct.resourcePool, ct.resizeDisk2,
).Return(nil)

// expects for tagging
Expand All @@ -221,7 +224,7 @@ func TestFactoryCreateIfMissingSuccessLibraryExists(t *testing.T) {
ct.govc.EXPECT().GetLibraryElementContentVersion(ct.ctx, ct.templateInLibrary).Return(ct.libraryContentDoesNotExist, nil)
ct.govc.EXPECT().ImportTemplate(ct.ctx, ct.templateLibrary, ct.ovaURL, ct.templateName).Return(nil)
ct.govc.EXPECT().DeployTemplateFromLibrary(
ct.ctx, ct.templateDir, ct.templateName, ct.templateLibrary, ct.datacenter, ct.datastore, ct.resourcePool, ct.resizeDisk2,
ct.ctx, ct.templateDir, ct.templateName, ct.templateLibrary, ct.datacenter, ct.datastore, ct.network, ct.resourcePool, ct.resizeDisk2,
).Return(nil)

// expects for tagging
Expand All @@ -237,7 +240,7 @@ func TestFactoryCreateIfMissingSuccessTemplateInLibraryExists(t *testing.T) {
ct.govc.EXPECT().LibraryElementExists(ct.ctx, ct.templateLibrary).Return(true, nil)
ct.govc.EXPECT().GetLibraryElementContentVersion(ct.ctx, ct.templateInLibrary).Return(ct.libraryContentValid, nil)
ct.govc.EXPECT().DeployTemplateFromLibrary(
ct.ctx, ct.templateDir, ct.templateName, ct.templateLibrary, ct.datacenter, ct.datastore, ct.resourcePool, ct.resizeDisk2,
ct.ctx, ct.templateDir, ct.templateName, ct.templateLibrary, ct.datacenter, ct.datastore, ct.network, ct.resourcePool, ct.resizeDisk2,
).Return(nil)

// expects for tagging
Expand All @@ -255,7 +258,7 @@ func TestFactoryCreateIfMissingSuccessTemplateInLibraryCorrupted(t *testing.T) {
ct.govc.EXPECT().DeleteLibraryElement(ct.ctx, ct.templateInLibrary).Return(nil)
ct.govc.EXPECT().ImportTemplate(ct.ctx, ct.templateLibrary, ct.ovaURL, ct.templateName)
ct.govc.EXPECT().DeployTemplateFromLibrary(
ct.ctx, ct.templateDir, ct.templateName, ct.templateLibrary, ct.datacenter, ct.datastore, ct.resourcePool, ct.resizeDisk2,
ct.ctx, ct.templateDir, ct.templateName, ct.templateLibrary, ct.datacenter, ct.datastore, ct.network, ct.resourcePool, ct.resizeDisk2,
).Return(nil)

// expects for tagging
Expand Down
8 changes: 4 additions & 4 deletions pkg/providers/vsphere/internal/templates/mocks/govc.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions pkg/providers/vsphere/mocks/client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pkg/providers/vsphere/vsphere.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ type ProviderGovcClient interface {
DatacenterExists(ctx context.Context, datacenter string) (bool, error)
NetworkExists(ctx context.Context, network string) (bool, error)
CreateLibrary(ctx context.Context, datastore, library string) error
DeployTemplateFromLibrary(ctx context.Context, templateDir, templateName, library, datacenter, datastore, resourcePool string, resizeDisk2 bool) error
DeployTemplateFromLibrary(ctx context.Context, templateDir, templateName, library, datacenter, datastore, network, resourcePool string, resizeDisk2 bool) error
ImportTemplate(ctx context.Context, library, ovaURL, name string) error
GetTags(ctx context.Context, path string) (tags []string, err error)
ListTags(ctx context.Context) ([]string, error)
Expand Down
2 changes: 1 addition & 1 deletion pkg/providers/vsphere/vsphere_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ func (pc *DummyProviderGovcClient) CreateLibrary(ctx context.Context, datastore,
return nil
}

func (pc *DummyProviderGovcClient) DeployTemplateFromLibrary(ctx context.Context, templateDir, templateName, library, datacenter, datastore, resourcePool string, resizeDisk2 bool) error {
func (pc *DummyProviderGovcClient) DeployTemplateFromLibrary(ctx context.Context, templateDir, templateName, library, datacenter, datastore, network, resourcePool string, resizeDisk2 bool) error {
return nil
}

Expand Down

0 comments on commit be81fa7

Please sign in to comment.