Skip to content

Commit

Permalink
asset/installconfig: Add default values for aws and libvirt
Browse files Browse the repository at this point in the history
Add default VPCCIDIR, network type, IfName, IPRange, imageURL.
Also set different default node numbers for different platform.

This is because on libvirt, there's a potential race for the network
setup on libvirt platform

Also ask users for the libvirt image URL.
  • Loading branch information
Yifan Gu authored and crawford committed Sep 23, 2018
1 parent 5dbbaa9 commit 070b77c
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 121 deletions.
43 changes: 42 additions & 1 deletion pkg/asset/installconfig/installconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ 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"
)

// installConfig generates the install-config.yml file.
Expand Down Expand Up @@ -63,6 +67,10 @@ func (a *installConfig) Generate(dependencies map[asset.Asset]*asset.State) (*as
},
BaseDomain: baseDomain,
Networking: types.Networking{
// TODO(yifan): Flannel is the temporal default network type for now,
// Need to update it to the new types.
Type: "flannel",

ServiceCIDR: ipnet.IPNet{
IPNet: defaultServiceCIDR,
},
Expand All @@ -89,12 +97,45 @@ func (a *installConfig) Generate(dependencies map[asset.Asset]*asset.State) (*as
case AWSPlatformType:
region := string(platformState.Contents[1].Data)
installConfig.AWS = &types.AWSPlatform{
Region: region,
Region: region,
VPCCIDRBlock: defaultVPCCIDR,
}
// Set the default master and worker nodes to 3 for AWS.
installConfig.Machines = []types.MachinePool{
{
Name: "master",
Replicas: func(x int64) *int64 { return &x }(3),
},
{
Name: "worker",
Replicas: func(x int64) *int64 { return &x }(3),
},
}
case LibvirtPlatformType:
uri := string(platformState.Contents[1].Data)
image := string(platformState.Contents[2].Data)

installConfig.Libvirt = &types.LibvirtPlatform{
URI: uri,
Network: types.LibvirtNetwork{
Name: clusterName,
IfName: defaultLibvirtNetworkIfName,
IPRange: defaultLibvirtNetworkIPRange,
},
DefaultMachinePlatform: &types.LibvirtMachinePoolPlatform{
Image: image,
},
}
// Set the default master and worker nodes to 1 for AWS.
installConfig.Machines = []types.MachinePool{
{
Name: "master",
Replicas: func(x int64) *int64 { return &x }(1),
},
{
Name: "worker",
Replicas: func(x int64) *int64 { return &x }(1),
},
}
default:
return nil, fmt.Errorf("unknown platform type %q", platform)
Expand Down
117 changes: 0 additions & 117 deletions pkg/asset/installconfig/installconfig_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package installconfig

import (
"fmt"
"net"
"testing"

Expand Down Expand Up @@ -62,122 +61,6 @@ func TestInstallConfigDependencies(t *testing.T) {
assert.Equal(t, exp, act, "unexpected dependency")
}

func TestInstallConfigGenerate(t *testing.T) {
cases := []struct {
name string
platformContents []string
expectedPlatformYaml string
}{
{
name: "aws",
platformContents: []string{
"aws",
"test-region",
},
expectedPlatformYaml: ` aws:
region: test-region
vpcCIDRBlock: ""
vpcID: ""`,
},
{
name: "libvirt",
platformContents: []string{
"libvirt",
"test-uri",
},
expectedPlatformYaml: ` libvirt:
URI: test-uri
masterIPs: null
network:
if: ""
ipRange: ""
name: ""`,
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
stock := &StockImpl{
clusterID: &testAsset{},
emailAddress: &testAsset{},
password: &testAsset{},
sshKey: &testAsset{},
baseDomain: &testAsset{},
clusterName: &testAsset{},
pullSecret: &testAsset{},
platform: &testAsset{},
}

installConfig := &installConfig{
assetStock: stock,
}

states := map[asset.Asset]*asset.State{
stock.clusterID: {
Contents: []asset.Content{{Data: []byte("test-cluster-id")}},
},
stock.emailAddress: {
Contents: []asset.Content{{Data: []byte("test-email")}},
},
stock.password: {
Contents: []asset.Content{{Data: []byte("test-password")}},
},
stock.sshKey: {
Contents: []asset.Content{{Data: []byte("test-sshkey")}},
},
stock.baseDomain: {
Contents: []asset.Content{{Data: []byte("test-domain")}},
},
stock.clusterName: {
Contents: []asset.Content{{Data: []byte("test-cluster-name")}},
},
stock.pullSecret: {
Contents: []asset.Content{{Data: []byte("test-pull-secret")}},
},
stock.platform: {
Contents: make([]asset.Content, len(tc.platformContents)),
},
}
for i, c := range tc.platformContents {
states[stock.platform].Contents[i].Data = []byte(c)
}

state, err := installConfig.Generate(states)
assert.NoError(t, err, "unexpected error generating asset")
assert.NotNil(t, state, "unexpected nil for asset state")

assert.Equal(t, 1, len(state.Contents), "unexpected number of contents in asset state")
assert.Equal(t, "install-config.yml", state.Contents[0].Name, "unexpected filename in asset state")

exp := fmt.Sprintf(`admin:
email: test-email
password: test-password
sshKey: test-sshkey
baseDomain: test-domain
clusterID: test-cluster-id
machines:
- name: master
platform: {}
replicas: 3
- name: worker
platform: {}
replicas: 3
metadata:
creationTimestamp: null
name: test-cluster-name
networking:
podCIDR: 10.2.0.0/16
serviceCIDR: 10.3.0.0/16
type: ""
platform:
%s
pullSecret: test-pull-secret
`, tc.expectedPlatformYaml)

assert.Equal(t, exp, string(state.Contents[0].Data), "unexpected data in install-config.yml")
})
}
}

// TestClusterDNSIP tests the ClusterDNSIP function.
func TestClusterDNSIP(t *testing.T) {
_, cidr, err := net.ParseCIDR("10.0.1.0/24")
Expand Down
9 changes: 8 additions & 1 deletion pkg/asset/installconfig/platform.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,16 +102,23 @@ func (a *Platform) awsPlatform() (*asset.State, error) {
}

func (a *Platform) libvirtPlatform() (*asset.State, error) {
var uri string
var uri, image string
survey.AskOne(&survey.Input{
Message: "URI",
Help: "The libvirt connection URI to be used. This must be accessible from the running cluster.",
Default: "qemu+tcp://192.168.122.1/system",
}, &uri, nil)

survey.AskOne(&survey.Input{
Message: "Image",
Help: "The URL to the OS image.",
Default: "",
}, &image, nil)

return assetStateForStringContents(
LibvirtPlatformType,
uri,
image,
), nil
}

Expand Down
1 change: 1 addition & 0 deletions pkg/types/config/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ func ConvertInstallConfigToTFVars(cfg *types.InstallConfig, bootstrapIgn string,
IfName: cfg.Platform.Libvirt.Network.IfName,
IPRange: cfg.Platform.Libvirt.Network.IPRange,
},
Image: cfg.Platform.Libvirt.DefaultMachinePlatform.Image,
MasterIPs: masterIPs,
}
}
Expand Down
8 changes: 6 additions & 2 deletions pkg/types/machinepools.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,12 @@ type EC2RootVolume struct {
type LibvirtMachinePoolPlatform struct {
// ImagePool is the name of the libvirt storage pool to which the storage
// volume containing the OS image belongs.
ImagePool string `json:"imagePool"`
ImagePool string `json:"imagePool,omitempty"`
// ImageVolume is the name of the libvirt storage volume containing the OS
// image.
ImageVolume string `json:"imageVolume"`
ImageVolume string `json:"imageVolume,omitempty"`

// Image is the URL to the OS image.
// E.g. "http://aos-ostree.rhev-ci-vms.eng.rdu2.redhat.com/rhcos/images/cloud/latest/rhcos-qemu.qcow2.gz"
Image string `json:"image"`
}

0 comments on commit 070b77c

Please sign in to comment.