From d65693719b154ed47ba09783b4fb6ca73b4de9b8 Mon Sep 17 00:00:00 2001 From: Abhinav Dahiya Date: Fri, 11 Jan 2019 10:42:49 -0800 Subject: [PATCH] pkg: use rhcos Image to fetch ami for AWS --- pkg/asset/cluster/tfvars.go | 8 +++++--- pkg/asset/machines/aws/machines.go | 9 +++++---- pkg/asset/machines/aws/machinesets.go | 4 ++-- pkg/asset/machines/master.go | 21 ++++++--------------- pkg/asset/machines/worker.go | 21 ++++++--------------- pkg/tfvars/tfvars.go | 18 ++++-------------- 6 files changed, 28 insertions(+), 53 deletions(-) diff --git a/pkg/asset/cluster/tfvars.go b/pkg/asset/cluster/tfvars.go index f8dd936fa8d..2e52253483e 100644 --- a/pkg/asset/cluster/tfvars.go +++ b/pkg/asset/cluster/tfvars.go @@ -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" ) @@ -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{}, } @@ -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") } diff --git a/pkg/asset/machines/aws/machines.go b/pkg/asset/machines/aws/machines.go index c117678ae69..f128c977c9a 100644 --- a/pkg/asset/machines/aws/machines.go +++ b/pkg/asset/machines/aws/machines.go @@ -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) } @@ -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") } @@ -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") @@ -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}, diff --git a/pkg/asset/machines/aws/machinesets.go b/pkg/asset/machines/aws/machinesets.go index 1c93d498674..4cff392b68c 100644 --- a/pkg/asset/machines/aws/machinesets.go +++ b/pkg/asset/machines/aws/machinesets.go @@ -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) } @@ -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") } diff --git a/pkg/asset/machines/master.go b/pkg/asset/machines/master.go index 06bf81d9602..11070f8c831 100644 --- a/pkg/asset/machines/master.go +++ b/pkg/asset/machines/master.go @@ -1,9 +1,7 @@ package machines import ( - "context" "fmt" - "time" "github.com/ghodss/yaml" "github.com/pkg/errors" @@ -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" @@ -43,6 +41,7 @@ func (m *Master) Name() string { func (m *Master) Dependencies() []asset.Asset { return []asset.Asset{ &installconfig.InstallConfig{}, + new(rhcos.Image), &machine.Master{}, } } @@ -50,8 +49,9 @@ func (m *Master) Dependencies() []asset.Asset { // 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} @@ -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 { @@ -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") } @@ -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), diff --git a/pkg/asset/machines/worker.go b/pkg/asset/machines/worker.go index a6a9845cc3c..be8a95b472e 100644 --- a/pkg/asset/machines/worker.go +++ b/pkg/asset/machines/worker.go @@ -2,10 +2,8 @@ package machines import ( "bytes" - "context" "fmt" "text/template" - "time" "github.com/ghodss/yaml" "github.com/pkg/errors" @@ -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" @@ -70,6 +68,7 @@ func (w *Worker) Name() string { func (w *Worker) Dependencies() []asset.Asset { return []asset.Asset{ &installconfig.InstallConfig{}, + new(rhcos.Image), &machine.Worker{}, } } @@ -77,8 +76,9 @@ func (w *Worker) Dependencies() []asset.Asset { // 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} @@ -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 { @@ -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") } @@ -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), diff --git a/pkg/tfvars/tfvars.go b/pkg/tfvars/tfvars.go index f20638f8ac0..5e1aab57587 100644 --- a/pkg/tfvars/tfvars.go +++ b/pkg/tfvars/tfvars.go @@ -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" @@ -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, @@ -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)) @@ -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 { @@ -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