Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose upstream list to user easy use it on your snippets #163

Merged
merged 1 commit into from
Feb 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 26 additions & 6 deletions cmd/plugin/rpaasv2/cmd/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"k8s.io/apimachinery/pkg/api/resource"
"k8s.io/apimachinery/pkg/util/duration"

"github.com/tsuru/rpaas-operator/api/v1alpha1"
rpaasclient "github.com/tsuru/rpaas-operator/pkg/rpaas/client"
"github.com/tsuru/rpaas-operator/pkg/rpaas/client/autogenerated"
clientTypes "github.com/tsuru/rpaas-operator/pkg/rpaas/client/types"
Expand Down Expand Up @@ -321,20 +320,41 @@ func writeInfoRoutesOnTableFormat(routes []clientTypes.Route) string {
return buffer.String()
}

func writeBindsOnTableFormat(binds []v1alpha1.Bind) string {
data := [][]string{}
func writeBindsOnTableFormat(binds []clientTypes.Bind) string {
rows := make([][]string, 0, len(binds))

hasUpstreams := false
for _, bind := range binds {
data = append(data, []string{bind.Name, bind.Host})
if len(bind.Upstreams) > 0 {
hasUpstreams = true
break
}
}

for _, bind := range binds {
row := []string{bind.Name, bind.Host}

if hasUpstreams {
row = append(row, strings.Join(bind.Upstreams, ", "))
}

rows = append(rows, row)
}
var buffer bytes.Buffer
table := tablewriter.NewWriter(&buffer)
table.SetHeader([]string{"App", "Address"})

headers := []string{"App", "Address"}
if hasUpstreams {
headers = append(headers, "Upstreams")
}

table.SetHeader(headers)
table.SetRowLine(true)
table.SetAutoFormatHeaders(false)
table.SetHeaderAlignment(tablewriter.ALIGN_LEFT)
table.SetAutoWrapText(true)
table.SetColumnAlignment([]int{tablewriter.ALIGN_LEFT, tablewriter.ALIGN_CENTER, tablewriter.ALIGN_CENTER, tablewriter.ALIGN_LEFT})
table.AppendBulk(data)
table.AppendBulk(rows)
table.Render()

return buffer.String()
Expand Down
8 changes: 4 additions & 4 deletions cmd/plugin/rpaasv2/cmd/info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func TestInfo(t *testing.T) {
{Host: "my-app.apps.tsuru.io", Port: 80},
{Host: "my-app.apps.tsuru.io", Port: 443},
},
Binds: []v1alpha1.Bind{
Binds: []clientTypes.Bind{
{
Name: "some-name",
Host: "some-host",
Expand Down Expand Up @@ -511,7 +511,7 @@ Events:
Name: "my-instance",
Addresses: []clientTypes.InstanceAddress{},
Plan: "basic",
Binds: []v1alpha1.Bind{},
Binds: []clientTypes.Bind{},
Replicas: autogenerated.PtrInt32(3),
Blocks: []clientTypes.Block{},
Routes: []clientTypes.Route{},
Expand Down Expand Up @@ -617,7 +617,7 @@ Pods: (current: 2 / desired: 3)
Name: "my-instance",
Addresses: []clientTypes.InstanceAddress{},
Plan: "basic",
Binds: []v1alpha1.Bind{},
Binds: []clientTypes.Bind{},
Replicas: autogenerated.PtrInt32(3),
Blocks: []clientTypes.Block{},
Routes: []clientTypes.Route{},
Expand Down Expand Up @@ -729,7 +729,7 @@ Pods: (current: 2 / desired: 3)
},
},
Plan: "basic",
Binds: []v1alpha1.Bind{
Binds: []clientTypes.Bind{
{
Name: "some-name",
Host: "some-host",
Expand Down
2 changes: 1 addition & 1 deletion internal/pkg/rpaas/k8s.go
Original file line number Diff line number Diff line change
Expand Up @@ -1689,7 +1689,7 @@ func (m *k8sRpaasManager) GetInstanceInfo(ctx context.Context, instanceName stri
Annotations: flatAnnotations,
Replicas: instance.Spec.Replicas,
Plan: instance.Spec.PlanName,
Binds: instance.Spec.Binds,
Binds: clientTypes.NewBinds(instance.Spec.Binds),
Flavors: instance.Spec.Flavors,
Shutdown: instance.Spec.Shutdown,
PlanOverride: instance.Spec.PlanTemplate,
Expand Down
21 changes: 8 additions & 13 deletions internal/pkg/rpaas/nginx/configuration_render.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"k8s.io/apimachinery/pkg/api/resource"

"github.com/tsuru/rpaas-operator/api/v1alpha1"
clientTypes "github.com/tsuru/rpaas-operator/pkg/rpaas/client/types"
"github.com/tsuru/rpaas-operator/pkg/util"
)

Expand All @@ -42,6 +43,7 @@ type ConfigurationData struct {
Instance *v1alpha1.RpaasInstance
NginxTLS []nginxv1alpha1.NginxTLS
Servers []*Server
Binds []clientTypes.Bind

// DEPRECATED: Modules is a map of installed modules, using a map instead of a slice
// allow us to use `hasKey` inside templates.
Expand All @@ -58,6 +60,7 @@ func (r *rpaasConfigurationRenderer) Render(c ConfigurationData) (string, error)
c.Servers = produceServers(&c.Instance.Spec, c.NginxTLS)
}
initListenOptions(c.Servers, c.Config)
c.Binds = clientTypes.NewBinds(c.Instance.Spec.Binds)
err := r.t.Execute(buffer, c)
if err != nil {
return "", err
Expand Down Expand Up @@ -305,6 +308,7 @@ var rawNginxConfiguration = `
{{- $config := .Config -}}
{{- $instance := .Instance -}}
{{- $nginxTLS := .NginxTLS -}}
{{- $binds := .Binds -}}
{{- $servers := .Servers -}}
{{- $httpBlock := renderInnerTemplate "http" . -}}

Expand Down Expand Up @@ -411,24 +415,15 @@ http {
{{- end }}
{{- end }}

{{- range $index, $bind := $instance.Spec.Binds }}
{{- if eq $index 0 }}
upstream rpaas_default_upstream {
server {{ $bind.Host }};

{{- with $config.UpstreamKeepalive }}
keepalive {{ . }};
{{- end }}
}
{{- end }}

upstream rpaas_backend_{{ $bind.Name }} {
{{- range $_, $bind := $binds }}
{{- range $_, $upstream := $bind.Upstreams }}
upstream {{ $upstream }} {
server {{ $bind.Host }};
{{- with $config.UpstreamKeepalive }}
keepalive {{ . }};
{{- end }}
}

{{- end }}
{{- end }}

{{- range $_, $location := $instance.Spec.Locations }}
Expand Down
2 changes: 1 addition & 1 deletion pkg/rpaas/client/info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func TestClientThroughTsuru_Info(t *testing.T) {
assert.Equal(t, r.Method, "GET")
assert.Equal(t, fmt.Sprintf("/1.20/services/%s/resources/%s/info", FakeTsuruService, "my-instance"), r.URL.RequestURI())
assert.Equal(t, "Bearer f4k3t0k3n", r.Header.Get("Authorization"))
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"]}`)
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"]}`)
w.WriteHeader(http.StatusOK)
},
},
Expand Down
31 changes: 30 additions & 1 deletion pkg/rpaas/client/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ type InstanceInfo struct {
Routes []Route `json:"routes,omitempty"`
Autoscale *autogenerated.Autoscale `json:"autoscale,omitempty"`
ACLs []AllowedUpstream `json:"acls,omitempty"`
Binds []v1alpha1.Bind `json:"binds,omitempty"`
Binds []Bind `json:"binds,omitempty"`
Team string `json:"team,omitempty"`
Name string `json:"name,omitempty"`
Service string `json:"service,omitempty"`
Expand Down Expand Up @@ -182,3 +182,32 @@ type MetadataItem struct {
Name string `json:"name"`
Value string `json:"value,omitempty"`
}

type Bind struct {
Name string `json:"name"`
Host string `json:"host"`
Upstreams []string `json:"upstreams,omitempty"`
}

func NewBinds(k8sBinds []v1alpha1.Bind) []Bind {
if k8sBinds == nil {
return nil
}
binds := make([]Bind, len(k8sBinds))
for i, b := range k8sBinds {
upstreams := []string{
"rpaas_backend_" + b.Name,
}

if i == 0 {
upstreams = append(upstreams, "rpaas_default_upstream")
}

binds[i] = Bind{
Name: b.Name,
Host: b.Host,
Upstreams: upstreams,
}
}
return binds
}
15 changes: 8 additions & 7 deletions pkg/web/info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/tsuru/rpaas-operator/api/v1alpha1"
"github.com/tsuru/rpaas-operator/internal/pkg/rpaas"
"github.com/tsuru/rpaas-operator/internal/pkg/rpaas/fake"
"github.com/tsuru/rpaas-operator/pkg/rpaas/client/autogenerated"
Expand Down Expand Up @@ -60,14 +59,16 @@ func Test_instanceInfo(t *testing.T) {
"tag1",
"tag2",
},
Binds: []v1alpha1.Bind{
Binds: []clientTypes.Bind{
{
Name: "app-default",
Host: "some host ip address",
Name: "app-default",
Host: "some host ip address",
Upstreams: []string{"rpaas_backend_app-default", "rpaas_default_upstream"},
},
{
Name: "app-backup",
Host: "some host backup ip address",
Name: "app-backup",
Host: "some host backup ip address",
Upstreams: []string{"rpaas_backend_app-backup"},
},
},
Routes: []clientTypes.Route{
Expand All @@ -90,7 +91,7 @@ func Test_instanceInfo(t *testing.T) {
},
},
expectedCode: http.StatusOK,
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}",
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}",
},
{
name: "when some error occurs while creating the info Payload",
Expand Down
Loading