Skip to content

Commit

Permalink
WIP: pkg/tfvars: Pull from []Machine instead of InstallConfig
Browse files Browse the repository at this point in the history
The previous implementation pulled from the install-config, but that
missed downstream changes like AWS instance type defaults being
applied in pkg/asset/machines.  With this commit, we pull that
information from the cluster-API types, since they're the last touch
point for that data.  The remaining information still comes from the
InstallConfig, but I've split it into explicit arguments to avoid
confusion about where data is coming from when InstallConfig's machine
pools, etc. overlap with clusterapi.Machine fields.
  • Loading branch information
wking committed Dec 5, 2018
1 parent 57d9180 commit 270da62
Show file tree
Hide file tree
Showing 6 changed files with 220 additions and 122 deletions.
68 changes: 62 additions & 6 deletions pkg/asset/cluster/tfvars.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@ package cluster
import (
"os"

"github.com/ghodss/yaml"
"github.com/openshift/installer/pkg/asset"
"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/machines"
"github.com/openshift/installer/pkg/tfvars"
"github.com/pkg/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
clusterapi "sigs.k8s.io/cluster-api/pkg/apis/cluster/v1alpha1"
)

const (
Expand Down Expand Up @@ -36,24 +40,76 @@ func (t *TerraformVariables) Dependencies() []asset.Asset {
&installconfig.InstallConfig{},
&bootstrap.Bootstrap{},
&machine.Master{},
&machines.Master{},
}
}

// Generate generates the terraform.tfvars file.
func (t *TerraformVariables) Generate(parents asset.Parents) error {
installConfig := &installconfig.InstallConfig{}
bootstrap := &bootstrap.Bootstrap{}
master := &machine.Master{}
parents.Get(installConfig, bootstrap, master)
bootstrapIgnAsset := &bootstrap.Bootstrap{}
masterIgnAsset := &machine.Master{}
mastersAsset := &machines.Master{}
parents.Get(installConfig, bootstrapIgnAsset, masterIgnAsset, mastersAsset)

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

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

data, err := tfvars.TFVars(installConfig.Config, bootstrapIgn, masterIgn)
var mastersList metav1.List
err := yaml.Unmarshal(mastersAsset.MachinesRaw, &mastersList)
if err != nil {
return errors.Wrap(err, "unmarshal master machine list")
}

masters := make([]*clusterapi.Machine, 0, len(mastersList.Items))
for _, item := range mastersList.Items {
var master clusterapi.Machine
err = yaml.Unmarshal(item.Raw, &master)
if err != nil {
return errors.Wrap(err, "unmarshal master machine item")
}
masters = append(masters, &master)
}

var vpcCIDR string
if installConfig.Config.Platform.AWS != nil {
vpcCIDR = installConfig.Config.Platform.AWS.VPCCIDRBlock
}

var bridge string
var image string
if installConfig.Config.Platform.Libvirt != nil {
vpcCIDR = installConfig.Config.Platform.Libvirt.Network.IPRange
bridge = installConfig.Config.Platform.Libvirt.Network.IfName
if installConfig.Config.Platform.Libvirt.DefaultMachinePlatform == nil {
return errors.New("libvirt image support currently requires defaultMachinePlatform")
}
image = installConfig.Config.Platform.Libvirt.DefaultMachinePlatform.Image
}

var cloud string
if installConfig.Config.Platform.OpenStack != nil {
vpcCIDR = installConfig.Config.Platform.OpenStack.NetworkCIDRBlock
cloud = installConfig.Config.Platform.OpenStack.Cloud
}

data, err := tfvars.TFVars(
installConfig.Config.ClusterID,
installConfig.Config.ObjectMeta.Name,
installConfig.Config.BaseDomain,
vpcCIDR,
bridge,
image,
cloud,
bootstrapIgn,
masterIgn,
masters,
)
if err != nil {
return errors.Wrap(err, "failed to get Tfvars")
}

t.File = &asset.File{
Filename: TfVarsFileName,
Data: data,
Expand Down
39 changes: 39 additions & 0 deletions pkg/tfvars/aws/aws.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
// Package aws contains AWS-specific Terraform-variable logic.
package aws

import (
"github.com/ghodss/yaml"
"github.com/pkg/errors"
awsprovider "sigs.k8s.io/cluster-api-provider-aws/pkg/apis/awsproviderconfig/v1alpha1"
)

// Endpoints is the type of the AWS endpoints.
type Endpoints string

Expand Down Expand Up @@ -54,3 +61,35 @@ type Worker struct {
CustomSubnets map[string]string `json:"aws_worker_custom_subnets,omitempty"`
IAMRoleName string `json:"aws_worker_iam_role_name,omitempty"`
}

// TFVars generates AWS-specific Terraform variables.
func TFVars(config []byte, vpcCIDR string) (*AWS, error) {
var cfg awsprovider.AWSMachineProviderConfig
err := yaml.Unmarshal(config, &cfg)
if err != nil {
return nil, errors.Wrap(err, "unmarshal AWS provider config")
}

tags := make(map[string]string, len(cfg.Tags))
for _, tag := range cfg.Tags {
tags[tag.Name] = tag.Value
}

return &AWS{
Endpoints: EndpointsAll, // Default value for endpoints.
Region: cfg.Placement.Region,
ExtraTags: tags,
//External: External{VPCID: config.Platform.AWS.VPCID}, FIXME: drop this or get it into the cluster API
VPCCIDRBlock: vpcCIDR,
EC2AMIOverride: *cfg.AMI.ID,
Master: Master{
EC2Type: cfg.InstanceType,
//IAMRoleName: *cfg.IAMInstanceProfile.ID, FIXME: this is for external roles
MasterRootVolume: MasterRootVolume{
//IOPS: config.m.Platform.AWS.EC2RootVolume.IOPS, FIXME: get these into the cluster API
//Size: m.Platform.AWS.EC2RootVolume.Size,
//Type: m.Platform.AWS.EC2RootVolume.Type,
},
},
}, nil
}
56 changes: 35 additions & 21 deletions pkg/tfvars/libvirt/libvirt.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
// Package libvirt contains libvirt-specific Terraform-variable logic.
package libvirt

import (
"fmt"
"net"

"github.com/apparentlymart/go-cidr/cidr"
"github.com/ghodss/yaml"
libvirtprovider "github.com/openshift/cluster-api-provider-libvirt/pkg/apis/libvirtproviderconfig/v1alpha1"
"github.com/pkg/errors"
)

// Libvirt encompasses configuration specific to libvirt.
Expand All @@ -22,34 +26,44 @@ type Network struct {
IPRange string `json:"libvirt_ip_range,omitempty"`
}

// TFVars fills in computed Terraform variables.
func (l *Libvirt) TFVars(masterCount int) error {
_, network, err := net.ParseCIDR(l.Network.IPRange)
// TFVars generates libvirt-specific Terraform variables.
func TFVars(config []byte, image string, vpcCIDR string, bridge string, masterCount int) (*Libvirt, error) {
var cfg libvirtprovider.LibvirtMachineProviderConfig
err := yaml.Unmarshal(config, &cfg)
if err != nil {
return fmt.Errorf("failed to parse libvirt network ipRange: %v", err)
return nil, errors.Wrap(err, "unmarshal libvirt provider config")
}

if l.BootstrapIP == "" {
ip, err := cidr.Host(network, 10)
if err != nil {
return fmt.Errorf("failed to generate bootstrap IP: %v", err)
}
l.BootstrapIP = ip.String()
_, network, err := net.ParseCIDR(vpcCIDR)
if err != nil {
return nil, errors.Errorf("failed to parse VPC CIDR %q: %v", vpcCIDR, err)
}

if len(l.MasterIPs) > 0 {
if len(l.MasterIPs) != masterCount {
return fmt.Errorf("length of MasterIPs doesn't match master count")
}
} else {
if ips, err := generateIPs("master", network, masterCount, 11); err == nil {
l.MasterIPs = ips
} else {
return err
}
bootstrapIP, err := cidr.Host(network, 10)
if err != nil {
return nil, errors.Errorf("failed to generate bootstrap IP: %v", err)
}

return nil
masterIPs, err := generateIPs("master", network, masterCount, 11)
if err != nil {
return nil, err
}

//FIXME
//if err := config.Libvirt.UseCachedImage(); err != nil {
// return nil, errors.Wrap(err, "failed to use cached libvirt image")
//}

return &Libvirt{
URI: cfg.URI,
Image: image,
Network: Network{
IfName: bridge,
IPRange: vpcCIDR,
},
BootstrapIP: bootstrapIP.String(),
MasterIPs: masterIPs,
}, nil
}

func generateIPs(name string, network *net.IPNet, count int, offset int) ([]string, error) {
Expand Down
38 changes: 38 additions & 0 deletions pkg/tfvars/openstack/openstack.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
// Package openstack contains OpenStack-specific Terraform-variable logic.
package openstack

import (
"github.com/ghodss/yaml"
"github.com/pkg/errors"
)

// OpenStack converts OpenStack related config.
type OpenStack struct {
BaseImage string `json:"openstack_base_image,omitempty"`
Expand Down Expand Up @@ -47,3 +53,35 @@ type Credentials struct {
UserID string `json:"openstack_credentials_user_id,omitempty"`
UserName string `json:"openstack_credentials_user_name,omitempty"`
}

type openStackMachineProviderConfig struct {
Placement placement `json:"placement"`
Image image `json:"image"`
}

type placement struct {
Region string `json:"region"`
}

type image struct {
ID string `json:"id"`
}

// TFVars generates OpenStack-specific Terraform variables.
func TFVars(config []byte, cloud string, vpcCIDR string) (*OpenStack, error) {
var cfg openStackMachineProviderConfig
err := yaml.Unmarshal(config, &cfg)
if err != nil {
return nil, errors.Wrap(err, "unmarshal OpenStack provider config")
}

return &OpenStack{
Region: cfg.Placement.Region,
NetworkCIDRBlock: vpcCIDR,
BaseImage: cfg.Image.ID,
//ExternalNetwork: FIXME: drop this or get it into the cluster API
Credentials: Credentials{
Cloud: cloud,
},
}, nil
}
Loading

0 comments on commit 270da62

Please sign in to comment.