Skip to content

Commit a4aa7dc

Browse files
authored
Expose upstream list to user easy use it on your snippets (#163)
1 parent 5dd9c22 commit a4aa7dc

File tree

7 files changed

+78
-33
lines changed

7 files changed

+78
-33
lines changed

cmd/plugin/rpaasv2/cmd/info.go

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import (
1919
"k8s.io/apimachinery/pkg/api/resource"
2020
"k8s.io/apimachinery/pkg/util/duration"
2121

22-
"github.com/tsuru/rpaas-operator/api/v1alpha1"
2322
rpaasclient "github.com/tsuru/rpaas-operator/pkg/rpaas/client"
2423
"github.com/tsuru/rpaas-operator/pkg/rpaas/client/autogenerated"
2524
clientTypes "github.com/tsuru/rpaas-operator/pkg/rpaas/client/types"
@@ -321,20 +320,41 @@ func writeInfoRoutesOnTableFormat(routes []clientTypes.Route) string {
321320
return buffer.String()
322321
}
323322

324-
func writeBindsOnTableFormat(binds []v1alpha1.Bind) string {
325-
data := [][]string{}
323+
func writeBindsOnTableFormat(binds []clientTypes.Bind) string {
324+
rows := make([][]string, 0, len(binds))
325+
326+
hasUpstreams := false
326327
for _, bind := range binds {
327-
data = append(data, []string{bind.Name, bind.Host})
328+
if len(bind.Upstreams) > 0 {
329+
hasUpstreams = true
330+
break
331+
}
332+
}
333+
334+
for _, bind := range binds {
335+
row := []string{bind.Name, bind.Host}
336+
337+
if hasUpstreams {
338+
row = append(row, strings.Join(bind.Upstreams, ", "))
339+
}
340+
341+
rows = append(rows, row)
328342
}
329343
var buffer bytes.Buffer
330344
table := tablewriter.NewWriter(&buffer)
331-
table.SetHeader([]string{"App", "Address"})
345+
346+
headers := []string{"App", "Address"}
347+
if hasUpstreams {
348+
headers = append(headers, "Upstreams")
349+
}
350+
351+
table.SetHeader(headers)
332352
table.SetRowLine(true)
333353
table.SetAutoFormatHeaders(false)
334354
table.SetHeaderAlignment(tablewriter.ALIGN_LEFT)
335355
table.SetAutoWrapText(true)
336356
table.SetColumnAlignment([]int{tablewriter.ALIGN_LEFT, tablewriter.ALIGN_CENTER, tablewriter.ALIGN_CENTER, tablewriter.ALIGN_LEFT})
337-
table.AppendBulk(data)
357+
table.AppendBulk(rows)
338358
table.Render()
339359

340360
return buffer.String()

cmd/plugin/rpaasv2/cmd/info_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ func TestInfo(t *testing.T) {
7171
{Host: "my-app.apps.tsuru.io", Port: 80},
7272
{Host: "my-app.apps.tsuru.io", Port: 443},
7373
},
74-
Binds: []v1alpha1.Bind{
74+
Binds: []clientTypes.Bind{
7575
{
7676
Name: "some-name",
7777
Host: "some-host",
@@ -511,7 +511,7 @@ Events:
511511
Name: "my-instance",
512512
Addresses: []clientTypes.InstanceAddress{},
513513
Plan: "basic",
514-
Binds: []v1alpha1.Bind{},
514+
Binds: []clientTypes.Bind{},
515515
Replicas: autogenerated.PtrInt32(3),
516516
Blocks: []clientTypes.Block{},
517517
Routes: []clientTypes.Route{},
@@ -617,7 +617,7 @@ Pods: (current: 2 / desired: 3)
617617
Name: "my-instance",
618618
Addresses: []clientTypes.InstanceAddress{},
619619
Plan: "basic",
620-
Binds: []v1alpha1.Bind{},
620+
Binds: []clientTypes.Bind{},
621621
Replicas: autogenerated.PtrInt32(3),
622622
Blocks: []clientTypes.Block{},
623623
Routes: []clientTypes.Route{},
@@ -729,7 +729,7 @@ Pods: (current: 2 / desired: 3)
729729
},
730730
},
731731
Plan: "basic",
732-
Binds: []v1alpha1.Bind{
732+
Binds: []clientTypes.Bind{
733733
{
734734
Name: "some-name",
735735
Host: "some-host",

internal/pkg/rpaas/k8s.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1689,7 +1689,7 @@ func (m *k8sRpaasManager) GetInstanceInfo(ctx context.Context, instanceName stri
16891689
Annotations: flatAnnotations,
16901690
Replicas: instance.Spec.Replicas,
16911691
Plan: instance.Spec.PlanName,
1692-
Binds: instance.Spec.Binds,
1692+
Binds: clientTypes.NewBinds(instance.Spec.Binds),
16931693
Flavors: instance.Spec.Flavors,
16941694
Shutdown: instance.Spec.Shutdown,
16951695
PlanOverride: instance.Spec.PlanTemplate,

internal/pkg/rpaas/nginx/configuration_render.go

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"k8s.io/apimachinery/pkg/api/resource"
1818

1919
"github.com/tsuru/rpaas-operator/api/v1alpha1"
20+
clientTypes "github.com/tsuru/rpaas-operator/pkg/rpaas/client/types"
2021
"github.com/tsuru/rpaas-operator/pkg/util"
2122
)
2223

@@ -42,6 +43,7 @@ type ConfigurationData struct {
4243
Instance *v1alpha1.RpaasInstance
4344
NginxTLS []nginxv1alpha1.NginxTLS
4445
Servers []*Server
46+
Binds []clientTypes.Bind
4547

4648
// DEPRECATED: Modules is a map of installed modules, using a map instead of a slice
4749
// allow us to use `hasKey` inside templates.
@@ -58,6 +60,7 @@ func (r *rpaasConfigurationRenderer) Render(c ConfigurationData) (string, error)
5860
c.Servers = produceServers(&c.Instance.Spec, c.NginxTLS)
5961
}
6062
initListenOptions(c.Servers, c.Config)
63+
c.Binds = clientTypes.NewBinds(c.Instance.Spec.Binds)
6164
err := r.t.Execute(buffer, c)
6265
if err != nil {
6366
return "", err
@@ -305,6 +308,7 @@ var rawNginxConfiguration = `
305308
{{- $config := .Config -}}
306309
{{- $instance := .Instance -}}
307310
{{- $nginxTLS := .NginxTLS -}}
311+
{{- $binds := .Binds -}}
308312
{{- $servers := .Servers -}}
309313
{{- $httpBlock := renderInnerTemplate "http" . -}}
310314
@@ -411,24 +415,15 @@ http {
411415
{{- end }}
412416
{{- end }}
413417
414-
{{- range $index, $bind := $instance.Spec.Binds }}
415-
{{- if eq $index 0 }}
416-
upstream rpaas_default_upstream {
417-
server {{ $bind.Host }};
418-
419-
{{- with $config.UpstreamKeepalive }}
420-
keepalive {{ . }};
421-
{{- end }}
422-
}
423-
{{- end }}
424-
425-
upstream rpaas_backend_{{ $bind.Name }} {
418+
{{- range $_, $bind := $binds }}
419+
{{- range $_, $upstream := $bind.Upstreams }}
420+
upstream {{ $upstream }} {
426421
server {{ $bind.Host }};
427422
{{- with $config.UpstreamKeepalive }}
428423
keepalive {{ . }};
429424
{{- end }}
430425
}
431-
426+
{{- end }}
432427
{{- end }}
433428
434429
{{- range $_, $location := $instance.Spec.Locations }}

pkg/rpaas/client/info_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func TestClientThroughTsuru_Info(t *testing.T) {
3434
assert.Equal(t, r.Method, "GET")
3535
assert.Equal(t, fmt.Sprintf("/1.20/services/%s/resources/%s/info", FakeTsuruService, "my-instance"), r.URL.RequestURI())
3636
assert.Equal(t, "Bearer f4k3t0k3n", r.Header.Get("Authorization"))
37-
fmt.Fprintf(w, `{"address":[{"hostname":"some-host","ip":"0.0.0.0"},{"hostname":"some-host2","ip":"0.0.0.1"}],"replicas":5,"plan":"basic","locations":[{"path":"some-path","destination":"some-destination"}],"binds":[{"name":"some-name","host":"some-host"},{"name":"some-name2","host":"some-host2"}],"team":"some team","name":"my-instance","description":"some description","tags":["tag1","tag2","tag3"]}`)
37+
fmt.Fprintf(w, `{"address":[{"hostname":"some-host","ip":"0.0.0.0"},{"hostname":"some-host2","ip":"0.0.0.1"}],"replicas":5,"plan":"basic","locations":[{"path":"some-path","destination":"some-destination"}],"binds":[{"name":"some-name","host":"some-host","upstreams":["rpaas_backend_app-default","rpaas_default_upstream"]},{"name":"some-name2","host":"some-host2","upstreams":["rpaas_backend_app-backend"]}],"team":"some team","name":"my-instance","description":"some description","tags":["tag1","tag2","tag3"]}`)
3838
w.WriteHeader(http.StatusOK)
3939
},
4040
},

pkg/rpaas/client/types/types.go

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ type InstanceInfo struct {
141141
Routes []Route `json:"routes,omitempty"`
142142
Autoscale *autogenerated.Autoscale `json:"autoscale,omitempty"`
143143
ACLs []AllowedUpstream `json:"acls,omitempty"`
144-
Binds []v1alpha1.Bind `json:"binds,omitempty"`
144+
Binds []Bind `json:"binds,omitempty"`
145145
Team string `json:"team,omitempty"`
146146
Name string `json:"name,omitempty"`
147147
Service string `json:"service,omitempty"`
@@ -182,3 +182,32 @@ type MetadataItem struct {
182182
Name string `json:"name"`
183183
Value string `json:"value,omitempty"`
184184
}
185+
186+
type Bind struct {
187+
Name string `json:"name"`
188+
Host string `json:"host"`
189+
Upstreams []string `json:"upstreams,omitempty"`
190+
}
191+
192+
func NewBinds(k8sBinds []v1alpha1.Bind) []Bind {
193+
if k8sBinds == nil {
194+
return nil
195+
}
196+
binds := make([]Bind, len(k8sBinds))
197+
for i, b := range k8sBinds {
198+
upstreams := []string{
199+
"rpaas_backend_" + b.Name,
200+
}
201+
202+
if i == 0 {
203+
upstreams = append(upstreams, "rpaas_default_upstream")
204+
}
205+
206+
binds[i] = Bind{
207+
Name: b.Name,
208+
Host: b.Host,
209+
Upstreams: upstreams,
210+
}
211+
}
212+
return binds
213+
}

pkg/web/info_test.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import (
1313
"github.com/stretchr/testify/assert"
1414
"github.com/stretchr/testify/require"
1515

16-
"github.com/tsuru/rpaas-operator/api/v1alpha1"
1716
"github.com/tsuru/rpaas-operator/internal/pkg/rpaas"
1817
"github.com/tsuru/rpaas-operator/internal/pkg/rpaas/fake"
1918
"github.com/tsuru/rpaas-operator/pkg/rpaas/client/autogenerated"
@@ -60,14 +59,16 @@ func Test_instanceInfo(t *testing.T) {
6059
"tag1",
6160
"tag2",
6261
},
63-
Binds: []v1alpha1.Bind{
62+
Binds: []clientTypes.Bind{
6463
{
65-
Name: "app-default",
66-
Host: "some host ip address",
64+
Name: "app-default",
65+
Host: "some host ip address",
66+
Upstreams: []string{"rpaas_backend_app-default", "rpaas_default_upstream"},
6767
},
6868
{
69-
Name: "app-backup",
70-
Host: "some host backup ip address",
69+
Name: "app-backup",
70+
Host: "some host backup ip address",
71+
Upstreams: []string{"rpaas_backend_app-backup"},
7172
},
7273
},
7374
Routes: []clientTypes.Route{
@@ -90,7 +91,7 @@ func Test_instanceInfo(t *testing.T) {
9091
},
9192
},
9293
expectedCode: http.StatusOK,
93-
expectedBody: "{\"addresses\":[{\"type\":\"cluster-external\",\"serviceName\":\"my-instance-service\",\"hostname\":\"some host name\",\"ip\":\"0.0.0.0\",\"status\":\"ready\"},{\"type\":\"cluster-external\",\"serviceName\":\"my-instance-service\",\"hostname\":\"some host name 2\",\"ip\":\"0.0.0.1\",\"status\":\"ready\"}],\"replicas\":5,\"plan\":\"basic\",\"routes\":[{\"path\":\"some location path\",\"destination\":\"some destination\"},{\"path\":\"some location path 2\",\"destination\":\"some destination 2\"}],\"autoscale\":{\"cpu\":70,\"maxReplicas\":3,\"memory\":1024,\"minReplicas\":1},\"binds\":[{\"name\":\"app-default\",\"host\":\"some host ip address\"},{\"name\":\"app-backup\",\"host\":\"some host backup ip address\"}],\"team\":\"some team\",\"name\":\"some rpaas instance name\",\"description\":\"some description\",\"tags\":[\"tag1\",\"tag2\"],\"shutdown\":false}",
94+
expectedBody: "{\"addresses\":[{\"type\":\"cluster-external\",\"serviceName\":\"my-instance-service\",\"hostname\":\"some host name\",\"ip\":\"0.0.0.0\",\"status\":\"ready\"},{\"type\":\"cluster-external\",\"serviceName\":\"my-instance-service\",\"hostname\":\"some host name 2\",\"ip\":\"0.0.0.1\",\"status\":\"ready\"}],\"replicas\":5,\"plan\":\"basic\",\"routes\":[{\"path\":\"some location path\",\"destination\":\"some destination\"},{\"path\":\"some location path 2\",\"destination\":\"some destination 2\"}],\"autoscale\":{\"cpu\":70,\"maxReplicas\":3,\"memory\":1024,\"minReplicas\":1},\"binds\":[{\"name\":\"app-default\",\"host\":\"some host ip address\",\"upstreams\":[\"rpaas_backend_app-default\",\"rpaas_default_upstream\"]},{\"name\":\"app-backup\",\"host\":\"some host backup ip address\",\"upstreams\":[\"rpaas_backend_app-backup\"]}],\"team\":\"some team\",\"name\":\"some rpaas instance name\",\"description\":\"some description\",\"tags\":[\"tag1\",\"tag2\"],\"shutdown\":false}",
9495
},
9596
{
9697
name: "when some error occurs while creating the info Payload",

0 commit comments

Comments
 (0)