Skip to content

Commit

Permalink
pkg/asset/installconfig: Pass platforms as JSON
Browse files Browse the repository at this point in the history
Instead of as an array of strings.  This makes it much easier to add
control over additional properties, because the consuming logic
remains "just unpack the JSON into the appropriate structure".
  • Loading branch information
wking committed Sep 28, 2018
1 parent f135d86 commit 149ac98
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 39 deletions.
28 changes: 7 additions & 21 deletions pkg/asset/installconfig/installconfig.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package installconfig

import (
"encoding/json"
"fmt"
"net"

Expand All @@ -17,11 +18,6 @@ import (
var (
defaultServiceCIDR = parseCIDR("10.3.0.0/16")
defaultPodCIDR = parseCIDR("10.2.0.0/16")
defaultVPCCIDR = "10.0.0.0/16"

defaultLibvirtNetworkIfName = "tt0"
defaultLibvirtNetworkIPRange = "192.168.124.0/24"
defaultLibvirtImageURL = "http://aos-ostree.rhev-ci-vms.eng.rdu2.redhat.com/rhcos/images/cloud/latest/rhcos-qemu.qcow2.gz"
)

// installConfig generates the install-config.yml file.
Expand Down Expand Up @@ -86,11 +82,10 @@ func (a *installConfig) Generate(dependencies map[asset.Asset]*asset.State) (*as
platform := string(platformState.Contents[0].Data)
switch platform {
case AWSPlatformType:
region := string(platformState.Contents[1].Data)
installConfig.AWS = &types.AWSPlatform{
Region: region,
VPCCIDRBlock: defaultVPCCIDR,
if err := json.Unmarshal(platformState.Contents[1].Data, &installConfig.AWS); err != nil {
return nil, err
}

installConfig.Machines = []types.MachinePool{
{
Name: "master",
Expand All @@ -102,19 +97,10 @@ func (a *installConfig) Generate(dependencies map[asset.Asset]*asset.State) (*as
},
}
case LibvirtPlatformType:
uri := string(platformState.Contents[1].Data)

installConfig.Libvirt = &types.LibvirtPlatform{
URI: uri,
Network: types.LibvirtNetwork{
Name: clusterName,
IfName: defaultLibvirtNetworkIfName,
IPRange: defaultLibvirtNetworkIPRange,
},
DefaultMachinePlatform: &types.LibvirtMachinePoolPlatform{
Image: defaultLibvirtImageURL,
},
if err := json.Unmarshal(platformState.Contents[1].Data, &installConfig.Libvirt); err != nil {
return nil, err
}
installConfig.Libvirt.Network.Name = clusterName
installConfig.Machines = []types.MachinePool{
{
Name: "master",
Expand Down
74 changes: 56 additions & 18 deletions pkg/asset/installconfig/platform.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package installconfig

import (
"encoding/json"
"fmt"
"net/url"
"sort"
Expand All @@ -9,6 +10,7 @@ import (
"github.com/AlecAivazis/survey"

"github.com/openshift/installer/pkg/asset"
"github.com/openshift/installer/pkg/types"
)

const (
Expand Down Expand Up @@ -41,6 +43,12 @@ var (
"us-west-1": "N. California",
"us-west-2": "Oregon",
}

defaultVPCCIDR = "10.0.0.0/16"

defaultLibvirtNetworkIfName = "tt0"
defaultLibvirtNetworkIPRange = "192.168.124.0/24"
defaultLibvirtImageURL = "http://aos-ostree.rhev-ci-vms.eng.rdu2.redhat.com/rhcos/images/cloud/latest/rhcos-qemu.qcow2.gz"
)

// Platform is an asset that queries the user for the platform on which to install
Expand Down Expand Up @@ -113,6 +121,10 @@ func (a *Platform) queryUserForPlatform() (string, error) {
}

func (a *Platform) awsPlatform() (*asset.State, error) {
platform := &types.AWSPlatform{
VPCCIDRBlock: defaultVPCCIDR,
}

longRegions := make([]string, 0, len(validAWSRegions))
shortRegions := make([]string, 0, len(validAWSRegions))
for id, location := range validAWSRegions {
Expand Down Expand Up @@ -148,14 +160,38 @@ func (a *Platform) awsPlatform() (*asset.State, error) {
if err != nil {
return nil, err
}
platform.Region = string(region.Contents[0].Data)

return assetStateForStringContents(
AWSPlatformType,
string(region.Contents[0].Data),
), nil
data, err := json.Marshal(platform)
if err != nil {
return nil, err
}

return &asset.State{
Contents: []asset.Content{
asset.Content{
Name: "platform",
Data: []byte(AWSPlatformType),
},
asset.Content{
Name: "platform.json",
Data: data,
},
},
}, nil
}

func (a *Platform) libvirtPlatform() (*asset.State, error) {
platform := &types.LibvirtPlatform{
Network: types.LibvirtNetwork{
IfName: defaultLibvirtNetworkIfName,
IPRange: defaultLibvirtNetworkIPRange,
},
DefaultMachinePlatform: &types.LibvirtMachinePoolPlatform{
Image: defaultLibvirtImageURL,
},
}

prompt := asset.UserProvided{
Question: &survey.Question{
Prompt: &survey.Input{
Expand All @@ -181,21 +217,23 @@ func (a *Platform) libvirtPlatform() (*asset.State, error) {
if err != nil {
return nil, err
}
platform.URI = string(uri.Contents[0].Data)

return assetStateForStringContents(
LibvirtPlatformType,
string(uri.Contents[0].Data),
), nil
}

func assetStateForStringContents(s ...string) *asset.State {
c := make([]asset.Content, len(s))
for i, d := range s {
c[i] = asset.Content{
Data: []byte(d),
}
data, err := json.Marshal(platform)
if err != nil {
return nil, err
}

return &asset.State{
Contents: c,
}
Contents: []asset.Content{
asset.Content{
Name: "platform-type",
Data: []byte(LibvirtPlatformType),
},
asset.Content{
Name: "platform.json",
Data: data,
},
},
}, nil
}

0 comments on commit 149ac98

Please sign in to comment.