Skip to content

Commit

Permalink
pkg: use rhcos Image to fetch ami for AWS
Browse files Browse the repository at this point in the history
  • Loading branch information
abhinavdahiya committed Jan 11, 2019
1 parent a454909 commit d656937
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 53 deletions.
8 changes: 5 additions & 3 deletions pkg/asset/cluster/tfvars.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/openshift/installer/pkg/asset/ignition/bootstrap"
"github.com/openshift/installer/pkg/asset/ignition/machine"
"github.com/openshift/installer/pkg/asset/installconfig"
"github.com/openshift/installer/pkg/asset/rhcos"
"github.com/openshift/installer/pkg/tfvars"
"github.com/pkg/errors"
)
Expand Down Expand Up @@ -34,6 +35,7 @@ func (t *TerraformVariables) Name() string {
func (t *TerraformVariables) Dependencies() []asset.Asset {
return []asset.Asset{
&installconfig.InstallConfig{},
new(rhcos.Image),
&bootstrap.Bootstrap{},
&machine.Master{},
}
Expand All @@ -44,13 +46,13 @@ func (t *TerraformVariables) Generate(parents asset.Parents) error {
installConfig := &installconfig.InstallConfig{}
bootstrap := &bootstrap.Bootstrap{}
master := &machine.Master{}
parents.Get(installConfig, bootstrap, master)
rhcosImage := new(rhcos.Image)
parents.Get(installConfig, bootstrap, master, rhcosImage)

bootstrapIgn := string(bootstrap.Files()[0].Data)

masterIgn := string(master.Files()[0].Data)

data, err := tfvars.TFVars(installConfig.Config, bootstrapIgn, masterIgn)
data, err := tfvars.TFVars(installConfig.Config, string(*rhcosImage), bootstrapIgn, masterIgn)
if err != nil {
return errors.Wrap(err, "failed to get Tfvars")
}
Expand Down
9 changes: 5 additions & 4 deletions pkg/asset/machines/aws/machines.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
)

// Machines returns a list of machines for a machinepool.
func Machines(config *types.InstallConfig, pool *types.MachinePool, role, userDataSecret string) ([]clusterapi.Machine, error) {
func Machines(config *types.InstallConfig, pool *types.MachinePool, osImage, role, userDataSecret string) ([]clusterapi.Machine, error) {
if configPlatform := config.Platform.Name(); configPlatform != aws.Name {
return nil, fmt.Errorf("non-AWS configuration: %q", configPlatform)
}
Expand All @@ -37,7 +37,7 @@ func Machines(config *types.InstallConfig, pool *types.MachinePool, role, userDa
var machines []clusterapi.Machine
for idx := int64(0); idx < total; idx++ {
azIndex := int(idx) % len(azs)
provider, err := provider(config.ClusterID, clustername, platform, mpool, azIndex, role, userDataSecret)
provider, err := provider(config.ClusterID, clustername, platform, mpool, osImage, azIndex, role, userDataSecret)
if err != nil {
return nil, errors.Wrap(err, "failed to create provider")
}
Expand Down Expand Up @@ -69,8 +69,9 @@ func Machines(config *types.InstallConfig, pool *types.MachinePool, role, userDa
return machines, nil
}

func provider(clusterID, clusterName string, platform *aws.Platform, mpool *aws.MachinePool, azIdx int, role, userDataSecret string) (*awsprovider.AWSMachineProviderConfig, error) {
func provider(clusterID, clusterName string, platform *aws.Platform, mpool *aws.MachinePool, osImage string, azIdx int, role, userDataSecret string) (*awsprovider.AWSMachineProviderConfig, error) {
az := mpool.Zones[azIdx]
amiID := osImage
tags, err := tagsFromUserTags(clusterID, clusterName, platform.UserTags)
if err != nil {
return nil, errors.Wrap(err, "failed to create awsprovider.TagSpecifications from UserTags")
Expand All @@ -81,7 +82,7 @@ func provider(clusterID, clusterName string, platform *aws.Platform, mpool *aws.
Kind: "AWSMachineProviderConfig",
},
InstanceType: mpool.InstanceType,
AMI: awsprovider.AWSResourceReference{ID: &mpool.AMIID},
AMI: awsprovider.AWSResourceReference{ID: &amiID},
Tags: tags,
IAMInstanceProfile: &awsprovider.AWSResourceReference{ID: pointer.StringPtr(fmt.Sprintf("%s-%s-profile", clusterName, role))},
UserDataSecret: &corev1.LocalObjectReference{Name: userDataSecret},
Expand Down
4 changes: 2 additions & 2 deletions pkg/asset/machines/aws/machinesets.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
)

// MachineSets returns a list of machinesets for a machinepool.
func MachineSets(config *types.InstallConfig, pool *types.MachinePool, role, userDataSecret string) ([]clusterapi.MachineSet, error) {
func MachineSets(config *types.InstallConfig, pool *types.MachinePool, osImage, role, userDataSecret string) ([]clusterapi.MachineSet, error) {
if configPlatform := config.Platform.Name(); configPlatform != aws.Name {
return nil, fmt.Errorf("non-AWS configuration: %q", configPlatform)
}
Expand All @@ -38,7 +38,7 @@ func MachineSets(config *types.InstallConfig, pool *types.MachinePool, role, use
replicas++
}

provider, err := provider(config.ClusterID, clustername, platform, mpool, idx, role, userDataSecret)
provider, err := provider(config.ClusterID, clustername, platform, mpool, osImage, idx, role, userDataSecret)
if err != nil {
return nil, errors.Wrap(err, "failed to create provider")
}
Expand Down
21 changes: 6 additions & 15 deletions pkg/asset/machines/master.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package machines

import (
"context"
"fmt"
"time"

"github.com/ghodss/yaml"
"github.com/pkg/errors"
Expand All @@ -17,7 +15,7 @@ import (
"github.com/openshift/installer/pkg/asset/machines/aws"
"github.com/openshift/installer/pkg/asset/machines/libvirt"
"github.com/openshift/installer/pkg/asset/machines/openstack"
"github.com/openshift/installer/pkg/rhcos"
"github.com/openshift/installer/pkg/asset/rhcos"
"github.com/openshift/installer/pkg/types"
awstypes "github.com/openshift/installer/pkg/types/aws"
libvirttypes "github.com/openshift/installer/pkg/types/libvirt"
Expand All @@ -43,15 +41,17 @@ func (m *Master) Name() string {
func (m *Master) Dependencies() []asset.Asset {
return []asset.Asset{
&installconfig.InstallConfig{},
new(rhcos.Image),
&machine.Master{},
}
}

// Generate generates the Master asset.
func (m *Master) Generate(dependencies asset.Parents) error {
installconfig := &installconfig.InstallConfig{}
rhcosImage := new(rhcos.Image)
mign := &machine.Master{}
dependencies.Get(installconfig, mign)
dependencies.Get(installconfig, rhcosImage, mign)

var err error
userDataMap := map[string][]byte{"master-user-data": mign.File.Data}
Expand All @@ -67,15 +67,6 @@ func (m *Master) Generate(dependencies asset.Parents) error {
mpool := defaultAWSMachinePoolPlatform()
mpool.Set(ic.Platform.AWS.DefaultMachinePlatform)
mpool.Set(pool.Platform.AWS)
if mpool.AMIID == "" {
ctx, cancel := context.WithTimeout(context.TODO(), 60*time.Second)
defer cancel()
ami, err := rhcos.AMI(ctx, rhcos.DefaultChannel, ic.Platform.AWS.Region)
if err != nil {
return errors.Wrap(err, "failed to determine default AMI")
}
mpool.AMIID = ami
}
if len(mpool.Zones) == 0 {
azs, err := aws.AvailabilityZones(ic.Platform.AWS.Region)
if err != nil {
Expand All @@ -84,7 +75,7 @@ func (m *Master) Generate(dependencies asset.Parents) error {
mpool.Zones = azs
}
pool.Platform.AWS = &mpool
machines, err := aws.Machines(ic, &pool, "master", "master-user-data")
machines, err := aws.Machines(ic, &pool, string(*rhcosImage), "master", "master-user-data")
if err != nil {
return errors.Wrap(err, "failed to create master machine objects")
}
Expand Down Expand Up @@ -125,7 +116,7 @@ func (m *Master) Generate(dependencies asset.Parents) error {
config := openstack.MasterConfig{
ClusterName: ic.ObjectMeta.Name,
Instances: instances,
Image: ic.Platform.OpenStack.BaseImage,
Image: string(*rhcosImage),
Region: ic.Platform.OpenStack.Region,
Machine: defaultOpenStackMachinePoolPlatform(ic.Platform.OpenStack.FlavorName),
Trunk: trunkSupportBoolean(ic.Platform.OpenStack.TrunkSupport),
Expand Down
21 changes: 6 additions & 15 deletions pkg/asset/machines/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@ package machines

import (
"bytes"
"context"
"fmt"
"text/template"
"time"

"github.com/ghodss/yaml"
"github.com/pkg/errors"
Expand All @@ -19,7 +17,7 @@ import (
"github.com/openshift/installer/pkg/asset/machines/aws"
"github.com/openshift/installer/pkg/asset/machines/libvirt"
"github.com/openshift/installer/pkg/asset/machines/openstack"
"github.com/openshift/installer/pkg/rhcos"
"github.com/openshift/installer/pkg/asset/rhcos"
"github.com/openshift/installer/pkg/types"
awstypes "github.com/openshift/installer/pkg/types/aws"
libvirttypes "github.com/openshift/installer/pkg/types/libvirt"
Expand Down Expand Up @@ -70,15 +68,17 @@ func (w *Worker) Name() string {
func (w *Worker) Dependencies() []asset.Asset {
return []asset.Asset{
&installconfig.InstallConfig{},
new(rhcos.Image),
&machine.Worker{},
}
}

// Generate generates the Worker asset.
func (w *Worker) Generate(dependencies asset.Parents) error {
installconfig := &installconfig.InstallConfig{}
rhcosImage := new(rhcos.Image)
wign := &machine.Worker{}
dependencies.Get(installconfig, wign)
dependencies.Get(installconfig, rhcosImage, wign)

var err error
userDataMap := map[string][]byte{"worker-user-data": wign.File.Data}
Expand All @@ -94,15 +94,6 @@ func (w *Worker) Generate(dependencies asset.Parents) error {
mpool := defaultAWSMachinePoolPlatform()
mpool.Set(ic.Platform.AWS.DefaultMachinePlatform)
mpool.Set(pool.Platform.AWS)
if mpool.AMIID == "" {
ctx, cancel := context.WithTimeout(context.TODO(), 60*time.Second)
defer cancel()
ami, err := rhcos.AMI(ctx, rhcos.DefaultChannel, ic.Platform.AWS.Region)
if err != nil {
return errors.Wrap(err, "failed to determine default AMI")
}
mpool.AMIID = ami
}
if len(mpool.Zones) == 0 {
azs, err := aws.AvailabilityZones(ic.Platform.AWS.Region)
if err != nil {
Expand All @@ -111,7 +102,7 @@ func (w *Worker) Generate(dependencies asset.Parents) error {
mpool.Zones = azs
}
pool.Platform.AWS = &mpool
sets, err := aws.MachineSets(ic, &pool, "worker", "worker-user-data")
sets, err := aws.MachineSets(ic, &pool, string(*rhcosImage), "worker", "worker-user-data")
if err != nil {
return errors.Wrap(err, "failed to create worker machine objects")
}
Expand Down Expand Up @@ -147,7 +138,7 @@ func (w *Worker) Generate(dependencies asset.Parents) error {
config := openstack.Config{
ClusterName: ic.ObjectMeta.Name,
Replicas: numOfWorkers,
Image: ic.Platform.OpenStack.BaseImage,
Image: string(*rhcosImage),
Region: ic.Platform.OpenStack.Region,
Machine: defaultOpenStackMachinePoolPlatform(ic.Platform.OpenStack.FlavorName),
Trunk: trunkSupportBoolean(ic.Platform.OpenStack.TrunkSupport),
Expand Down
18 changes: 4 additions & 14 deletions pkg/tfvars/tfvars.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,8 @@
package tfvars

import (
"context"
"encoding/json"
"time"

"github.com/openshift/installer/pkg/rhcos"
"github.com/openshift/installer/pkg/tfvars/aws"
"github.com/openshift/installer/pkg/tfvars/libvirt"
"github.com/openshift/installer/pkg/tfvars/openstack"
Expand All @@ -31,7 +28,7 @@ type config struct {

// TFVars converts the InstallConfig and Ignition content to
// terraform.tfvar JSON.
func TFVars(cfg *types.InstallConfig, bootstrapIgn, masterIgn string) ([]byte, error) {
func TFVars(cfg *types.InstallConfig, osImage, bootstrapIgn, masterIgn string) ([]byte, error) {
config := &config{
ClusterID: cfg.ClusterID,
Name: cfg.ObjectMeta.Name,
Expand Down Expand Up @@ -76,17 +73,10 @@ func TFVars(cfg *types.InstallConfig, bootstrapIgn, masterIgn string) ([]byte, e
}

if cfg.Platform.AWS != nil {
ctx, cancel := context.WithTimeout(context.TODO(), 30*time.Second)
defer cancel()
ami, err := rhcos.AMI(ctx, rhcos.DefaultChannel, cfg.Platform.AWS.Region)
if err != nil {
return nil, errors.Wrap(err, "failed to determine default AMI")
}

config.AWS = aws.AWS{
Region: cfg.Platform.AWS.Region,
ExtraTags: cfg.Platform.AWS.UserTags,
EC2AMIOverride: ami,
EC2AMIOverride: osImage,
}
} else if cfg.Platform.Libvirt != nil {
masterIPs := make([]string, len(cfg.Platform.Libvirt.MasterIPs))
Expand All @@ -98,7 +88,7 @@ func TFVars(cfg *types.InstallConfig, bootstrapIgn, masterIgn string) ([]byte, e
Network: libvirt.Network{
IfName: cfg.Platform.Libvirt.Network.IfName,
},
Image: cfg.Platform.Libvirt.Image,
Image: osImage,
MasterIPs: masterIPs,
}
if err := config.Libvirt.TFVars(&cfg.Networking.MachineCIDR.IPNet, config.Masters); err != nil {
Expand All @@ -110,7 +100,7 @@ func TFVars(cfg *types.InstallConfig, bootstrapIgn, masterIgn string) ([]byte, e
} else if cfg.Platform.OpenStack != nil {
config.OpenStack = openstack.OpenStack{
Region: cfg.Platform.OpenStack.Region,
BaseImage: cfg.Platform.OpenStack.BaseImage,
BaseImage: osImage,
}
config.OpenStack.Credentials.Cloud = cfg.Platform.OpenStack.Cloud
config.OpenStack.ExternalNetwork = cfg.Platform.OpenStack.ExternalNetwork
Expand Down

0 comments on commit d656937

Please sign in to comment.