Skip to content

Commit

Permalink
Merge pull request #492 from kubernetes-sigs/471-default-target-type
Browse files Browse the repository at this point in the history
Added the option to provide a default target type.
  • Loading branch information
bigkraig committed Jul 26, 2018
2 parents 3e303f7 + 2aaf92e commit d9939a7
Show file tree
Hide file tree
Showing 30 changed files with 662 additions and 601 deletions.
34 changes: 17 additions & 17 deletions cmd/flags.go
Expand Up @@ -30,11 +30,13 @@ import (

"github.com/kubernetes-sigs/aws-alb-ingress-controller/internal/ingress/annotations/class"
"github.com/kubernetes-sigs/aws-alb-ingress-controller/internal/ingress/annotations/parser"
"github.com/kubernetes-sigs/aws-alb-ingress-controller/internal/ingress/controller"
"github.com/kubernetes-sigs/aws-alb-ingress-controller/internal/ingress/controller/config"
ing_net "github.com/kubernetes-sigs/aws-alb-ingress-controller/internal/net"
)

func parseFlags() (bool, *controller.Configuration, error) {
func parseFlags() (bool, *config.Configuration, error) {
cfg := config.NewDefault()

var (
flags = pflag.NewFlagSet("", pflag.ExitOnError)

Expand All @@ -54,7 +56,7 @@ All ingress classes are satisfied if this parameter is left empty.`)
// configMap = flags.String("configmap", "",
// `Name of the ConfigMap containing custom global configurations for the controller.`)

resyncPeriod = flags.Duration("sync-period", 30*time.Second,
resyncPeriod = flags.Duration("sync-period", cfg.ResyncPeriod,
`Period at which the controller forces the repopulation of its local object stores.`)

watchNamespace = flags.String("watch-namespace", apiv1.NamespaceAll,
Expand All @@ -65,11 +67,6 @@ namespaces are watched if this parameter is left empty.`)
profiling = flags.Bool("profiling", true,
`Enable profiling via web interface host:port/debug/pprof/`)

defHealthzURL = flags.String("health-check-path", "/healthz",
`URL path of the health check endpoint.
Configured inside the NGINX status server. All requests received on the port
defined by the healthz-port parameter are forwarded internally to this path.`)

electionID = flags.String("election-id", "ingress-controller-leader",
`Election id to use for Ingress status updates.`)

Expand All @@ -88,24 +85,27 @@ defined by the healthz-port parameter are forwarded internally to this path.`)
albNamePrefix = flags.String("alb-name-prefix", "",
`Prefix to add to ALB resources (11 alphanumeric characters or less)`)

healthcheckPeriod = flags.Duration("health-check-period", 1*time.Minute,
healthcheckPeriod = flags.Duration("health-check-period", cfg.HealthCheckPeriod,
`Period at which the controller executes AWS health checks for its healthz endpoint.`)

targetType = flags.String("target-type", cfg.DefaultTargetType,
`Default target type to use for target groups, must be "instance" or "pod"`)

restrictScheme = flags.Bool("restrict-scheme", false,
`Restrict the scheme to internal except for whitelisted namespaces`)

restrictSchemeNamespace = flags.String("restrict-scheme-namespace", "default",
restrictSchemeNamespace = flags.String("restrict-scheme-namespace", cfg.RestrictSchemeNamespace,
`The namespace with the ConfigMap containing the allowed ingresses. Only respected when restrict-scheme is true.`)

awsSyncPeriod = flags.Duration("aws-sync-period", 60*time.Minute,
awsSyncPeriod = flags.Duration("aws-sync-period", cfg.AWSSyncPeriod,
`Period at which the controller refreshes the state from AWS.`)

awsAPIMaxRetries = flags.Int("aws-max-retries", 10,
awsAPIMaxRetries = flags.Int("aws-max-retries", cfg.AWSAPIMaxRetries,
`Maximum number of times to retry the AWS API.`)

awsAPIDebug = flags.Bool("aws-api-debug", false,
`Enable debug logging of AWS API`)
healthzPort = flags.Int("healthz-port", 10254, "Port to use for the healthz endpoint.")
healthzPort = flags.Int("healthz-port", cfg.HealthzPort, "Port to use for the healthz endpoint.")

_ = flags.String("default-backend-service", "", `No longer used, will be removed in next release`)
)
Expand Down Expand Up @@ -183,7 +183,7 @@ defined by the healthz-port parameter are forwarded internally to this path.`)
awsAPIMaxRetries = &i
}

config := &controller.Configuration{
config := &config.Configuration{
ClusterName: *clusterName,
ALBNamePrefix: *albNamePrefix,
RestrictScheme: *restrictScheme,
Expand All @@ -192,6 +192,7 @@ defined by the healthz-port parameter are forwarded internally to this path.`)
AWSAPIMaxRetries: *awsAPIMaxRetries,
AWSAPIDebug: *awsAPIDebug,
HealthCheckPeriod: *healthcheckPeriod,
DefaultTargetType: *targetType,

APIServerHost: *apiserverHost,
KubeConfigFile: *kubeConfigFile,
Expand All @@ -200,9 +201,8 @@ defined by the healthz-port parameter are forwarded internally to this path.`)
ResyncPeriod: *resyncPeriod,
Namespace: *watchNamespace,
// ConfigMapName: *configMap,
DefaultHealthzURL: *defHealthzURL,
SyncRateLimit: *syncRateLimit,
HealthzPort: *healthzPort,
SyncRateLimit: *syncRateLimit,
HealthzPort: *healthzPort,
}

return false, config, nil
Expand Down
51 changes: 24 additions & 27 deletions internal/alb/lb/loadbalancer.go
Expand Up @@ -10,6 +10,8 @@ import (
"strings"
"time"

"github.com/kubernetes-sigs/aws-alb-ingress-controller/internal/k8s"

"github.com/kubernetes-sigs/aws-alb-ingress-controller/internal/aws/albrgt"

extensions "k8s.io/api/extensions/v1beta1"
Expand All @@ -22,32 +24,27 @@ import (
"github.com/kubernetes-sigs/aws-alb-ingress-controller/internal/aws/albec2"
"github.com/kubernetes-sigs/aws-alb-ingress-controller/internal/aws/albelbv2"
"github.com/kubernetes-sigs/aws-alb-ingress-controller/internal/aws/albwaf"
"github.com/kubernetes-sigs/aws-alb-ingress-controller/internal/ingress/annotations"
"github.com/kubernetes-sigs/aws-alb-ingress-controller/internal/ingress/controller/store"
"github.com/kubernetes-sigs/aws-alb-ingress-controller/pkg/util/log"
util "github.com/kubernetes-sigs/aws-alb-ingress-controller/pkg/util/types"
api "k8s.io/api/core/v1"
)

type NewDesiredLoadBalancerOptions struct {
ALBNamePrefix string
Namespace string
IngressName string
ExistingLoadBalancer *LoadBalancer
Logger *log.Logger
Store store.Storer
Ingress *extensions.Ingress
IngressAnnotations *annotations.Ingress
CommonTags util.ELBv2Tags
}

// NewDesiredLoadBalancer returns a new loadbalancer.LoadBalancer based on the opts provided.
func NewDesiredLoadBalancer(o *NewDesiredLoadBalancerOptions) (newLoadBalancer *LoadBalancer, err error) {
name := createLBName(o.Namespace, o.IngressName, o.ALBNamePrefix)
name := createLBName(o.Ingress.Namespace, o.Ingress.Name, o.Store.GetConfig().ALBNamePrefix)

lbTags := util.ELBv2Tags{&elbv2.Tag{
Key: aws.String("kubernetes.io/ingress-name"),
Value: aws.String(o.Namespace + "/" + o.IngressName),
Value: aws.String(k8s.MetaNamespaceKey(o.Ingress)),
}}

for i := range o.CommonTags {
Expand All @@ -62,36 +59,41 @@ func NewDesiredLoadBalancer(o *NewDesiredLoadBalancerOptions) (newLoadBalancer *
return nil, err
}

annos, err := o.Store.GetIngressAnnotations(k8s.MetaNamespaceKey(o.Ingress))
if err != nil {
return nil, err
}

newLoadBalancer = &LoadBalancer{
id: name,
attributes: attributes{desired: o.IngressAnnotations.LoadBalancer.Attributes},
attributes: attributes{desired: annos.LoadBalancer.Attributes},
tags: tags{desired: lbTags},
options: options{
desired: opts{
webACLId: o.IngressAnnotations.LoadBalancer.WebACLId,
webACLId: annos.LoadBalancer.WebACLId,
},
},
lb: lb{
desired: &elbv2.LoadBalancer{
AvailabilityZones: o.IngressAnnotations.LoadBalancer.Subnets.AsAvailabilityZones(),
AvailabilityZones: annos.LoadBalancer.Subnets.AsAvailabilityZones(),
LoadBalancerName: aws.String(name),
Scheme: o.IngressAnnotations.LoadBalancer.Scheme,
IpAddressType: o.IngressAnnotations.LoadBalancer.IPAddressType,
SecurityGroups: o.IngressAnnotations.LoadBalancer.SecurityGroups,
Scheme: annos.LoadBalancer.Scheme,
IpAddressType: annos.LoadBalancer.IPAddressType,
SecurityGroups: annos.LoadBalancer.SecurityGroups,
VpcId: vpc,
},
},
logger: o.Logger,
}

lsps := portList{}
for _, port := range o.IngressAnnotations.LoadBalancer.Ports {
for _, port := range annos.LoadBalancer.Ports {
lsps = append(lsps, port.Port)
}

if len(newLoadBalancer.lb.desired.SecurityGroups) == 0 {
newLoadBalancer.options.desired.ports = lsps
newLoadBalancer.options.desired.inboundCidrs = o.IngressAnnotations.LoadBalancer.InboundCidrs
newLoadBalancer.options.desired.inboundCidrs = annos.LoadBalancer.InboundCidrs
}

var existingtgs tg.TargetGroups
Expand All @@ -106,7 +108,7 @@ func NewDesiredLoadBalancer(o *NewDesiredLoadBalancerOptions) (newLoadBalancer *
existinglb.options.desired.webACLId = newLoadBalancer.options.desired.webACLId
if len(o.ExistingLoadBalancer.lb.desired.SecurityGroups) == 0 {
existinglb.options.desired.ports = lsps
existinglb.options.desired.inboundCidrs = o.IngressAnnotations.LoadBalancer.InboundCidrs
existinglb.options.desired.inboundCidrs = annos.LoadBalancer.InboundCidrs
}

newLoadBalancer = existinglb
Expand All @@ -116,13 +118,10 @@ func NewDesiredLoadBalancer(o *NewDesiredLoadBalancerOptions) (newLoadBalancer *

// Assemble the target groups
newLoadBalancer.targetgroups, err = tg.NewDesiredTargetGroups(&tg.NewDesiredTargetGroupsOptions{
IngressRules: o.Ingress.Spec.Rules,
Ingress: o.Ingress,
LoadBalancerID: newLoadBalancer.id,
ExistingTargetGroups: existingtgs,
Store: o.Store,
IngressAnnotations: o.IngressAnnotations,
ALBNamePrefix: o.ALBNamePrefix,
Namespace: o.Namespace,
CommonTags: o.CommonTags,
Logger: o.Logger,
})
Expand All @@ -133,9 +132,9 @@ func NewDesiredLoadBalancer(o *NewDesiredLoadBalancerOptions) (newLoadBalancer *

// Assemble the listeners
newLoadBalancer.listeners, err = ls.NewDesiredListeners(&ls.NewDesiredListenersOptions{
IngressRules: o.Ingress.Spec.Rules,
Ingress: o.Ingress,
Store: o.Store,
ExistingListeners: existingls,
Annotations: o.IngressAnnotations,
Logger: o.Logger,
})

Expand Down Expand Up @@ -166,10 +165,9 @@ func tagsFromLB(r util.ELBv2Tags) (string, string, error) {
}

type NewCurrentLoadBalancerOptions struct {
LoadBalancer *elbv2.LoadBalancer
TargetGroups map[string][]*elbv2.TargetGroup
ALBNamePrefix string
Logger *log.Logger
LoadBalancer *elbv2.LoadBalancer
TargetGroups map[string][]*elbv2.TargetGroup
Logger *log.Logger
}

// NewCurrentLoadBalancer returns a new loadbalancer.LoadBalancer based on an elbv2.LoadBalancer.
Expand Down Expand Up @@ -253,7 +251,6 @@ func NewCurrentLoadBalancer(o *NewCurrentLoadBalancerOptions) (newLoadBalancer *

newLoadBalancer.targetgroups, err = tg.NewCurrentTargetGroups(&tg.NewCurrentTargetGroupsOptions{
TargetGroups: targetGroups,
ALBNamePrefix: o.ALBNamePrefix,
LoadBalancerID: newLoadBalancer.id,
Logger: o.Logger,
})
Expand Down

0 comments on commit d9939a7

Please sign in to comment.