Skip to content
This repository has been archived by the owner on Sep 19, 2018. It is now read-only.

Commit

Permalink
Merge pull request #11 from utilitywarehouse/ignore-label
Browse files Browse the repository at this point in the history
Ignore Label
  • Loading branch information
alkar committed Mar 6, 2017
2 parents fd7e734 + 281bfe8 commit 3787ab6
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 63 deletions.
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ You can test it locally (please refer to the command line help for more options)
```sh
./ingress53 \
-route53-zone-id=XXXXXXXXXXXXXX \
-label-name=ingress53.target \
-target=private.cluster-entrypoint.com \
-target=public.cluster-entrypoint.com \
-default-target=private.cluster-entrypoint.com \
Expand Down Expand Up @@ -134,7 +133,6 @@ spec:
image: utilitywarehouse/ingress53:v1.0.0
args:
- -route53-zone-id=XXXXXXXXXXXXXX
- -label-name=ingress53.target
- -target=private.cluster-entrypoint.com
- -target=public.cluster-entrypoint.com
- -default-target=private.cluster-entrypoint.com
Expand Down
30 changes: 23 additions & 7 deletions ingress_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ var (
Name: "privateIngressHostsAB",
Namespace: api.NamespaceDefault,
Labels: map[string]string{
LabelName: PrivateTarget,
testTargetLabelName: testPrivateTarget,
},
},
Spec: v1beta1.IngressSpec{
Expand All @@ -31,12 +31,28 @@ var (
},
}

privateIngressHostAIgnored = &v1beta1.Ingress{
ObjectMeta: v1.ObjectMeta{
Name: "privateIngressHostA",
Namespace: api.NamespaceDefault,
Labels: map[string]string{
testTargetLabelName: testPrivateTarget,
testIgnoreLabelName: "true",
},
},
Spec: v1beta1.IngressSpec{
Rules: []v1beta1.IngressRule{
{Host: "a.example.com"},
},
},
}

publicIngressHostC = &v1beta1.Ingress{
ObjectMeta: v1.ObjectMeta{
Name: "publicIngressHostCD",
Namespace: api.NamespaceDefault,
Labels: map[string]string{
LabelName: PublicTarget,
testTargetLabelName: testPublicTarget,
},
},
Spec: v1beta1.IngressSpec{
Expand All @@ -51,7 +67,7 @@ var (
Name: "publicIngressHostCD",
Namespace: api.NamespaceDefault,
Labels: map[string]string{
LabelName: PublicTarget,
testTargetLabelName: testPublicTarget,
},
},
Spec: v1beta1.IngressSpec{
Expand All @@ -66,7 +82,7 @@ var (
Name: "ingressHostE",
Namespace: api.NamespaceDefault,
Labels: map[string]string{
LabelName: PrivateTarget,
testTargetLabelName: testPrivateTarget,
},
},
Spec: v1beta1.IngressSpec{
Expand All @@ -81,7 +97,7 @@ var (
Name: "ingressHostE",
Namespace: api.NamespaceDefault,
Labels: map[string]string{
LabelName: PrivateTarget,
testTargetLabelName: testPrivateTarget,
},
},
Spec: v1beta1.IngressSpec{
Expand All @@ -96,7 +112,7 @@ var (
Name: "ingressHostE",
Namespace: api.NamespaceDefault,
Labels: map[string]string{
LabelName: PublicTarget,
testTargetLabelName: testPublicTarget,
},
},
Spec: v1beta1.IngressSpec{
Expand Down Expand Up @@ -124,7 +140,7 @@ var (
Name: "nonRegisteredIngress",
Namespace: api.NamespaceDefault,
Labels: map[string]string{
LabelName: "non-registered-target.aws.com",
testTargetLabelName: "non-registered-target.aws.com",
},
},
Spec: v1beta1.IngressSpec{
Expand Down
22 changes: 12 additions & 10 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,13 @@ var (
// init.
targets strslice

kubeConfig = flag.String("kubernetes-config", "", "path to the kubeconfig file, if unspecified then in-cluster config will be used")
labelName = flag.String("label-name", "", "Kubernetes key of the label that specifies the target type")
defaultTarget = flag.String("default-target", "", "Default target to use in the absense of matching labels")
r53ZoneID = flag.String("route53-zone-id", "", "route53 hosted DNS zone id")
debugLogs = flag.Bool("debug", false, "enables debug logs")
dryRun = flag.Bool("dry-run", false, "if set, ingress53 will not make any Route53 changes")
kubeConfig = flag.String("kubernetes-config", "", "path to the kubeconfig file, if unspecified then in-cluster config will be used")
targetLabelName = flag.String("target-label", "ingress53.target", "Kubernetes key of the label that specifies the target type")
ingoreLabelName = flag.String("ignore-label", "ingress53.ignore", "Kubernetes key of the label that determines whether an ingress should be ignored")
defaultTarget = flag.String("default-target", "", "Default target to use in the absense of matching labels")
r53ZoneID = flag.String("route53-zone-id", "", "route53 hosted DNS zone id")
debugLogs = flag.Bool("debug", false, "enables debug logs")
dryRun = flag.Bool("dry-run", false, "if set, ingress53 will not make any Route53 changes")

metricUpdatesApplied = prometheus.NewCounterVec(
prometheus.CounterOpts{
Expand Down Expand Up @@ -102,10 +103,11 @@ func main() {
log.SetOutput(luf)

ro := registratorOptions{
Targets: targets,
LabelName: *labelName,
DefaultTarget: *defaultTarget,
Route53ZoneID: *r53ZoneID,
Targets: targets,
TargetLabelName: *targetLabelName,
IgnoreLabelName: *ingoreLabelName,
DefaultTarget: *defaultTarget,
Route53ZoneID: *r53ZoneID,
}
if *kubeConfig != "" {
config, err := clientcmd.BuildConfigFromFlags("", *kubeConfig)
Expand Down
43 changes: 28 additions & 15 deletions registrator.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,18 @@ type cnameRecord struct {
type registrator struct {
dnsZone
*ingressWatcher
options registratorOptions
sats []selectorAndTarget
updateQueue chan cnameChange
options registratorOptions
sats []selectorAndTarget
ignoreSelector labels.Selector
updateQueue chan cnameChange
}

type registratorOptions struct {
AWSSessionOptions *session.Options
KubernetesConfig *rest.Config
Targets []string // required
LabelName string // required
TargetLabelName string // required
IgnoreLabelName string // required
DefaultTarget string // required
Route53ZoneID string // required
ResyncPeriod time.Duration
Expand All @@ -68,27 +70,27 @@ type selectorAndTarget struct {
Target string
}

func newRegistrator(zoneID string, targets []string, labelName, defaultTarget string) (*registrator, error) {
func newRegistrator(zoneID string, targets []string, targetLabelName, ignoreLabelName, defaultTarget string) (*registrator, error) {
return newRegistratorWithOptions(
registratorOptions{
Route53ZoneID: zoneID,
Targets: targets,
LabelName: labelName,
DefaultTarget: defaultTarget,
Route53ZoneID: zoneID,
Targets: targets,
TargetLabelName: targetLabelName,
IgnoreLabelName: ignoreLabelName,
DefaultTarget: defaultTarget,
})
}

func newRegistratorWithOptions(options registratorOptions) (*registrator, error) {
// check required options are set
if len(options.Targets) == 0 || options.Route53ZoneID == "" || options.LabelName == "" {
if len(options.Targets) == 0 || options.Route53ZoneID == "" || options.TargetLabelName == "" || options.IgnoreLabelName == "" {
return nil, errRegistratorMissingOption
}

var sats []selectorAndTarget

for _, target := range options.Targets {
var sb bytes.Buffer
sb.WriteString(options.LabelName)
sb.WriteString(options.TargetLabelName)
sb.WriteString("=")
sb.WriteString(target)

Expand All @@ -99,6 +101,11 @@ func newRegistratorWithOptions(options registratorOptions) (*registrator, error)
sats = append(sats, selectorAndTarget{Selector: s, Target: target})
}

ignoreLabel, err := labels.Parse(fmt.Sprintf("%s=true", options.IgnoreLabelName))
if err != nil {
return nil, err
}

if options.AWSSessionOptions == nil {
options.AWSSessionOptions = &session.Options{}
}
Expand All @@ -116,9 +123,10 @@ func newRegistratorWithOptions(options registratorOptions) (*registrator, error)
}

return &registrator{
options: options,
sats: sats,
updateQueue: make(chan cnameChange, 64),
options: options,
sats: sats,
ignoreSelector: ignoreLabel,
updateQueue: make(chan cnameChange, 64),
}, nil
}

Expand Down Expand Up @@ -268,6 +276,11 @@ func (r *registrator) applyBatch(changes []cnameChange) {
}

func (r *registrator) getTargetForIngress(ingress *v1beta1.Ingress) string {
if r.ignoreSelector.Matches(labels.Set(ingress.Labels)) {
log.Printf("[DEBUG] found ignore label for ingress: %s", ingress.Name)
return ""
}

for _, sat := range r.sats {
if sat.Selector.Matches(labels.Set(ingress.Labels)) {
return sat.Target
Expand Down
Loading

0 comments on commit 3787ab6

Please sign in to comment.