Skip to content
This repository was archived by the owner on Jan 21, 2020. It is now read-only.

Commit 832f2f7

Browse files
kaufersDavid Chung
authored andcommitted
Ingress controller source key selection support (#703)
Signed-off-by: Steven Kaufer <[email protected]>
1 parent 3714614 commit 832f2f7

File tree

3 files changed

+50
-1
lines changed

3 files changed

+50
-1
lines changed

pkg/controller/ingress/managed.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"github.com/docker/infrakit/pkg/spi/group"
1919
"github.com/docker/infrakit/pkg/spi/instance"
2020
"github.com/docker/infrakit/pkg/spi/loadbalancer"
21+
"github.com/docker/infrakit/pkg/template"
2122
"github.com/docker/infrakit/pkg/types"
2223
)
2324

@@ -71,6 +72,9 @@ type managed struct {
7172
groupClientsLock gsync.RWMutex
7273

7374
lock gsync.RWMutex
75+
76+
// template that we use to render with a source instance.Description to get the link Key
77+
sourceKeySelectorTemplate *template.Template
7478
}
7579

7680
func (c *managed) state() ingress.Properties {

pkg/controller/ingress/sync.go

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"github.com/docker/infrakit/pkg/controller/ingress/types"
66
"github.com/docker/infrakit/pkg/spi/instance"
77
"github.com/docker/infrakit/pkg/spi/loadbalancer"
8+
"github.com/docker/infrakit/pkg/template"
89
)
910

1011
func (c *managed) syncRoutesL4() error {
@@ -42,6 +43,23 @@ func (c *managed) syncRoutesL4() error {
4243
return nil
4344
}
4445

46+
func (c *managed) getSourceKeySelectorTemplate() (*template.Template, error) {
47+
c.lock.Lock()
48+
defer c.lock.Unlock()
49+
50+
if c.options.SourceKeySelector != "" {
51+
if c.sourceKeySelectorTemplate == nil {
52+
t, err := types.TemplateFrom([]byte(c.options.SourceKeySelector))
53+
if err != nil {
54+
return nil, err
55+
}
56+
c.sourceKeySelectorTemplate = t
57+
}
58+
}
59+
60+
return c.sourceKeySelectorTemplate, nil
61+
}
62+
4563
func (c *managed) syncBackends() error {
4664
groupsByVhost, err := c.groups()
4765
log.Debug("groups by vhost", "groups", groupsByVhost)
@@ -78,6 +96,7 @@ func (c *managed) syncBackends() error {
7896
registered.Add(b)
7997
}
8098
}
99+
log.Info("Registered backends", "backends", registered)
81100

82101
// all the nodes from all the groups and nodes
83102
nodes := mapset.NewSet()
@@ -105,10 +124,25 @@ func (c *managed) syncBackends() error {
105124
log.Debug("found backends", "groupID", gid, "desc", desc, "vhost", vhost, "L4", l4.Name())
106125

107126
for _, inst := range desc.Instances {
108-
nodes.Add(inst.ID)
127+
t, err := c.getSourceKeySelectorTemplate()
128+
if err != nil {
129+
return err
130+
}
131+
if t == nil {
132+
nodes.Add(inst.ID)
133+
} else {
134+
view, err := t.Render(inst)
135+
if err != nil {
136+
log.Error("cannot index entry", "instance.Description", inst, "err", err)
137+
continue
138+
}
139+
nodes.Add(instance.ID(view))
140+
}
109141
}
110142
}
111143

144+
log.Info("Group data", "nodes", nodes)
145+
112146
// compute the difference between registered and nodes
113147
toRemove := []instance.ID{}
114148
for n := range registered.Difference(nodes).Iter() {

pkg/controller/ingress/types/types.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/docker/infrakit/pkg/spi/group"
1010
"github.com/docker/infrakit/pkg/spi/instance"
1111
"github.com/docker/infrakit/pkg/spi/loadbalancer"
12+
"github.com/docker/infrakit/pkg/template"
1213
"github.com/docker/infrakit/pkg/types"
1314
)
1415

@@ -112,4 +113,14 @@ type Options struct {
112113
// SyncInterval is how often to run the sync. The syntax is the string form
113114
// of Go time.Duration (e.g. 1min)
114115
SyncInterval types.Duration
116+
117+
// SourceKeySelector is a string template for selecting the join key from
118+
// a source instance.Description.
119+
SourceKeySelector string
120+
}
121+
122+
// TemplateFrom returns a template after it has un-escapes any escape sequences
123+
func TemplateFrom(source []byte) (*template.Template, error) {
124+
buff := template.Unescape(source)
125+
return template.NewTemplate("str://"+string(buff), template.Options{MultiPass: false})
115126
}

0 commit comments

Comments
 (0)