Skip to content

Commit

Permalink
Respect Topology when assigning floating ips or not
Browse files Browse the repository at this point in the history
  • Loading branch information
Ole Markus With committed Aug 8, 2020
1 parent 6ae2bf8 commit 6ea564b
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 4 deletions.
1 change: 1 addition & 0 deletions pkg/apis/kops/validation/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ go_library(
"helpers.go",
"instancegroup.go",
"legacy.go",
"openstack.go",
"validation.go",
],
importpath = "k8s.io/kops/pkg/apis/kops/validation",
Expand Down
35 changes: 35 additions & 0 deletions pkg/apis/kops/validation/openstack.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
Copyright 2020 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package validation

import (
"k8s.io/apimachinery/pkg/util/validation/field"
"k8s.io/kops/pkg/apis/kops"
)

func openstackValidateCluster(c *kops.Cluster) (errList field.ErrorList) {
if c.Spec.CloudConfig.Openstack.Router == nil || c.Spec.CloudConfig.Openstack.Router.ExternalNetwork == nil {
topology := c.Spec.Topology
if topology == nil || topology.Nodes == kops.TopologyPublic {
errList = append(errList, field.Forbidden(field.NewPath("spec", "topology", "nodes"), "Public topology requires an external network"))
}
if topology == nil || topology.Masters == kops.TopologyPublic {
errList = append(errList, field.Forbidden(field.NewPath("spec", "topology", "masters"), "Public topology requires an external network"))
}
}
return errList
}
74 changes: 74 additions & 0 deletions pkg/apis/kops/validation/openstack_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package validation

import (
"testing"

"k8s.io/kops/upup/pkg/fi"

"k8s.io/kops/pkg/apis/kops"
)

func Test_ValidateTopology(t *testing.T) {
grid := []struct {
Input kops.ClusterSpec
ExpectedErrors []string
}{
{
Input: kops.ClusterSpec{
CloudConfig: &kops.CloudConfiguration{
Openstack: &kops.OpenstackConfiguration{},
},
},
ExpectedErrors: []string{
"Forbidden::spec.topology.nodes",
"Forbidden::spec.topology.masters",
},
},
{
Input: kops.ClusterSpec{
CloudConfig: &kops.CloudConfiguration{
Openstack: &kops.OpenstackConfiguration{
Router: &kops.OpenstackRouter{},
},
},
},
ExpectedErrors: []string{
"Forbidden::spec.topology.nodes",
"Forbidden::spec.topology.masters",
},
},
{

Input: kops.ClusterSpec{
CloudConfig: &kops.CloudConfiguration{
Openstack: &kops.OpenstackConfiguration{},
},
Topology: &kops.TopologySpec{
Masters: kops.TopologyPrivate,
Nodes: kops.TopologyPrivate,
},
},
ExpectedErrors: []string{},
},
{
Input: kops.ClusterSpec{
CloudConfig: &kops.CloudConfiguration{
Openstack: &kops.OpenstackConfiguration{
Router: &kops.OpenstackRouter{
ExternalNetwork: fi.String("foo"),
},
},
},
},
ExpectedErrors: []string{},
},
}

for _, g := range grid {
cluster := &kops.Cluster{
Spec: g.Input,
}
errs := openstackValidateCluster(cluster)
testErrors(t, g.Input, errs, g.ExpectedErrors)
}
}
2 changes: 2 additions & 0 deletions pkg/apis/kops/validation/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ func newValidateCluster(cluster *kops.Cluster) field.ErrorList {
allErrs = append(allErrs, awsValidateCluster(cluster)...)
case kops.CloudProviderGCE:
allErrs = append(allErrs, gceValidateCluster(cluster)...)
case kops.CloudProviderOpenstack:
allErrs = append(allErrs, openstackValidateCluster(cluster)...)
}

return allErrs
Expand Down
9 changes: 5 additions & 4 deletions pkg/model/openstackmodel/servergroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,8 @@ func (b *ServerGroupModelBuilder) buildInstances(c *fi.ModelBuilderContext, sg *
}
c.AddTask(instanceTask)

// Associate a floating IP to the master and bastion always if we have external network in router
// associate it to a node if bastion is not used
// Associate a floating IP to the instances if we have external network in router
// and respective topology is "public"
if b.Cluster.Spec.CloudConfig.Openstack != nil && b.Cluster.Spec.CloudConfig.Openstack.Router != nil {
if ig.Spec.AssociatePublicIP != nil && !fi.BoolValue(ig.Spec.AssociatePublicIP) {
continue
Expand All @@ -161,7 +161,8 @@ func (b *ServerGroupModelBuilder) buildInstances(c *fi.ModelBuilderContext, sg *
c.AddTask(t)
instanceTask.FloatingIP = t
case kops.InstanceGroupRoleMaster:
if b.Cluster.Spec.CloudConfig.Openstack.Loadbalancer == nil {

if b.Cluster.Spec.Topology == nil || b.Cluster.Spec.Topology.Masters != kops.TopologyPrivate {
t := &openstacktasks.FloatingIP{
Name: fi.String(fmt.Sprintf("%s-%s", "fip", *instanceTask.Name)),
Lifecycle: b.Lifecycle,
Expand All @@ -171,7 +172,7 @@ func (b *ServerGroupModelBuilder) buildInstances(c *fi.ModelBuilderContext, sg *
instanceTask.FloatingIP = t
}
default:
if b.Cluster.Spec.Topology == nil || b.Cluster.Spec.Topology.Nodes == "public" {
if b.Cluster.Spec.Topology == nil || b.Cluster.Spec.Topology.Nodes != kops.TopologyPrivate {
t := &openstacktasks.FloatingIP{
Name: fi.String(fmt.Sprintf("%s-%s", "fip", *instanceTask.Name)),
Lifecycle: b.Lifecycle,
Expand Down
3 changes: 3 additions & 0 deletions pkg/model/openstackmodel/servergroup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -864,6 +864,9 @@ func Test_ServerGroupModelBuilder(t *testing.T) {
},
},
},
Topology: &kops.TopologySpec{
Masters: kops.TopologyPrivate,
},
Subnets: []kops.ClusterSubnetSpec{
{
Name: "subnet-a",
Expand Down

0 comments on commit 6ea564b

Please sign in to comment.