Skip to content

Commit

Permalink
Support internal (gossip) names for AWS also
Browse files Browse the repository at this point in the history
  • Loading branch information
justinsb committed Jun 19, 2017
1 parent 0def5cd commit 9d40b0e
Show file tree
Hide file tree
Showing 6 changed files with 128 additions and 10 deletions.
1 change: 1 addition & 0 deletions hack/.packages
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ k8s.io/kops/pkg/util/stringorslice
k8s.io/kops/pkg/validation
k8s.io/kops/protokube/cmd/protokube
k8s.io/kops/protokube/pkg/gossip
k8s.io/kops/protokube/pkg/gossip/aws
k8s.io/kops/protokube/pkg/gossip/dns
k8s.io/kops/protokube/pkg/gossip/dns/provider
k8s.io/kops/protokube/pkg/gossip/gce
Expand Down
14 changes: 14 additions & 0 deletions pkg/model/awsmodel/api_loadbalancer.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@ import (
"github.com/golang/glog"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/kops/pkg/apis/kops"
"k8s.io/kops/pkg/dns"
"k8s.io/kops/upup/pkg/fi"
"k8s.io/kops/upup/pkg/fi/cloudup/awstasks"
"k8s.io/kops/upup/pkg/fi/fitasks"
)

const LoadBalancerDefaultIdleTimeout = 5 * time.Minute
Expand Down Expand Up @@ -185,6 +187,18 @@ func (b *APILoadBalancerBuilder) Build(c *fi.ModelBuilderContext) error {
c.AddTask(t)
}

if dns.IsGossipHostname(b.Cluster.Name) {
// Ensure the ELB hostname is included in the TLS certificate,
// if we're not going to use an alias for it
// TODO: I don't love this technique for finding the task by name & modifying it
masterKeypairTask, found := c.Tasks["Keypair/master"]
if !found {
return fmt.Errorf("keypair/master task not found")
}
masterKeypair := masterKeypairTask.(*fitasks.Keypair)
masterKeypair.AlternateNameTasks = append(masterKeypair.AlternateNameTasks, elb)
}

for _, ig := range b.MasterInstanceGroups() {
t := &awstasks.LoadBalancerAttachment{
Name: s("api-" + ig.ObjectMeta.Name),
Expand Down
24 changes: 15 additions & 9 deletions pkg/model/dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ type DNSModelBuilder struct {
var _ fi.ModelBuilder = &DNSModelBuilder{}

func (b *DNSModelBuilder) ensureDNSZone(c *fi.ModelBuilderContext) error {
if dns.IsGossipHostname(b.Cluster.Name) {
return nil
}

// Configuration for a DNS zone
dnsZone := &awstasks.DNSZone{
Name: s(b.NameForDNSZone()),
Expand Down Expand Up @@ -88,17 +92,19 @@ func (b *DNSModelBuilder) Build(c *fi.ModelBuilderContext) error {
// This will point our DNS to the load balancer, and put the pieces
// together for kubectl to be work

if err := b.ensureDNSZone(c); err != nil {
return err
}
if !dns.IsGossipHostname(b.Cluster.Name) {
if err := b.ensureDNSZone(c); err != nil {
return err
}

apiDnsName := &awstasks.DNSName{
Name: s(b.Cluster.Spec.MasterPublicName),
Zone: b.LinkToDNSZone(),
ResourceType: s("A"),
TargetLoadBalancer: b.LinkToELB("api"),
apiDnsName := &awstasks.DNSName{
Name: s(b.Cluster.Spec.MasterPublicName),
Zone: b.LinkToDNSZone(),
ResourceType: s("A"),
TargetLoadBalancer: b.LinkToELB("api"),
}
c.AddTask(apiDnsName)
}
c.AddTask(apiDnsName)
}

if b.UsesBastionDns() {
Expand Down
73 changes: 73 additions & 0 deletions protokube/pkg/gossip/aws/seeds.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
Copyright 2017 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 aws

import (
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/aws/aws-sdk-go/service/ec2/ec2iface"
"k8s.io/kops/protokube/pkg/gossip"
)

type SeedProvider struct {
ec2 ec2iface.EC2API
tags map[string]string
}

var _ gossip.SeedProvider = &SeedProvider{}

func (p *SeedProvider) GetSeeds() ([]string, error) {
request := &ec2.DescribeInstancesInput{}
for k, v := range p.tags {
filter := &ec2.Filter{
Name: aws.String("tag:" + k),
Values: aws.StringSlice([]string{v}),
}
request.Filters = append(request.Filters, filter)
}
request.Filters = append(request.Filters, &ec2.Filter{
Name: aws.String("instance-state-name"),
Values: aws.StringSlice([]string{"running", "pending"}),
})

var seeds []string
err := p.ec2.DescribeInstancesPages(request, func(p *ec2.DescribeInstancesOutput, lastPage bool) (shouldContinue bool) {
for _, r := range p.Reservations {
for _, i := range r.Instances {
ip := aws.StringValue(i.PrivateIpAddress)
if ip != "" {
seeds = append(seeds, ip)
}
}
}
return true
})

if err != nil {
return nil, fmt.Errorf("error querying for EC2 instances: %v", err)
}

return seeds, nil
}

func NewSeedProvider(ec2 ec2iface.EC2API, tags map[string]string) (*SeedProvider, error) {
return &SeedProvider{
ec2: ec2,
tags: tags,
}, nil
}
6 changes: 5 additions & 1 deletion protokube/pkg/protokube/aws_volume.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/golang/glog"
"k8s.io/kops/protokube/pkg/gossip"
gossipaws "k8s.io/kops/protokube/pkg/gossip/aws"
"k8s.io/kops/upup/pkg/fi/cloudup/awsup"
"net"
"strings"
Expand Down Expand Up @@ -364,7 +365,10 @@ func (a *AWSVolumes) AttachVolume(volume *Volume) error {
}

func (a *AWSVolumes) GossipSeeds() (gossip.SeedProvider, error) {
return nil, fmt.Errorf("AWS seed provider not yet implemented")
tags := make(map[string]string)
tags[awsup.TagClusterName] = a.clusterTag

return gossipaws.NewSeedProvider(a.ec2, tags)
}

func (a *AWSVolumes) InstanceID() string {
Expand Down
20 changes: 20 additions & 0 deletions upup/pkg/fi/cloudup/awstasks/load_balancer.go
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,26 @@ func (e *LoadBalancer) Find(c *fi.Context) (*LoadBalancer, error) {
return actual, nil
}

var _ fi.HasAddress = &LoadBalancer{}

func (e *LoadBalancer) FindIPAddress(context *fi.Context) (*string, error) {
cloud := context.Cloud.(awsup.AWSCloud)

lb, err := FindLoadBalancerByNameTag(cloud, fi.StringValue(e.Name))
if err != nil {
return nil, err
}
if lb == nil {
return nil, nil
}

lbDnsName := fi.StringValue(lb.DNSName)
if lbDnsName == "" {
return nil, nil
}
return &lbDnsName, nil
}

func (e *LoadBalancer) Run(c *fi.Context) error {
// TODO: Make Normalize a standard method
e.Normalize()
Expand Down

0 comments on commit 9d40b0e

Please sign in to comment.