Skip to content

Commit

Permalink
Switched to a const for use-annotation
Browse files Browse the repository at this point in the history
  • Loading branch information
bigkraig committed Sep 17, 2018
1 parent a777672 commit 86cb17e
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 19 deletions.
10 changes: 6 additions & 4 deletions internal/alb/ls/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"

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

"github.com/aws/aws-sdk-go/aws"
Expand Down Expand Up @@ -138,7 +139,7 @@ func NewCurrentListener(o *NewCurrentListenerOptions) (*Listener, error) {
}
} else {
defaultBackend = &extensions.IngressBackend{
ServicePort: intstr.FromString("use-annotation"),
ServicePort: intstr.FromString(action.UseActionAnnotation),
}
}

Expand All @@ -151,7 +152,7 @@ func NewCurrentListener(o *NewCurrentListenerOptions) (*Listener, error) {
}

func (l *Listener) resolveDefaultBackend(rOpts *ReconcileOptions) (*elbv2.Action, error) {
if l.defaultBackend.ServicePort.String() == "use-annotation" {
if l.defaultBackend.ServicePort.String() == action.UseActionAnnotation {
if l.defaultBackend.ServiceName == Default404 {
return default404Action(), nil
}
Expand All @@ -163,8 +164,8 @@ func (l *Listener) resolveDefaultBackend(rOpts *ReconcileOptions) (*elbv2.Action

ruleConfig, ok := annos.Action.Actions[l.defaultBackend.ServiceName]
if !ok {
return nil, fmt.Errorf("`servicePort: use-annotation` was requested for"+
"`serviceName: %v` but an annotation for that action does not exist", l.defaultBackend.ServiceName)
return nil, fmt.Errorf("`servicePort: %s` was requested for"+
"`serviceName: %v` but an annotation for that action does not exist", action.UseActionAnnotation, l.defaultBackend.ServiceName)
}

return ruleConfig, nil
Expand All @@ -184,6 +185,7 @@ func (l *Listener) resolveDefaultBackend(rOpts *ReconcileOptions) (*elbv2.Action
// Reconcile compares the current and desired state of this Listener instance. Comparison
// results in no action, the creation, the deletion, or the modification of an AWS listener to
// satisfy the ingress's current state.

func (l *Listener) Reconcile(rOpts *ReconcileOptions) (err error) {
// If there is a desired listener, set some of the ARNs which are not available when we assemble the desired state
if l.ls.desired != nil {
Expand Down
9 changes: 5 additions & 4 deletions internal/alb/rs/rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"strconv"

"github.com/kubernetes-sigs/aws-alb-ingress-controller/internal/ingress/annotations/action"
"github.com/kubernetes-sigs/aws-alb-ingress-controller/internal/ingress/controller/store"

"github.com/aws/aws-sdk-go/aws"
Expand Down Expand Up @@ -47,16 +48,16 @@ func NewDesiredRule(o *NewDesiredRuleOptions) (*Rule, error) {
}

// Requested an `use-annotation` type rule
if o.Ingress != nil && o.SvcPort.String() == "use-annotation" {
if o.Ingress != nil && o.SvcPort.String() == action.UseActionAnnotation {
annos, err := o.Store.GetIngressAnnotations(k8s.MetaNamespaceKey(o.Ingress))
if err != nil {
return nil, err
}

ruleConfig, ok := annos.Action.Actions[o.SvcName]
if !ok {
return nil, fmt.Errorf("`servicePort: use-annotation` was requested for"+
"`serviceName: %v` but an annotation for that action does not exist", o.SvcName)
return nil, fmt.Errorf("`servicePort: %s was requested for"+
"`serviceName: %v` but an annotation for that action does not exist", action.UseActionAnnotation, o.SvcName)
}

r.Actions[0] = ruleConfig
Expand Down Expand Up @@ -250,7 +251,7 @@ func (r *Rule) needsModification() bool {
case !conditionsEqual(crs.Conditions, drs.Conditions):
r.logger.Debugf("Conditions needs to be changed (%v != %v)", log.Prettify(crs.Conditions), log.Prettify(drs.Conditions))
return true
case r.svc.current.name != r.svc.desired.name && r.svc.current.port.String() != "use-annotation":
case r.svc.current.name != r.svc.desired.name && r.svc.current.port.String() != action.UseActionAnnotation:
r.logger.Debugf("SvcName needs to be changed (%v != %v)", r.svc.current.name, r.svc.desired.name)
return true
case r.svc.current.port.String() != r.svc.desired.port.String():
Expand Down
15 changes: 8 additions & 7 deletions internal/alb/rs/rule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"testing"

"github.com/kubernetes-sigs/aws-alb-ingress-controller/internal/ingress/annotations/action"
extensions "k8s.io/api/extensions/v1beta1"
"k8s.io/apimachinery/pkg/util/intstr"

Expand Down Expand Up @@ -32,12 +33,12 @@ func TestNewDesiredRule(t *testing.T) {
{
Priority: 1,
SvcName: "fixed-response-action",
SvcPort: intstr.FromString("use-annotation"),
SvcPort: intstr.FromString(action.UseActionAnnotation),
Ingress: dummy.NewIngress(),
Store: store.NewDummy(),
TargetPort: 0,
ExpectedRule: Rule{
svc: svc{desired: service{name: "fixed-response-action", port: intstr.FromString("use-annotation"), targetPort: 0}},
svc: svc{desired: service{name: "fixed-response-action", port: intstr.FromString(action.UseActionAnnotation), targetPort: 0}},
rs: rs{
desired: &elbv2.Rule{
Priority: aws.String("1"),
Expand All @@ -59,12 +60,12 @@ func TestNewDesiredRule(t *testing.T) {
{
Priority: 1,
SvcName: "redirect",
SvcPort: intstr.FromString("use-annotation"),
SvcPort: intstr.FromString(action.UseActionAnnotation),
Ingress: dummy.NewIngress(),
Store: store.NewDummy(),
TargetPort: 0,
ExpectedRule: Rule{
svc: svc{desired: service{name: "redirect", port: intstr.FromString("use-annotation"), targetPort: 0}},
svc: svc{desired: service{name: "redirect", port: intstr.FromString(action.UseActionAnnotation), targetPort: 0}},
rs: rs{
desired: &elbv2.Rule{
Priority: aws.String("1"),
Expand Down Expand Up @@ -869,7 +870,7 @@ func TestRuleValid(t *testing.T) {
{
Priority: 1,
SvcName: "redirect", // redirect https to https, invalid
SvcPort: intstr.FromString("use-annotation"),
SvcPort: intstr.FromString(action.UseActionAnnotation),
Ingress: dummy.NewIngress(),
Store: store.NewDummy(),
TargetPort: 0,
Expand All @@ -879,7 +880,7 @@ func TestRuleValid(t *testing.T) {
{
Priority: 1,
SvcName: "redirect", // redirect http to https, valid
SvcPort: intstr.FromString("use-annotation"),
SvcPort: intstr.FromString(action.UseActionAnnotation),
Ingress: dummy.NewIngress(),
Store: store.NewDummy(),
TargetPort: 0,
Expand All @@ -889,7 +890,7 @@ func TestRuleValid(t *testing.T) {
{
Priority: 1,
SvcName: "redirect-path2", // redirect https to https, non-standard path, valid
SvcPort: intstr.FromString("use-annotation"),
SvcPort: intstr.FromString(action.UseActionAnnotation),
Ingress: dummy.NewIngress(),
Store: store.NewDummy(),
TargetPort: 0,
Expand Down
5 changes: 3 additions & 2 deletions internal/alb/rs/rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package rs
import (
"fmt"

"github.com/kubernetes-sigs/aws-alb-ingress-controller/internal/ingress/annotations/action"
"k8s.io/apimachinery/pkg/util/intstr"

"github.com/aws/aws-sdk-go/aws/awsutil"
Expand Down Expand Up @@ -47,7 +48,7 @@ func NewCurrentRules(o *NewCurrentRulesOptions) (Rules, error) {
svcPort = tg.SvcPort
targetPort = tg.TargetPort
} else {
svcPort = intstr.FromString("use-annotation")
svcPort = intstr.FromString(action.UseActionAnnotation)
}

newRule := NewCurrentRule(&NewCurrentRuleOptions{
Expand Down Expand Up @@ -93,7 +94,7 @@ func NewDesiredRules(o *NewDesiredRulesOptions) (Rules, int, error) {
for _, path := range paths {
var targetPort int

if path.Backend.ServicePort.String() != "use-annotation" {
if path.Backend.ServicePort.String() != action.UseActionAnnotation {
i := o.TargetGroups.LookupByBackend(path.Backend)

if i < 0 {
Expand Down
3 changes: 2 additions & 1 deletion internal/alb/tg/targetgroups.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

extensions "k8s.io/api/extensions/v1beta1"

"github.com/kubernetes-sigs/aws-alb-ingress-controller/internal/ingress/annotations/action"
"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"
Expand Down Expand Up @@ -121,7 +122,7 @@ func NewDesiredTargetGroups(o *NewDesiredTargetGroupsOptions) (TargetGroups, err
}

for _, backend := range backends {
if backend.ServicePort.String() == "use-annotation" {
if backend.ServicePort.String() == action.UseActionAnnotation {
// action annotations do not need target groups
continue
}
Expand Down
3 changes: 2 additions & 1 deletion internal/albingress/albingresses.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"k8s.io/client-go/tools/record"

"github.com/kubernetes-sigs/aws-alb-ingress-controller/internal/aws/albelbv2"
"github.com/kubernetes-sigs/aws-alb-ingress-controller/internal/ingress/annotations/action"
"github.com/kubernetes-sigs/aws-alb-ingress-controller/internal/ingress/annotations/class"
"github.com/kubernetes-sigs/aws-alb-ingress-controller/internal/ingress/controller/store"
"github.com/kubernetes-sigs/aws-alb-ingress-controller/internal/ingress/metric"
Expand Down Expand Up @@ -234,7 +235,7 @@ func applyDefaults(i *extensions.Ingress) {
if i.Spec.Backend == nil {
i.Spec.Backend = &extensions.IngressBackend{
ServiceName: ls.Default404,
ServicePort: intstr.FromString("use-annotation"),
ServicePort: intstr.FromString(action.UseActionAnnotation),
}
}
}
2 changes: 2 additions & 0 deletions internal/ingress/annotations/action/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"github.com/kubernetes-sigs/aws-alb-ingress-controller/internal/ingress/resolver"
)

const UseActionAnnotation = "use-annotation"

type Config struct {
Actions map[string]*elbv2.Action
}
Expand Down

0 comments on commit 86cb17e

Please sign in to comment.