Skip to content

Commit

Permalink
Merge pull request #2474 from justinsb/subnet_tagging
Browse files Browse the repository at this point in the history
Use explicit tag management in network tasks
  • Loading branch information
justinsb authored May 2, 2017
2 parents fc6587e + fb6d171 commit b351696
Show file tree
Hide file tree
Showing 20 changed files with 164 additions and 76 deletions.
23 changes: 23 additions & 0 deletions pkg/model/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ import (
"k8s.io/kops/pkg/apis/kops/util"
"k8s.io/kops/pkg/featureflag"
"k8s.io/kops/pkg/model/components"
"k8s.io/kops/upup/pkg/fi"
"k8s.io/kops/upup/pkg/fi/cloudup/awstasks"
"k8s.io/kops/upup/pkg/fi/cloudup/awsup"
)

var UseLegacyELBName = featureflag.New("UseLegacyELBName", featureflag.Bool(false))
Expand Down Expand Up @@ -189,6 +191,27 @@ func (m *KopsModelContext) CloudTagsForInstanceGroup(ig *kops.InstanceGroup) (ma
return labels, nil
}

// CloudTags computes the tags to apply to a normal cloud resource with the specified name
func (m *KopsModelContext) CloudTags(name string, shared bool) map[string]string {
tags := make(map[string]string)

switch fi.CloudProviderID(m.Cluster.Spec.CloudProvider) {
case fi.CloudProviderAWS:
tags[awsup.TagClusterName] = m.Cluster.ObjectMeta.Name
if name != "" {
tags["Name"] = name
}

if shared {
tags["kubernetes.io/cluster/"+m.Cluster.ObjectMeta.Name] = "shared"
} else {
tags["kubernetes.io/cluster/"+m.Cluster.ObjectMeta.Name] = "owned"
}

}
return tags
}

func (m *KopsModelContext) UsesBastionDns() bool {
if m.Cluster.Spec.Topology.Bastion != nil && m.Cluster.Spec.Topology.Bastion.BastionPublicName != "" {
return true
Expand Down
11 changes: 9 additions & 2 deletions pkg/model/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,17 @@ func (b *NetworkModelBuilder) Build(c *fi.ModelBuilderContext) error {
}

sharedVPC := b.Cluster.SharedVPC()
vpcName := b.ClusterName()

// VPC that holds everything for the cluster
{
tags := b.CloudTags(vpcName, sharedVPC)

t := &awstasks.VPC{
Name: s(b.ClusterName()),
Name: s(vpcName),
Shared: fi.Bool(sharedVPC),
EnableDNSSupport: fi.Bool(true),
Tags: tags,
}

if sharedVPC && VersionGTE(kubernetesVersion, 1, 5) {
Expand Down Expand Up @@ -133,13 +137,16 @@ func (b *NetworkModelBuilder) Build(c *fi.ModelBuilderContext) error {
for i := range b.Cluster.Spec.Subnets {
subnetSpec := &b.Cluster.Spec.Subnets[i]
sharedSubnet := subnetSpec.ProviderID != ""
subnetName := subnetSpec.Name + "." + b.ClusterName()
tags := b.CloudTags(subnetName, sharedSubnet)

subnet := &awstasks.Subnet{
Name: s(subnetSpec.Name + "." + b.ClusterName()),
Name: s(subnetName),
VPC: b.LinkToVPC(),
AvailabilityZone: s(subnetSpec.Zone),
CIDR: s(subnetSpec.CIDR),
Shared: fi.Bool(sharedSubnet),
Tags: tags,
}

if subnetSpec.ProviderID != "" {
Expand Down
10 changes: 6 additions & 4 deletions tests/integration/complex/kubernetes.tf
Original file line number Diff line number Diff line change
Expand Up @@ -342,8 +342,9 @@ resource "aws_subnet" "us-test-1a-complex-example-com" {
availability_zone = "us-test-1a"

tags = {
KubernetesCluster = "complex.example.com"
Name = "us-test-1a.complex.example.com"
KubernetesCluster = "complex.example.com"
Name = "us-test-1a.complex.example.com"
"kubernetes.io/cluster/complex.example.com" = "owned"
}
}

Expand All @@ -353,8 +354,9 @@ resource "aws_vpc" "complex-example-com" {
enable_dns_support = true

tags = {
KubernetesCluster = "complex.example.com"
Name = "complex.example.com"
KubernetesCluster = "complex.example.com"
Name = "complex.example.com"
"kubernetes.io/cluster/complex.example.com" = "owned"
}
}

Expand Down
20 changes: 12 additions & 8 deletions tests/integration/ha/kubernetes.tf
Original file line number Diff line number Diff line change
Expand Up @@ -512,8 +512,9 @@ resource "aws_subnet" "us-test-1a-ha-example-com" {
availability_zone = "us-test-1a"

tags = {
KubernetesCluster = "ha.example.com"
Name = "us-test-1a.ha.example.com"
KubernetesCluster = "ha.example.com"
Name = "us-test-1a.ha.example.com"
"kubernetes.io/cluster/ha.example.com" = "owned"
}
}

Expand All @@ -523,8 +524,9 @@ resource "aws_subnet" "us-test-1b-ha-example-com" {
availability_zone = "us-test-1b"

tags = {
KubernetesCluster = "ha.example.com"
Name = "us-test-1b.ha.example.com"
KubernetesCluster = "ha.example.com"
Name = "us-test-1b.ha.example.com"
"kubernetes.io/cluster/ha.example.com" = "owned"
}
}

Expand All @@ -534,8 +536,9 @@ resource "aws_subnet" "us-test-1c-ha-example-com" {
availability_zone = "us-test-1c"

tags = {
KubernetesCluster = "ha.example.com"
Name = "us-test-1c.ha.example.com"
KubernetesCluster = "ha.example.com"
Name = "us-test-1c.ha.example.com"
"kubernetes.io/cluster/ha.example.com" = "owned"
}
}

Expand All @@ -545,8 +548,9 @@ resource "aws_vpc" "ha-example-com" {
enable_dns_support = true

tags = {
KubernetesCluster = "ha.example.com"
Name = "ha.example.com"
KubernetesCluster = "ha.example.com"
Name = "ha.example.com"
"kubernetes.io/cluster/ha.example.com" = "owned"
}
}

Expand Down
10 changes: 6 additions & 4 deletions tests/integration/minimal-141/kubernetes.tf
Original file line number Diff line number Diff line change
Expand Up @@ -342,8 +342,9 @@ resource "aws_subnet" "us-test-1a-minimal-141-example-com" {
availability_zone = "us-test-1a"

tags = {
KubernetesCluster = "minimal-141.example.com"
Name = "us-test-1a.minimal-141.example.com"
KubernetesCluster = "minimal-141.example.com"
Name = "us-test-1a.minimal-141.example.com"
"kubernetes.io/cluster/minimal-141.example.com" = "owned"
}
}

Expand All @@ -353,8 +354,9 @@ resource "aws_vpc" "minimal-141-example-com" {
enable_dns_support = true

tags = {
KubernetesCluster = "minimal-141.example.com"
Name = "minimal-141.example.com"
KubernetesCluster = "minimal-141.example.com"
Name = "minimal-141.example.com"
"kubernetes.io/cluster/minimal-141.example.com" = "owned"
}
}

Expand Down
8 changes: 8 additions & 0 deletions tests/integration/minimal/cloudformation.json
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,10 @@
{
"Key": "Name",
"Value": "us-test-1a.minimal.example.com"
},
{
"Key": "kubernetes.io/cluster/minimal.example.com",
"Value": "owned"
}
]
}
Expand Down Expand Up @@ -437,6 +441,10 @@
{
"Key": "Name",
"Value": "minimal.example.com"
},
{
"Key": "kubernetes.io/cluster/minimal.example.com",
"Value": "owned"
}
]
}
Expand Down
10 changes: 6 additions & 4 deletions tests/integration/minimal/kubernetes.tf
Original file line number Diff line number Diff line change
Expand Up @@ -342,8 +342,9 @@ resource "aws_subnet" "us-test-1a-minimal-example-com" {
availability_zone = "us-test-1a"

tags = {
KubernetesCluster = "minimal.example.com"
Name = "us-test-1a.minimal.example.com"
KubernetesCluster = "minimal.example.com"
Name = "us-test-1a.minimal.example.com"
"kubernetes.io/cluster/minimal.example.com" = "owned"
}
}

Expand All @@ -353,8 +354,9 @@ resource "aws_vpc" "minimal-example-com" {
enable_dns_support = true

tags = {
KubernetesCluster = "minimal.example.com"
Name = "minimal.example.com"
KubernetesCluster = "minimal.example.com"
Name = "minimal.example.com"
"kubernetes.io/cluster/minimal.example.com" = "owned"
}
}

Expand Down
15 changes: 9 additions & 6 deletions tests/integration/privatecalico/kubernetes.tf
Original file line number Diff line number Diff line change
Expand Up @@ -614,8 +614,9 @@ resource "aws_subnet" "us-test-1a-privatecalico-example-com" {
availability_zone = "us-test-1a"

tags = {
KubernetesCluster = "privatecalico.example.com"
Name = "us-test-1a.privatecalico.example.com"
KubernetesCluster = "privatecalico.example.com"
Name = "us-test-1a.privatecalico.example.com"
"kubernetes.io/cluster/privatecalico.example.com" = "owned"
}
}

Expand All @@ -625,8 +626,9 @@ resource "aws_subnet" "utility-us-test-1a-privatecalico-example-com" {
availability_zone = "us-test-1a"

tags = {
KubernetesCluster = "privatecalico.example.com"
Name = "utility-us-test-1a.privatecalico.example.com"
KubernetesCluster = "privatecalico.example.com"
Name = "utility-us-test-1a.privatecalico.example.com"
"kubernetes.io/cluster/privatecalico.example.com" = "owned"
}
}

Expand All @@ -636,8 +638,9 @@ resource "aws_vpc" "privatecalico-example-com" {
enable_dns_support = true

tags = {
KubernetesCluster = "privatecalico.example.com"
Name = "privatecalico.example.com"
KubernetesCluster = "privatecalico.example.com"
Name = "privatecalico.example.com"
"kubernetes.io/cluster/privatecalico.example.com" = "owned"
}
}

Expand Down
15 changes: 9 additions & 6 deletions tests/integration/privatecanal/kubernetes.tf
Original file line number Diff line number Diff line change
Expand Up @@ -605,8 +605,9 @@ resource "aws_subnet" "us-test-1a-privatecanal-example-com" {
availability_zone = "us-test-1a"

tags = {
KubernetesCluster = "privatecanal.example.com"
Name = "us-test-1a.privatecanal.example.com"
KubernetesCluster = "privatecanal.example.com"
Name = "us-test-1a.privatecanal.example.com"
"kubernetes.io/cluster/privatecanal.example.com" = "owned"
}
}

Expand All @@ -616,8 +617,9 @@ resource "aws_subnet" "utility-us-test-1a-privatecanal-example-com" {
availability_zone = "us-test-1a"

tags = {
KubernetesCluster = "privatecanal.example.com"
Name = "utility-us-test-1a.privatecanal.example.com"
KubernetesCluster = "privatecanal.example.com"
Name = "utility-us-test-1a.privatecanal.example.com"
"kubernetes.io/cluster/privatecanal.example.com" = "owned"
}
}

Expand All @@ -627,8 +629,9 @@ resource "aws_vpc" "privatecanal-example-com" {
enable_dns_support = true

tags = {
KubernetesCluster = "privatecanal.example.com"
Name = "privatecanal.example.com"
KubernetesCluster = "privatecanal.example.com"
Name = "privatecanal.example.com"
"kubernetes.io/cluster/privatecanal.example.com" = "owned"
}
}

Expand Down
15 changes: 9 additions & 6 deletions tests/integration/privatedns1/kubernetes.tf
Original file line number Diff line number Diff line change
Expand Up @@ -610,8 +610,9 @@ resource "aws_subnet" "us-test-1a-privatedns1-example-com" {
availability_zone = "us-test-1a"

tags = {
KubernetesCluster = "privatedns1.example.com"
Name = "us-test-1a.privatedns1.example.com"
KubernetesCluster = "privatedns1.example.com"
Name = "us-test-1a.privatedns1.example.com"
"kubernetes.io/cluster/privatedns1.example.com" = "owned"
}
}

Expand All @@ -621,8 +622,9 @@ resource "aws_subnet" "utility-us-test-1a-privatedns1-example-com" {
availability_zone = "us-test-1a"

tags = {
KubernetesCluster = "privatedns1.example.com"
Name = "utility-us-test-1a.privatedns1.example.com"
KubernetesCluster = "privatedns1.example.com"
Name = "utility-us-test-1a.privatedns1.example.com"
"kubernetes.io/cluster/privatedns1.example.com" = "owned"
}
}

Expand All @@ -632,8 +634,9 @@ resource "aws_vpc" "privatedns1-example-com" {
enable_dns_support = true

tags = {
KubernetesCluster = "privatedns1.example.com"
Name = "privatedns1.example.com"
KubernetesCluster = "privatedns1.example.com"
Name = "privatedns1.example.com"
"kubernetes.io/cluster/privatedns1.example.com" = "owned"
}
}

Expand Down
10 changes: 6 additions & 4 deletions tests/integration/privatedns2/kubernetes.tf
Original file line number Diff line number Diff line change
Expand Up @@ -596,8 +596,9 @@ resource "aws_subnet" "us-test-1a-privatedns2-example-com" {
availability_zone = "us-test-1a"

tags = {
KubernetesCluster = "privatedns2.example.com"
Name = "us-test-1a.privatedns2.example.com"
KubernetesCluster = "privatedns2.example.com"
Name = "us-test-1a.privatedns2.example.com"
"kubernetes.io/cluster/privatedns2.example.com" = "owned"
}
}

Expand All @@ -607,7 +608,8 @@ resource "aws_subnet" "utility-us-test-1a-privatedns2-example-com" {
availability_zone = "us-test-1a"

tags = {
KubernetesCluster = "privatedns2.example.com"
Name = "utility-us-test-1a.privatedns2.example.com"
KubernetesCluster = "privatedns2.example.com"
Name = "utility-us-test-1a.privatedns2.example.com"
"kubernetes.io/cluster/privatedns2.example.com" = "owned"
}
}
15 changes: 9 additions & 6 deletions tests/integration/privateflannel/kubernetes.tf
Original file line number Diff line number Diff line change
Expand Up @@ -605,8 +605,9 @@ resource "aws_subnet" "us-test-1a-privateflannel-example-com" {
availability_zone = "us-test-1a"

tags = {
KubernetesCluster = "privateflannel.example.com"
Name = "us-test-1a.privateflannel.example.com"
KubernetesCluster = "privateflannel.example.com"
Name = "us-test-1a.privateflannel.example.com"
"kubernetes.io/cluster/privateflannel.example.com" = "owned"
}
}

Expand All @@ -616,8 +617,9 @@ resource "aws_subnet" "utility-us-test-1a-privateflannel-example-com" {
availability_zone = "us-test-1a"

tags = {
KubernetesCluster = "privateflannel.example.com"
Name = "utility-us-test-1a.privateflannel.example.com"
KubernetesCluster = "privateflannel.example.com"
Name = "utility-us-test-1a.privateflannel.example.com"
"kubernetes.io/cluster/privateflannel.example.com" = "owned"
}
}

Expand All @@ -627,8 +629,9 @@ resource "aws_vpc" "privateflannel-example-com" {
enable_dns_support = true

tags = {
KubernetesCluster = "privateflannel.example.com"
Name = "privateflannel.example.com"
KubernetesCluster = "privateflannel.example.com"
Name = "privateflannel.example.com"
"kubernetes.io/cluster/privateflannel.example.com" = "owned"
}
}

Expand Down
Loading

0 comments on commit b351696

Please sign in to comment.