Skip to content

Commit b2d6fa4

Browse files
committed
validate: simplify CIDR validation
Much of the code that made of the CIDR validation was there to give a custom description of what made the CIDR provided invalid. This has been removed in favor of using the error returned by the golang library's ParseCIDR function. Additionally, the code to determine whether two CIDRs overlap was unnecessary complex because it did not take advantage of the fact that CIDRs only overlap if one is a subset of the other. https://jira.coreos.com/browse/CORS-850
1 parent d813360 commit b2d6fa4

File tree

26 files changed

+538
-193
lines changed

26 files changed

+538
-193
lines changed

Gopkg.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/asset/ignition/machine/master_test.go

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package machine
22

33
import (
4-
"net"
54
"testing"
65

76
"github.com/stretchr/testify/assert"
@@ -24,12 +23,7 @@ func TestMasterGenerate(t *testing.T) {
2423
},
2524
BaseDomain: "test-domain",
2625
Networking: types.Networking{
27-
ServiceCIDR: ipnet.IPNet{
28-
IPNet: func(s string) net.IPNet {
29-
_, cidr, _ := net.ParseCIDR(s)
30-
return *cidr
31-
}("10.0.1.0/24"),
32-
},
26+
ServiceCIDR: *ipnet.MustParseCIDR("10.0.1.0/24"),
3327
},
3428
Platform: types.Platform{
3529
AWS: &aws.Platform{

pkg/asset/ignition/machine/worker_test.go

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package machine
22

33
import (
4-
"net"
54
"testing"
65

76
"github.com/stretchr/testify/assert"
@@ -19,12 +18,7 @@ func TestWorkerGenerate(t *testing.T) {
1918
installConfig := &installconfig.InstallConfig{
2019
Config: &types.InstallConfig{
2120
Networking: types.Networking{
22-
ServiceCIDR: ipnet.IPNet{
23-
IPNet: func(s string) net.IPNet {
24-
_, cidr, _ := net.ParseCIDR(s)
25-
return *cidr
26-
}("10.0.1.0/24"),
27-
},
21+
ServiceCIDR: *ipnet.MustParseCIDR("10.0.1.0/24"),
2822
},
2923
Platform: types.Platform{
3024
AWS: &aws.Platform{

pkg/asset/installconfig/aws/aws.go

Lines changed: 8 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -16,41 +16,19 @@ import (
1616
"github.com/sirupsen/logrus"
1717
survey "gopkg.in/AlecAivazis/survey.v1"
1818

19+
"github.com/openshift/installer/pkg/ipnet"
1920
"github.com/openshift/installer/pkg/types/aws"
2021
)
2122

22-
const (
23-
defaultVPCCIDR = "10.0.0.0/16"
24-
)
25-
2623
var (
27-
validAWSRegions = map[string]string{
28-
"ap-northeast-1": "Tokyo",
29-
"ap-northeast-2": "Seoul",
30-
"ap-northeast-3": "Osaka-Local",
31-
"ap-south-1": "Mumbai",
32-
"ap-southeast-1": "Singapore",
33-
"ap-southeast-2": "Sydney",
34-
"ca-central-1": "Central",
35-
"cn-north-1": "Beijing",
36-
"cn-northwest-1": "Ningxia",
37-
"eu-central-1": "Frankfurt",
38-
"eu-west-1": "Ireland",
39-
"eu-west-2": "London",
40-
"eu-west-3": "Paris",
41-
"sa-east-1": "São Paulo",
42-
"us-east-1": "N. Virginia",
43-
"us-east-2": "Ohio",
44-
"us-west-1": "N. California",
45-
"us-west-2": "Oregon",
46-
}
24+
defaultVPCCIDR = ipnet.MustParseCIDR("10.0.0.0/16")
4725
)
4826

4927
// Platform collects AWS-specific configuration.
5028
func Platform() (*aws.Platform, error) {
51-
longRegions := make([]string, 0, len(validAWSRegions))
52-
shortRegions := make([]string, 0, len(validAWSRegions))
53-
for id, location := range validAWSRegions {
29+
longRegions := make([]string, 0, len(aws.ValidRegions))
30+
shortRegions := make([]string, 0, len(aws.ValidRegions))
31+
for id, location := range aws.ValidRegions {
5432
longRegions = append(longRegions, fmt.Sprintf("%s (%s)", id, location))
5533
shortRegions = append(shortRegions, id)
5634
}
@@ -59,7 +37,7 @@ func Platform() (*aws.Platform, error) {
5937
})
6038

6139
defaultRegion := "us-east-1"
62-
_, ok := validAWSRegions[defaultRegion]
40+
_, ok := aws.ValidRegions[defaultRegion]
6341
if !ok {
6442
panic(fmt.Sprintf("installer bug: invalid default AWS region %q", defaultRegion))
6543
}
@@ -81,7 +59,7 @@ func Platform() (*aws.Platform, error) {
8159

8260
defaultRegionPointer := ssn.Config.Region
8361
if defaultRegionPointer != nil && *defaultRegionPointer != "" {
84-
_, ok := validAWSRegions[*defaultRegionPointer]
62+
_, ok := aws.ValidRegions[*defaultRegionPointer]
8563
if ok {
8664
defaultRegion = *defaultRegionPointer
8765
} else {
@@ -98,7 +76,7 @@ func Platform() (*aws.Platform, error) {
9876
Prompt: &survey.Select{
9977
Message: "Region",
10078
Help: "The AWS region to be used for installation.",
101-
Default: fmt.Sprintf("%s (%s)", defaultRegion, validAWSRegions[defaultRegion]),
79+
Default: fmt.Sprintf("%s (%s)", defaultRegion, aws.ValidRegions[defaultRegion]),
10280
Options: longRegions,
10381
},
10482
Validate: survey.ComposeValidators(survey.Required, func(ans interface{}) error {

pkg/asset/installconfig/installconfig.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"github.com/openshift/installer/pkg/asset"
1313
"github.com/openshift/installer/pkg/ipnet"
1414
"github.com/openshift/installer/pkg/types"
15+
"github.com/openshift/installer/pkg/types/validation"
1516
)
1617

1718
const (
@@ -156,6 +157,10 @@ func (a *InstallConfig) Load(f asset.FileFetcher) (found bool, err error) {
156157
return false, errors.Wrapf(err, "failed to unmarshal")
157158
}
158159

160+
if err := validation.ValidateInstallConfig(config).ToAggregate(); err != nil {
161+
return false, errors.Wrapf(err, "invalid %q file", installConfigFilename)
162+
}
163+
159164
a.File, a.Config = file, config
160165
return true, nil
161166
}

pkg/asset/installconfig/libvirt/libvirt.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,18 @@ import (
77
"github.com/pkg/errors"
88
survey "gopkg.in/AlecAivazis/survey.v1"
99

10+
"github.com/openshift/installer/pkg/ipnet"
1011
"github.com/openshift/installer/pkg/rhcos"
1112
"github.com/openshift/installer/pkg/types/libvirt"
1213
"github.com/openshift/installer/pkg/validate"
1314
)
1415

1516
const (
16-
defaultNetworkIfName = "tt0"
17-
defaultNetworkIPRange = "192.168.126.0/24"
17+
defaultNetworkIfName = "tt0"
18+
)
19+
20+
var (
21+
defaultNetworkIPRange = ipnet.MustParseCIDR("192.168.126.0/24")
1822
)
1923

2024
// Platform collects libvirt-specific configuration.
@@ -42,7 +46,7 @@ func Platform() (*libvirt.Platform, error) {
4246
return &libvirt.Platform{
4347
Network: libvirt.Network{
4448
IfName: defaultNetworkIfName,
45-
IPRange: defaultNetworkIPRange,
49+
IPRange: *defaultNetworkIPRange,
4650
},
4751
DefaultMachinePlatform: &libvirt.MachinePool{
4852
Image: qcowImage,

pkg/asset/installconfig/openstack/openstack.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,12 @@ import (
1212
"github.com/pkg/errors"
1313
survey "gopkg.in/AlecAivazis/survey.v1"
1414

15+
"github.com/openshift/installer/pkg/ipnet"
1516
"github.com/openshift/installer/pkg/types/openstack"
1617
)
1718

18-
const (
19-
defaultVPCCIDR = "10.0.0.0/16"
19+
var (
20+
defaultNetworkCIDR = ipnet.MustParseCIDR("10.0.0.0/16")
2021
)
2122

2223
// Read the valid cloud names from the clouds.yaml
@@ -235,7 +236,7 @@ func Platform() (*openstack.Platform, error) {
235236
}
236237

237238
return &openstack.Platform{
238-
NetworkCIDRBlock: defaultVPCCIDR,
239+
NetworkCIDRBlock: *defaultNetworkCIDR,
239240
Region: region,
240241
BaseImage: image,
241242
Cloud: cloud,

pkg/asset/machines/libvirt/machines.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ func provider(clusterName string, platform *libvirt.Platform, userDataSecret str
7777
BaseVolumeID: fmt.Sprintf("/var/lib/libvirt/images/%s-base", clusterName),
7878
},
7979
NetworkInterfaceName: clusterName,
80-
NetworkInterfaceAddress: platform.Network.IPRange,
80+
NetworkInterfaceAddress: platform.Network.IPRange.String(),
8181
Autostart: false,
8282
URI: platform.URI,
8383
}

pkg/ipnet/ipnet.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,22 @@ func (ipnet *IPNet) UnmarshalJSON(b []byte) (err error) {
7070

7171
return nil
7272
}
73+
74+
// ParseCIDR parses a CIDR from its string representation.
75+
func ParseCIDR(s string) (*IPNet, error) {
76+
_, cidr, err := net.ParseCIDR(s)
77+
if err != nil {
78+
return nil, err
79+
}
80+
return &IPNet{IPNet: *cidr}, nil
81+
}
82+
83+
// MustParseCIDR parses a CIDR from its string representation. If the parse fails,
84+
// the function will panic.
85+
func MustParseCIDR(s string) *IPNet {
86+
cidr, err := ParseCIDR(s)
87+
if err != nil {
88+
panic(err)
89+
}
90+
return cidr
91+
}

pkg/tfvars/libvirt/libvirt.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ type Libvirt struct {
1818

1919
// Network describes a libvirt network configuration.
2020
type Network struct {
21-
IfName string `json:"libvirt_network_if,omitempty"`
22-
IPRange string `json:"libvirt_ip_range,omitempty"`
21+
IfName string `json:"libvirt_network_if"`
22+
IPRange string `json:"libvirt_ip_range"`
2323
}
2424

2525
// TFVars fills in computed Terraform variables.

0 commit comments

Comments
 (0)