Skip to content

Commit 6032573

Browse files
committed
Handle upgrade with invalid CIDR stored in default ClusterNetwork
We now fix invalid CIDRs before storing them into the ClusterNetwork object, but we need to make sure that's not considered an illegal change from a previous invalid-but-working state stored by an older version that didn't fix them.
1 parent 34e9053 commit 6032573

File tree

2 files changed

+47
-4
lines changed

2 files changed

+47
-4
lines changed

pkg/network/master/master.go

+6-3
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ func Start(networkConfig osconfigapi.MasterNetworkConfig, networkClient networkc
110110
glog.Errorf("Cluster contains objects incompatible with new ClusterNetwork: %v", err)
111111
}
112112
} else {
113-
configChanged, err := clusterNetworkChanged(configCN, existingCN)
113+
configChanged, err := clusterNetworkChanged(configCN, existingCN, networkConfig)
114114
if err != nil {
115115
return false, err
116116
}
@@ -184,9 +184,13 @@ func (master *OsdnMaster) checkClusterNetworkAgainstClusterObjects() error {
184184
return master.networkInfo.CheckClusterObjects(subnets, pods, services)
185185
}
186186

187-
func clusterNetworkChanged(obj *networkapi.ClusterNetwork, old *networkapi.ClusterNetwork) (bool, error) {
187+
func clusterNetworkChanged(obj *networkapi.ClusterNetwork, old *networkapi.ClusterNetwork, networkConfig osconfigapi.MasterNetworkConfig) (bool, error) {
188188

189189
if old.ServiceNetwork != obj.ServiceNetwork {
190+
if old.ServiceNetwork == networkConfig.ServiceNetworkCIDR {
191+
// We corrected it from invalid to valid
192+
return true, nil
193+
}
190194
return true, fmt.Errorf("cannot change the serviceNetworkCIDR of an already-deployed cluster")
191195
} else if old.PluginName != obj.PluginName {
192196
return true, nil
@@ -208,6 +212,5 @@ func clusterNetworkChanged(obj *networkapi.ClusterNetwork, old *networkapi.Clust
208212
}
209213
}
210214
return changed, nil
211-
212215
}
213216
}

pkg/network/master/master_test.go

+41-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package master
33
import (
44
"testing"
55

6+
osconfigapi "github.com/openshift/origin/pkg/cmd/server/api"
67
networkapi "github.com/openshift/origin/pkg/network/apis/network"
78
"github.com/openshift/origin/pkg/network/common"
89
)
@@ -85,7 +86,10 @@ func Test_clusterNetworkChanged(t *testing.T) {
8586
expectChanged = true
8687
}
8788

88-
changed, err := clusterNetworkChanged(&newCN, &origCN)
89+
networkConfig := osconfigapi.MasterNetworkConfig{
90+
ServiceNetworkCIDR: newCN.ServiceNetwork,
91+
}
92+
changed, err := clusterNetworkChanged(&newCN, &origCN, networkConfig)
8993
if changed != expectChanged {
9094
t.Fatalf("unexpected result (%t instead of %t) on %q: %s -> %s", changed, expectChanged, test.name, common.ClusterNetworkToString(&origCN), common.ClusterNetworkToString(&newCN))
9195
}
@@ -94,3 +98,39 @@ func Test_clusterNetworkChanged(t *testing.T) {
9498
}
9599
}
96100
}
101+
102+
func Test_clusterNetworkChanged_BadCIDR(t *testing.T) {
103+
badCN := networkapi.ClusterNetwork{
104+
ClusterNetworks: []networkapi.ClusterNetworkEntry{{CIDR: "10.128.1.0/14", HostSubnetLength: 10}},
105+
ServiceNetwork: "172.30.1.0/16",
106+
PluginName: "redhat/openshift-ovs-subnet",
107+
}
108+
correctedCN := networkapi.ClusterNetwork{
109+
ClusterNetworks: []networkapi.ClusterNetworkEntry{{CIDR: "10.128.0.0/14", HostSubnetLength: 10}},
110+
ServiceNetwork: "172.30.0.0/16",
111+
PluginName: "redhat/openshift-ovs-subnet",
112+
}
113+
badNetworkConfig := osconfigapi.MasterNetworkConfig{
114+
ServiceNetworkCIDR: "172.30.1.0/16",
115+
}
116+
117+
// Older releases would store bad CIDR values into default ClusterNetwork.
118+
// Current master will always construct a corrected ClusterNetwork; this should
119+
// be reported as changed but not an error (so the etcd value will get updated).
120+
changed, err := clusterNetworkChanged(&correctedCN, &badCN, badNetworkConfig)
121+
if !changed {
122+
t.Fatalf("ClusterNetwork upgrade from bad CIDR to corrected was not considered a change")
123+
}
124+
if err != nil {
125+
t.Fatalf("ClusterNetwork upgrade from bad CIDR to corrected was considered an error: %v", err)
126+
}
127+
128+
// Should work after the etcd value is corrected even if master-config isn't
129+
changed, err = clusterNetworkChanged(&correctedCN, &correctedCN, badNetworkConfig)
130+
if changed {
131+
t.Fatalf("ClusterNetwork unexpectedly considered changed")
132+
}
133+
if err != nil {
134+
t.Fatalf("ClusterNetwork unexpectedly considered an error: %v", err)
135+
}
136+
}

0 commit comments

Comments
 (0)