Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add GCE Router task #11184

Merged
merged 1 commit into from
Apr 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions cmd/kops/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,11 @@ func TestMinimalGCE(t *testing.T) {
newIntegrationTest("minimal-gce.example.com", "minimal_gce").runTestTerraformGCE(t)
}

// TestMinimalGCE runs tests on a minimal GCE configuration with private topology.
func TestMinimalGCEPrivate(t *testing.T) {
newIntegrationTest("minimal-gce-private.example.com", "minimal_gce_private").runTestTerraformGCE(t)
}

// TestHA runs the test on a simple HA configuration, similar to kops create cluster minimal.example.com --zones us-west-1a,us-west-1b,us-west-1c --master-count=3
func TestHA(t *testing.T) {
newIntegrationTest("ha.example.com", "ha").withZones(3).runTestTerraformAWS(t)
Expand Down
2 changes: 2 additions & 0 deletions pkg/model/gcemodel/autoscalinggroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ func (b *AutoscalingGroupModelBuilder) buildInstanceTemplate(c *fi.ModelBuilderC
// TODO: Support preemptible nodes?
Preemptible: fi.Bool(false),

HasExternalIP: fi.Bool(b.Cluster.Spec.Topology.Masters == kops.TopologyPublic),

Scopes: []string{
"compute-rw",
"monitoring",
Expand Down
24 changes: 24 additions & 0 deletions pkg/model/gcemodel/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package gcemodel
import (
"fmt"

"k8s.io/kops/pkg/apis/kops"
"k8s.io/kops/upup/pkg/fi"
"k8s.io/kops/upup/pkg/fi/cloudup/gce"
"k8s.io/kops/upup/pkg/fi/cloudup/gcetasks"
Expand Down Expand Up @@ -64,7 +65,30 @@ func (b *NetworkModelBuilder) Build(c *fi.ModelBuilderContext) error {

t.GCEName = t.Name
c.AddTask(t)
}

// Create a CloudNAT for private topology.
if b.Cluster.Spec.Topology.Masters == kops.TopologyPrivate {
var hasPrivateSubnet bool
for _, subnet := range b.Cluster.Spec.Subnets {
if subnet.Type == kops.SubnetTypePrivate {
hasPrivateSubnet = true
break
}
}

if hasPrivateSubnet {
r := &gcetasks.Router{
Name: s(b.SafeObjectName("nat")),
Lifecycle: b.Lifecycle,
Network: s(b.LinkToNetwork().URL(b.Cluster.Spec.Project)),
Region: s(b.Region),
NATIPAllocationOption: s(gcetasks.NATIPAllocationOptionAutoOnly),
SourceSubnetworkIPRangesToNAT: s(gcetasks.SourceSubnetworkIPRangesAll),
}
c.AddTask(r)
}
}

return nil
}
57 changes: 57 additions & 0 deletions pkg/resources/gce/gce.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ const (
typeAddress = "Address"
typeRoute = "Route"
typeSubnet = "Subnet"
typeRouter = "Router"
typeDNSRecord = "DNSRecord"
)

Expand Down Expand Up @@ -96,6 +97,7 @@ func ListResourcesGCE(gceCloud gce.GCECloud, clusterName string, region string)
// TODO: Find routes via instances (via instance groups)
d.listAddresses,
d.listSubnets,
d.listRouters,
}
for _, fn := range listFunctions {
resourceTrackers, err := fn()
Expand Down Expand Up @@ -774,6 +776,61 @@ func deleteSubnet(cloud fi.Cloud, r *resources.Resource) error {
return c.WaitForOp(op)
}

func (d *clusterDiscoveryGCE) listRouters() ([]*resources.Resource, error) {
c := d.gceCloud

var resourceTrackers []*resources.Resource
ctx := context.Background()

err := c.Compute().Routers.List(c.Project(), c.Region()).Pages(ctx, func(page *compute.RouterList) error {
for _, o := range page.Items {
if !d.matchesClusterName(o.Name) {
klog.V(8).Infof("skipping Router with name %q", o.Name)
continue
}

resourceTracker := &resources.Resource{
Name: o.Name,
ID: o.Name,
Type: typeRouter,
Deleter: deleteRouter,
Obj: o,
}

klog.V(4).Infof("found resource: %s", o.SelfLink)
resourceTrackers = append(resourceTrackers, resourceTracker)
}
return nil
})
if err != nil {
return nil, fmt.Errorf("error listing routers: %v", err)
}

return resourceTrackers, nil
}

func deleteRouter(cloud fi.Cloud, r *resources.Resource) error {
c := cloud.(gce.GCECloud)
o := r.Obj.(*compute.Router)

klog.V(2).Infof("deleting GCE router %s", o.SelfLink)
u, err := gce.ParseGoogleCloudURL(o.SelfLink)
if err != nil {
return err
}

op, err := c.Compute().Routers.Delete(u.Project, u.Region, u.Name).Do()
if err != nil {
if gce.IsNotFound(err) {
klog.Infof("router not found, assuming deleted: %q", o.SelfLink)
return nil
}
return fmt.Errorf("error deleting router %s: %v", o.SelfLink, err)
}

return c.WaitForOp(op)
}

func (d *clusterDiscoveryGCE) matchesClusterName(name string) bool {
return d.matchesClusterNameMultipart(name, 1)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
admin: ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAAAgQCtWu40XQo8dczLsCq0OWV+hxm9uV3WxeH9Kgh4sMzQxNtoU1pvW0XdjpkBesRKGoolfWeCLXWxpyQb1IaiMkKoz7MdhQ/6UKjMjP66aFWWp3pwD0uj0HuJ7tq4gKHKRYGTaZIRWpzUiANBrjugVgA+Sd7E/mYwc/DMXkIyRZbvhQ==
Loading